引导新的Strapi角色权限

时间:2019-07-19 06:30:52

标签: javascript node.js graphql strapi

问题与Strapi - Open source Node.js Headless CMS

中的角色权限有关

如何相对于AuthenticatedPublic角色创建新的bandi角色权限?我想在引导函数中使用所有相同的permissions创建新角色。我不确定payload的外观如何来创建新的role

const payload = ?
strapi.plugins['users-permissions']
    .queries('role', 'users-permissions')
    .create(payload)

我检查了相对的stackoverflow问题,但不是关于创建新的bandi角色权限 Bootstraping Strapi Role Permissions

主要问题

  1. 如何找到解决上述问题的最佳方法?

附带问题

  1. 是否已记录pluginsservices的架构(如果可以)在哪里找到?
  2. 哪个API最好使用services实例中的ormqueries

2 个答案:

答案 0 :(得分:1)

好的,因为数据结构可以更改,所以我将为您提供访问有用数据的方法,这些数据将帮助您处理所需的内容。

在此this file中,您将找到用于创建角色的服务功能以及用于初始化权限的功能。

您可以在bootstrap.js文件中编写代码。

  1. 您将必须调用使您生成权限对象的函数
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);
  1. 自定义此对象(permissions对象)以允许所需的控制器功能。

  2. 并最终创建您的角色

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);

答案 1 :(得分:1)

我接受了@Jim LAURIE的回答,并将其与更新现有角色的权限相关。看node_modules/strapi-plugin-users-permissions/services/UsersPermissions.js的指针非常有用:)

我有一个pagepost类型的代码,此代码可让默认的经过身份验证的用户和公共用户对它们执行所有查看操作。

(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 () => {

  // https://stackoverflow.com/a/57184017/268555
  // 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;
};