2020-05-18 19:54:43 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const { yup } = require('strapi-utils');
|
2020-06-15 19:11:36 +02:00
|
|
|
const _ = require('lodash');
|
2020-07-01 11:51:37 +02:00
|
|
|
const {
|
|
|
|
checkFieldsAreCorrectlyNested,
|
|
|
|
checkFieldsDontHaveDuplicates,
|
|
|
|
} = require('./common-functions');
|
2020-05-18 19:54:43 +02:00
|
|
|
|
2020-05-27 16:06:15 +02:00
|
|
|
const email = yup
|
|
|
|
.string()
|
|
|
|
.email()
|
|
|
|
.min(1);
|
|
|
|
|
|
|
|
const firstname = yup.string().min(1);
|
|
|
|
|
|
|
|
const lastname = yup.string().min(1);
|
2020-05-18 19:54:43 +02:00
|
|
|
|
2020-05-27 16:06:15 +02:00
|
|
|
const username = yup.string().min(1);
|
|
|
|
|
|
|
|
const password = yup
|
|
|
|
.string()
|
|
|
|
.min(8)
|
|
|
|
.matches(/[a-z]/, '${path} must contain at least one lowercase character')
|
|
|
|
.matches(/[A-Z]/, '${path} must contain at least one uppercase character')
|
|
|
|
.matches(/\d/, '${path} must contain at least one number');
|
|
|
|
|
2020-05-28 16:56:44 +02:00
|
|
|
const roles = yup.array(yup.strapiID()).min(1);
|
2020-05-27 16:06:15 +02:00
|
|
|
|
2020-06-09 17:45:53 +02:00
|
|
|
const isAPluginName = yup
|
|
|
|
.string()
|
|
|
|
.test('is-a-plugin-name', 'is not a plugin name', function(value) {
|
|
|
|
return [undefined, 'admin', ...Object.keys(strapi.plugins)].includes(value)
|
|
|
|
? true
|
|
|
|
: this.createError({ path: this.path, message: `${this.path} is not an existing plugin` });
|
|
|
|
});
|
|
|
|
|
2020-06-18 11:40:50 +02:00
|
|
|
const arrayOfConditionNames = yup
|
2020-06-15 19:11:36 +02:00
|
|
|
.array()
|
|
|
|
.of(yup.string())
|
|
|
|
.test('is-an-array-of-conditions', 'is not a plugin name', function(value) {
|
2020-06-18 15:34:09 +02:00
|
|
|
const ids = strapi.admin.services.permission.conditionProvider.getAll().map(c => c.id);
|
2020-06-15 19:11:36 +02:00
|
|
|
return _.isUndefined(value) || _.difference(value, ids).length === 0
|
|
|
|
? true
|
|
|
|
: this.createError({ path: this.path, message: `contains conditions that don't exist` });
|
|
|
|
});
|
|
|
|
|
2020-07-03 18:33:38 +02:00
|
|
|
const checkCTPermsDeleteHaveFieldsToNull = permissions =>
|
2020-07-06 09:56:37 +02:00
|
|
|
!Array.isArray(permissions) ||
|
2020-07-03 18:33:38 +02:00
|
|
|
permissions.every(
|
|
|
|
perm => perm.action !== 'plugins::content-manager.explorer.delete' || _.isNil(perm.fields)
|
|
|
|
);
|
|
|
|
|
|
|
|
const permissionsAreEquals = (a, b) =>
|
|
|
|
a.action === b.action && (a.subject === b.subject || (_.isNil(a.subject) && _.isNil(b.subject)));
|
|
|
|
|
|
|
|
const checkNoDuplicatedPermissions = permissions =>
|
2020-07-06 09:56:37 +02:00
|
|
|
!Array.isArray(permissions) ||
|
2020-07-03 18:33:38 +02:00
|
|
|
permissions.every((permA, i) =>
|
|
|
|
permissions.slice(i + 1).every(permB => !permissionsAreEquals(permA, permB))
|
|
|
|
);
|
|
|
|
|
2020-07-06 09:56:37 +02:00
|
|
|
const updatePermissions = yup
|
|
|
|
.object()
|
|
|
|
.shape({
|
2020-07-03 18:33:38 +02:00
|
|
|
permissions: yup
|
|
|
|
.array()
|
2020-07-06 09:56:37 +02:00
|
|
|
.requiredAllowEmpty()
|
|
|
|
.of(
|
|
|
|
yup
|
|
|
|
.object()
|
|
|
|
.shape({
|
|
|
|
action: yup.string().required(),
|
|
|
|
subject: yup.string().nullable(),
|
|
|
|
fields: yup
|
|
|
|
.array()
|
|
|
|
.of(yup.string())
|
|
|
|
.nullable()
|
|
|
|
.test(
|
|
|
|
'field-nested',
|
|
|
|
'Fields format are incorrect (bad nesting).',
|
|
|
|
checkFieldsAreCorrectlyNested
|
|
|
|
)
|
|
|
|
.test(
|
|
|
|
'field-nested',
|
|
|
|
'Fields format are incorrect (duplicates).',
|
|
|
|
checkFieldsDontHaveDuplicates
|
|
|
|
),
|
|
|
|
conditions: arrayOfConditionNames,
|
|
|
|
})
|
|
|
|
.test(
|
|
|
|
'delete-fields-are-null',
|
|
|
|
'Some permissions are duplicated (same action and subject)',
|
|
|
|
checkNoDuplicatedPermissions
|
|
|
|
)
|
|
|
|
.test(
|
|
|
|
'delete-fields-are-null',
|
|
|
|
'The action "plugins::content-manager.explorer.delete" must have fields set to null or undefined',
|
|
|
|
checkCTPermsDeleteHaveFieldsToNull
|
|
|
|
)
|
|
|
|
.noUnknown()
|
2020-07-03 18:33:38 +02:00
|
|
|
),
|
2020-07-06 09:56:37 +02:00
|
|
|
})
|
|
|
|
.required()
|
|
|
|
.noUnknown();
|
2020-06-23 16:31:16 +02:00
|
|
|
|
2020-05-27 16:06:15 +02:00
|
|
|
module.exports = {
|
|
|
|
email,
|
|
|
|
firstname,
|
|
|
|
lastname,
|
|
|
|
username,
|
|
|
|
password,
|
|
|
|
roles,
|
2020-06-09 17:45:53 +02:00
|
|
|
isAPluginName,
|
2020-06-18 11:40:50 +02:00
|
|
|
arrayOfConditionNames,
|
2020-07-06 09:56:37 +02:00
|
|
|
updatePermissions,
|
2020-05-27 16:06:15 +02:00
|
|
|
};
|