2020-05-22 12:58:14 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const { yup, formatYupErrors } = require('strapi-utils');
|
|
|
|
|
|
|
|
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-04 18:30:26 +02:00
|
|
|
const roleUpdateSchema = yup
|
|
|
|
.object()
|
|
|
|
.shape({
|
|
|
|
name: yup.string().min(1),
|
|
|
|
description: yup.string().nullable(),
|
|
|
|
})
|
|
|
|
.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-12 18:42:07 +02:00
|
|
|
const adminRole = await strapi.admin.services.role.getAdmin();
|
2020-06-16 18:49:49 +02:00
|
|
|
return !adminRole || !ids.map(String).includes(String(adminRole.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-12 18:42:07 +02:00
|
|
|
const adminRole = await strapi.admin.services.role.getAdmin();
|
2020-06-16 18:49:49 +02:00
|
|
|
return !adminRole || String(id) !== String(adminRole.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
|
|
|
};
|
|
|
|
|
|
|
|
const validateRoleUpdateInput = async data => {
|
2020-06-04 18:30:26 +02:00
|
|
|
return roleUpdateSchema.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,
|
|
|
|
validateRoleUpdateInput,
|
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
|
|
|
};
|