mirror of
https://github.com/strapi/strapi.git
synced 2025-09-07 15:49:24 +00:00
Fix pr comments (add doc, simplify engine code, add async test condition)
Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu>
This commit is contained in:
parent
a0b4132217
commit
027848eaa7
@ -44,7 +44,7 @@ describe('Permissions Engine', () => {
|
||||
},
|
||||
},
|
||||
conditions: {
|
||||
isBob: user => user.firstname === 'Bob',
|
||||
isBob: async user => new Promise(resolve => resolve(user.firstname === 'Bob')),
|
||||
isAdmin: user => user.title === 'admin',
|
||||
isCreatedBy: user => ({ created_by: user.firstname }),
|
||||
isContainedIn: { firstname: { $in: ['Alice', 'Foo'] } },
|
||||
|
@ -64,6 +64,11 @@ const assign = async (roleID, permissions = []) => {
|
||||
return newPermissions;
|
||||
};
|
||||
|
||||
/**
|
||||
* Find all permissions for a user
|
||||
* @param roles
|
||||
* @returns {Promise<*[]|*>}
|
||||
*/
|
||||
const findUserPermissions = async ({ roles }) => {
|
||||
if (!_.isArray(roles)) {
|
||||
return [];
|
||||
@ -72,6 +77,11 @@ const findUserPermissions = async ({ roles }) => {
|
||||
return strapi.query('permission', 'admin').find({ role_in: roles.map(_.property('id')) });
|
||||
};
|
||||
|
||||
/**
|
||||
* Removes unwanted fields from a permission
|
||||
* @param permission
|
||||
* @returns {*}
|
||||
*/
|
||||
const sanitizePermission = permission =>
|
||||
_.pick(permission, ['action', 'subject', 'fields', 'conditions']);
|
||||
|
||||
|
@ -54,17 +54,11 @@ module.exports = conditionProvider => ({
|
||||
const resolveConditions = map(conditionProvider.get);
|
||||
|
||||
// Filter conditions, only keeps objects and functions
|
||||
const filterValidConditions = filter(
|
||||
condition => _.isFunction(condition) || _.isObject(condition)
|
||||
);
|
||||
const filterValidConditions = filter(_.isObject);
|
||||
|
||||
// Evaluate the conditions if they're a function, returns the object otherwise
|
||||
const evaluateConditions = conditions =>
|
||||
Promise.all(
|
||||
conditions.map(async cond =>
|
||||
_.isFunction(cond) ? await cond(user, options) : Promise.resolve(cond)
|
||||
)
|
||||
);
|
||||
Promise.all(conditions.map(cond => (_.isFunction(cond) ? cond(user, options) : cond)));
|
||||
|
||||
// Only keeps 'true' booleans or objects as condition's result
|
||||
const filterValidResults = filter(result => result === true || _.isObject(result));
|
||||
|
Loading…
x
Reference in New Issue
Block a user