AccessRule
API reference › yii › filters › AccessRule
This class represents an access rule defined by the AccessControl action filter.
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$actions | array| | List of action IDs that this rule applies to. Default: null. | AccessRule |
$allow | bool | Whether this is an ‘allow’ rule or ‘deny’ rule. Default: false. | AccessRule |
$behaviors read-only | array<yii\<static>> | List of behaviors attached to this component. | yii\ |
$controllers | array| | List of the controller IDs that this rule applies to. Default: null. | AccessRule |
$denyCallback | callable| | A callback that will be called if this rule determines the access to the current action should be denied. Default: null. | AccessRule |
$ips | array| | List of user IP addresses that this rule applies to. Default: null. | AccessRule |
$matchCallback | callable | A callback that will be called to determine if the rule should be applied. Default: null. | AccessRule |
$permissions | array| | List of RBAC (Role-Based Access Control) permissions that this rules applies to. User::can() will be called to check access. Default: null. Available since 2.0.12. | AccessRule |
$roleParams | array|Closure | Parameters to pass to the User::can() function for evaluating user permissions in $roles. Default: []. Available since 2.0.12. | AccessRule |
$roles | array| | List of roles that this rule applies to (requires properly configured User component). Default: null. | AccessRule |
$verbs | array| | List of request methods (e.g. GET, POST) that this rule applies to. Default: null. | AccessRule |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | yii\ |
__call() | Calls the named method which is not a class method. | yii\ |
__clone() | This method is called after the object is created by cloning an existing one. | yii\ |
__get() | Returns the value of a component property. | yii\ |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\ |
__set() | Sets the value of a component property. | yii\ |
__unset() | Sets a component property to be null. | yii\ |
allows() | Checks whether the Web user is allowed to perform the specified action. | AccessRule |
attachBehavior() | Attaches a behavior to this component. | yii\ |
attachBehaviors() | Attaches a list of behaviors to the component. | yii\ |
behaviors() | Returns a list of behaviors that this component should behave as. | yii\ |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\ |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\ |
className() static | Returns the fully qualified name of this class. | yii\ |
detachBehavior() | Detaches a behavior from the component. | yii\ |
detachBehaviors() | Detaches all behaviors from the component. | yii\ |
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | yii\ |
getBehavior() | Returns the named behavior object. | yii\ |
getBehaviors() | Returns all behaviors attached to this component. | yii\ |
hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. | yii\ |
hasMethod() | Returns a value indicating whether a method is defined. | yii\ |
hasProperty() | Returns a value indicating whether a property is defined for this component. | yii\ |
init() | Initializes the object. | yii\ |
off() | Detaches an existing event handler from this component. | yii\ |
on() | Attaches an event handler to an event. | yii\ |
trigger() | Triggers an event. | yii\ |
matchAction() protected | AccessRule | |
matchController() protected | AccessRule | |
matchCustom() protected | AccessRule | |
matchIP() protected | AccessRule | |
matchRole() protected | AccessRule | |
matchVerb() protected | AccessRule |
Property details
Section titled “Property details”$actions
Section titled “$actions ”public array|null $actions = nullList of action IDs that this rule applies to.
The comparison is case-sensitive. If not set or empty, it means this rule applies to all actions.
$controllers
Section titled “$controllers ”public array|null $controllers = nullList of the controller IDs that this rule applies to.
The comparison uses Controller::$uniqueId, so each controller ID is prefixed
with the module ID (if any). For a product controller in the application, you would specify
this property like ['product'] and if that controller is located in a shop module, this
would be ['shop/product'].
The comparison is case-sensitive.
If not set or empty, it means this rule applies to all controllers.
Since version 2.0.12 controller IDs can be specified as wildcards, e.g. module/*.
$denyCallback
Section titled “$denyCallback ”public callable|null $denyCallback = nullA callback that will be called if this rule determines the access to the current action should be denied.
This is the case when this rule matches
and $allow is set to false.
If not set, the behavior will be determined by AccessControl,
either using AccessControl::denyAccess()
or AccessControl::$denyCallback, if configured.
The signature of the callback should be as follows:
function ($rule, $action)where $rule is this rule, and $action is the current action object.
public array|null $ips = nullList of user IP addresses that this rule applies to.
An IP address
can contain the wildcard * at the end so that it matches IP addresses with the same prefix.
For example, ‘192.168.*’ matches all IP addresses in the segment ‘192.168.’.
It may also contain a pattern/mask like ‘172.16.0.0/12’ which would match all IPs from the
20-bit private network block in RFC1918.
If not set or empty, it means this rule applies to all IP addresses.
$matchCallback
Section titled “$matchCallback ”public callable $matchCallback = nullA callback that will be called to determine if the rule should be applied.
The signature of the callback should be as follows:
function ($rule, $action)where $rule is this rule, and $action is the current action object.
The callback should return a boolean value indicating whether this rule should be applied.
$permissions
Section titled “$permissions ”public array|null $permissions = nullList of RBAC (Role-Based Access Control) permissions that this rules applies to. User::can() will be called to check access.
If this property is not set or empty, it means this rule applies regardless of permissions.
Since
2.0.12
$roleParams
Section titled “$roleParams ”public array|object|Closure $roleParams = []Parameters to pass to the User::can() function for evaluating user permissions in $roles.
If this is an array, it will be passed directly to User::can(). For example for passing an
ID from the current request, you may use the following:
['postId' => Yii::$app->request->get('id')]You may also specify a closure that returns an array. This can be used to evaluate the array values only if they are needed, for example when a model needs to be loaded like in the following code:
'rules' => [ [ 'allow' => true, 'actions' => ['update'], 'roles' => ['updatePost'], 'roleParams' => function($rule) { return ['post' => Post::findOne(Yii::$app->request->get('id'))]; }, ],],A reference to the AccessRule instance will be passed to the closure as the first parameter.
Since
2.0.12
$roles
Section titled “$roles ”public array|null $roles = nullList of roles that this rule applies to (requires properly configured User component).
Two special roles are recognized, and they are checked via User::$isGuest:
?: matches a guest user (not authenticated yet)@: matches an authenticated user
If you are using RBAC (Role-Based Access Control), you may also specify role names.
In this case, User::can() will be called to check access.
Note that it is preferred to check for permissions instead.
If this property is not set or empty, it means this rule applies regardless of roles.
$verbs
Section titled “$verbs ”public array|null $verbs = nullList of request methods (e.g. GET, POST) that this rule applies to.
If not set or empty, it means this rule applies to all request methods.
Method details
Section titled “Method details”allows()
Section titled “allows() ”public function allows(yii\base\Action $action, yii\web\User|false $user, yii\web\Request $request): bool|nullChecks whether the Web user is allowed to perform the specified action.
Parameters
| Parameter | Type | Description |
|---|---|---|
$action | yii\ | The action to be performed |
$user | yii\| | The user object or false in case of detached User component |
$request | yii\ |
Returns
bool|null: true if the user is allowed, false if the user is denied, null if the rule does not apply to the user
matchAction()
Section titled “matchAction() ”protected function matchAction(yii\base\Action $action): boolParameters
| Parameter | Type | Description |
|---|---|---|
$action | yii\ | The action |
Returns
bool: Whether the rule applies to the action
matchController()
Section titled “matchController() ”protected function matchController(yii\base\Controller|null $controller): boolParameters
| Parameter | Type | Description |
|---|---|---|
$controller | yii\| | The controller, or null when the action runs as a standalone action dispatched by Module::$actionMap (since 22.0). |
Returns
bool: Whether the rule applies to the controller.
matchCustom()
Section titled “matchCustom() ”protected function matchCustom(yii\base\Action $action): boolParameters
| Parameter | Type | Description |
|---|---|---|
$action | yii\ | The action to be performed |
Returns
bool: Whether the rule should be applied
matchIP()
Section titled “matchIP() ”protected function matchIP(string|null $ip): boolParameters
| Parameter | Type | Description |
|---|---|---|
$ip | string| | The IP address |
Returns
bool: Whether the rule applies to the IP address
matchRole()
Section titled “matchRole() ”protected function matchRole(yii\web\User $user): boolParameters
| Parameter | Type | Description |
|---|---|---|
$user | yii\ | The user object |
Returns
bool: Whether the rule applies to the role
Throws
yii\base\InvalidConfigException: if User component is detached
matchVerb()
Section titled “matchVerb() ”protected function matchVerb(string $verb): boolParameters
| Parameter | Type | Description |
|---|---|---|
$verb | string | The request method. |
Returns
bool: Whether the rule applies to the request