Skip to content
PHP Ecosystem
Explore

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.

PropertyTypeDescriptionDefined by
$assignmentFilestringThe path of the PHP script that contains the authorization assignments. Default: '@app/rbac/assignments.php'.PhpManager
$behaviors read-onlyarray<yii\base\Behavior<static>>List of behaviors attached to this component.yii\base\Component
$defaultRoleInstances read-onlyRole[]Default roles.BaseManager
$itemFilestringThe path of the PHP script that contains the authorization items. Default: '@app/rbac/items.php'.PhpManager
$permissions read-onlyPermission[]All permissions in the system.BaseManager
$roles read-onlyRole[]All roles in the system.BaseManager
$ruleFilestringThe path of the PHP script that contains the authorization rules. Default: '@app/rbac/rules.php'.PhpManager
$assignments protectedarrayDefault: [].PhpManager
$children protectedarrayDefault: [].PhpManager
$defaultRoles protectedarrayA list of role names that are assigned to every user automatically without calling assign(). Default: [].BaseManager
$items protectedItem[]Default: [].PhpManager
$rules protectedRule[]Default: [].PhpManager
MethodDescriptionDefined by
__construct()Constructor.yii\base\BaseObject
__call()Calls the named method which is not a class method.yii\base\Component
__clone()This method is called after the object is created by cloning an existing one.yii\base\Component
__get()Returns the value of a component property.yii\base\Component
__isset()Checks if a property is set, i.e. defined and not null.yii\base\Component
__set()Sets the value of a component property.yii\base\Component
__unset()Sets a component property to be null.yii\base\Component
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\base\Component
attachBehaviors()Attaches a list of behaviors to the component.yii\base\Component
behaviors()Returns a list of behaviors that this component should behave as.yii\base\Component
canAddChild()Checks the possibility of adding a child to parent.PhpManager
canGetProperty()Returns a value indicating whether a property can be read.yii\base\Component
canSetProperty()Returns a value indicating whether a property can be set.yii\base\Component
checkAccess()PhpManager
className() staticReturns the fully qualified name of this class.yii\base\BaseObject
createPermission()Creates a new Permission object.BaseManager
createRole()Creates a new Role object.BaseManager
detachBehavior()Detaches a behavior from the component.yii\base\Component
detachBehaviors()Detaches all behaviors from the component.yii\base\Component
ensureBehaviors()Makes sure that the behaviors declared in behaviors() are attached to this component.yii\base\Component
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\base\Component
getBehaviors()Returns all behaviors attached to this component.yii\base\Component
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 rolesBaseManager
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\base\Component
hasMethod()Returns a value indicating whether a method is defined.yii\base\Component
hasProperty()Returns a value indicating whether a property is defined for this component.yii\base\Component
init()Initializes the application component.PhpManager
off()Detaches an existing event handler from this component.yii\base\Component
on()Attaches an event handler to an event.yii\base\Component
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 rolesBaseManager
trigger()Triggers an event.yii\base\Component
update()Updates the specified role, permission or rule in the system.BaseManager
updateRule()Updates a rule to the RBAC system.PhpManager
addItem() protectedAdds an auth item to the RBAC system.PhpManager
addRule() protectedAdds a rule to the RBAC system.PhpManager
checkAccessRecursive() protectedPerforms access check for the specified user.PhpManager
detectLoop() protectedChecks whether there is a loop in the authorization item hierarchy.PhpManager
executeRule() protectedExecutes the rule associated with the specified auth item.BaseManager
getChildrenRecursive() protectedRecursively finds all children and grand children of the specified item.PhpManager
getDirectPermissionsByUser() protectedReturns all permissions that are directly assigned to user.PhpManager
getInheritedPermissionsByUser() protectedReturns all permissions that the user inherits from the roles assigned to him.PhpManager
hasNoAssignments() protectedChecks whether array of $assignments is empty and $defaultRoles property is empty as well.BaseManager
invalidateScriptCache() protectedInvalidates precompiled script cache (OPcache) for the given file.PhpManager
load() protectedLoads authorization data from persistent storage.PhpManager
loadFromFile() protectedLoads the authorization data from a PHP script file.PhpManager
removeAllItems() protectedRemoves all auth items of the specified type.PhpManager
removeRule() protectedRemoves a rule from the RBAC system.PhpManager
save() protectedSaves authorization data into persistent storage.PhpManager
saveAssignments() protectedSaves assignments data into persistent storage.PhpManager
saveItems() protectedSaves items data into persistent storage.PhpManager
saveRules() protectedSaves rules data into persistent storage.PhpManager
saveToFile() protectedSaves the authorization data to a PHP script file.PhpManager
updateItem() protectedUpdates an auth item in the RBAC system.PhpManager
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.

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.

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.

