2020-05-22 12:58:14 +02:00
|
|
|
'use strict';
|
|
|
|
|
2020-06-16 13:51:34 +02:00
|
|
|
const { yup, formatYupErrors } = require('strapi-utils');
|
2020-05-27 13:15:52 +02:00
|
|
|
const {
|
|
|
|
validateRoleCreateInput,
|
|
|
|
validateRoleDeleteInput,
|
2020-07-17 10:48:51 +02:00
|
|
|
validateRolesDeleteInput,
|
2020-05-27 13:15:52 +02:00
|
|
|
} = require('../validation/role');
|
2021-03-25 14:59:44 +01:00
|
|
|
const { getService } = require('../../utils');
|
2020-06-09 17:45:53 +02:00
|
|
|
const { validatedUpdatePermissionsInput } = require('../validation/permission');
|
2020-06-23 16:31:16 +02:00
|
|
|
const { SUPER_ADMIN_CODE } = require('../../services/constants');
|
2020-05-22 12:58:14 +02:00
|
|
|
|
|
|
|
module.exports = {
|
2020-06-08 17:55:22 +02:00
|
|
|
/**
|
|
|
|
* Create a new role
|
|
|
|
* @param {KoaContext} ctx - koa context
|
|
|
|
*/
|
2020-05-22 12:58:14 +02:00
|
|
|
async create(ctx) {
|
|
|
|
try {
|
|
|
|
await validateRoleCreateInput(ctx.request.body);
|
|
|
|
} catch (err) {
|
|
|
|
return ctx.badRequest('ValidationError', err);
|
|
|
|
}
|
|
|
|
|
2021-03-25 14:59:44 +01:00
|
|
|
const roleService = getService('role');
|
|
|
|
|
|
|
|
const role = await roleService.create(ctx.request.body);
|
|
|
|
const sanitizedRole = roleService.sanitizeRole(role);
|
2020-05-22 12:58:14 +02:00
|
|
|
|
|
|
|
ctx.created({ data: sanitizedRole });
|
|
|
|
},
|
2020-06-08 17:55:22 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a role
|
|
|
|
* @param {KoaContext} ctx - koa context
|
|
|
|
*/
|
2020-05-27 16:27:09 +02:00
|
|
|
async deleteOne(ctx) {
|
|
|
|
const { id } = ctx.params;
|
|
|
|
|
2020-06-12 18:42:07 +02:00
|
|
|
try {
|
|
|
|
await validateRoleDeleteInput(id);
|
|
|
|
} catch (err) {
|
|
|
|
return ctx.badRequest('ValidationError', err);
|
|
|
|
}
|
|
|
|
|
2021-03-25 14:59:44 +01:00
|
|
|
const roleService = getService('role');
|
2020-05-27 16:27:09 +02:00
|
|
|
|
2021-03-25 14:59:44 +01:00
|
|
|
const roles = await roleService.deleteByIds([id]);
|
|
|
|
const sanitizedRole = roles.map(roleService.sanitizeRole)[0] || null;
|
2020-05-27 16:27:09 +02:00
|
|
|
|
2020-07-15 15:46:59 +02:00
|
|
|
return ctx.deleted({
|
2020-05-27 16:27:09 +02:00
|
|
|
data: sanitizedRole,
|
2020-07-15 15:46:59 +02:00
|
|
|
});
|
2020-05-27 16:27:09 +02:00
|
|
|
},
|
2020-06-08 17:55:22 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* delete several roles
|
|
|
|
* @param {KoaContext} ctx - koa context
|
|
|
|
*/
|
2020-05-27 16:27:09 +02:00
|
|
|
async deleteMany(ctx) {
|
2020-05-28 17:32:44 +02:00
|
|
|
const { body } = ctx.request;
|
2020-05-27 13:15:52 +02:00
|
|
|
try {
|
2020-06-12 18:42:07 +02:00
|
|
|
await validateRolesDeleteInput(body);
|
2020-05-27 13:15:52 +02:00
|
|
|
} catch (err) {
|
|
|
|
return ctx.badRequest('ValidationError', err);
|
|
|
|
}
|
|
|
|
|
2021-03-25 14:59:44 +01:00
|
|
|
const roleService = getService('role');
|
|
|
|
|
|
|
|
const roles = await roleService.deleteByIds(body.ids);
|
|
|
|
const sanitizedRoles = roles.map(roleService.sanitizeRole);
|
2020-05-27 13:15:52 +02:00
|
|
|
|
2020-07-15 15:46:59 +02:00
|
|
|
return ctx.deleted({
|
2020-05-27 13:15:52 +02:00
|
|
|
data: sanitizedRoles,
|
2020-07-15 15:46:59 +02:00
|
|
|
});
|
2020-05-27 13:15:52 +02:00
|
|
|
},
|
2020-06-08 17:55:22 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the permissions assigned to a role
|
|
|
|
* @param {KoaContext} ctx - koa context
|
|
|
|
*/
|
|
|
|
async updatePermissions(ctx) {
|
|
|
|
const { id } = ctx.params;
|
2021-03-25 14:59:44 +01:00
|
|
|
const { body: input } = ctx.request;
|
|
|
|
|
|
|
|
const roleService = getService('role');
|
|
|
|
const permissionService = getService('permission');
|
|
|
|
|
|
|
|
const role = await roleService.findOne({ id });
|
2020-06-08 17:55:22 +02:00
|
|
|
|
2020-06-23 16:31:16 +02:00
|
|
|
if (!role) {
|
|
|
|
return ctx.notFound('role.notFound');
|
|
|
|
}
|
|
|
|
|
2020-06-08 17:55:22 +02:00
|
|
|
try {
|
2020-06-23 16:31:16 +02:00
|
|
|
if (role.code === SUPER_ADMIN_CODE) {
|
2021-03-25 14:59:44 +01:00
|
|
|
throw formatYupErrors(new yup.ValidationError("Super admin permissions can't be edited."));
|
2020-06-16 13:51:34 +02:00
|
|
|
}
|
2021-03-25 14:59:44 +01:00
|
|
|
|
2020-06-08 17:55:22 +02:00
|
|
|
await validatedUpdatePermissionsInput(input);
|
|
|
|
} catch (err) {
|
|
|
|
return ctx.badRequest('ValidationError', err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!role) {
|
|
|
|
return ctx.notFound('role.notFound');
|
|
|
|
}
|
|
|
|
|
2021-03-25 14:59:44 +01:00
|
|
|
const permissions = await roleService.assignPermissions(role.id, input.permissions);
|
|
|
|
const sanitizedPermissions = permissions.map(permissionService.sanitizePermission);
|
2020-06-08 17:55:22 +02:00
|
|
|
|
|
|
|
ctx.body = {
|
2021-03-25 14:59:44 +01:00
|
|
|
data: sanitizedPermissions,
|
2020-06-08 17:55:22 +02:00
|
|
|
};
|
|
|
|
},
|
2020-05-22 12:58:14 +02:00
|
|
|
};
|