Skip to content
PHP Ecosystem
Explore

BaseManager

API reference › yii › rbac › BaseManager

abstract classyii\rbac\BaseManager
Extendsyii\base\Component » yii\base\BaseObject
ImplementsManagerInterface, yii\base\Configurable
Since2.0
Sourceframework/rbac/BaseManager.php
2 subclasses

BaseManager is a base class implementing ManagerInterface for RBAC management.

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

PropertyTypeDescriptionDefined by
$behaviors read-onlyarray<yii\base\Behavior<static>>List of behaviors attached to this component.yii\base\Component
$defaultRoleInstances read-onlyRole[]Default roles.BaseManager
$permissions read-onlyPermission[]All permissions in the system.BaseManager
$roles read-onlyRole[]All roles in the system.BaseManager
$rules read-onlyRule[]The rules indexed by the rule namesManagerInterface
$defaultRoles protectedarrayA list of role names that are assigned to every user automatically without calling assign(). Default: [].BaseManager
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() abstractAdds an item as a child of another item.ManagerInterface
assign() abstractAssigns a role to a user.ManagerInterface
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() abstractChecks the possibility of adding a child to parent.ManagerInterface
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
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() abstractReturns the assignment information regarding a role and a user.ManagerInterface
getAssignments() abstractReturns all role assignment information for the specified user.ManagerInterface
getBehavior()Returns the named behavior object.yii\base\Component
getBehaviors()Returns all behaviors attached to this component.yii\base\Component
getChildren() abstractReturns the child permissions and/or roles.ManagerInterface
getChildRoles() abstractReturns child roles of the role specified. Depth isn’t limited.ManagerInterface
getDefaultRoleInstances()Returns defaultRoles as array of Role objects.BaseManager
getDefaultRoles()Get default rolesBaseManager
getPermission()Returns the named permission.BaseManager
getPermissions()Returns all permissions in the system.BaseManager
getPermissionsByRole() abstractReturns all permissions that the specified role represents.ManagerInterface
getPermissionsByUser() abstractReturns all permissions that the user has.ManagerInterface
getRole()Returns the named role.BaseManager
getRoles()Returns all roles in the system.BaseManager
getRolesByUser() abstractReturns the roles that are assigned to the user via assign().ManagerInterface
getRule() abstractReturns the rule of the specified name.ManagerInterface
getRules() abstractReturns all rules available in the system.ManagerInterface
getUserIdsByRole() abstractReturns all user IDs assigned to the role specified.ManagerInterface
hasChild() abstractReturns a value indicating whether the child already exists for the parent.ManagerInterface
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 object.yii\base\BaseObject
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() abstractRemoves all authorization data, including roles, permissions, rules, and assignments.ManagerInterface
removeAllAssignments() abstractRemoves all role assignments.ManagerInterface
removeAllPermissions() abstractRemoves all permissions.ManagerInterface
removeAllRoles() abstractRemoves all roles.ManagerInterface
removeAllRules() abstractRemoves all rules.ManagerInterface
removeChild() abstractRemoves a child from its parent.ManagerInterface
removeChildren() abstractRemoved all children form their parent.ManagerInterface
revoke() abstractRevokes a role from a user.ManagerInterface
revokeAll() abstractRevokes all roles from a user.ManagerInterface
setDefaultRoles()Set default rolesBaseManager
trigger()Triggers an event.yii\base\Component
update()Updates the specified role, permission or rule in the system.BaseManager
addItem() protected abstractAdds an auth item to the RBAC system.BaseManager
addRule() protected abstractAdds a rule to the RBAC system.BaseManager
executeRule() protectedExecutes the rule associated with the specified auth item.BaseManager
getItem() protected abstractReturns the named auth item.BaseManager
getItems() protected abstractReturns the items of the specified type.BaseManager
hasNoAssignments() protectedChecks whether array of $assignments is empty and $defaultRoles property is empty as well.BaseManager
removeItem() protected abstractRemoves an auth item from the RBAC system.BaseManager
removeRule() protected abstractRemoves a rule from the RBAC system.BaseManager
updateItem() protected abstractUpdates an auth item in the RBAC system.BaseManager
updateRule() protected abstractUpdates a rule to the RBAC system.BaseManager
public yii\rbac\Role[] $defaultRoleInstances

Default roles.

The array is indexed by the role names.

Access

Read-only (virtual property).

public yii\rbac\Permission[] $permissions

All permissions in the system.

The array is indexed by the permission names.

Access

Read-only (virtual property).

public yii\rbac\Role[] $roles

All roles in the system.

The array is indexed by the role names.

Access

Read-only (virtual property).

protected array $defaultRoles = []

A list of role names that are assigned to every user automatically without calling assign().

Note that these roles are applied to users, regardless of their state of authentication.

public function add(mixed $object): bool

Adds a role, permission or rule to the RBAC system.

Parameters

ParameterTypeDescription
$objectmixed

Returns

bool: Whether the role, permission or rule 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)
public function createPermission(mixed $name): yii\rbac\Permission

Creates a new Permission object.

