Specifies whether the underlying grants object is frozen and all functionality for modifying it is disabled.
Documented separately in enums/Action
Documented separately in AccessControlError
Documented separately in enums/Possession
Alias of grant().
Gets an instance of Query object. This is used to check whether the
defined access is allowed for the given role(s) and resource. This
object provides chainable methods to define and query the access
permissions to be checked.
A single role (as a string), a
list of roles (as an array) or an
{@link ?api=ac#AccessControl~IQueryInfo|IQueryInfo object} that fully
or partially defines the access to be checked.
Query inner class}. Gets an instance of Access object. This is used to deny access to
specified resource(s) for the given role(s). Denying will only remove a
previously created grant. So if not granted before, you don't need to
deny an access.
A single role (as a
string), a list of roles (as an array) or an
{@link ?api=ac#AccessControl~IAccessInfo|IAccessInfo object} that
fully or partially defines the access to be denied.
The returned object provides chainable properties to
build and define the access to be granted. See
{@link ?api=ac#AccessControl~Access|Access inner class}.
Extends the given role(s) with privileges of one or more other roles.
Role(s) to be extended. Single role
as a String or multiple roles as an Array. Note that if a
role does not exist, it will be automatically created.
Role(s) to inherit from.
Single role as a String or multiple roles as an Array. Note
that if a extender role does not exist, it will throw.
AccessControl instance for chaining. Alias of getInheritedRolesOf
Gets the internal grants object that stores all current grants.
Gets the list of inherited roles by the given role.
Target role name.
Gets all the unique resources that are granted access for at least one role.
Gets all the unique roles that have at least one access information.
Gets an instance of Grant (inner) object. This is used to grant access
to specified resource(s) for the given role(s).
Access inner class}.Checks whether grants include the given resource or resources.
Resource to be checked. You can also pass an array of strings to check multiple resources at once.
Checks whether the grants include the given role or roles.
Role to be checked. You can also pass an array of strings to check multiple roles at once.
Freezes the underlying grants model and disables all functionality for
modifying it. This is useful when you want to restrict any changes. Any
attempts to modify (such as #setGrants(), #reset(), #grant(),
#deny(), etc) will throw after grants are locked. Note that there
is no unlock() method. It's like you lock the door and swallow the
key. ;)
Remember that this does not prevent the AccessControl instance from
being altered/replaced. Only the grants inner object is locked.
A note about performance: This uses recursive Object.freeze().
In NodeJS & V8, enumeration performance is not impacted because of this.
In fact, it increases the performance because of V8 optimization.
AccessControl instance for chaining. Gets an instance of Permission object that checks and defines the
granted access permissions for the target resource and role. Normally
you would use AccessControl#can() method to check for permissions but
this is useful if you need to check at once by passing a IQueryInfo
object; instead of chaining methods (as in
.can(<role>).<action>(<resource>)).
A fulfilled
{@link ?api=ac#AccessControl~IQueryInfo|IQueryInfo object}.
Permission inner class}. Alias of can().
Alias of deny().
Removes all the given resources for all roles, at once.
Pass the roles argument to remove access to resources for those
roles only.
A single or array of resources to be removed.
AccessControl instance for chaining.Removes all the given role(s) and their granted permissions, at once.
An array of roles to be removed. Also accepts a string that can be used to remove a single role.
AccessControl instance for chaining.Resets the internal grants object and removes all previous grants.
AccessControl instance for chaining.Sets all access grants at once, from an object or array. Note that this will reset the object and remove all previous grants.
A list containing the access grant definitions.
AccessControl instance for chaining.A utility method for deep cloning the given data object(s) while filtering its properties by the given attribute (glob) notations. Includes all matched properties and removes the rest.
Note that this should be used to manipulate data / arbitrary objects with enumerable properties. It will not deal with preserving the prototype-chain of the given object.
A single or array of data objects to be filtered.
The attribute glob notation(s)
to be processed. You can use wildcard stars (*) and negate
the notation by prepending a bang (!). A negated notation
will be excluded. Order of the globs do not matter, they will
be logically sorted. Loose globs will be processed first and
verbose globs or normal notations will be processed last.
e.g. [ "car.model", "*", "!car.*" ]
will be sorted as:
[ "*", "!car.*", "car.model" ].
Passing no parameters or passing an empty string ("" or [""])
will empty the source object.
Checks whether the given object is an instance of AccessControl.Error.
Object to be checked.
Alias of isACError
AccessControl class that implements RBAC (Role-Based Access Control) basics and ABAC (Attribute-Based Access Control) resource and action attributes.
Construct an
AccessControlinstance by either passing a grants object (or array fetched from database) or simply omitgrantsparameter if you are willing to build it programmatically.The
grantsobject can also be an array, such as a flat list fetched from a database.We turn this list into a hashtable for better performance. We aggregate the list by roles first, resources second. If possession (in action value or as a separate property) is omitted, it will default to
"any". e.g."create"➞"create:any"Below are equivalent:
So we can also initialize with this flat list of grants:
Onur Yıldırım onur@cutepilot.com
MIT
const ac = new AccessControl(grants);
ac.grant('admin').createAny('profile');
// or you can chain methods ac.grant('admin') .createAny('profile') .readAny('profile', ["*", "!password"]) .readAny('video') .deleteAny('video');
// since these permissions have common resources, there is an alternative way: ac.grant('admin') .resource('profile').createAny().readAny(null, ["*", "!password"]) .resource('video').readAny()..deleteAny();
ac.grant('user') .readOwn('profile', ["uid", "email", "address.", "account.", "!account.roles"]) .updateOwn('profile', ["uid", "email", "password", "address.", "!account.roles"]) .deleteOwn('profile') .createOwn('video', ["", "!geo."]) .readAny('video') .updateOwn('video', ["", "!geo.*"]) .deleteOwn('video');
// now we can check for granted or denied permissions const permission = ac.can('admin').readAny('profile'); permission.granted // true permission.attributes // ["*", "!password"] permission.filter(data) // { uid, email, address, account } // deny permission ac.deny('admin').createAny('profile'); ac.can('admin').createAny('profile').granted; // false
// To add a grant but deny access via attributes ac.grant('admin').createAny('profile', []); // no attributes allowed ac.can('admin').createAny('profile').granted; // false
// To prevent any more changes: ac.lock();