Namespace: \PhpRbac\Rbac

Rbac->assign()

Description
public bool Rbac->assign(mixed $Role, mixed $Permission)

Assign a role to a permission.

Alias for Rbac->{Entity}->assign().

Parameters
Role
Accepts one of the following:
  • int ID
  • string Title
  • string Path
Permission
Accepts one of the following:
  • int ID
  • string Title
  • string Path
Return Values

Returns true if successful, false if unsuccessful.

Rbac->check()

Description
public bool Rbac->check(mixed $Permission, int $UserID = null)

Checks whether a user has a permission or not.

Parameters
Permission
Accepts one of the following:
  • int ID
  • string Title
  • string Path
UserID
User ID of a user. Must be an int.
Return Values

Returns true if a user has a permission, false if otherwise.

Rbac->enforce()

Description
public mixed Rbac->enforce(mixed $Permission, int $UserID = null)

Enforces a permission on a user.

Parameters
Permission
Accepts one of the following:
  • int ID
  • string Title
  • string Path
UserID
User ID of a user. Must be an int.
Return Values

Returns true if the user has the permission.

If the user does not have the permission two things happen:

  • A 403 HTTP status code header will be sent to the web client.
  • Script execution will terminate with a 'Forbidden: You do not have permission to access this resource.' message.

Rbac->reset()

Warning: This method removes all Roles, Permissions and Assignments from the database. Usually used for testing purposes.
Description
public bool Rbac->reset(bool $Ensure = false)

Remove all roles, permissions and assignments.

Parameters
Ensure
This is a required boolean parameter. If true is not passed an \Exception will be thrown.
Return Values

Returns true if a all roles, permissions and assignments have been reset to default values, false if otherwise.

If $ensure does not equal true, an \Exception will be thrown.

{Entity} methods are methods that are shared between the Rbac->Permissions and Rbac->Roles Classes. The return value is specific to which Class you are using.

Example:

  • Rbac->Permissions->add() will add a Permission
  • Rbac->Roles->add() will add a Role

Rbac->{Entity}->add()

Description
public int Rbac->{Entity}->add(string $Title, string $Description, int $ParentID = null)

Adds a new Role or Permission.

Parameters
Title
Accepts string Title of the new entry.
Description
Accepts string Description of the new entry.
ParentID
Optional int ID of the parent Entity in the hierarchy.
Return Values

Returns int ID of the new entry.

Rbac->{Entity}->addPath()

Description
public bool Rbac->{Entity}->addPath(string $Path, array $Descriptions = null)

Adds a Path and all its components.

Will not replace or create siblings if a component exists.

Parameters
Path
Accepts a string Path. Must begin with a / (forward slash).

Example:
$myPath = '/blog_admin/blog_editor/blog_writer/blog_member';
Descriptions
Accepts an array of string Descriptions. Will add with empty Description if not available.

Example:
$pathDescriptions = array(
    'Blog Admin',
    'Blog Editor',
    'Blog Writer',
    'Blog Member'
);
Return Values

Returns int Number of Entities created (0 if none created).

Rbac->{Entity}->assign()

Description
public bool Rbac->{Entity}->assign(mixed $Role, mixed $Permission)

Assigns a role to a permission (or vice-verse).

Parameters
Role
Accepts one of the following:
  • int ID
  • string Title
  • string Path
Permission
Accepts one of the following:
  • int ID
  • string Title
  • string Path
Return Values

Returns true if successful, false if association already exists.

Rbac->{Entity}->children()

Description
public array Rbac->{Entity}->children(int $ID)

Returns children of an Entity.

Parameters
ID
int ID of an Entity
Return Values

Returns a multidimensional array containing all children of the Entity.

Rbac->{Entity}->count()

Description
public int Rbac->{Entity}->count()

Return count of the entity.

Parameters
No Parameters.
Return Values

Returns the int count of created Entities.

Examples:
// Returns 20 if 20 Permissions have been created
$countPermissions = $rbac->Permissions->count();

// Returns 20 if 20 Roles have been created
$countRoles = $rbac->Roles->count();

Rbac->{Entity}->depth()

Description
public int Rbac->{Entity}->depth(int $ID)

Return depth of an Entity.

Parameters
ID
Accepts the int ID of an Entity
Return Values

Returns an int depth of specified Entity.

Rbac->{Entity}->descendants()

Description
public array Rbac->{Entity}->descendants(int $ID)

Returns descendants of an Entity, with their depths in integer.

Parameters
ID
Accepts an int ID of a specific Entity
Return Values

