Class yii\rbac\DbManager

Inheritanceyii\rbac\DbManager » yii\rbac\BaseManager » yii\base\Component » yii\base\BaseObject
Implementsyii\base\Configurable, yii\rbac\ManagerInterface
Available since version2.0
Source Code https://github.com/yiisoft/yii2/blob/master/framework/rbac/DbManager.php

DbManager represents an authorization manager that stores authorization information in database.

The database connection is specified by $db. The database schema could be initialized by applying migration:

yii migrate --migrationPath=@yii/rbac/migrations/

If you don't want to use migration and need SQL instead, files for all databases are in migrations directory.

You may change the names of the tables used to store the authorization and rule data by setting $itemTable, $itemChildTable, $assignmentTable and $ruleTable.

For more details and usage information on DbManager, see the guide article on security authorization.

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$assignmentTable string The name of the table storing authorization item assignments. yii\rbac\DbManager
$cache yii\caching\CacheInterface|array|string The cache used to improve RBAC performance. yii\rbac\DbManager
$cacheKey string The key used to store RBAC data in cache yii\rbac\DbManager
$db yii\db\Connection|array|string The DB connection object or the application component ID of the DB connection. yii\rbac\DbManager
$itemChildTable string The name of the table storing authorization item hierarchy. yii\rbac\DbManager
$itemTable string The name of the table storing authorization items. yii\rbac\DbManager
$ruleTable string The name of the table storing rules. yii\rbac\DbManager

Protected Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$checkAccessAssignments array User assignments (user id => Assignment[]) yii\rbac\DbManager
$defaultRoles array A list of role names that are assigned to every user automatically without calling assign(). yii\rbac\BaseManager
$items yii\rbac\Item[] All auth items (name => Item) yii\rbac\DbManager
$parents array Auth item parent-child relationships (childName => list of parents) yii\rbac\DbManager
$rules yii\rbac\Rule[] All auth rules (name => Rule) yii\rbac\DbManager

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__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
__construct() Constructor. yii\base\BaseObject
__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() {@inheritdoc} yii\rbac\BaseManager
addChild() {@inheritdoc} yii\rbac\DbManager
assign() {@inheritdoc} yii\rbac\DbManager
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() {@inheritdoc} yii\rbac\DbManager
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() {@inheritdoc} yii\rbac\DbManager
className() Returns the fully qualified name of this class. yii\base\BaseObject
createPermission() {@inheritdoc} yii\rbac\BaseManager
createRole() {@inheritdoc} yii\rbac\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() {@inheritdoc} yii\rbac\DbManager
getAssignments() {@inheritdoc} yii\rbac\DbManager
getBehavior() Returns the named behavior object. yii\base\Component
getBehaviors() Returns all behaviors attached to this component. yii\base\Component
getChildRoles() {@inheritdoc} yii\rbac\DbManager
getChildren() {@inheritdoc} yii\rbac\DbManager
getDefaultRoleInstances() Returns defaultRoles as array of Role objects. yii\rbac\BaseManager
getDefaultRoles() Get default roles yii\rbac\BaseManager
getPermission() {@inheritdoc} yii\rbac\BaseManager
getPermissions() {@inheritdoc} yii\rbac\BaseManager
getPermissionsByRole() {@inheritdoc} yii\rbac\DbManager
getPermissionsByUser() {@inheritdoc} yii\rbac\DbManager
getRole() {@inheritdoc} yii\rbac\BaseManager
getRoles() {@inheritdoc} yii\rbac\BaseManager
getRolesByUser() {@inheritdoc} The roles returned by this method include the roles assigned via $defaultRoles. yii\rbac\DbManager
getRule() {@inheritdoc} yii\rbac\DbManager
getRules() {@inheritdoc} yii\rbac\DbManager
getUserIdsByRole() Returns all role assignment information for the specified role. yii\rbac\DbManager
hasChild() {@inheritdoc} yii\rbac\DbManager
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. yii\rbac\DbManager
invalidateCache() yii\rbac\DbManager
loadFromCache() yii\rbac\DbManager
off() Detaches an existing event handler from this component. yii\base\Component
on() Attaches an event handler to an event. yii\base\Component
remove() {@inheritdoc} yii\rbac\BaseManager
removeAll() {@inheritdoc} yii\rbac\DbManager
removeAllAssignments() {@inheritdoc} yii\rbac\DbManager
removeAllPermissions() {@inheritdoc} yii\rbac\DbManager
removeAllRoles() {@inheritdoc} yii\rbac\DbManager
removeAllRules() {@inheritdoc} yii\rbac\DbManager
removeChild() {@inheritdoc} yii\rbac\DbManager
removeChildren() {@inheritdoc} yii\rbac\DbManager
revoke() {@inheritdoc} yii\rbac\DbManager
revokeAll() {@inheritdoc} yii\rbac\DbManager
setDefaultRoles() Set default roles yii\rbac\BaseManager
trigger() Triggers an event. yii\base\Component
update() {@inheritdoc} yii\rbac\BaseManager

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
addItem() {@inheritdoc} yii\rbac\DbManager
addRule() {@inheritdoc} yii\rbac\DbManager
checkAccessFromCache() Performs access check for the specified user based on the data loaded from cache. yii\rbac\DbManager
checkAccessRecursive() Performs access check for the specified user. yii\rbac\DbManager
detectLoop() Checks whether there is a loop in the authorization item hierarchy. yii\rbac\DbManager
executeRule() Executes the rule associated with the specified auth item. yii\rbac\BaseManager
getChildrenList() Returns the children for every parent. yii\rbac\DbManager
getChildrenRecursive() Recursively finds all children and grand children of the specified item. yii\rbac\DbManager
getDirectPermissionsByUser() Returns all permissions that are directly assigned to user. yii\rbac\DbManager
getInheritedPermissionsByUser() Returns all permissions that the user inherits from the roles assigned to him. yii\rbac\DbManager
getItem() {@inheritdoc} yii\rbac\DbManager
getItems() {@inheritdoc} yii\rbac\DbManager
hasNoAssignments() Checks whether array of $assignments is empty and $defaultRoles property is empty as well. yii\rbac\BaseManager
isEmptyUserId() Check whether $userId is empty. yii\rbac\DbManager
populateItem() Populates an auth item with the data fetched from database. yii\rbac\DbManager
removeAllItems() Removes all auth items of the specified type. yii\rbac\DbManager
removeItem() {@inheritdoc} yii\rbac\DbManager
removeRule() {@inheritdoc} yii\rbac\DbManager
supportsCascadeUpdate() Returns a value indicating whether the database supports cascading update and delete. yii\rbac\DbManager
updateItem() {@inheritdoc} yii\rbac\DbManager
updateRule() {@inheritdoc} yii\rbac\DbManager

