【问题标题】:Strapi Plugin Route Default PermissionStrapi 插件路由默认权限
【发布时间】:2022-04-16 06:26:55
【问题描述】:

我正在为 Strapi 构建一个包含多个路由的插件,例如:

    {
      "method": "GET",
      "path": "/preAnalyzeImportFile",
      "handler": "ImportConfig.preAnalyzeImportFile",
      "config": {
        "policies": ["global.isAuthenticated"]
      }
    }

安装插件后,任何经过身份验证的用户都应该能够使用新路由。我可以手动更改权限以使路由正常工作,但这不应该是使用插件所需的工作流程。

如何设置插件路由的默认权限?

【问题讨论】:

    标签: strapi


    【解决方案1】:

    没有关于如何在 Strapi 中执行此操作的文档,但是。

    这里是如何使用permissions函数来获取、创建、更新权限strapi.plugins['users-permissions'].models.permission。那么怎么处理呢。

    您必须在./config/function/bootstrap.js 中编写代码。 每次您的服务器启动时都会执行此代码。

    要创建您的权限,您必须找到要更新的角色(类型为authenticatedstrapi.plugins['users-permissions'].models.role.find

    当您拥有角色的 ID 后,您将使用strapi.plugins['users-permissions'].models.permission.create 创建权限

    要发送的对象参数:

    • 类型:将是您的插件的名称
    • controller:将是您的控制器的名称importconfig 在您的情况下
    • action:函数名称preanalyzeimportfile 在你的情况下
    • 启用:真
    • 角色:您要应用此策略的角色 ID

    【讨论】:

    • 插件安装后有没有办法运行一次?
    • 您不能使用post-install 脚本,因为您将无权访问strapi 变量。您必须在每次开始时检查它。但是解决方案可以是在您的插件中添加一个配置文件,并将 init 属性默认设置为 false,并在您第一次创建权限时将其更新为 true。
    • 太棒了——我会这么做的。我没有看到使用插件中的自定义配置的示例 - 你能指出我的文档或使用它自己的配置的插件(除了路由/查询/功能/策略之外的其他配置)吗?
    • #3 这似乎没问题:await strapi .query('role', 'users-permissions') .findOne({ type: 'authenticated' })
    • 是的,如果您想存储插件的设置是否完成,您可以使用core store。你可以在这里找到这个 API 的使用文档strapi.io/documentation/3.x.x/configurations/…
    【解决方案2】:

    这是您设置权限的方式。

    // In your bootstrap.js file
    'use strict';
    module.exports = async () => {
    
        const authenticated = await strapi.query('role', 'users-permissions').findOne({ type: 'authenticated' });
        authenticated.permissions.forEach(permission => {
    
            if (permission.type === 'application'){ // Whatever permissions you want to change
                let newPermission = permission;
                newPermission.enabled = true; // Editing permission as needed
    
                strapi.query('permission', 'users-permissions').update( { id: newPermission.id }, newPermission ); // Updating Strapi with the permission
            }
        });
        return;
    };
    

    【讨论】:

      【解决方案3】:

      对于 Strapi 版本 3.0.0-beta.x 及更高版本,

      Create a global policy

      ./config/policies/中创建一个名为isAuthenticated.js的JavaScript文件

      路径:./config/policies/isAuthenticated.js

      module.exports = async (ctx, next) => {
        if (ctx.state.user) {
          // Go to next policy or will reach the controller's action.
          return await next();
      }
      
        ctx.unauthorized(`You're not logged in!`);
      };
      

      在这里,我们正在验证会话是否打开。如果是这种情况,我们调用next() 方法将执行下一个策略或控制器的操作。否则返回 401 错误。

      Use the policy in your routes

       {
         "method": "GET",
         "path": "/preAnalyzeImportFile",
         "handler": "ImportConfig.preAnalyzeImportFile",
         "config": {
           "policies": ["global::isAuthenticated"]
        }
      }
      

      【讨论】:

        【解决方案4】:

        这是在 Strapi v4 中以编程方式设置插件权限的方法:

          const wantedPermission =await strapi
           .query("plugin::users-permissions.permission")
           .findOne({where: {action: "api::testssd.testssd.find"}});
        
          const publicRole = await strapi
          .query('plugin::users-permissions.role')
          .findOne({where: {type: "public"}, populate: ['users', 'permissions']});
        
          //add the permission to the public role in the db if not already present
          if (!publicRole.permissions.some(permission => permission.action == "api::testssd.testssd.find")) {
            publicRole.permissions.push(wantedPermission);
            await strapi.query('plugin::users-permissions.role').update({
               where: {id: publicRole.id},
              data: {permissions: publicRole.permissions},
            });
          }
        

        【讨论】:

          猜你喜欢
          • 2012-12-20
          • 2020-04-09
          • 1970-01-01
          • 2016-10-15
          • 2019-10-25
          • 2010-09-18
          • 1970-01-01
          • 2022-11-04
          • 2012-02-24
          相关资源
          最近更新 更多