【问题标题】:Bootstraping new Strapi Role Permissions引导新的 Strapi 角色权限
【发布时间】:2019-07-19 06:30:52
【问题描述】:

问题是关于Strapi - Open source Node.js Headless CMS中的角色权限

如何创建相对于AuthenticatedPublic 角色的新strapi 角色权限?我想在 bootstrap 函数中使用所有 permissions 创建新角色。我不确定payload 应该如何创建新的role

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

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

主要问题

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

附带问题

  1. 是否记录了 pluginsservices 的架构(如果是)在哪里可以找到它?
  2. queries 实例中使用servicesorm 哪个api 更好?

【问题讨论】:

  • 您好!您想在第一个引导程序上创建新角色吗?是后吗?关于这个主题,没有文档。
  • 你好吉姆!是的,我想在第一个引导程序上创建新角色,并通过所有可用权限扩展此角色。我将根据每个新角色的自定义要求为新角色配置权限 - 这是下一步

标签: javascript node.js graphql strapi


【解决方案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);

【讨论】:

  • 非常感谢。它工作得很好。有效载荷非常复杂 - 你认为这是必要的吗?
  • 我看到您使用服务。哪个api更好地使用查询实例中的服务或orm? :)
  • 我建议你使用服务。因为您可以在一点添加自定义逻辑,该逻辑将应用于使用这些服务的所有地方。所以更容易在你的代码中传播修改。
  • 非常感谢。遗憾的是,这些事情根本没有记录在案。
【解决方案2】:

我接受了@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;
};

【讨论】:

    猜你喜欢
    • 2019-11-09
    • 1970-01-01
    • 2021-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-15
    • 1970-01-01
    相关资源
    最近更新 更多