Property Details

$assignmentTable public property

The name of the table storing authorization item assignments. Defaults to "auth_assignment".

public string $assignmentTable '{{%auth_assignment}}'
$cache public property (available since version 2.0.3)

The cache used to improve RBAC performance. This can be one of the following:

  • an application component ID (e.g. cache)
  • a configuration array
  • a yii\caching\Cache object

When this is not set, it means caching is not enabled.

Note that by enabling RBAC cache, all auth items, rules and auth item parent-child relationships will be cached and loaded into memory. This will improve the performance of RBAC permission check. However, it does require extra memory and as a result may not be appropriate if your RBAC system contains too many auth items. You should seek other RBAC implementations (e.g. RBAC based on Redis storage) in this case.

Also note that if you modify RBAC items, rules or parent-child relationships from outside of this component, you have to manually call invalidateCache() to ensure data consistency.

$cacheKey public property (available since version 2.0.3)

The key used to store RBAC data in cache

See also $cache.

public string $cacheKey 'rbac'
$checkAccessAssignments protected property

User assignments (user id => Assignment[])

$db public property

The DB connection object or the application component ID of the DB connection. After the DbManager object is created, if you want to change this property, you should only assign it with a DB connection object. Starting from version 2.0.2, this can also be a configuration array for creating the object.

$itemChildTable public property

The name of the table storing authorization item hierarchy. Defaults to "auth_item_child".

public string $itemChildTable '{{%auth_item_child}}'
$itemTable public property

The name of the table storing authorization items. Defaults to "auth_item".

public string $itemTable '{{%auth_item}}'
$items protected property

All auth items (name => Item)

protected yii\rbac\Item[] $items null
$parents protected property

Auth item parent-child relationships (childName => list of parents)

protected array $parents null
$ruleTable public property

The name of the table storing rules. Defaults to "auth_rule".

public string $ruleTable '{{%auth_rule}}'
$rules protected property

All auth rules (name => Rule)

protected yii\rbac\Rule[] $rules null

Method Details

addChild() public method

{@inheritdoc}

public void addChild ( $parent, $child )
$parent
$child
addItem() protected method

{@inheritdoc}

protected void addItem ( $item )
$item
addRule() protected method

{@inheritdoc}

protected void addRule ( $rule )
$rule
assign() public method

{@inheritdoc}

public void assign ( $role, $userId )
$role
$userId
canAddChild() public method (available since version 2.0.8)

{@inheritdoc}

public void canAddChild ( $parent, $child )
$parent
$child
checkAccess() public method

{@inheritdoc}

public void checkAccess ( $userId, $permissionName, $params = [] )
$userId
$permissionName
$params
checkAccessFromCache() protected method (available since version 2.0.3)

Performs access check for the specified user based on the data loaded from cache.

This method is internally called by checkAccess() when $cache is enabled.

protected boolean checkAccessFromCache ( $user, $itemName, $params, $assignments )
$user string|integer

The user ID. This should can be either an integer or a string representing the unique identifier of a user. See yii\web\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 yii\rbac\Assignment[]

The assignments to the specified user

return boolean

Whether the operations can be performed by the user.

checkAccessRecursive() protected method

Performs access check for the specified user.

This method is internally called by checkAccess().

protected boolean checkAccessRecursive ( $user, $itemName, $params, $assignments )
$user string|integer

The user ID. This should can be either an integer or a string representing the unique identifier of a user. See yii\web\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 yii\rbac\Assignment[]

