PhpManager
API reference › yii › rbac › PhpManager
PhpManager represents an authorization manager that stores authorization information in terms of a PHP script file.
The authorization data will be saved to and loaded from three files
specified by $itemFile, $assignmentFile and $ruleFile.
PhpManager is mainly suitable for authorization data that is not too big
(for example, the authorization data for a personal blog system).
Use DbManager for more complex authorization data.
For more details and usage information on PhpManager, see the guide article on security authorization.
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$assignmentFile | string | The path of the PHP script that contains the authorization assignments. Default: '@app/rbac/assignments.php'. | PhpManager |
$behaviors read-only | array<yii\<static>> | List of behaviors attached to this component. | yii\ |
$defaultRoleInstances read-only | Role[] | Default roles. | BaseManager |
$itemFile | string | The path of the PHP script that contains the authorization items. Default: '@app/rbac/items.php'. | PhpManager |
$permissions read-only | Permission[] | All permissions in the system. | BaseManager |
$roles read-only | Role[] | All roles in the system. | BaseManager |
$ruleFile | string | The path of the PHP script that contains the authorization rules. Default: '@app/rbac/rules.php'. | PhpManager |
$assignments protected | array | Default: []. | PhpManager |
$children protected | array | Default: []. | PhpManager |
$defaultRoles protected | array | A list of role names that are assigned to every user automatically without calling assign(). Default: []. | BaseManager |
$items protected | Item[] | Default: []. | PhpManager |
$rules protected | Rule[] | Default: []. | PhpManager |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | yii\ |
__call() | Calls the named method which is not a class method. | yii\ |
__clone() | This method is called after the object is created by cloning an existing one. | yii\ |
__get() | Returns the value of a component property. | yii\ |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\ |
__set() | Sets the value of a component property. | yii\ |
__unset() | Sets a component property to be null. | yii\ |
add() | Adds a role, permission or rule to the RBAC system. | BaseManager |
addChild() | Adds an item as a child of another item. | PhpManager |
assign() | Assigns a role to a user. | PhpManager |
attachBehavior() | Attaches a behavior to this component. | yii\ |
attachBehaviors() | Attaches a list of behaviors to the component. | yii\ |
behaviors() | Returns a list of behaviors that this component should behave as. | yii\ |
canAddChild() | Checks the possibility of adding a child to parent. | PhpManager |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\ |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\ |
checkAccess() | PhpManager | |
className() static | Returns the fully qualified name of this class. | yii\ |
createPermission() | Creates a new Permission object. | BaseManager |
createRole() | Creates a new Role object. | BaseManager |
detachBehavior() | Detaches a behavior from the component. | yii\ |
detachBehaviors() | Detaches all behaviors from the component. | yii\ |
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | yii\ |
getAssignment() | Returns the assignment information regarding a role and a user. | PhpManager |
getAssignments() | Returns all role assignment information for the specified user. | PhpManager |
getBehavior() | Returns the named behavior object. | yii\ |
getBehaviors() | Returns all behaviors attached to this component. | yii\ |
getChildren() | Returns the child permissions and/or roles. | PhpManager |
getChildRoles() | Returns child roles of the role specified. Depth isn’t limited. | PhpManager |
getDefaultRoleInstances() | Returns defaultRoles as array of Role objects. | BaseManager |
getDefaultRoles() | Get default roles | BaseManager |
getItem() | Returns the named auth item. | PhpManager |
getItems() | Returns the items of the specified type. | PhpManager |
getPermission() | Returns the named permission. | BaseManager |
getPermissions() | Returns all permissions in the system. | BaseManager |
getPermissionsByRole() | Returns all permissions that the specified role represents. | PhpManager |
getPermissionsByUser() | Returns all permissions that the user has. | PhpManager |
getRole() | Returns the named role. | BaseManager |
getRoles() | Returns all roles in the system. | BaseManager |
getRolesByUser() | The roles returned by this method include the roles assigned via $defaultRoles. | PhpManager |
getRule() | Returns the rule of the specified name. | PhpManager |
getRules() | Returns all rules available in the system. | PhpManager |
getUserIdsByRole() | Returns all user IDs assigned to the role specified. | PhpManager |
hasChild() | Returns a value indicating whether the child already exists for the parent. | PhpManager |
hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. | yii\ |
hasMethod() | Returns a value indicating whether a method is defined. | yii\ |
hasProperty() | Returns a value indicating whether a property is defined for this component. | yii\ |
init() | Initializes the application component. | PhpManager |
off() | Detaches an existing event handler from this component. | yii\ |
on() | Attaches an event handler to an event. | yii\ |
remove() | Removes a role, permission or rule from the RBAC system. | BaseManager |
removeAll() | Removes all authorization data, including roles, permissions, rules, and assignments. | PhpManager |
removeAllAssignments() | Removes all role assignments. | PhpManager |
removeAllPermissions() | Removes all permissions. | PhpManager |
removeAllRoles() | Removes all roles. | PhpManager |
removeAllRules() | Removes all rules. | PhpManager |
removeChild() | Removes a child from its parent. | PhpManager |
removeChildren() | Removed all children form their parent. | PhpManager |
removeItem() | Removes an auth item from the RBAC system. | PhpManager |
revoke() | Revokes a role from a user. | PhpManager |
revokeAll() | Revokes all roles from a user. | PhpManager |
setDefaultRoles() | Set default roles | BaseManager |
trigger() | Triggers an event. | yii\ |
update() | Updates the specified role, permission or rule in the system. | BaseManager |
updateRule() | Updates a rule to the RBAC system. | PhpManager |
addItem() protected | Adds an auth item to the RBAC system. | PhpManager |
addRule() protected | Adds a rule to the RBAC system. | PhpManager |
checkAccessRecursive() protected | Performs access check for the specified user. | PhpManager |
detectLoop() protected | Checks whether there is a loop in the authorization item hierarchy. | PhpManager |
executeRule() protected | Executes the rule associated with the specified auth item. | BaseManager |
getChildrenRecursive() protected | Recursively finds all children and grand children of the specified item. | PhpManager |
getDirectPermissionsByUser() protected | Returns all permissions that are directly assigned to user. | PhpManager |
getInheritedPermissionsByUser() protected | Returns all permissions that the user inherits from the roles assigned to him. | PhpManager |
hasNoAssignments() protected | Checks whether array of $assignments is empty and $defaultRoles property is empty as well. | BaseManager |
invalidateScriptCache() protected | Invalidates precompiled script cache (OPcache) for the given file. | PhpManager |
load() protected | Loads authorization data from persistent storage. | PhpManager |
loadFromFile() protected | Loads the authorization data from a PHP script file. | PhpManager |
removeAllItems() protected | Removes all auth items of the specified type. | PhpManager |
removeRule() protected | Removes a rule from the RBAC system. | PhpManager |
save() protected | Saves authorization data into persistent storage. | PhpManager |
saveAssignments() protected | Saves assignments data into persistent storage. | PhpManager |
saveItems() protected | Saves items data into persistent storage. | PhpManager |
saveRules() protected | Saves rules data into persistent storage. | PhpManager |
saveToFile() protected | Saves the authorization data to a PHP script file. | PhpManager |
updateItem() protected | Updates an auth item in the RBAC system. | PhpManager |
Property details
Section titled “Property details”$assignmentFile
Section titled “$assignmentFile ”public string $assignmentFile = '@app/rbac/assignments.php'The path of the PHP script that contains the authorization assignments.
This can be either a file path or a path alias to the file. Make sure this file is writable by the Web server process if the authorization needs to be changed online.
$itemFile
Section titled “$itemFile ”public string $itemFile = '@app/rbac/items.php'The path of the PHP script that contains the authorization items.
This can be either a file path or a path alias to the file. Make sure this file is writable by the Web server process if the authorization needs to be changed online.
$ruleFile
Section titled “$ruleFile ”public string $ruleFile = '@app/rbac/rules.php'The path of the PHP script that contains the authorization rules.
This can be either a file path or a path alias to the file. Make sure this file is writable by the Web server process if the authorization needs to be changed online.
Method details
Section titled “Method details”addChild()
Section titled “addChild() ”public function addChild(mixed $parent, mixed $child): boolAdds an item as a child of another item.
Parameters
| Parameter | Type | Description |
|---|---|---|
$parent | mixed | |
$child | mixed |
Returns
bool: Whether the child successfully added
Throws
yii\base\Exception: if the parent-child relationship already exists or if a loop has been detected.
assign()
Section titled “assign() ”public function assign(mixed $role, mixed $userId): yii\rbac\AssignmentAssigns a role to a user.
Parameters
| Parameter | Type | Description |
|---|---|---|
$role | mixed | |
$userId | mixed | The user ID (see User::$id) |
Returns
Assignment: The role assignment information.
Throws
Exception: if the role has already been assigned to the user
canAddChild()
Section titled “canAddChild() ”public function canAddChild(mixed $parent, mixed $child): boolChecks the possibility of adding a child to parent.
Parameters
| Parameter | Type | Description |
|---|---|---|
$parent | mixed | The parent item |
$child | mixed | The child item to be added to the hierarchy |
Returns
bool: Possibility of adding
Since
2.0.8
checkAccess()
Section titled “checkAccess() ”public function checkAccess(mixed $userId, mixed $permissionName, mixed $params = [])Parameters
| Parameter | Type | Description |
|---|---|---|
$userId | mixed | |
$permissionName | mixed | |
$params | mixed |
getAssignment()
Section titled “getAssignment() ”public function getAssignment(mixed $roleName, mixed $userId): yii\rbac\Assignment|nullReturns the assignment information regarding a role and a user.
Parameters
| Parameter | Type | Description |
|---|---|---|
$roleName | mixed | The role name |
$userId | mixed | The user ID (see User::$id) |
Returns
Assignment|null: The assignment information. Null is returned if the role is not assigned to the user.
getAssignments()
Section titled “getAssignments() ”public function getAssignments(mixed $userId): yii\rbac\Assignment[]Returns all role assignment information for the specified user.
Parameters
| Parameter | Type | Description |
|---|---|---|
$userId | mixed | The user ID (see User::$id) |
Returns
Assignment[]: The assignments indexed by role names. An empty array will be returned if there is no role assigned to the user.
getChildren()
Section titled “getChildren() ”public function getChildren(mixed $name): yii\rbac\Item[]Returns the child permissions and/or roles.
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | mixed | The parent name |
Returns
Item[]: The child permissions and/or roles
getChildRoles()
Section titled “getChildRoles() ”public function getChildRoles(mixed $roleName): yii\rbac\Role[]Returns child roles of the role specified. Depth isn’t limited.
Parameters
| Parameter | Type | Description |
|---|---|---|
$roleName | mixed | Name of the role to file child roles for |
Returns
Role[]: Child roles. The array is indexed by the role names. First element is an instance of the parent Role itself.
Throws
yii\base\InvalidArgumentException: if Role was not found that are getting by $roleName
Since
2.0.10
getItem()
Section titled “getItem() ”public function getItem(mixed $name): yii\rbac\Item|nullReturns the named auth item.
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | mixed | The auth item name. |
Returns
Item|null: The auth item corresponding to the specified name. Null is returned if no such item.
getItems()
Section titled “getItems() ”public function getItems(mixed $type): ($type is yii\rbac\Item::TYPE_ROLE ? yii\rbac\Role[] : yii\rbac\Permission[])Returns the items of the specified type.
Parameters
| Parameter | Type | Description |
|---|---|---|
$type | mixed | The auth item type (either Item::TYPE_ROLE or Item::TYPE_PERMISSION |
Returns
($type is Item::TYPE_ROLE ? Role[] : Permission[]): The auth items of the specified type.
getPermissionsByRole()
Section titled “getPermissionsByRole() ”public function getPermissionsByRole(mixed $roleName): yii\rbac\Permission[]Returns all permissions that the specified role represents.
Parameters
| Parameter | Type | Description |
|---|---|---|
$roleName | mixed | The role name |
Returns
Permission[]: All permissions that the role represents. The array is indexed by the permission names.
getPermissionsByUser()
Section titled “getPermissionsByUser() ”public function getPermissionsByUser(mixed $userId): yii\rbac\Permission[]Returns all permissions that the user has.
Parameters
| Parameter | Type | Description |
|---|---|---|
$userId | mixed | The user ID (see User::$id) |
Returns
Permission[]: All permissions that the user has. The array is indexed by the permission names.
getRolesByUser()
Section titled “getRolesByUser() ”public function getRolesByUser(mixed $userId): mixedThe roles returned by this method include the roles assigned via $defaultRoles.
Parameters
| Parameter | Type | Description |
|---|---|---|
$userId | mixed |
Returns
mixed
getRule()
Section titled “getRule() ”public function getRule(mixed $name): yii\rbac\Rule|nullReturns the rule of the specified name.
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | mixed | The rule name |
Returns
Rule|null: The rule object, or null if the specified name does not correspond to a rule.
getRules()
Section titled “getRules() ”public function getRules(): yii\rbac\Rule[]Returns all rules available in the system.
Returns
Rule[]: The rules indexed by the rule names
getUserIdsByRole()
Section titled “getUserIdsByRole() ”public function getUserIdsByRole(mixed $roleName): arrayReturns all user IDs assigned to the role specified.
Parameters
| Parameter | Type | Description |
|---|---|---|
$roleName | mixed |
Returns
array: Array of user ID strings
Since
2.0.7
hasChild()
Section titled “hasChild() ”public function hasChild(mixed $parent, mixed $child): boolReturns a value indicating whether the child already exists for the parent.
Parameters
| Parameter | Type | Description |
|---|---|---|
$parent | mixed | |
$child | mixed |
Returns
bool: Whether $child is already a child of $parent
init()
Section titled “init() ”public function init(): mixedInitializes the application component.
This method overrides parent implementation by loading the authorization data from PHP script.
Returns
mixed
removeAll()
Section titled “removeAll() ”public function removeAll(): mixedRemoves all authorization data, including roles, permissions, rules, and assignments.
Returns
mixed
removeAllAssignments()
Section titled “removeAllAssignments() ”public function removeAllAssignments(): mixedRemoves all role assignments.
Returns
mixed
removeAllPermissions()
Section titled “removeAllPermissions() ”public function removeAllPermissions(): mixedRemoves all permissions.
All parent child relations will be adjusted accordingly.
Returns
mixed
removeAllRoles()
Section titled “removeAllRoles() ”public function removeAllRoles(): mixedRemoves all roles.
All parent child relations will be adjusted accordingly.
Returns
mixed
removeAllRules()
Section titled “removeAllRules() ”public function removeAllRules(): mixedRemoves all rules.
All roles and permissions which have rules will be adjusted accordingly.
Returns
mixed
removeChild()
Section titled “removeChild() ”public function removeChild(mixed $parent, mixed $child): boolRemoves a child from its parent.
Note, the child item is not deleted. Only the parent-child relationship is removed.
Parameters
| Parameter | Type | Description |
|---|---|---|
$parent | mixed | |
$child | mixed |
Returns
bool: Whether the removal is successful
removeChildren()
Section titled “removeChildren() ”public function removeChildren(mixed $parent): boolRemoved all children form their parent.
Note, the children items are not deleted. Only the parent-child relationships are removed.
Parameters
| Parameter | Type | Description |
|---|---|---|
$parent | mixed |
Returns
bool: Whether the removal is successful
removeItem()
Section titled “removeItem() ”public function removeItem(mixed $item): boolRemoves an auth item from the RBAC system.
Parameters
| Parameter | Type | Description |
|---|---|---|
$item | mixed | The item to remove |
Returns
bool: Whether the role or permission is successfully removed
Throws
Exception: if data validation or saving fails (such as the name of the role or permission is not unique)
revoke()
Section titled “revoke() ”public function revoke(mixed $role, mixed $userId): boolRevokes a role from a user.
Parameters
| Parameter | Type | Description |
|---|---|---|
$role | mixed | |
$userId | mixed | The user ID (see User::$id) |
Returns
bool: Whether the revoking is successful
revokeAll()
Section titled “revokeAll() ”public function revokeAll(mixed $userId): boolRevokes all roles from a user.
Parameters
| Parameter | Type | Description |
|---|---|---|
$userId | mixed | The user ID (see User::$id) |
Returns
bool: Whether the revoking is successful
updateRule()
Section titled “updateRule() ”public function updateRule(mixed $name, mixed $rule): boolUpdates a rule to the RBAC system.
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | mixed | The name of the rule being updated |
$rule | mixed | The updated rule |
Returns
bool: Whether the rule is successfully updated
Throws
Exception: if data validation or saving fails (such as the name of the rule is not unique)
addItem()
Section titled “addItem() ”protected function addItem(mixed $item): boolAdds an auth item to the RBAC system.
Parameters
| Parameter | Type | Description |
|---|---|---|
$item | mixed | The item to add |
Returns
bool: Whether the auth item is successfully added to the system
Throws
Exception: if data validation or saving fails (such as the name of the role or permission is not unique)
addRule()
Section titled “addRule() ”protected function addRule(mixed $rule): boolAdds a rule to the RBAC system.
Parameters
| Parameter | Type | Description |
|---|---|---|
$rule | mixed | The rule to add |
Returns
bool: Whether the rule is successfully added to the system
Throws
Exception: if data validation or saving fails (such as the name of the rule is not unique)
checkAccessRecursive()
Section titled “checkAccessRecursive() ”protected function checkAccessRecursive(string|int $user, string $itemName, array $params, yii\rbac\Assignment[] $assignments): boolPerforms access check for the specified user.
This method is internally called by checkAccess().
Parameters
| Parameter | Type | Description |
|---|---|---|
$user | string| | The user ID. This should can be either an integer or a string representing the unique identifier of a user. See User::$id. |
$itemName | string | The name of the operation that need access check |
$params | array | Name-value pairs that would be passed to rules associated with the tasks and roles assigned to the user. A param with name ‘user’ is added to this array, which holds the value of $userId. |
$assignments | Assignment[] | The assignments to the specified user |
Returns
bool: Whether the operations can be performed by the user.
detectLoop()
Section titled “detectLoop() ”protected function detectLoop(yii\rbac\Item $parent, yii\rbac\Item $child): boolChecks whether there is a loop in the authorization item hierarchy.
getChildrenRecursive()
Section titled “getChildrenRecursive() ”protected function getChildrenRecursive(string $name, array &$result): mixedRecursively finds all children and grand children of the specified item.
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | string | The name of the item whose children are to be looked for. |
$result | array | The children and grand children (in array keys) |
Returns
mixed
getDirectPermissionsByUser()
Section titled “getDirectPermissionsByUser() ”protected function getDirectPermissionsByUser(string|int $userId): yii\rbac\Permission[]Returns all permissions that are directly assigned to user.
Parameters
| Parameter | Type | Description |
|---|---|---|
$userId | string| | The user ID (see User::$id) |
Returns
Permission[]: All direct permissions that the user has. The array is indexed by the permission names.
Since
2.0.7
getInheritedPermissionsByUser()
Section titled “getInheritedPermissionsByUser() ”protected function getInheritedPermissionsByUser(string|int $userId): yii\rbac\Permission[]Returns all permissions that the user inherits from the roles assigned to him.
Parameters
| Parameter | Type | Description |
|---|---|---|
$userId | string| | The user ID (see User::$id) |
Returns
Permission[]: All inherited permissions that the user has. The array is indexed by the permission names.
Since
2.0.7
invalidateScriptCache()
Section titled “invalidateScriptCache() ”protected function invalidateScriptCache(string $file): mixedInvalidates precompiled script cache (OPcache) for the given file.
Parameters
| Parameter | Type | Description |
|---|---|---|
$file | string | The file path. |
Returns
mixed
Since
2.0.9
load()
Section titled “load() ”protected function load(): mixedLoads authorization data from persistent storage.
Returns
mixed
loadFromFile()
Section titled “loadFromFile() ”protected function loadFromFile(string $file): arrayLoads the authorization data from a PHP script file.
Parameters
| Parameter | Type | Description |
|---|---|---|
$file | string | The file path. |
Returns
array: The authorization data
See also
removeAllItems()
Section titled “removeAllItems() ”protected function removeAllItems(int $type): mixedRemoves all auth items of the specified type.
Parameters
| Parameter | Type | Description |
|---|---|---|
$type | int | The auth item type (either Item::TYPE_PERMISSION or Item::TYPE_ROLE) |
Returns
mixed
removeRule()
Section titled “removeRule() ”protected function removeRule(mixed $rule): boolRemoves a rule from the RBAC system.
Parameters
| Parameter | Type | Description |
|---|---|---|
$rule | mixed | The rule to remove |
Returns
bool: Whether the rule is successfully removed
Throws
Exception: if data validation or saving fails (such as the name of the rule is not unique)
save()
Section titled “save() ”protected function save(): mixedSaves authorization data into persistent storage.
Returns
mixed
saveAssignments()
Section titled “saveAssignments() ”protected function saveAssignments(): mixedSaves assignments data into persistent storage.
Returns
mixed
saveItems()
Section titled “saveItems() ”protected function saveItems(): mixedSaves items data into persistent storage.
Returns
mixed
saveRules()
Section titled “saveRules() ”protected function saveRules(): mixedSaves rules data into persistent storage.
Returns
mixed
saveToFile()
Section titled “saveToFile() ”protected function saveToFile(array $data, string $file): mixedSaves the authorization data to a PHP script file.
Parameters
| Parameter | Type | Description |
|---|---|---|
$data | array | The authorization data |
$file | string | The file path. |
Returns
mixed
See also
updateItem()
Section titled “updateItem() ”protected function updateItem(mixed $name, mixed $item): boolUpdates an auth item in the RBAC system.
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | mixed | The name of the item being updated |
$item | mixed | The updated item |
Returns
bool: Whether the auth item is successfully updated
Throws
Exception: if data validation or saving fails (such as the name of the role or permission is not unique)