Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat(cms): add bootstrap functions to add editor permissions #6

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions cms-backend/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// import type { Core } from '@strapi/strapi';
import type { Core } from '@strapi/strapi';

export default {
/**
Expand All @@ -16,5 +16,44 @@ export default {
* This gives you an opportunity to set up your data model,
* run jobs, or perform some special logic.
*/
bootstrap(/* { strapi }: { strapi: Core.Strapi } */) {},
async bootstrap({ strapi }: { strapi: Core.Strapi }) {
// Get the role ID of the Editor
const editorRole = await strapi.query('plugin::users-permissions.role').findOne({
where: { name: 'Editor' },
});

if (!editorRole) {
strapi.log.error('Editor role not found');
return;
}

const editorRoleId = editorRole.id;

// Define the default permissions for new content types (optional - customize as needed)
const defaultPermissions = ['create', 'read', 'update', 'delete'];

// Get the editor role permissions
const rolePermissions = await strapi.query('plugin::users-permissions.permission').findMany({
where: { role: editorRoleId },
});

const contentTypesWithPermission = rolePermissions.map((permission) => permission.controller);

const contentTypes = Object.keys(strapi.contentTypes);

for (const contentType of contentTypes) {
if (!contentTypesWithPermission.includes(contentType)) {
const permissionsToAdd = defaultPermissions.map((action) => ({
action: `plugin::content-manager.explorer.${contentType}.${action}`,
role: editorRoleId,
}));

await strapi.query('plugin::users-permissions.permission').createMany({
data: permissionsToAdd,
});

strapi.log.info(`Editor permissions granted for the content type: ${contentType}`);
}
}
},
};