Roles utility module for handling role-based access control and object merging
- Source
Methods
(inner) check(obj, user_roles) → {Object|boolean}
Checks if an object should be accessible based on user roles
Name | Type | Description | ||||||
---|---|---|---|---|---|---|---|---|
obj | Object | The object to check access for Properties
| ||||||
user_roles | Array.<string> | Array of roles assigned to the user |
- Source
Returns the original object if access is granted, false otherwise
- Type:
- Object |
boolean
// Object with unrestricted access
check({ roles: { '*': true }, data: 'content' }, ['user']) // returns object
// Object with role restriction
check({ roles: { admin: true }, data: 'content' }, ['user']) // returns false
check({ roles: { admin: true }, data: 'content' }, ['admin']) // returns object
// Object with negated roles
check({ roles: { '!guest': true }, data: 'content' }, ['guest']) // returns false
check({ roles: { '!guest': true }, data: 'content' }, ['user']) // returns object
(inner) notIncludesNegatedRole(role, user_roles) → {Boolean}
The utility method checks whether a negated role [prefixed with an exclamation mark !] is not included in the array of user roles.
Name | Type | Description |
---|---|---|
role | String | A role name |
user_roles | Array.<string> | Array of roles assigned to the user |
- Source
True if the negated role is not included in the user_roles array.
- Type:
- Boolean
(inner) objMerge(obj, user_roles) → {Object}
Recursively merges role-specific object properties based on user roles. The function handles several special cases:
- Recursively processes nested objects
- Handles arrays by mapping over their elements
- Processes negated roles (prefixed with '!')
- Preserves the original object if conditions aren't met
- Skip null or undefined values
const obj = {
name: 'layer',
roles: {
admin: { secretField: 'sensitive' },
user: { publicField: 'visible' }
}
};
// With admin role
objMerge(obj, ['admin']);
// Returns: { name: 'layer', secretField: 'sensitive', roles: {...} }
// With user role
objMerge(obj, ['user']);
// Returns: { name: 'layer', publicField: 'visible', roles: {...} }
Name | Type | Description |
---|---|---|
obj | Object | The object to process |
user_roles | Array.<string> | Array of roles assigned to the user |
Name | Type | Description |
---|---|---|
obj.roles | roles | Role configuration object |
- Source
Processed object with merged role-specific properties
- Type:
- Object