public function addChild(mixed $parent, mixed $child): bool

Adds an item as a child of another item.

Parameters

ParameterTypeDescription
$parentmixed
$childmixed

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.
public function assign(mixed $role, mixed $userId): yii\rbac\Assignment

Assigns a role to a user.

Parameters

ParameterTypeDescription
$rolemixed
$userIdmixedThe user ID (see User::$id)

Returns

Assignment: The role assignment information.

Throws

  • Exception: if the role has already been assigned to the user
public function canAddChild(mixed $parent, mixed $child): bool

Checks the possibility of adding a child to parent.

Parameters

ParameterTypeDescription
$parentmixedThe parent item
$childmixedThe child item to be added to the hierarchy

Returns

bool: Possibility of adding

Since

2.0.8

public function checkAccess(mixed $userId, mixed $permissionName, mixed $params = [])

Parameters

ParameterTypeDescription
$userIdmixed
$permissionNamemixed
$paramsmixed
public function getAssignment(mixed $roleName, mixed $userId): yii\rbac\Assignment|null

Returns the assignment information regarding a role and a user.

Parameters

ParameterTypeDescription
$roleNamemixedThe role name
$userIdmixedThe user ID (see User::$id)

Returns

Assignment|null: The assignment information. Null is returned if the role is not assigned to the user.

public function getAssignments(mixed $userId): yii\rbac\Assignment[]

Returns all role assignment information for the specified user.

Parameters

ParameterTypeDescription
$userIdmixedThe 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.

public function getChildren(mixed $name): yii\rbac\Item[]

Returns the child permissions and/or roles.

Parameters

ParameterTypeDescription
$namemixedThe parent name

Returns

Item[]: The child permissions and/or roles

public function getChildRoles(mixed $roleName): yii\rbac\Role[]

Returns child roles of the role specified. Depth isn’t limited.

Parameters

ParameterTypeDescription
$roleNamemixedName 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

Since

2.0.10

public function getItem(mixed $name): yii\rbac\Item|null

Returns the named auth item.

Parameters

ParameterTypeDescription
$namemixedThe auth item name.

Returns

Item|null: The auth item corresponding to the specified name. Null is returned if no such item.

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

ParameterTypeDescription
$typemixedThe 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.

public function getPermissionsByRole(mixed $roleName): yii\rbac\Permission[]

Returns all permissions that the specified role represents.

Parameters

ParameterTypeDescription
$roleNamemixedThe role name

Returns

Permission[]: All permissions that the role represents. The array is indexed by the permission names.

public function getPermissionsByUser(mixed $userId): yii\rbac\Permission[]

Returns all permissions that the user has.

Parameters

ParameterTypeDescription
$userIdmixedThe user ID (see User::$id)

Returns

Permission[]: All permissions that the user has. The array is indexed by the permission names.

public function getRolesByUser(mixed $userId): mixed

The roles returned by this method include the roles assigned via $defaultRoles.

Parameters

ParameterTypeDescription
$userIdmixed

Returns

mixed

public function getRule(mixed $name): yii\rbac\Rule|null

Returns the rule of the specified name.

Parameters

ParameterTypeDescription
$namemixedThe rule name

Returns

Rule|null: The rule object, or null if the specified name does not correspond to a rule.

public function getRules(): yii\rbac\Rule[]

Returns all rules available in the system.

Returns

Rule[]: The rules indexed by the rule names

public function getUserIdsByRole(mixed $roleName): array

Returns all user IDs assigned to the role specified.

Parameters

ParameterTypeDescription
$roleNamemixed

Returns

array: Array of user ID strings

Since

2.0.7

public function hasChild(mixed $parent, mixed $child): bool

Returns a value indicating whether the child already exists for the parent.

Parameters

ParameterTypeDescription
$parentmixed
$childmixed

Returns

bool: Whether $child is already a child of $parent

public function init(): mixed

Initializes the application component.

This method overrides parent implementation by loading the authorization data from PHP script.

Returns

mixed

public function removeAll(): mixed

Removes all authorization data, including roles, permissions, rules, and assignments.

Returns

mixed

public function removeAllAssignments(): mixed

Removes all role assignments.

Returns

mixed

public function removeAllPermissions(): mixed

Removes all permissions.

All parent child relations will be adjusted accordingly.

Returns

mixed

public function removeAllRoles(): mixed

Removes all roles.

All parent child relations will be adjusted accordingly.

Returns

mixed

public function removeAllRules(): mixed

Removes all rules.

All roles and permissions which have rules will be adjusted accordingly.

Returns

mixed

public function removeChild(mixed $parent, mixed $child): bool

Removes a child from its parent.

