# PhpManager

> PhpManager represents an authorization manager that stores authorization information in terms of a PHP script file.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [rbac](/docs/api/yii/rbac/index.md) › PhpManager

| class | `yii\rbac\PhpManager` |
| --- | --- |
| Extends | [`BaseManager`](/docs/api/yii/rbac/basemanager/index.md) » [`yii\base\Component`](/docs/api/yii/base/component/index.md) » [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`ManagerInterface`](/docs/api/yii/rbac/managerinterface/index.md), [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md) |
| Since | 2.0 |
| Source | [`framework/rbac/PhpManager.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/rbac/PhpManager.php#L37) |

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`](/docs/api/yii/rbac/phpmanager/index.md#property-itemFile), [`$assignmentFile`](/docs/api/yii/rbac/phpmanager/index.md#property-assignmentFile) and [`$ruleFile`](/docs/api/yii/rbac/phpmanager/index.md#property-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`](/docs/api/yii/rbac/dbmanager/index.md) for more complex authorization data.

For more details and usage information on PhpManager, see the [guide article on security authorization](/docs/guide/security-authorization/index.md).

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| [`$assignmentFile`](#property-assignmentFile) | `string` | The path of the PHP script that contains the authorization assignments. Default: `'@app/rbac/assignments.php'`. | `PhpManager` |
| <span id="property-behaviors"></span>[`$behaviors`](/docs/api/yii/base/component/index.md#property-behaviors) read-only | `array<`[`yii\base\Behavior`](/docs/api/yii/base/behavior/index.md)`<static>>` | List of behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="property-defaultRoleInstances"></span>[`$defaultRoleInstances`](/docs/api/yii/rbac/basemanager/index.md#property-defaultRoleInstances) read-only | [`Role`](/docs/api/yii/rbac/role/index.md)`[]` | Default roles. | [`BaseManager`](/docs/api/yii/rbac/basemanager/index.md) |
| [`$itemFile`](#property-itemFile) | `string` | The path of the PHP script that contains the authorization items. Default: `'@app/rbac/items.php'`. | `PhpManager` |
| <span id="property-permissions"></span>[`$permissions`](/docs/api/yii/rbac/basemanager/index.md#property-permissions) read-only | [`Permission`](/docs/api/yii/rbac/permission/index.md)`[]` | All permissions in the system. | [`BaseManager`](/docs/api/yii/rbac/basemanager/index.md) |
| <span id="property-roles"></span>[`$roles`](/docs/api/yii/rbac/basemanager/index.md#property-roles) read-only | [`Role`](/docs/api/yii/rbac/role/index.md)`[]` | All roles in the system. | [`BaseManager`](/docs/api/yii/rbac/basemanager/index.md) |
| [`$ruleFile`](#property-ruleFile) | `string` | The path of the PHP script that contains the authorization rules. Default: `'@app/rbac/rules.php'`. | `PhpManager` |
| <span id="property-assignments"></span>`$assignments` protected | `array` | Default: `[]`. | `PhpManager` |
| <span id="property-children"></span>`$children` protected | `array` | Default: `[]`. | `PhpManager` |
| <span id="property-defaultRoles"></span>[`$defaultRoles`](/docs/api/yii/rbac/basemanager/index.md#property-defaultRoles) protected | `array` | A list of role names that are assigned to every user automatically without calling [`assign()`](/docs/api/yii/rbac/managerinterface/index.md#method-assign). Default: `[]`. | [`BaseManager`](/docs/api/yii/rbac/basemanager/index.md) |
| <span id="property-items"></span>`$items` protected | [`Item`](/docs/api/yii/rbac/item/index.md)`[]` | Default: `[]`. | `PhpManager` |
| <span id="property-rules"></span>`$rules` protected | [`Rule`](/docs/api/yii/rbac/rule/index.md)`[]` | Default: `[]`. | `PhpManager` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| <span id="method-__construct"></span>[`__construct()`](/docs/api/yii/base/baseobject/index.md#method-__construct) | Constructor. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__call"></span>[`__call()`](/docs/api/yii/base/component/index.md#method-__call) | Calls the named method which is not a class method. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__clone"></span>[`__clone()`](/docs/api/yii/base/component/index.md#method-__clone) | This method is called after the object is created by cloning an existing one. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/component/index.md#method-__get) | Returns the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/component/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/component/index.md#method-__set) | Sets the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/component/index.md#method-__unset) | Sets a component property to be null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-add"></span>[`add()`](/docs/api/yii/rbac/basemanager/index.md#method-add) | Adds a role, permission or rule to the RBAC system. | [`BaseManager`](/docs/api/yii/rbac/basemanager/index.md) |
| [`addChild()`](#method-addChild) | Adds an item as a child of another item. | `PhpManager` |
| [`assign()`](#method-assign) | Assigns a role to a user. | `PhpManager` |
| <span id="method-attachBehavior"></span>[`attachBehavior()`](/docs/api/yii/base/component/index.md#method-attachBehavior) | Attaches a behavior to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-attachBehaviors"></span>[`attachBehaviors()`](/docs/api/yii/base/component/index.md#method-attachBehaviors) | Attaches a list of behaviors to the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-behaviors"></span>[`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) | Returns a list of behaviors that this component should behave as. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`canAddChild()`](#method-canAddChild) | Checks the possibility of adding a child to parent. | `PhpManager` |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/component/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/component/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`checkAccess()`](#method-checkAccess) |  | `PhpManager` |
| <span id="method-className"></span>[`className()`](/docs/api/yii/base/baseobject/index.md#method-className) static | Returns the fully qualified name of this class. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-createPermission"></span>[`createPermission()`](/docs/api/yii/rbac/basemanager/index.md#method-createPermission) | Creates a new Permission object. | [`BaseManager`](/docs/api/yii/rbac/basemanager/index.md) |
| <span id="method-createRole"></span>[`createRole()`](/docs/api/yii/rbac/basemanager/index.md#method-createRole) | Creates a new Role object. | [`BaseManager`](/docs/api/yii/rbac/basemanager/index.md) |
| <span id="method-detachBehavior"></span>[`detachBehavior()`](/docs/api/yii/base/component/index.md#method-detachBehavior) | Detaches a behavior from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-detachBehaviors"></span>[`detachBehaviors()`](/docs/api/yii/base/component/index.md#method-detachBehaviors) | Detaches all behaviors from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-ensureBehaviors"></span>[`ensureBehaviors()`](/docs/api/yii/base/component/index.md#method-ensureBehaviors) | Makes sure that the behaviors declared in [`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) are attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`getAssignment()`](#method-getAssignment) | Returns the assignment information regarding a role and a user. | `PhpManager` |
| [`getAssignments()`](#method-getAssignments) | Returns all role assignment information for the specified user. | `PhpManager` |
| <span id="method-getBehavior"></span>[`getBehavior()`](/docs/api/yii/base/component/index.md#method-getBehavior) | Returns the named behavior object. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getBehaviors"></span>[`getBehaviors()`](/docs/api/yii/base/component/index.md#method-getBehaviors) | Returns all behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`getChildren()`](#method-getChildren) | Returns the child permissions and/or roles. | `PhpManager` |
| [`getChildRoles()`](#method-getChildRoles) | Returns child roles of the role specified. Depth isn't limited. | `PhpManager` |
| <span id="method-getDefaultRoleInstances"></span>[`getDefaultRoleInstances()`](/docs/api/yii/rbac/basemanager/index.md#method-getDefaultRoleInstances) | Returns defaultRoles as array of Role objects. | [`BaseManager`](/docs/api/yii/rbac/basemanager/index.md) |
| <span id="method-getDefaultRoles"></span>[`getDefaultRoles()`](/docs/api/yii/rbac/basemanager/index.md#method-getDefaultRoles) | Get default roles | [`BaseManager`](/docs/api/yii/rbac/basemanager/index.md) |
| [`getItem()`](#method-getItem) | Returns the named auth item. | `PhpManager` |
| [`getItems()`](#method-getItems) | Returns the items of the specified type. | `PhpManager` |
| <span id="method-getPermission"></span>[`getPermission()`](/docs/api/yii/rbac/basemanager/index.md#method-getPermission) | Returns the named permission. | [`BaseManager`](/docs/api/yii/rbac/basemanager/index.md) |
| <span id="method-getPermissions"></span>[`getPermissions()`](/docs/api/yii/rbac/basemanager/index.md#method-getPermissions) | Returns all permissions in the system. | [`BaseManager`](/docs/api/yii/rbac/basemanager/index.md) |
| [`getPermissionsByRole()`](#method-getPermissionsByRole) | Returns all permissions that the specified role represents. | `PhpManager` |
| [`getPermissionsByUser()`](#method-getPermissionsByUser) | Returns all permissions that the user has. | `PhpManager` |
| <span id="method-getRole"></span>[`getRole()`](/docs/api/yii/rbac/basemanager/index.md#method-getRole) | Returns the named role. | [`BaseManager`](/docs/api/yii/rbac/basemanager/index.md) |
| <span id="method-getRoles"></span>[`getRoles()`](/docs/api/yii/rbac/basemanager/index.md#method-getRoles) | Returns all roles in the system. | [`BaseManager`](/docs/api/yii/rbac/basemanager/index.md) |
| [`getRolesByUser()`](#method-getRolesByUser) | The roles returned by this method include the roles assigned via [`$defaultRoles`](/docs/api/yii/rbac/basemanager/index.md#property-defaultRoles). | `PhpManager` |
| [`getRule()`](#method-getRule) | Returns the rule of the specified name. | `PhpManager` |
| [`getRules()`](#method-getRules) | Returns all rules available in the system. | `PhpManager` |
| [`getUserIdsByRole()`](#method-getUserIdsByRole) | Returns all user IDs assigned to the role specified. | `PhpManager` |
| [`hasChild()`](#method-hasChild) | Returns a value indicating whether the child already exists for the parent. | `PhpManager` |
| <span id="method-hasEventHandlers"></span>[`hasEventHandlers()`](/docs/api/yii/base/component/index.md#method-hasEventHandlers) | Returns a value indicating whether there is any handler attached to the named event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/component/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/component/index.md#method-hasProperty) | Returns a value indicating whether a property is defined for this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`init()`](#method-init) | Initializes the application component. | `PhpManager` |
| <span id="method-off"></span>[`off()`](/docs/api/yii/base/component/index.md#method-off) | Detaches an existing event handler from this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-on"></span>[`on()`](/docs/api/yii/base/component/index.md#method-on) | Attaches an event handler to an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-remove"></span>[`remove()`](/docs/api/yii/rbac/basemanager/index.md#method-remove) | Removes a role, permission or rule from the RBAC system. | [`BaseManager`](/docs/api/yii/rbac/basemanager/index.md) |
| [`removeAll()`](#method-removeAll) | Removes all authorization data, including roles, permissions, rules, and assignments. | `PhpManager` |
| [`removeAllAssignments()`](#method-removeAllAssignments) | Removes all role assignments. | `PhpManager` |
| [`removeAllPermissions()`](#method-removeAllPermissions) | Removes all permissions. | `PhpManager` |
| [`removeAllRoles()`](#method-removeAllRoles) | Removes all roles. | `PhpManager` |
| [`removeAllRules()`](#method-removeAllRules) | Removes all rules. | `PhpManager` |
| [`removeChild()`](#method-removeChild) | Removes a child from its parent. | `PhpManager` |
| [`removeChildren()`](#method-removeChildren) | Removed all children form their parent. | `PhpManager` |
| [`removeItem()`](#method-removeItem) | Removes an auth item from the RBAC system. | `PhpManager` |
| [`revoke()`](#method-revoke) | Revokes a role from a user. | `PhpManager` |
| [`revokeAll()`](#method-revokeAll) | Revokes all roles from a user. | `PhpManager` |
| <span id="method-setDefaultRoles"></span>[`setDefaultRoles()`](/docs/api/yii/rbac/basemanager/index.md#method-setDefaultRoles) | Set default roles | [`BaseManager`](/docs/api/yii/rbac/basemanager/index.md) |
| <span id="method-trigger"></span>[`trigger()`](/docs/api/yii/base/component/index.md#method-trigger) | Triggers an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-update"></span>[`update()`](/docs/api/yii/rbac/basemanager/index.md#method-update) | Updates the specified role, permission or rule in the system. | [`BaseManager`](/docs/api/yii/rbac/basemanager/index.md) |
| [`updateRule()`](#method-updateRule) | Updates a rule to the RBAC system. | `PhpManager` |
| [`addItem()`](#method-addItem) protected | Adds an auth item to the RBAC system. | `PhpManager` |
| [`addRule()`](#method-addRule) protected | Adds a rule to the RBAC system. | `PhpManager` |
| [`checkAccessRecursive()`](#method-checkAccessRecursive) protected | Performs access check for the specified user. | `PhpManager` |
| [`detectLoop()`](#method-detectLoop) protected | Checks whether there is a loop in the authorization item hierarchy. | `PhpManager` |
| <span id="method-executeRule"></span>[`executeRule()`](/docs/api/yii/rbac/basemanager/index.md#method-executeRule) protected | Executes the rule associated with the specified auth item. | [`BaseManager`](/docs/api/yii/rbac/basemanager/index.md) |
| [`getChildrenRecursive()`](#method-getChildrenRecursive) protected | Recursively finds all children and grand children of the specified item. | `PhpManager` |
| [`getDirectPermissionsByUser()`](#method-getDirectPermissionsByUser) protected | Returns all permissions that are directly assigned to user. | `PhpManager` |
| [`getInheritedPermissionsByUser()`](#method-getInheritedPermissionsByUser) protected | Returns all permissions that the user inherits from the roles assigned to him. | `PhpManager` |
| <span id="method-hasNoAssignments"></span>[`hasNoAssignments()`](/docs/api/yii/rbac/basemanager/index.md#method-hasNoAssignments) protected | Checks whether array of $assignments is empty and [`$defaultRoles`](/docs/api/yii/rbac/basemanager/index.md#property-defaultRoles) property is empty as well. | [`BaseManager`](/docs/api/yii/rbac/basemanager/index.md) |
| [`invalidateScriptCache()`](#method-invalidateScriptCache) protected | Invalidates precompiled script cache (OPcache) for the given file. | `PhpManager` |
| [`load()`](#method-load) protected | Loads authorization data from persistent storage. | `PhpManager` |
| [`loadFromFile()`](#method-loadFromFile) protected | Loads the authorization data from a PHP script file. | `PhpManager` |
| [`removeAllItems()`](#method-removeAllItems) protected | Removes all auth items of the specified type. | `PhpManager` |
| [`removeRule()`](#method-removeRule) protected | Removes a rule from the RBAC system. | `PhpManager` |
| [`save()`](#method-save) protected | Saves authorization data into persistent storage. | `PhpManager` |
| [`saveAssignments()`](#method-saveAssignments) protected | Saves assignments data into persistent storage. | `PhpManager` |
| [`saveItems()`](#method-saveItems) protected | Saves items data into persistent storage. | `PhpManager` |
| [`saveRules()`](#method-saveRules) protected | Saves rules data into persistent storage. | `PhpManager` |
| [`saveToFile()`](#method-saveToFile) protected | Saves the authorization data to a PHP script file. | `PhpManager` |
| [`updateItem()`](#method-updateItem) protected | Updates an auth item in the RBAC system. | `PhpManager` |

## Property details

### $assignmentFile <span id="property-assignmentFile"></span>

```php
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](/docs/guide/concept-aliases/index.md) to the file.
Make sure this file is writable by the Web server process if the authorization needs to be changed online.

### $itemFile <span id="property-itemFile"></span>

```php
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](/docs/guide/concept-aliases/index.md) to the file.
Make sure this file is writable by the Web server process if the authorization needs to be changed online.

### $ruleFile <span id="property-ruleFile"></span>

```php
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](/docs/guide/concept-aliases/index.md) to the file.
Make sure this file is writable by the Web server process if the authorization needs to be changed online.

## Method details

### addChild() <span id="method-addChild"></span>

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

Adds 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`](/docs/api/yii/base/exception/index.md): if the parent-child relationship already exists or if a loop has been detected.

### assign() <span id="method-assign"></span>

```php
public function assign(mixed $role, mixed $userId): yii\rbac\Assignment
```

Assigns a role to a user.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$role` | `mixed` |  |
| `$userId` | `mixed` | The user ID (see [`User::$id`](/docs/api/yii/web/user/index.md#property-id)) |

**Returns**

[`Assignment`](/docs/api/yii/rbac/assignment/index.md): The role assignment information.

**Throws**

- [`Exception`](https://www.php.net/manual/en/class.exception.php): if the role has already been assigned to the user

### canAddChild() <span id="method-canAddChild"></span>

```php
public function canAddChild(mixed $parent, mixed $child): bool
```

Checks 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() <span id="method-checkAccess"></span>

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

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$userId` | `mixed` |  |
| `$permissionName` | `mixed` |  |
| `$params` | `mixed` |  |

### getAssignment() <span id="method-getAssignment"></span>

```php
public function getAssignment(mixed $roleName, mixed $userId): yii\rbac\Assignment|null
```

Returns 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`](/docs/api/yii/web/user/index.md#property-id)) |

**Returns**

[`Assignment`](/docs/api/yii/rbac/assignment/index.md)`|null`: The assignment information. Null is returned if the role is not assigned to the user.

### getAssignments() <span id="method-getAssignments"></span>

```php
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`](/docs/api/yii/web/user/index.md#property-id)) |

**Returns**

[`Assignment`](/docs/api/yii/rbac/assignment/index.md)`[]`: The assignments indexed by role names. An empty array will be returned if there is no role assigned to the user.

### getChildren() <span id="method-getChildren"></span>

```php
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`](/docs/api/yii/rbac/item/index.md)`[]`: The child permissions and/or roles

### getChildRoles() <span id="method-getChildRoles"></span>

```php
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`](/docs/api/yii/rbac/role/index.md)`[]`: Child roles. The array is indexed by the role names. First element is an instance of the parent Role itself.

**Throws**

- [`yii\base\InvalidArgumentException`](/docs/api/yii/base/invalidargumentexception/index.md): if Role was not found that are getting by $roleName

**Since**

2.0.10

### getItem() <span id="method-getItem"></span>

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

Returns the named auth item.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `mixed` | The auth item name. |

**Returns**

[`Item`](/docs/api/yii/rbac/item/index.md)`|null`: The auth item corresponding to the specified name. Null is returned if no such item.

### getItems() <span id="method-getItems"></span>

```php
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`](/docs/api/yii/rbac/item/index.md#const-TYPE_ROLE) or [`Item::TYPE_PERMISSION`](/docs/api/yii/rbac/item/index.md#const-TYPE_PERMISSION) |

**Returns**

`($type is` [`Item`](/docs/api/yii/rbac/item/index.md)`::TYPE_ROLE ?` [`Role`](/docs/api/yii/rbac/role/index.md)`[] :` [`Permission`](/docs/api/yii/rbac/permission/index.md)`[])`: The auth items of the specified type.

### getPermissionsByRole() <span id="method-getPermissionsByRole"></span>

```php
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`](/docs/api/yii/rbac/permission/index.md)`[]`: All permissions that the role represents. The array is indexed by the permission names.

### getPermissionsByUser() <span id="method-getPermissionsByUser"></span>

```php
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`](/docs/api/yii/web/user/index.md#property-id)) |

**Returns**

[`Permission`](/docs/api/yii/rbac/permission/index.md)`[]`: All permissions that the user has. The array is indexed by the permission names.

### getRolesByUser() <span id="method-getRolesByUser"></span>

```php
public function getRolesByUser(mixed $userId): mixed
```

The roles returned by this method include the roles assigned via [`$defaultRoles`](/docs/api/yii/rbac/basemanager/index.md#property-defaultRoles).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$userId` | `mixed` |  |

**Returns**

`mixed`

### getRule() <span id="method-getRule"></span>

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

Returns the rule of the specified name.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `mixed` | The rule name |

**Returns**

[`Rule`](/docs/api/yii/rbac/rule/index.md)`|null`: The rule object, or null if the specified name does not correspond to a rule.

### getRules() <span id="method-getRules"></span>

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

Returns all rules available in the system.

**Returns**

[`Rule`](/docs/api/yii/rbac/rule/index.md)`[]`: The rules indexed by the rule names

### getUserIdsByRole() <span id="method-getUserIdsByRole"></span>

```php
public function getUserIdsByRole(mixed $roleName): array
```

Returns 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() <span id="method-hasChild"></span>

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

Returns 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() <span id="method-init"></span>

```php
public function init(): mixed
```

Initializes the application component.

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

**Returns**

`mixed`

### removeAll() <span id="method-removeAll"></span>

```php
public function removeAll(): mixed
```

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

**Returns**

`mixed`

### removeAllAssignments() <span id="method-removeAllAssignments"></span>

```php
public function removeAllAssignments(): mixed
```

Removes all role assignments.

**Returns**

`mixed`

### removeAllPermissions() <span id="method-removeAllPermissions"></span>

```php
public function removeAllPermissions(): mixed
```

Removes all permissions.

All parent child relations will be adjusted accordingly.

**Returns**

`mixed`

### removeAllRoles() <span id="method-removeAllRoles"></span>

```php
public function removeAllRoles(): mixed
```

Removes all roles.

All parent child relations will be adjusted accordingly.

**Returns**

`mixed`

### removeAllRules() <span id="method-removeAllRules"></span>

```php
public function removeAllRules(): mixed
```

Removes all rules.

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

**Returns**

`mixed`

### removeChild() <span id="method-removeChild"></span>

```php
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**

| Parameter | Type | Description |
| --- | --- | --- |
| `$parent` | `mixed` |  |
| `$child` | `mixed` |  |

**Returns**

`bool`: Whether the removal is successful

### removeChildren() <span id="method-removeChildren"></span>

```php
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**

| Parameter | Type | Description |
| --- | --- | --- |
| `$parent` | `mixed` |  |

**Returns**

`bool`: Whether the removal is successful

### removeItem() <span id="method-removeItem"></span>

```php
public function removeItem(mixed $item): bool
```

Removes 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`](https://www.php.net/manual/en/class.exception.php): if data validation or saving fails (such as the name of the role or permission is not unique)

### revoke() <span id="method-revoke"></span>

```php
public function revoke(mixed $role, mixed $userId): bool
```

Revokes a role from a user.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$role` | `mixed` |  |
| `$userId` | `mixed` | The user ID (see [`User::$id`](/docs/api/yii/web/user/index.md#property-id)) |

**Returns**

`bool`: Whether the revoking is successful

### revokeAll() <span id="method-revokeAll"></span>

```php
public function revokeAll(mixed $userId): bool
```

Revokes all roles from a user.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$userId` | `mixed` | The user ID (see [`User::$id`](/docs/api/yii/web/user/index.md#property-id)) |

**Returns**

`bool`: Whether the revoking is successful

### updateRule() <span id="method-updateRule"></span>

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

Updates 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`](https://www.php.net/manual/en/class.exception.php): if data validation or saving fails (such as the name of the rule is not unique)

### addItem() <span id="method-addItem"></span>

```php
protected function addItem(mixed $item): bool
```

Adds 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`](https://www.php.net/manual/en/class.exception.php): if data validation or saving fails (such as the name of the role or permission is not unique)

### addRule() <span id="method-addRule"></span>

```php
protected function addRule(mixed $rule): bool
```

Adds 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`](https://www.php.net/manual/en/class.exception.php): if data validation or saving fails (such as the name of the rule is not unique)

### checkAccessRecursive() <span id="method-checkAccessRecursive"></span>

```php
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()`](/docs/api/yii/rbac/phpmanager/index.md#method-checkAccess).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$user` | `string\|int` | The user ID. This should can be either an integer or a string representing the unique identifier of a user. See [`User::$id`](/docs/api/yii/web/user/index.md#property-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`](/docs/api/yii/rbac/assignment/index.md)`[]` | The assignments to the specified user |

**Returns**

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

### detectLoop() <span id="method-detectLoop"></span>

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

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

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$parent` | [`Item`](/docs/api/yii/rbac/item/index.md) | Parent item |
| `$child` | [`Item`](/docs/api/yii/rbac/item/index.md) | The child item that is to be added to the hierarchy |

**Returns**

`bool`: Whether a loop exists

### getChildrenRecursive() <span id="method-getChildrenRecursive"></span>

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

Recursively 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() <span id="method-getDirectPermissionsByUser"></span>

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

Returns all permissions that are directly assigned to user.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$userId` | `string\|int` | The user ID (see [`User::$id`](/docs/api/yii/web/user/index.md#property-id)) |

**Returns**

[`Permission`](/docs/api/yii/rbac/permission/index.md)`[]`: All direct permissions that the user has. The array is indexed by the permission names.

**Since**

2.0.7

### getInheritedPermissionsByUser() <span id="method-getInheritedPermissionsByUser"></span>

```php
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\|int` | The user ID (see [`User::$id`](/docs/api/yii/web/user/index.md#property-id)) |

**Returns**

[`Permission`](/docs/api/yii/rbac/permission/index.md)`[]`: All inherited permissions that the user has. The array is indexed by the permission names.

**Since**

2.0.7

### invalidateScriptCache() <span id="method-invalidateScriptCache"></span>

```php
protected function invalidateScriptCache(string $file): mixed
```

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

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$file` | `string` | The file path. |

**Returns**

`mixed`

**Since**

2.0.9

### load() <span id="method-load"></span>

```php
protected function load(): mixed
```

Loads authorization data from persistent storage.

**Returns**

`mixed`

### loadFromFile() <span id="method-loadFromFile"></span>

```php
protected function loadFromFile(string $file): array
```

Loads the authorization data from a PHP script file.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$file` | `string` | The file path. |

**Returns**

`array`: The authorization data

**See also**

- [`saveToFile()`](/docs/api/yii/rbac/phpmanager/index.md#method-saveToFile)

### removeAllItems() <span id="method-removeAllItems"></span>

```php
protected function removeAllItems(int $type): mixed
```

Removes 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() <span id="method-removeRule"></span>

```php
protected function removeRule(mixed $rule): bool
```

Removes 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`](https://www.php.net/manual/en/class.exception.php): if data validation or saving fails (such as the name of the rule is not unique)

### save() <span id="method-save"></span>

```php
protected function save(): mixed
```

Saves authorization data into persistent storage.

**Returns**

`mixed`

### saveAssignments() <span id="method-saveAssignments"></span>

```php
protected function saveAssignments(): mixed
```

Saves assignments data into persistent storage.

**Returns**

`mixed`

### saveItems() <span id="method-saveItems"></span>

```php
protected function saveItems(): mixed
```

Saves items data into persistent storage.

**Returns**

`mixed`

### saveRules() <span id="method-saveRules"></span>

```php
protected function saveRules(): mixed
```

Saves rules data into persistent storage.

**Returns**

`mixed`

### saveToFile() <span id="method-saveToFile"></span>

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

Saves 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**

- [`loadFromFile()`](/docs/api/yii/rbac/phpmanager/index.md#method-loadFromFile)

### updateItem() <span id="method-updateItem"></span>

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

Updates 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`](https://www.php.net/manual/en/class.exception.php): if data validation or saving fails (such as the name of the role or permission is not unique)