Role
Role class Manages role identity and capability checks within the WP-Node context.
Description
- Encapsulates a user's role and capabilities.
- Supports capability checks with hierarchical roles like
administrator
andsuperadmin
. - Works in both single-site and multi-site WordPress setups.
- Integrates with the
Capabilities
service for advanced permission checking.
Initializes a role with:
primaryName
: base role name like"editor"
(default is"anonymous"
).capabilities
: an array of WordPress-style capabilities.
Properties
names: Set<string>;
All role names associated with the current user. Stored in lowercase.
capabilities: Set<string>;
Set of granted capability strings, like "edit_posts"
or "delete_users"
.
primaryName: types.RoleNames;
Primary role name assigned to this Role instance.
Methods
addNames
addNames(names: string[]): void
Adds additional role names to the names
set.
add
add(capabilities: string[]): void
Adds capability strings to the capabilities
set.
is
is<T extends types.RoleNames>(roleName: T): boolean
Checks if this role matches the provided role name.
has
has<T extends typeof ROLE_CAPABILITIES[number]>(cap: T): boolean
Checks if this role has the given capability.
isAdmin
isAdmin(): boolean
Returns true
if the role is administrator
or has super admin privileges.
isSuperAdmin
isSuperAdmin(): boolean
Returns true
if:
- On multisite and role includes
"superadmin"
. - Or if not on multisite and the role has the
"delete_users"
capability.
can
can<T extends types.RoleCapabilityActions>(
action: T,
user: number | User,
...args: any[]
): Promise<boolean>
Asynchronously checks if the role can perform the specified action:
- Returns
false
if any result isDO_NOT_ALLOW
. - Returns
true
only if all required capabilities are present.