Strapi Plugin Route Default Permission

情到浓时终转凉″ 提交于 2019-12-24 20:52:00

问题


I am building a plugin for Strapi with several routes, for example:

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

When the plugin is installed, any authenticated user should be able to use the new routes. I can change the permissions manually so that the routes work, but that should not be a required workflow to use the plugin.

How do I set default permissions for plugin routes?


回答1:


There is no documentation about how to do it in Strapi but.

Here is how to use permissions function to get, create, update permissions strapi.plugins['users-permissions'].models.permission. So how to deal with.

You will have to write your code in the ./config/function/bootstrap.js. This code is executed every time your server start.

To create your permission you will have to find the role you want to update (with the type authenticated) strapi.plugins['users-permissions'].models.role.find.

When you have the id of the role you will create a permission with strapi.plugins['users-permissions'].models.permission.create

Object params to send:

  • type: will be the name of your plugin
  • controller: will be the name of your controller importconfig in your case
  • action: the name of the function preanalyzeimportfile in your case
  • enabled: true
  • role: the role id you want to apply this policy


来源:https://stackoverflow.com/questions/55924388/strapi-plugin-route-default-permission

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!