问题
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