Custom plugin menu link permissions

Hi everyone!

I’m writing a custom plugin. I’m adding a menu link using the addMenuLink API, but I want to display this menu link depending on the user role (e.g. only to super-admin).

The Menu API documentation says something about a permissions Array of Objects, but I can’t find anywhere how to fill this property.

Another official guide has the following example code, but it is still not clear:

      permissions: [
        // Uncomment to set the permissions of the plugin here
        // {
        //   action: '', // the action name should be plugin::plugin-name.actionType
        //   subject: null,
        // },
      ],

How can I set the permissions?

Thanks!

I’m using Strapi v4.1.9

1 Like

Hello ! I’m stuck with the same issue.

Did you find a solution ?

thx

I found the solution ! But you will have to take the enterprise edition.

The first thing to do is to edit the file /plugins/YourPlugin/server/bootstrap.js

'use strict';

// Role Based Access Control
const RBAC_ACTIONS = [
  {
    section: 'plugins',
    displayName: 'A description of the role',
    uid: 'read',
    pluginName: 'your-plugin-name',
  },
];

/**
 *
 * @param {{strapi: import("@strapi/strapi").Strapi}} args
 */
module.exports = async ({ strapi }) => {
  await strapi.admin.services.permission.actionProvider.registerMany(RBAC_ACTIONS);

  const pluginStore = strapi.store({
    environment: '',
    type: 'plugin',
    name: 'your-plugin-name',
  });
};

Then you add the permission into the file /plugins/YourPlugin/admin/index.js

register(app) {
    app.addMenuLink({
      to: `/plugins/${ pluginId }`,
      icon: PluginIcon,
      intlLabel: {
        id: `${ pluginId }.plugin.name`,
        defaultMessage: "My Plugin",
      },
      Component: async () => {
        const component = await import(/* webpackChunkName: "[request]" */ './pages/App');

        return component;
      },
      permissions: [
        {
          action: "plugin::your-plugin-name.read",
          subject: null
        }
      ]
    });
    app.registerPlugin({
      id: pluginId,
      initializer: Initializer,
      isReady: false,
      name,
    });
  },

You can add other roles in the array RBAC_ACTIONS. The uid property is what you call in your action.

Once you’ve done that. Build your project. Log as Super Admin and go to the settings > Administration Panel > Roles
Then edit a role. Go to the plugins tab. You will see your plugin with the role you add in RBAC_ACTIONS.

If you have the community edition. The checkbox will be disabled. You have to buy a licence to manipulate the roles. You can test the enterprise edition for free during 14 days.

Good luck

1 Like

this works now with community… But i’m curious where in the docs this is explained ?
of if you got repository references can you share ?

edit I found it:

2 Likes