How to Register Permissions in Strapi Plugin

m4r14

mark vachi

Posted on October 31, 2024

How to Register Permissions in Strapi Plugin

Registering permissions in a Strapi plugin helps manage user access to specific actions. Here’s a quick guide:

Step 1: Define Permission Actions

In bootstrap.ts, define the permission action:

// src/plugins/exporter/server/bootstrap.ts
import { Strapi } from '@strapi/strapi';

export default ({ strapi }: { strapi: Strapi }) => {
  registerPermissionActions();
};

const registerPermissionActions = async () => {
  const actions = [
    {
      section: 'plugins',
      displayName: 'Export',
      uid: 'export',
      pluginName: 'exporter',
    },
  ];

  await strapi.admin.services.permission.actionProvider.registerMany(actions);
};
Enter fullscreen mode Exit fullscreen mode

Step 2: Add Permissions to the Admin Menu

In index.tsx, link the permission to the admin interface:

// src/plugins/exporter/admin/src/index.tsx
export default {
  register(app: any) {
    app.addMenuLink({
      // other properties
      permissions: [
        {
          action: 'plugin::exporter.export', // Format: plugin::plugin-name.actionType
          subject: null,
        },
      ],
    });
  },
};
Enter fullscreen mode Exit fullscreen mode

Summary

These steps allow you to register and manage permissions effectively within your Strapi plugin.

💖 💪 🙅 🚩
m4r14
mark vachi

Posted on October 31, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related