Roles utility module for handling role-based access control and object merging

Methods

(inner) check(obj, user_roles) → {Object|boolean}

Checks if an object should be accessible based on user roles

Parameters:
NameTypeDescription
objObject

The object to check access for

Properties
NameTypeDescription
rolesroles

Role configuration object

user_rolesArray.<string>

Array of roles assigned to the user

Returns:

Returns the original object if access is granted, false otherwise

Type: 
Object | boolean
Example
// 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.

Parameters:
NameTypeDescription
roleString

A role name

user_rolesArray.<string>

Array of roles assigned to the user

Returns:

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: {...} }
Parameters:
NameTypeDescription
objObject

The object to process

user_rolesArray.<string>

Array of roles assigned to the user

Properties
NameTypeDescription
obj.rolesroles

Role configuration object

Returns:

Processed object with merged role-specific properties

Type: 
Object