Class yii\mongodb\rbac\MongoDbManager

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

MongoDbManager represents an authorization manager that stores authorization information in MongoDB.

Manager uses 3 collections for the RBAC data storage:

These collection are better to be pre-created with search fields indexed.

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$assignmentCollection string|array The name of the collection storing authorization item assignments. yii\mongodb\rbac\MongoDbManager
$cache yii\caching\Cache|array|string The cache used to improve RBAC performance. yii\mongodb\rbac\MongoDbManager
$cacheKey string The key used to store RBAC data in cache yii\mongodb\rbac\MongoDbManager
$db yii\mongodb\Connection|array|string The MongoDB connection object or the application component ID of the MongoDB connection. yii\mongodb\rbac\MongoDbManager
$defaultRoles array A list of role names that are assigned to every user automatically without calling assign(). yii\rbac\BaseManager
$itemCollection string|array The name of the collection storing authorization items. yii\mongodb\rbac\MongoDbManager
$ruleCollection string|array The name of the collection storing rules. yii\mongodb\rbac\MongoDbManager

Protected Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$items yii\rbac\Item[] All auth items (name => Item) yii\mongodb\rbac\MongoDbManager
$rules yii\rbac\Rule[] All auth rules (name => Rule) yii\mongodb\rbac\MongoDbManager

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() Adds a role, permission or rule to the RBAC system. yii\rbac\BaseManager
addChild() {@inheritdoc} yii\mongodb\rbac\MongoDbManager
assign() {@inheritdoc} yii\mongodb\rbac\MongoDbManager
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\mongodb\rbac\MongoDbManager
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\mongodb\rbac\MongoDbManager
className() Returns the fully qualified name of this class. yii\base\BaseObject
createPermission() Creates a new Permission object. yii\rbac\BaseManager
createRole() Creates a new Role object. 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\mongodb\rbac\MongoDbManager
getAssignments() {@inheritdoc} yii\mongodb\rbac\MongoDbManager
getBehavior() Returns the named behavior object. yii\base\Component
getBehaviors() Returns all behaviors attached to this component. yii\base\Component
getChildRoles() {@inheritdoc} yii\mongodb\rbac\MongoDbManager
getChildren() {@inheritdoc} yii\mongodb\rbac\MongoDbManager
getDefaultRoleInstances() Returns defaultRoles as array of Role objects. yii\rbac\BaseManager
getPermission() Returns the named permission. yii\rbac\BaseManager
getPermissions() Returns all permissions in the system. yii\rbac\BaseManager
getPermissionsByRole() {@inheritdoc} yii\mongodb\rbac\MongoDbManager
getPermissionsByUser() {@inheritdoc} yii\mongodb\rbac\MongoDbManager
getRole() Returns the named role. yii\rbac\BaseManager
getRoles() Returns all roles in the system. yii\rbac\BaseManager
getRolesByUser() {@inheritdoc} yii\mongodb\rbac\MongoDbManager
getRule() {@inheritdoc} yii\mongodb\rbac\MongoDbManager
getRules() {@inheritdoc} yii\mongodb\rbac\MongoDbManager
getUserIdsByRole() {@inheritdoc} yii\mongodb\rbac\MongoDbManager
hasChild() {@inheritdoc} yii\mongodb\rbac\MongoDbManager
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\mongodb\rbac\MongoDbManager
invalidateCache() Invalidates RBAC related cache yii\mongodb\rbac\MongoDbManager
loadFromCache() Loads data from cache yii\mongodb\rbac\MongoDbManager
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. yii\rbac\BaseManager
removeAll() {@inheritdoc} yii\mongodb\rbac\MongoDbManager
removeAllAssignments() {@inheritdoc} yii\mongodb\rbac\MongoDbManager
removeAllPermissions() {@inheritdoc} yii\mongodb\rbac\MongoDbManager
removeAllRoles() {@inheritdoc} yii\mongodb\rbac\MongoDbManager
removeAllRules() {@inheritdoc} yii\mongodb\rbac\MongoDbManager
removeChild() {@inheritdoc} yii\mongodb\rbac\MongoDbManager
removeChildren() {@inheritdoc} yii\mongodb\rbac\MongoDbManager
revoke() {@inheritdoc} yii\mongodb\rbac\MongoDbManager
revokeAll() {@inheritdoc} yii\mongodb\rbac\MongoDbManager
trigger() Triggers an event. yii\base\Component
update() Updates the specified role, permission or rule in the system. yii\rbac\BaseManager

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
addItem() {@inheritdoc} yii\mongodb\rbac\MongoDbManager
addRule() {@inheritdoc} yii\mongodb\rbac\MongoDbManager
checkAccessFromCache() Performs access check for the specified user based on the data loaded from cache. yii\mongodb\rbac\MongoDbManager
checkAccessRecursive() Performs access check for the specified user. yii\mongodb\rbac\MongoDbManager
detectLoop() Checks whether there is a loop in the authorization item hierarchy. yii\mongodb\rbac\MongoDbManager
executeRule() Executes the rule associated with the specified auth item. yii\rbac\BaseManager
getChildrenList() Returns the children for every parent. yii\mongodb\rbac\MongoDbManager
getChildrenRecursive() Recursively finds all children and grand children of the specified item. yii\mongodb\rbac\MongoDbManager
getItem() {@inheritdoc} yii\mongodb\rbac\MongoDbManager
getItems() {@inheritdoc} yii\mongodb\rbac\MongoDbManager
hasNoAssignments() Checks whether array of $assignments is empty and $defaultRoles property is empty as well. yii\rbac\BaseManager
populateItem() Populates an auth item with the data fetched from collection yii\mongodb\rbac\MongoDbManager
removeAllItems() Removes all auth items of the specified type. yii\mongodb\rbac\MongoDbManager
removeItem() {@inheritdoc} yii\mongodb\rbac\MongoDbManager
removeRule() {@inheritdoc} yii\mongodb\rbac\MongoDbManager
updateItem() {@inheritdoc} yii\mongodb\rbac\MongoDbManager
updateRule() {@inheritdoc} yii\mongodb\rbac\MongoDbManager

Property Details

$assignmentCollection public property

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

public string|array $assignmentCollection 'auth_assignment'
$cache public property

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

The key used to store RBAC data in cache

See also $cache.

public string $cacheKey 'rbac'
$db public property

The MongoDB connection object or the application component ID of the MongoDB connection. After the MongoDbManager object is created, if you want to change this property, you should only assign it with a MongoDB connection object.

$itemCollection public property

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

public string|array $itemCollection 'auth_item'
$items protected property

All auth items (name => Item)

protected yii\rbac\Item[] $items null
$ruleCollection public property

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

public string|array $ruleCollection '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

{@inheritdoc}

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

{@inheritdoc}

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

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 (available since version 2.1.2)

{@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)

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}

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

{@inheritdoc}

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

{@inheritdoc}

public void getRules ( )
getUserIdsByRole() public method

{@inheritdoc}

public void getUserIdsByRole ( $roleName )
$roleName
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 MongoDB connection.

public void init ( )
invalidateCache() public method

Invalidates RBAC related cache

public void invalidateCache ( )
loadFromCache() public method

Loads data from cache

public void loadFromCache ( )
populateItem() protected method

Populates an auth item with the data fetched from collection

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

The data from the auth item collection

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
updateItem() protected method

{@inheritdoc}

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

{@inheritdoc}

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