2020-05-22 12:58:14 +02:00
|
|
|
'use strict';
|
|
|
|
|
2020-12-21 17:11:48 +01:00
|
|
|
const { yup, formatYupErrors } = require('strapi-utils');
|
2020-05-22 12:58:14 +02:00
|
|
|
|
2021-01-27 11:52:02 +01:00
|
|
|
const { features } = require('../../../strapi/lib/utils/ee');
|
|
|
|
|
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-12-21 17:11:48 +01:00
|
|
|
.test('roles-deletion-checks', 'Roles deletion checks have failed', async function(ids) {
|
|
|
|
try {
|
|
|
|
await strapi.admin.services.role.checkRolesIdForDeletion(ids);
|
2021-01-27 11:52:02 +01:00
|
|
|
|
|
|
|
if (features.isEnabled('sso')) {
|
|
|
|
await strapi.admin.services.role.ssoCheckRolesIdForDeletion(ids);
|
|
|
|
}
|
2020-12-21 17:11:48 +01:00
|
|
|
} catch (e) {
|
|
|
|
return this.createError({ path: 'ids', message: e.message });
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
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-12-21 17:11:48 +01:00
|
|
|
.test('no-admin-single-delete', 'Role deletion checks have failed', async function(id) {
|
|
|
|
try {
|
|
|
|
await strapi.admin.services.role.checkRolesIdForDeletion([id]);
|
2021-01-27 11:52:02 +01:00
|
|
|
|
|
|
|
if (features.isEnabled('sso')) {
|
|
|
|
await strapi.admin.services.role.ssoCheckRolesIdForDeletion([id]);
|
|
|
|
}
|
2020-12-21 17:11:48 +01:00
|
|
|
} catch (e) {
|
|
|
|
return this.createError({ path: 'id', message: e.message });
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
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
|
|
|
};
|