Search code examples
javascriptnode.jsgraphqlstrapi

Bootstraping new Strapi Role Permissions


The question is about role permissions in Strapi - Open source Node.js Headless CMS

How to create new strapi role permissions relative to Authenticated or Public role? I want to create new role with these same all permissions in the bootstrap function. I'm not sure how should look payload to create new role.

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

I checked relative stackoverflow question but isn't about creating new strapi role permission Bootstraping Strapi Role Permissions!

main problem

  1. How look the best way to resolve above problem?

side questions

  1. Is documented schema for plugins or services if yes where could find it?
  2. Which api is better use services or orm from queries instance?

Solution

  • OKay so since the data structure can change I will give you the way to access to useful data that will help you to deal with what you want.

    In this this file you will find service function that create roles and the function that inits permissions.

    You can write your code in your bootstrap.js file.

    1. You will have to call the functions that let you generate permission object
    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. Customize this object (the permissions object) to allow controller functions you want.

    2. And finaly create your role

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