2020-06-09 11:48:49 +02:00
|
|
|
'use strict';
|
|
|
|
|
2020-06-12 18:42:07 +02:00
|
|
|
const _ = require('lodash');
|
2020-06-08 11:01:20 +02:00
|
|
|
const adminActions = require('../admin-actions');
|
2020-06-02 17:59:57 +02:00
|
|
|
|
2020-06-08 15:13:26 +02:00
|
|
|
const registerPermissionActions = () => {
|
2020-06-09 19:00:57 +02:00
|
|
|
const { actionProvider } = strapi.admin.services.permission;
|
2020-06-08 11:01:20 +02:00
|
|
|
actionProvider.register(adminActions.actions);
|
2020-06-02 17:59:57 +02:00
|
|
|
};
|
2020-06-08 15:13:26 +02:00
|
|
|
|
2020-06-12 18:42:07 +02:00
|
|
|
const registerAdminConditions = () => {
|
|
|
|
const { conditionProvider } = strapi.admin.services.permission;
|
|
|
|
|
|
|
|
conditionProvider.register({
|
|
|
|
displayName: 'Is Creator',
|
|
|
|
name: 'is-creator',
|
|
|
|
plugin: 'admin',
|
|
|
|
handler: user => ({ 'created_by.id': user.id }),
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-06-08 15:13:26 +02:00
|
|
|
const cleanPermissionInDatabase = async () => {
|
2020-06-11 10:54:26 +02:00
|
|
|
const { actionProvider } = strapi.admin.services.permission;
|
2020-06-08 15:13:26 +02:00
|
|
|
const dbPermissions = await strapi.admin.services.permission.find();
|
|
|
|
const allActionsMap = actionProvider.getAllByMap();
|
|
|
|
const permissionsToRemoveIds = [];
|
|
|
|
|
|
|
|
dbPermissions.forEach(perm => {
|
|
|
|
if (
|
|
|
|
!allActionsMap.has(perm.action) ||
|
|
|
|
(allActionsMap.get(perm.action).section === 'contentTypes' &&
|
|
|
|
!allActionsMap.get(perm.action).subjects.includes(perm.subject))
|
|
|
|
) {
|
|
|
|
permissionsToRemoveIds.push(perm.id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
await strapi.admin.services.permission.deleteByIds(permissionsToRemoveIds);
|
|
|
|
};
|
|
|
|
|
2020-06-12 18:42:07 +02:00
|
|
|
const getNestedFields = (attributes, fieldPath = '', nestingLevel = 3) => {
|
|
|
|
if (nestingLevel === 0) {
|
|
|
|
return fieldPath ? [fieldPath] : [];
|
|
|
|
}
|
2020-06-16 11:13:01 +02:00
|
|
|
|
2020-06-12 18:42:07 +02:00
|
|
|
const fields = [];
|
|
|
|
_.forIn(attributes, (attribute, attributeName) => {
|
|
|
|
const newFieldPath = fieldPath ? `${fieldPath}.${attributeName}` : attributeName;
|
|
|
|
|
|
|
|
if (attribute.type === 'component') {
|
|
|
|
const component = strapi.components[attribute.component];
|
|
|
|
const componentFields = getNestedFields(component.attributes, newFieldPath, nestingLevel - 1);
|
|
|
|
fields.push(...componentFields);
|
|
|
|
} else {
|
|
|
|
fields.push(newFieldPath);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return fields;
|
|
|
|
};
|
|
|
|
|
|
|
|
const createRolesIfNeeded = async () => {
|
|
|
|
const someRolesExist = await strapi.admin.services.role.exists();
|
|
|
|
if (someRolesExist) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const defaultActionsIds = [
|
|
|
|
'plugins::content-manager.read',
|
|
|
|
'plugins::content-manager.create',
|
|
|
|
'plugins::content-manager.update',
|
|
|
|
'plugins::content-manager.delete',
|
|
|
|
];
|
2020-06-15 11:54:44 +02:00
|
|
|
const allActions = strapi.admin.services.permission.actionProvider.getAll();
|
2020-06-12 18:42:07 +02:00
|
|
|
const contentTypesActions = allActions.filter(a => defaultActionsIds.includes(a.actionId));
|
|
|
|
|
|
|
|
await strapi.admin.services.role.create({
|
|
|
|
name: 'Super Admin',
|
|
|
|
code: 'strapi-super-admin',
|
|
|
|
description: 'Super Admins can access and manage all features and settings.',
|
2020-06-18 11:19:27 +02:00
|
|
|
});
|
2020-06-12 18:42:07 +02:00
|
|
|
|
|
|
|
const editorRole = await strapi.admin.services.role.create({
|
|
|
|
name: 'Editor',
|
|
|
|
code: 'strapi-editor',
|
|
|
|
description: 'Editors can manage and publish contents including those of other users.',
|
|
|
|
});
|
|
|
|
|
|
|
|
const authorRole = await strapi.admin.services.role.create({
|
|
|
|
name: 'Author',
|
|
|
|
code: 'strapi-author',
|
|
|
|
description: 'Authors can manage and publish their own content.',
|
|
|
|
});
|
|
|
|
|
|
|
|
const editorPermissions = [];
|
|
|
|
contentTypesActions.forEach(action => {
|
|
|
|
_.forIn(strapi.contentTypes, contentType => {
|
|
|
|
if (action.subjects.includes(contentType.uid)) {
|
|
|
|
const fields = getNestedFields(contentType.attributes);
|
|
|
|
editorPermissions.push({
|
|
|
|
action: action.actionId,
|
|
|
|
subject: contentType.uid,
|
|
|
|
fields,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-06-15 19:11:36 +02:00
|
|
|
const authorPermissions = _.cloneDeep(editorPermissions);
|
|
|
|
authorPermissions.forEach(p => (p.conditions = ['isOwner']));
|
|
|
|
|
2020-06-12 18:42:07 +02:00
|
|
|
await strapi.admin.services.permission.assign(editorRole.id, editorPermissions);
|
2020-06-15 19:11:36 +02:00
|
|
|
await strapi.admin.services.permission.assign(authorRole.id, authorPermissions);
|
2020-06-12 18:42:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const displayWarningIfNoSuperAdmin = async () => {
|
|
|
|
const adminRole = await strapi.admin.services.role.getAdminWithUsersCount();
|
|
|
|
const someUsersExists = await strapi.admin.services.user.exists();
|
|
|
|
if (!adminRole) {
|
|
|
|
return strapi.log.warn("Your application doesn't have a super admin role.");
|
|
|
|
} else if (someUsersExists && adminRole.usersCount === 0) {
|
|
|
|
return strapi.log.warn("Your application doesn't have a super admin user.");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const displayWarningIfUsersDontHaveRole = async () => {
|
|
|
|
const count = await strapi.admin.services.user.countUsersWithoutRole();
|
|
|
|
|
|
|
|
if (count > 0) {
|
|
|
|
strapi.log.warn(`You have ${count} user${count === 1 ? '' : 's'} without any role.`);
|
|
|
|
}
|
2020-06-16 11:13:01 +02:00
|
|
|
};
|
|
|
|
|
2020-06-08 15:13:26 +02:00
|
|
|
module.exports = async () => {
|
2020-06-16 11:13:01 +02:00
|
|
|
registerAdminConditions();
|
2020-06-08 15:13:26 +02:00
|
|
|
registerPermissionActions();
|
|
|
|
await cleanPermissionInDatabase();
|
2020-06-12 18:42:07 +02:00
|
|
|
await createRolesIfNeeded();
|
|
|
|
await displayWarningIfNoSuperAdmin();
|
|
|
|
await displayWarningIfUsersDontHaveRole();
|
2020-06-08 15:13:26 +02:00
|
|
|
};
|