Returns an array with keys as titles and:

  • string Title
  • int ID
  • int Depth
  • string Description

Rbac->{Entity}->edit()

Description
public bool Rbac->{Entity}->edit(int $ID, string $NewTitle = null, string $NewDescription = null)

Edits an Entity, changing the Title and/or Description. Maintains ID.

Parameters
ID
Accepts the int ID of the Entity you would like to change.
NewTitle
Accepts a new string Title.
NewDescription
Accepts a new string Description.
Return Values

Returns true if successful, false if the Entity does not exist.

Rbac->{Entity}->getDescription()

Description
public mixed Rbac->{Entity}->getDescription(int $ID)

Return description of an Entity.

Parameters
ID
Accepts the int ID of an Entity
Return Values

Returns the string Description if Entity exists, null if Entity does not exist.

Rbac->{Entity}->getPath()

Description
public string Rbac->{Entity}->getPath(int $ID)

Returns the Path of an Entity.

Parameters
ID
Accepts the int ID of an Entity
Return Values

Returns the string Path of the specified Entity.

Rbac->{Entity}->getTitle()

Description
public string Rbac->{Entity}->getTitle(int $ID)

Returns the Title of an Entity.

Parameters
ID
Accepts the int ID of an Entity
Return Values

Returns the string Title of the specified Entity.

Rbac->{Entity}->parentNode()

Description
public array Rbac->{Entity}->parentNode(int $ID)

Returns parent node of an Entity.

Parameters
ID
Accepts the int ID of an Entity
Return Values

Returns an array including:

  • string Title
  • string Description
  • int ID

Rbac->{Entity}->pathId()

Description
public mixed Rbac->{Entity}->pathId(string $Path)

Returns ID of a Path.

Parameters
Path
Accepts a string Path
Return Values

Returns the int ID of the Path if it exists, null if the Path does not exist.

Example:
$somePath = '/role1/role2/role3'; // A single slash is root

Rbac->{Entity}->returnId()

Description
public mixed Rbac->{Entity}->returnId(string $Entity = null)

Returns the ID of an Entity.

Parameters
Entity
Accepts one of the following:
  • string Title
  • string Path
Return Values

Returns the Entity's ID if successful, null if unsuccessful.

Rbac->{Entity}->titleId()

Description
public int Rbac->{Entity}->titleId(string $Title)

Returns the ID belonging to a Title. Returns the ID to the first node found with the specified Title.

Parameters
Title
Accepts a string Title
Return Values

Returns the int ID of specified Title. Returns the int ID to the first node found with the specified Title.

Rbac->{Entity}->unassign()

Description
public bool Rbac->{Entity}->unassign(mixed $Role, mixed $Permission)

Unassigns a Role-Permission relation.

Parameters
Role
Accepts one of the following:
  • int ID
  • string Title
  • string Path
Permission
Accepts one of the following:
  • int ID
  • string Title
  • string Path
Return Values

Returns true if successful, false if unsuccessful.

Rbac->{Entity}->reset()

Warning: This method removes all Entities of the specified type from the database. Usually used for testing purposes.
Description
public int Rbac->{Entity}->reset(bool $Ensure = false)

Reset the table back to its initial state.

Keep in mind that this will not touch relations.

Examples:
// Resets all Permissions
$rbac->Permissions->reset(true);

// Resets all Roles
$rbac->Roles->reset(true);
Parameters
Ensure
Accepts true or false. Must be true to work, otherwise an \Exception will be thrown.
Return Values

Returns the int count of deleted entries.

Rbac->{Entity}->resetAssignments()

Description
public int Rbac->{Entity}->resetAssignments(bool $Ensure = false)

Remove all role-permission relations.

Mostly used for testing.

Parameters
Ensure
Accepts true or false. Must be true to work, otherwise an \Exception will be thrown.
Return Values

Returns the int count of deleted assignments.

These methods can only be called using the Rbac->Permissions Class.

Rbac->Permissions->remove()

Description
public bool Rbac->Permissions->remove(int $ID, bool $Recursive = false)

Remove Permissions from system.

Parameters
ID
The int ID of the Permission.
Recursive
If set to true, all descendants of the Permission will also be removed.
Return Values

Returns true if successful, false if unsuccessful.

Rbac->Permissions->roles()

Description
public array Rbac->Permissions->roles(mixed $Permission, bool $OnlyIDs = true)

Returns all Roles assigned to a Permission.

Parameters
Permission
Accepts one of the following:
  • int ID
  • string Title
  • string Path
OnlyIDs
If true, result will be a 1D array of IDs
Return Values

