引导新的 Strapi 角色权限
Bootstraping new Strapi Role Permissions
问题是关于 Strapi - Open source Node.js Headless CMS
中的角色权限
如何创建新的 strapi 角色相对于 Authenticated
或 Public
角色的权限?我想在 bootstrap 函数中使用这些相同的所有 permissions
创建新角色。我不确定 payload
应该如何创建新的 role
.
const payload = ?
strapi.plugins['users-permissions']
.queries('role', 'users-permissions')
.create(payload)
我检查了相关的 Whosebug 问题,但不是关于创建新的 strapi 角色权限
!
主要问题
- 如何寻找解决上述问题的最佳方法?
边题
- 是否记录了
plugins
或 services
的架构,如果是,在哪里可以找到它?
- 使用
queries
实例中的 services
或 orm
哪个 api 更好?
好的,既然数据结构可以改变,我将为您提供访问有用数据的方法,这将帮助您处理您想要的事情。
在此this file中,您将找到创建角色的服务函数和初始化权限的函数。
您可以在 bootstrap.js
文件中编写代码。
- 您将必须调用让您生成权限对象的函数
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);
自定义此对象(permissions
对象)以允许您需要的控制器功能。
最后创建你的角色
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);
我采纳了@Jim LAURIE 的回答,并使其与更新现有角色的权限相关。查看 node_modules/strapi-plugin-users-permissions/services/UsersPermissions.js
的指针非常有用:)
我有一个 page
和 post
类型,此代码允许默认身份验证和 public 用户对他们执行所有查看操作。
(PS 我正在使用 TypeScript 注释,但这是普通的 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 () => {
//
// Ref to https://github.com/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;
};
问题是关于 Strapi - Open source Node.js Headless CMS
中的角色权限如何创建新的 strapi 角色相对于 Authenticated
或 Public
角色的权限?我想在 bootstrap 函数中使用这些相同的所有 permissions
创建新角色。我不确定 payload
应该如何创建新的 role
.
const payload = ?
strapi.plugins['users-permissions']
.queries('role', 'users-permissions')
.create(payload)
我检查了相关的 Whosebug 问题,但不是关于创建新的 strapi 角色权限
主要问题
- 如何寻找解决上述问题的最佳方法?
边题
- 是否记录了
plugins
或services
的架构,如果是,在哪里可以找到它? - 使用
queries
实例中的services
或orm
哪个 api 更好?
好的,既然数据结构可以改变,我将为您提供访问有用数据的方法,这将帮助您处理您想要的事情。
在此this file中,您将找到创建角色的服务函数和初始化权限的函数。
您可以在 bootstrap.js
文件中编写代码。
- 您将必须调用让您生成权限对象的函数
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);
自定义此对象(
permissions
对象)以允许您需要的控制器功能。最后创建你的角色
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);
我采纳了@Jim LAURIE 的回答,并使其与更新现有角色的权限相关。查看 node_modules/strapi-plugin-users-permissions/services/UsersPermissions.js
的指针非常有用:)
我有一个 page
和 post
类型,此代码允许默认身份验证和 public 用户对他们执行所有查看操作。
(PS 我正在使用 TypeScript 注释,但这是普通的 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 () => {
//
// Ref to https://github.com/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;
};