AccessControl
API reference › yii › filters › AccessControl
AccessControl provides simple access control based on a set of rules.
AccessControl is an action filter. It will check its $rules to find
the first rule that matches the current context variables (such as user IP address, user role).
The matching rule will dictate whether to allow or deny the access to the requested controller
action. If no rule matches, the access will be denied.
To use AccessControl, declare it in the behaviors() method of your controller class.
For example, the following declarations will allow authenticated users to access the “create”
and “update” actions and deny all other users from accessing these two actions.
public function behaviors(){ return [ 'access' => [ 'class' => \yii\filters\AccessControl::class, 'only' => ['create', 'update'], 'rules' => [ // deny all POST requests [ 'allow' => false, 'verbs' => ['POST'] ], // allow authenticated users [ 'allow' => true, 'roles' => ['@'], ], // everything else is denied ], ], ];}Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$denyCallback | callable| | A callback that will be called if the access should be denied to the current user. Default: null. | AccessControl |
$except | array | List of action IDs that this filter should not apply to. Default: []. | yii\ |
$only | array | List of action IDs that this filter should apply to. Default: []. | yii\ |
$owner | yii\ | The owner of this behavior Default: null. | yii\ |
$ruleConfig | array | The default configuration of access rules. Default: ['class' => 'yii\. | AccessControl |
$rules | array | A list of access rule objects or configuration arrays for creating the rule objects. Default: []. | AccessControl |
$user | yii\| | The user object representing the authentication status or the ID of the user application component. Default: 'user'. | AccessControl |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | yii\ |
__call() | Calls the named method which is not a class method. | yii\ |
__get() | Returns the value of an object property. | yii\ |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\ |
__set() | Sets value of an object property. | yii\ |
__unset() | Sets an object property to null. | yii\ |
afterAction() | This method is invoked right after an action is executed. | yii\ |
afterFilter() | yii\ | |
attach() | Attaches the behavior object to the component. | yii\ |
beforeAction() | This method is invoked right before an action is to be executed (after all possible filters.) You may override this method to do last-minute preparation for the action. | AccessControl |
beforeFilter() | 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\ |
detach() | Detaches the behavior object from the component. | yii\ |
events() | Declares event handlers for the $owner’s events. | yii\ |
hasMethod() | Returns a value indicating whether a method is defined. | yii\ |
hasProperty() | Returns a value indicating whether a property is defined. | yii\ |
init() | Initializes the $rules array by instantiating rule objects from configurations. | AccessControl |
denyAccess() protected | Denies the access of the user. | AccessControl |
getActionId() protected | Returns an action ID by converting Action::$uniqueId into an ID relative to the module. | yii\ |
isActive() protected | Returns a value indicating whether the filter is active for the given action. | yii\ |
Property details
Section titled “Property details”$denyCallback
Section titled “$denyCallback ”public callable|null $denyCallback = nullA callback that will be called if the access should be denied to the current user.
This is the case when either no rule matches, or a rule with
$allow set to false matches.
If not set, denyAccess() will be called.
The signature of the callback should be as follows:
function ($rule, $action)where $rule is the rule that denies the user, and $action is the current action object.
$rule can be null if access is denied because none of the rules matched.
$ruleConfig
Section titled “$ruleConfig ”public array $ruleConfig = ['class' => 'yii\filters\AccessRule']The default configuration of access rules.
Individual rule configurations
specified via $rules will take precedence when the same property of the rule is configured.
$rules
Section titled “$rules ”public array $rules = []A list of access rule objects or configuration arrays for creating the rule objects.
If a rule is specified via a configuration array, it will be merged with $ruleConfig first
before it is used for creating the rule object.
$user
Section titled “$user ”public yii\web\User|array|string|false $user = 'user'The user object representing the authentication status or the ID of the user application component.
Starting from version 2.0.2, this can also be a configuration array for creating the object.
Starting from version 2.0.12, you can set it to false to explicitly switch this component support off for the filter.
Method details
Section titled “Method details”beforeAction()
Section titled “beforeAction() ”public function beforeAction(mixed $action): boolThis method is invoked right before an action is to be executed (after all possible filters.) You may override this method to do last-minute preparation for the action.
Parameters
| Parameter | Type | Description |
|---|---|---|
$action | mixed | The action to be executed. |
Returns
bool: Whether the action should continue to be executed.
init()
Section titled “init() ”public function init(): mixedInitializes the $rules array by instantiating rule objects from configurations.
Returns
mixed
denyAccess()
Section titled “denyAccess() ”protected function denyAccess(yii\web\User|false $user): mixedDenies the access of the user.
The default implementation will redirect the user to the login page if he is a guest; if the user is already logged, a 403 HTTP exception will be thrown.
Parameters
| Parameter | Type | Description |
|---|---|---|
$user | yii\| | The current user or boolean false in case of detached User component |
Returns
mixed
Throws
yii\web\ForbiddenHttpException: if the user is already logged in or in case of detached User component.