Returns a 1D or 2D array depending on the parameter $OnlyIDs.

Returns null if no Roles are assigned to specified Permission.

Rbac->Permissions->unassignRoles()

Description
public int Rbac->Permissions->unassignRoles(int $ID)

Unassigns all Roles belonging to a Permission.

Parameters
ID
Accepts the int ID of the Permission in question.
Return Values

Returns int number of assignments deleted.

These methods can only be called using the Rbac->Roles Class.

Rbac->Roles->hasPermission()

Description
public bool Rbac->Roles->hasPermission(int $Role, integer $Permission)

Checks to see if a Role has a Permission or not.

Parameters
Role
Accepts the int ID of a Role
Permission
Accepts the int ID of a Permission
Return Values

Returns true if the specified Role has the specified Permission, false if otherwise.

Rbac->Roles->permissions()

Description
public mixed Rbac->Roles->permissions(int $Role, bool $OnlyIDs = true)

Returns all Permissions assigned to a Role.

Parameters
Role
The int ID of the Role you would like to inspect.
OnlyIDs
If set to true, result is a 1D array of Permission ID's.
If set to false the result is a 2d array that includes the ID, Title and Description of Permissions assigned to the Role.
Return Values

If the parameter $OnlyIDs is set to true, result is a 1D array of Permission ID's.

If the parameter $OnlyIDs is set to false the result is a 2d array that includes the ID, Title and Description of Permissions assigned to the Role.

Returns null if unsuccessful.

Rbac->Roles->remove()

Description
public bool Rbac->Roles->remove(int $ID, bool $Recursive = false)

Remove Roles from system.

Parameters
ID
The int ID of the Role
Recursive
If set to true, all descendants of the Permission will also be removed.
Return Values

Returns true if successful, false if unsuccessful.

Rbac->Roles->unassignPermissions()

Description
public int Rbac->Roles->unassignPermissions(int $ID)

Unassigns all Permissions belonging to a Role.

Parameters
ID
Accepts the int ID of the Role in question.
Return Values

Returns int number of assignments deleted.

Rbac->Roles->unassignUsers()

Description
public int Rbac->Roles->unassignUsers(int $ID)

Unassign all Users that have a certain Role.

Parameters
ID
Accepts the int ID of a Role
Return Values

Returns int number of assignments deleted.

These methods can only be called using the Rbac->Users Class.

Rbac->Users->allRoles()

Description
public mixed Rbac->Users->allRoles(int $UserID = null)

Returns all Roles of a User.

Parameters
UserID
Accepts the int ID of a User (Not optional)
Throws RbacUserNotProvidedException if not provided
Return Values

Returns an array of all Roles assigned to the User.

Returns null if no Roles are assigned to the User.

Rbac->Users->assign()

Description
public bool Rbac->Users->assign(mixed $Role, int $UserID = null)

Assigns a role to a user

Parameters
Role
Accepts one of the following:
  • int ID
  • string Title
  • string Path
UserID
Accepts an int UserID, provided from external User Management System.
Use 0 for Guest.
Return Values

Returns true if the assignment was created successfuly.

Returns false if the assignment already exists.

Throws \RbacUserNotProvidedException Exception if UserID is not provided.

Rbac->Users->hasRole()

Description
public bool Rbac->Users->hasRole(mixed $Role, int $UserID = null)

Checks to see whether a User has a Role or not.

Parameters
Role
Accepts one of the following:
  • int ID
  • string Title
  • string Path
UserID
Accepts an int UserID, provided from external User Management System.
Return Values

Returns true if successful, false if unsuccessful.

Rbac->Users->resetAssignments()

Description
public int Rbac->Users->resetAssignments(bool $Ensure = false)

Remove all Role-User assignments.

Mostly used for testing.

Parameters
Ensure
Accepts true or false. Must be true to work, otherwise an \Exception will be thrown.
Return Values

Returns the int count of deleted assignments.

Rbac->Users->roleCount()

Description
public int Rbac->Users->roleCount(int $UserID = null)

Return count of Roles assigned to a User.

Parameters
UserID
Accepts the int ID of a User (Not optional)
Throws RbacUserNotProvidedException if not provided
Return Values

Returns int number of Roles assigned to the User.

Rbac->Users->unassign()

Description
public bool Rbac->Users->unassign(mixed $Role, int $UserID = null)

Unassigns a Role from a User.

Parameters
Role
Accepts one of the following:
  • int ID
  • string Title
  • string Path
UserID
Accepts an int UserID, provided from external User Management System.
Use 0 for Guest.
Return Values

Returns true if successful, false if unsuccessful.