The assignments to the specified user

return boolean

Whether the operations can be performed by the user.

detectLoop() protected method

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

protected boolean detectLoop ( $parent, $child )
$parent yii\rbac\Item

The parent item

$child yii\rbac\Item

The child item to be added to the hierarchy

return boolean

Whether a loop exists

getAssignment() public method

{@inheritdoc}

public void getAssignment ( $roleName, $userId )
$roleName
$userId
getAssignments() public method

{@inheritdoc}

public void getAssignments ( $userId )
$userId
getChildRoles() public method

{@inheritdoc}

public void getChildRoles ( $roleName )
$roleName
getChildren() public method

{@inheritdoc}

public void getChildren ( $name )
$name
getChildrenList() protected method

Returns the children for every parent.

protected array getChildrenList ( )
return array

The children list. Each array key is a parent item name, and the corresponding array value is a list of child item names.

getChildrenRecursive() protected method

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

protected void getChildrenRecursive ( $name, $childrenList, &$result )
$name string

The name of the item whose children are to be looked for.

$childrenList array

The child list built via getChildrenList()

$result array

The children and grand children (in array keys)

getDirectPermissionsByUser() protected method (available since version 2.0.7)

Returns all permissions that are directly assigned to user.

protected yii\rbac\Permission[] getDirectPermissionsByUser ( $userId )
$userId string|integer

The user ID (see yii\web\User::id)

return yii\rbac\Permission[]

All direct permissions that the user has. The array is indexed by the permission names.

getInheritedPermissionsByUser() protected method (available since version 2.0.7)

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

protected yii\rbac\Permission[] getInheritedPermissionsByUser ( $userId )
$userId string|integer

The user ID (see yii\web\User::id)

return yii\rbac\Permission[]

All inherited permissions that the user has. The array is indexed by the permission names.

getItem() protected method

{@inheritdoc}

protected void getItem ( $name )
$name
getItems() protected method

{@inheritdoc}

protected void getItems ( $type )
$type
getPermissionsByRole() public method

{@inheritdoc}

public void getPermissionsByRole ( $roleName )
$roleName
getPermissionsByUser() public method

{@inheritdoc}

public void getPermissionsByUser ( $userId )
$userId
getRolesByUser() public method

{@inheritdoc} The roles returned by this method include the roles assigned via $defaultRoles.

public void getRolesByUser ( $userId )
$userId
getRule() public method

{@inheritdoc}

public void getRule ( $name )
$name
getRules() public method

{@inheritdoc}

public void getRules ( )
getUserIdsByRole() public method (available since version 2.0.7)

Returns all role assignment information for the specified role.

public string[] getUserIdsByRole ( $roleName )
$roleName string
return string[]

The ids. An empty array will be returned if role is not assigned to any user.

hasChild() public method

{@inheritdoc}

public void hasChild ( $parent, $child )
$parent
$child
init() public method

Initializes the application component.

This method overrides the parent implementation by establishing the database connection.

public void init ( )
invalidateCache() public method

public void invalidateCache ( )
isEmptyUserId() protected method (available since version 2.0.26)

Check whether $userId is empty.

protected boolean isEmptyUserId ( $userId )
$userId mixed
loadFromCache() public method

public void loadFromCache ( )
populateItem() protected method

Populates an auth item with the data fetched from database.

protected yii\rbac\Item populateItem ( $row )
$row array

The data from the auth item table

return yii\rbac\Item

The populated auth item instance (either Role or Permission)

removeAll() public method

{@inheritdoc}

public void removeAll ( )
removeAllAssignments() public method

{@inheritdoc}

public void removeAllAssignments ( )
removeAllItems() protected method

Removes all auth items of the specified type.

protected void removeAllItems ( $type )
$type integer

The auth item type (either Item::TYPE_PERMISSION or Item::TYPE_ROLE)

removeAllPermissions() public method

{@inheritdoc}

public void removeAllPermissions ( )
removeAllRoles() public method

{@inheritdoc}

public void removeAllRoles ( )
removeAllRules() public method

{@inheritdoc}

public void removeAllRules ( )
removeChild() public method

{@inheritdoc}

public void removeChild ( $parent, $child )
$parent
$child
removeChildren() public method

{@inheritdoc}

public void removeChildren ( $parent )
$parent
removeItem() protected method

{@inheritdoc}

protected void removeItem ( $item )
$item
removeRule() protected method

{@inheritdoc}

protected void removeRule ( $rule )
$rule
revoke() public method

{@inheritdoc}

public void revoke ( $role, $userId )
$role
$userId
revokeAll() public method

{@inheritdoc}

public void revokeAll ( $userId )
$userId
supportsCascadeUpdate() protected method

Returns a value indicating whether the database supports cascading update and delete.

The default implementation will return false for SQLite database and true for all other databases.

protected boolean supportsCascadeUpdate ( )
return boolean

Whether the database supports cascading update and delete.

updateItem() protected method

{@inheritdoc}

protected void updateItem ( $name, $item )
$name
$item
updateRule() protected method

{@inheritdoc}

protected void updateRule ( $name, $rule )
$name
$rule