Note that the newly created permission is not added to the RBAC system yet. You must fill in the needed data and call add() to add it to the system.

Parameters

ParameterTypeDescription
$namemixedThe permission name

Returns

Permission: The new Permission object

public function createRole(mixed $name): yii\rbac\Role

Creates a new Role object.

Note that the newly created role is not added to the RBAC system yet. You must fill in the needed data and call add() to add it to the system.

Parameters

ParameterTypeDescription
$namemixedThe role name

Returns

Role: The new Role object

public function getDefaultRoleInstances(): yii\rbac\Role[]

Returns defaultRoles as array of Role objects.

Returns

Role[]: Default roles. The array is indexed by the role names

Since

2.0.12

public function getDefaultRoles(): string[]

Get default roles

Returns

string[]: Default roles

Since

2.0.14

public function getPermission(mixed $name): yii\rbac\Permission|null

Returns the named permission.

Parameters

ParameterTypeDescription
$namemixedThe permission name.

Returns

Permission|null: The permission corresponding to the specified name. Null is returned if no such permission.

public function getPermissions(): yii\rbac\Permission[]

Returns all permissions in the system.

Returns

Permission[]: All permissions in the system. The array is indexed by the permission names.

public function getRole(mixed $name): yii\rbac\Role|null

Returns the named role.

Parameters

ParameterTypeDescription
$namemixedThe role name.

Returns

Role|null: The role corresponding to the specified name. Null is returned if no such role.

public function getRoles(): yii\rbac\Role[]

Returns all roles in the system.

Returns

Role[]: All roles in the system. The array is indexed by the role names.

public function remove(mixed $object): bool

Removes a role, permission or rule from the RBAC system.

Parameters

ParameterTypeDescription
$objectmixed

Returns

bool: Whether the role, permission or rule is successfully removed

public function setDefaultRoles(string[]|Closure $roles): mixed

Set default roles

Parameters

ParameterTypeDescription
$rolesstring[]|ClosureEither array of roles or a callable returning it

Returns

mixed

Throws

Since

2.0.14

public function update(mixed $name, mixed $object): bool

Updates the specified role, permission or rule in the system.

Parameters

ParameterTypeDescription
$namemixedThe old name of the role, permission or rule
$objectmixed

Returns

bool: Whether the update is successful

Throws

  • Exception: if data validation or saving fails (such as the name of the role or permission is not unique)
abstract protected function addItem(yii\rbac\Item $item): bool

Adds an auth item to the RBAC system.

Parameters

ParameterTypeDescription
$itemItemThe 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)
abstract protected function addRule(yii\rbac\Rule $rule): bool

Adds a rule to the RBAC system.

Parameters

ParameterTypeDescription
$ruleRuleThe 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 executeRule(string|int $user, yii\rbac\Item $item, array $params): bool

Executes the rule associated with the specified auth item.

If the item does not specify a rule, this method will return true. Otherwise, it will return the value of Rule::execute().

Parameters

ParameterTypeDescription
$userstring|intThe user ID. This should be either an integer or a string representing the unique identifier of a user. See User::$id.
$itemItemThe auth item that needs to execute its rule
$paramsarrayParameters passed to CheckAccessInterface::checkAccess() and will be passed to the rule

Returns

bool: The return value of Rule::execute(). If the auth item does not specify a rule, true will be returned.

Throws

abstract protected function getItem(string $name): yii\rbac\Item|null

Returns the named auth item.

Parameters

ParameterTypeDescription
$namestringThe auth item name.

Returns

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

abstract protected function getItems(yii\rbac\Item::TYPE_ROLE|yii\rbac\Item::TYPE_PERMISSION $type): ($type is yii\rbac\Item::TYPE_ROLE ? yii\rbac\Role[] : yii\rbac\Permission[])

Returns the items of the specified type.

Parameters

ParameterTypeDescription
$typeItem::TYPE_ROLE|Item::TYPE_PERMISSIONThe 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.

protected function hasNoAssignments(yii\rbac\Assignment[] $assignments): bool

Checks whether array of $assignments is empty and $defaultRoles property is empty as well.

Parameters

ParameterTypeDescription
$assignmentsAssignment[]Array of user’s assignments

Returns

bool: Whether array of $assignments is empty and $defaultRoles property is empty as well

Since

2.0.11

abstract protected function removeItem(yii\rbac\Item $item): bool

Removes an auth item from the RBAC system.

Parameters

ParameterTypeDescription
$itemItemThe 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)
abstract protected function removeRule(yii\rbac\Rule $rule): bool

Removes a rule from the RBAC system.

Parameters

ParameterTypeDescription
$ruleRuleThe 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)
abstract protected function updateItem(string $name, yii\rbac\Item $item): bool

Updates an auth item in the RBAC system.

Parameters

ParameterTypeDescription
$namestringThe name of the item being updated
$itemItemThe 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)
abstract protected function updateRule(string $name, yii\rbac\Rule $rule): bool

Updates a rule to the RBAC system.

Parameters

ParameterTypeDescription
$namestringThe name of the rule being updated
$ruleRuleThe 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)