Note, the child item is not deleted. Only the parent-child relationship is removed.

Parameters

ParameterTypeDescription
$parentmixed
$childmixed

Returns

bool: Whether the removal is successful

public function removeChildren(mixed $parent): bool

Removed all children form their parent.

Note, the children items are not deleted. Only the parent-child relationships are removed.

Parameters

ParameterTypeDescription
$parentmixed

Returns

bool: Whether the removal is successful

public function removeItem(mixed $item): bool

Removes an auth item from the RBAC system.

Parameters

ParameterTypeDescription
$itemmixedThe 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)
public function revoke(mixed $role, mixed $userId): bool

Revokes a role from a user.

Parameters

ParameterTypeDescription
$rolemixed
$userIdmixedThe user ID (see User::$id)

Returns

bool: Whether the revoking is successful

public function revokeAll(mixed $userId): bool

Revokes all roles from a user.

Parameters

ParameterTypeDescription
$userIdmixedThe user ID (see User::$id)

Returns

bool: Whether the revoking is successful

public function updateRule(mixed $name, mixed $rule): bool

Updates a rule to the RBAC system.

Parameters

ParameterTypeDescription
$namemixedThe name of the rule being updated
$rulemixedThe 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)
protected function addItem(mixed $item): bool

Adds an auth item to the RBAC system.

Parameters

ParameterTypeDescription
$itemmixedThe 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)
protected function addRule(mixed $rule): bool

Adds a rule to the RBAC system.

Parameters

ParameterTypeDescription
$rulemixedThe 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)
protected function checkAccessRecursive(string|int $user, string $itemName, array $params, yii\rbac\Assignment[] $assignments): bool

Performs access check for the specified user.

This method is internally called by checkAccess().

Parameters

ParameterTypeDescription
$userstring|intThe user ID. This should can be either an integer or a string representing the unique identifier of a user. See User::$id.
$itemNamestringThe name of the operation that need access check
$paramsarrayName-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.
$assignmentsAssignment[]The assignments to the specified user

Returns

bool: Whether the operations can be performed by the user.

protected function detectLoop(yii\rbac\Item $parent, yii\rbac\Item $child): bool

Checks whether there is a loop in the authorization item hierarchy.

Parameters

ParameterTypeDescription
$parentItemParent item
$childItemThe child item that is to be added to the hierarchy

Returns

bool: Whether a loop exists

protected function getChildrenRecursive(string $name, array &$result): mixed

Recursively finds all children and grand children of the specified item.

Parameters

ParameterTypeDescription
$namestringThe name of the item whose children are to be looked for.
$resultarrayThe children and grand children (in array keys)

Returns

mixed

protected function getDirectPermissionsByUser(string|int $userId): yii\rbac\Permission[]

Returns all permissions that are directly assigned to user.

Parameters

ParameterTypeDescription
$userIdstring|intThe 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

protected function getInheritedPermissionsByUser(string|int $userId): yii\rbac\Permission[]

Returns all permissions that the user inherits from the roles assigned to him.

Parameters

ParameterTypeDescription
$userIdstring|intThe 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

protected function invalidateScriptCache(string $file): mixed

Invalidates precompiled script cache (OPcache) for the given file.

Parameters

ParameterTypeDescription
$filestringThe file path.

Returns

mixed

Since

2.0.9

protected function load(): mixed

Loads authorization data from persistent storage.

Returns

mixed

protected function loadFromFile(string $file): array

Loads the authorization data from a PHP script file.

Parameters

ParameterTypeDescription
$filestringThe file path.

Returns

array: The authorization data

See also

protected function removeAllItems(int $type): mixed

Removes all auth items of the specified type.

Parameters

ParameterTypeDescription
$typeintThe auth item type (either Item::TYPE_PERMISSION or Item::TYPE_ROLE)

Returns

mixed

protected function removeRule(mixed $rule): bool

Removes a rule from the RBAC system.

Parameters

ParameterTypeDescription
$rulemixedThe 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)
protected function save(): mixed

Saves authorization data into persistent storage.

Returns

mixed

protected function saveAssignments(): mixed

Saves assignments data into persistent storage.

Returns

mixed

protected function saveItems(): mixed

Saves items data into persistent storage.

Returns

mixed

protected function saveRules(): mixed

Saves rules data into persistent storage.

Returns

mixed

protected function saveToFile(array $data, string $file): mixed

Saves the authorization data to a PHP script file.

Parameters

ParameterTypeDescription
$dataarrayThe authorization data
$filestringThe file path.

Returns

mixed

See also

protected function updateItem(mixed $name, mixed $item): bool

Updates an auth item in the RBAC system.

Parameters

ParameterTypeDescription
$namemixedThe name of the item being updated
$itemmixedThe 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)