2020-05-22 12:58:14 +02:00
|
|
|
'use strict';
|
|
|
|
|
2020-06-23 16:31:16 +02:00
|
|
|
const { yup, formatYupErrors, stringIncludes, stringEquals } = require('strapi-utils');
|
2020-05-22 12:58:14 +02:00
|
|
|
|
|
|
|
const handleReject = error => Promise.reject(formatYupErrors(error));
|
|
|
|
|
2020-06-04 18:30:26 +02:00
|
|
|
const roleCreateSchema = yup
|
2020-05-22 12:58:14 +02:00
|
|
|
.object()
|
|
|
|
.shape({
|
|
|
|
name: yup
|
|
|
|
.string()
|
|
|
|
.min(1)
|
|
|
|
.required(),
|
2020-05-29 11:09:17 +02:00
|
|
|
description: yup.string().nullable(),
|
2020-05-22 12:58:14 +02:00
|
|
|
})
|
|
|
|
.noUnknown();
|
|
|
|
|
2020-06-12 18:42:07 +02:00
|
|
|
const rolesDeleteSchema = yup
|
2020-05-28 17:32:44 +02:00
|
|
|
.object()
|
|
|
|
.shape({
|
|
|
|
ids: yup
|
|
|
|
.array()
|
2020-06-04 10:25:02 +02:00
|
|
|
.of(yup.strapiID())
|
2020-05-28 17:32:44 +02:00
|
|
|
.min(1)
|
2020-06-12 18:42:07 +02:00
|
|
|
.required()
|
2020-06-15 11:54:44 +02:00
|
|
|
.test('no-admin-many-delete', 'You cannot delete the super admin role', async ids => {
|
2020-06-18 11:40:50 +02:00
|
|
|
const superAdminRole = await strapi.admin.services.role.getSuperAdmin();
|
2020-06-23 16:31:16 +02:00
|
|
|
return !superAdminRole || !stringIncludes(ids, superAdminRole.id);
|
2020-06-12 18:42:07 +02:00
|
|
|
}),
|
2020-05-28 17:32:44 +02:00
|
|
|
})
|
|
|
|
.noUnknown();
|
|
|
|
|
2020-06-12 18:42:07 +02:00
|
|
|
const roleDeleteSchema = yup
|
|
|
|
.strapiID()
|
|
|
|
.required()
|
2020-06-15 11:54:44 +02:00
|
|
|
.test('no-admin-single-delete', 'You cannot delete the super admin role', async function(id) {
|
2020-06-18 11:40:50 +02:00
|
|
|
const superAdminRole = await strapi.admin.services.role.getSuperAdmin();
|
2020-06-23 16:31:16 +02:00
|
|
|
return !superAdminRole || !stringEquals(id, superAdminRole.id)
|
2020-06-12 18:42:07 +02:00
|
|
|
? true
|
2020-06-15 11:54:44 +02:00
|
|
|
: this.createError({ path: 'id', message: `You cannot delete the super admin role` });
|
2020-06-12 18:42:07 +02:00
|
|
|
});
|
|
|
|
|
2020-05-22 12:58:14 +02:00
|
|
|
const validateRoleCreateInput = async data => {
|
2020-06-04 18:30:26 +02:00
|
|
|
return roleCreateSchema.validate(data, { strict: true, abortEarly: false }).catch(handleReject);
|
2020-05-22 12:58:14 +02:00
|
|
|
};
|
|
|
|
|
2020-06-12 18:42:07 +02:00
|
|
|
const validateRolesDeleteInput = async data => {
|
|
|
|
return rolesDeleteSchema.validate(data, { strict: true, abortEarly: false }).catch(handleReject);
|
|
|
|
};
|
|
|
|
|
2020-05-27 13:15:52 +02:00
|
|
|
const validateRoleDeleteInput = async data => {
|
|
|
|
return roleDeleteSchema.validate(data, { strict: true, abortEarly: false }).catch(handleReject);
|
|
|
|
};
|
|
|
|
|
2020-05-22 12:58:14 +02:00
|
|
|
module.exports = {
|
|
|
|
validateRoleCreateInput,
|
2020-06-12 18:42:07 +02:00
|
|
|
validateRolesDeleteInput,
|
2020-05-27 13:15:52 +02:00
|
|
|
validateRoleDeleteInput,
|
2020-05-22 12:58:14 +02:00
|
|
|
};
|