Class yii\mongodb\rbac\MongoDbManager
Inheritance | yii\mongodb\rbac\MongoDbManager » yii\rbac\BaseManager |
---|---|
Available since version | 2.0.5 |
MongoDbManager represents an authorization manager that stores authorization information in MongoDB.
Manager uses 3 collections for the RBAC data storage:
- $itemCollection - stores item data and item parents list
- $assignmentCollection - stores assignments info
- $ruleCollection - stores rule data
These collection are better to be pre-created with search fields indexed.
Public Properties
Property | Type | Description | Defined 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 |
$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
Property | Type | Description | Defined 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
Protected Methods
Property Details
The name of the collection storing authorization item assignments. Defaults to "auth_assignment".
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.
The key used to store RBAC data in cache
See also $cache.
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.
The name of the collection storing authorization items. Defaults to "auth_item".
All auth items (name => Item)
The name of the collection storing rules. Defaults to "auth_rule".
All auth rules (name => Rule)
Method Details
public void addChild ( $parent, $child ) | ||
$parent | ||
$child |
protected void addItem ( $item ) | ||
$item |
protected void addRule ( $rule ) | ||
$rule |
public void assign ( $role, $userId ) | ||
$role | ||
$userId |
public void canAddChild ( $parent, $child ) | ||
$parent | ||
$child |
public void checkAccess ( $userId, $permissionName, $params = [] ) | ||
$userId | ||
$permissionName | ||
$params |
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 |
$assignments | \yii\rbac\Assignment[] | The assignments to the specified user |
return | boolean | Whether the operations can be performed by the user. |
---|
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 |
$assignments | \yii\rbac\Assignment[] | The assignments to the specified user |
return | boolean | Whether the operations can be performed by the user. |
---|
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 |
---|
public void getAssignment ( $roleName, $userId ) | ||
$roleName | ||
$userId |
public void getAssignments ( $userId ) | ||
$userId |
public void getChildren ( $name ) | ||
$name |
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. |
---|
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) |
protected void getItem ( $name ) | ||
$name |
protected void getItems ( $type ) | ||
$type |
public void getPermissionsByRole ( $roleName ) | ||
$roleName |
public void getPermissionsByUser ( $userId ) | ||
$userId |
public void getRolesByUser ( $userId ) | ||
$userId |
public void getRule ( $name ) | ||
$name |
public void getRules ( ) |
public void getUserIdsByRole ( $roleName ) | ||
$roleName |
public void hasChild ( $parent, $child ) | ||
$parent | ||
$child |
Initializes the application component.
This method overrides the parent implementation by establishing the MongoDB connection.
public void init ( ) |
Invalidates RBAC related cache
public void invalidateCache ( ) |
Loads data from cache
public void loadFromCache ( ) |
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) |
---|
public void removeAll ( ) |
public void removeAllAssignments ( ) |
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) |
public void removeAllPermissions ( ) |
public void removeAllRoles ( ) |
public void removeAllRules ( ) |
public void removeChild ( $parent, $child ) | ||
$parent | ||
$child |
public void removeChildren ( $parent ) | ||
$parent |
protected void removeItem ( $item ) | ||
$item |
protected void removeRule ( $rule ) | ||
$rule |
public void revoke ( $role, $userId ) | ||
$role | ||
$userId |
public void revokeAll ( $userId ) | ||
$userId |
protected void updateItem ( $name, $item ) | ||
$name | ||
$item |
protected void updateRule ( $name, $rule ) | ||
$name | ||
$rule |