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: {
|
conditions: {
|
||||||
isBob: user => user.firstname === 'Bob',
|
isBob: async user => new Promise(resolve => resolve(user.firstname === 'Bob')),
|
||||||
isAdmin: user => user.title === 'admin',
|
isAdmin: user => user.title === 'admin',
|
||||||
isCreatedBy: user => ({ created_by: user.firstname }),
|
isCreatedBy: user => ({ created_by: user.firstname }),
|
||||||
isContainedIn: { firstname: { $in: ['Alice', 'Foo'] } },
|
isContainedIn: { firstname: { $in: ['Alice', 'Foo'] } },
|
||||||
|
@ -64,6 +64,11 @@ const assign = async (roleID, permissions = []) => {
|
|||||||
return newPermissions;
|
return newPermissions;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find all permissions for a user
|
||||||
|
* @param roles
|
||||||
|
* @returns {Promise<*[]|*>}
|
||||||
|
*/
|
||||||
const findUserPermissions = async ({ roles }) => {
|
const findUserPermissions = async ({ roles }) => {
|
||||||
if (!_.isArray(roles)) {
|
if (!_.isArray(roles)) {
|
||||||
return [];
|
return [];
|
||||||
@ -72,6 +77,11 @@ const findUserPermissions = async ({ roles }) => {
|
|||||||
return strapi.query('permission', 'admin').find({ role_in: roles.map(_.property('id')) });
|
return strapi.query('permission', 'admin').find({ role_in: roles.map(_.property('id')) });
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes unwanted fields from a permission
|
||||||
|
* @param permission
|
||||||
|
* @returns {*}
|
||||||
|
*/
|
||||||
const sanitizePermission = permission =>
|
const sanitizePermission = permission =>
|
||||||
_.pick(permission, ['action', 'subject', 'fields', 'conditions']);
|
_.pick(permission, ['action', 'subject', 'fields', 'conditions']);
|
||||||
|
|
||||||
|
@ -54,17 +54,11 @@ module.exports = conditionProvider => ({
|
|||||||
const resolveConditions = map(conditionProvider.get);
|
const resolveConditions = map(conditionProvider.get);
|
||||||
|
|
||||||
// Filter conditions, only keeps objects and functions
|
// Filter conditions, only keeps objects and functions
|
||||||
const filterValidConditions = filter(
|
const filterValidConditions = filter(_.isObject);
|
||||||
condition => _.isFunction(condition) || _.isObject(condition)
|
|
||||||
);
|
|
||||||
|
|
||||||
// Evaluate the conditions if they're a function, returns the object otherwise
|
// Evaluate the conditions if they're a function, returns the object otherwise
|
||||||
const evaluateConditions = conditions =>
|
const evaluateConditions = conditions =>
|
||||||
Promise.all(
|
Promise.all(conditions.map(cond => (_.isFunction(cond) ? cond(user, options) : cond)));
|
||||||
conditions.map(async cond =>
|
|
||||||
_.isFunction(cond) ? await cond(user, options) : Promise.resolve(cond)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
// Only keeps 'true' booleans or objects as condition's result
|
// Only keeps 'true' booleans or objects as condition's result
|
||||||
const filterValidResults = filter(result => result === true || _.isObject(result));
|
const filterValidResults = filter(result => result === true || _.isObject(result));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user