backend/src/bootstrap/set-permissions.ts (view raw)
1export default async ({ strapi }) => {
2 const { permissions = [] } = strapi.config;
3 const roles = await strapi.query("plugin::users-permissions.role").findMany();
4
5 for (const role of roles) {
6 const rolePermissions = permissions.roles[role.type];
7
8 if (!rolePermissions) continue;
9
10 await Promise.all(
11 rolePermissions.map(async (action) => {
12 const existingPerm = await strapi
13 .query("plugin::users-permissions.permission")
14 .findOne({ where: { action, role: role.id } });
15
16 if (!existingPerm) {
17 strapi.log.debug(`Create permission ${action} for role ${role.type}`);
18 strapi.query("plugin::users-permissions.permission").create({
19 data: {
20 action,
21 role: role.id,
22 },
23 });
24 }
25 })
26 );
27 }
28};