admin管理员组文章数量:1317909
The question is about role permissions in Strapi - Open source Node.js Headless CMS
How to create new strapi role permissions relative to Authenticated
or Public
role? I want to create new role with these same all permissions
in the bootstrap function. I'm not sure how should look payload
to create new role
.
const payload = ?
strapi.plugins['users-permissions']
.queries('role', 'users-permissions')
.create(payload)
I checked relative stackoverflow question but isn't about creating new strapi role permission Bootstraping Strapi Role Permissions!
main problem
- How look the best way to resolve above problem?
side questions
- Is documented schema for
plugins
orservices
if yes where could find it? - Which api is better use
services
ororm
fromqueries
instance?
The question is about role permissions in Strapi - Open source Node.js Headless CMS
How to create new strapi role permissions relative to Authenticated
or Public
role? I want to create new role with these same all permissions
in the bootstrap function. I'm not sure how should look payload
to create new role
.
const payload = ?
strapi.plugins['users-permissions']
.queries('role', 'users-permissions')
.create(payload)
I checked relative stackoverflow question but isn't about creating new strapi role permission Bootstraping Strapi Role Permissions!
main problem
- How look the best way to resolve above problem?
side questions
- Is documented schema for
plugins
orservices
if yes where could find it? - Which api is better use
services
ororm
fromqueries
instance?
- Hello! Do you want to create the new role on the first bootstrap? Are it's after? About this topic, there is no documentation. – Jim LAURIE Commented Jul 22, 2019 at 14:45
- Hello Jim! Yes, I want to create the new role on the first bootstrap and extend this role by all permissions which are available. I'm going to configure permissions for new role by custom requirements per new role - it's next step – Daniel Karski Commented Jul 23, 2019 at 6:40
2 Answers
Reset to default 9OKay so since the data structure can change I will give you the way to access to useful data that will help you to deal with what you want.
In this this file you will find service function that create roles and the function that inits permissions.
You can write your code in your bootstrap.js
file.
- You will have to call the functions that let you generate permission object
const lang = 'en';
const plugins = await strapi.plugins[
'users-permissions'
].services.userspermissions.getPlugins(lang);
const permissions = await strapi.plugins[
'users-permissions'
].services.userspermissions.getActions(plugins);
Customize this object (the
permissions
object) to allow controller functions you want.And finaly create your role
const role = {
name: 'NewRole',
description: 'can be an empty string',
permissions, // That is the step 1/2 object
users: []
};
await strapi.plugins[
'users-permissions'
].services.userspermissions.createRole(role);
I took @Jim LAURIE 's answer and made it relevant to updating permissions on existing roles. The pointer to look at node_modules/strapi-plugin-users-permissions/services/UsersPermissions.js
was very useful :)
I have a page
and post
type, and this code lets the default authenticated and public users do all view actions on them.
(PS I'm using TypeScript annotations, but this is plain JS).
// bootstrap.js
'use strict';
const _ = require("lodash");
/**
* Bootstrap function, run every startup.
* See https://strapi.io/documentation/v3.x/concepts/configurations.html#bootstrap
*/
module.exports = async () => {
// https://stackoverflow./a/57184017/268555
// Ref to https://github./strapi/strapi/blob/master/packages/strapi-plugin-users-permissions/services/UsersPermissions.js
const service = await strapi.plugins["users-permissions"].services.userspermissions;
const plugins = await service.getPlugins("en");
/** @type Role[] */
const roles = await service.getRoles();
/**
* @param {Role["type"]} type
*/
const getRole = async (type) => {
const {id} = _.find(roles, x => x.type === type);
return service.getRole(id, plugins);
}
/**
* @param {Role} role
* @param {PluginPermissionKey} type
* @param {string} controller
* @param {string} action
* @param {boolean} enabled
*/
const setPermission = (role, type, controller, action, enabled) => {
try {
role.permissions[type].controllers[controller][action].enabled = enabled;
}
catch (e) {
console.error(`Couldn't set permission ${role.name} ${type}:${controller}:${action}:${enabled}`);
}
}
const authRole = await getRole("authenticated");
setPermission(authRole, "application", "page", "count", true);
setPermission(authRole, "application", "page", "find", true);
setPermission(authRole, "application", "page", "findone", true);
setPermission(authRole, "application", "post", "count", true);
setPermission(authRole, "application", "post", "find", true);
setPermission(authRole, "application", "post", "findone", true);
await service.updateRole(authRole.id, authRole);
const publicRole = await getRole("public");
setPermission(publicRole, "application", "page", "count", true);
setPermission(publicRole, "application", "page", "find", true);
setPermission(publicRole, "application", "page", "findone", true);
setPermission(publicRole, "application", "post", "count", true);
setPermission(publicRole, "application", "post", "find", true);
setPermission(publicRole, "application", "post", "findone", true);
await service.updateRole(publicRole.id, publicRole);
return;
};
本文标签: javascriptBootstraping new Strapi Role PermissionsStack Overflow
版权声明:本文标题:javascript - Bootstraping new Strapi Role Permissions - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742027846a2415889.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论