Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
4c8a1179
Commit
4c8a1179
authored
Nov 01, 2013
by
Alexander Makarov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
started authorization docs
parent
b3550192
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
124 additions
and
0 deletions
+124
-0
authorization.md
docs/guide/authorization.md
+124
-0
No files found.
docs/guide/authorization.md
View file @
4c8a1179
Authorization
=============
Authorization is the process of verifying that user has enough permissions to do something. Yii provides several methods
of controlling it.
Access control basics
---------------------
Basic acces control is very simple to implement using
[
[\yii\web\AccessControl
]
]:
```
php
class
SiteController
extends
Controller
{
public
function
behaviors
()
{
return
[
'access'
=>
[
'class'
=>
\yii\web\AccessControl
::
className
(),
'only'
=>
[
'login'
,
'logout'
,
'signup'
],
'rules'
=>
[
[
'actions'
=>
[
'login'
,
'signup'
],
'allow'
=>
true
,
'roles'
=>
[
'?'
],
],
[
'actions'
=>
[
'logout'
],
'allow'
=>
true
,
'roles'
=>
[
'@'
],
],
],
],
];
}
// ...
```
In the code above we're attaching access control behavior to a controller. Since there's
`only`
option specified, it
will be applied to 'login', 'logout' and 'signup' actions only. A set of rules that are basically options for
[
[\yii\web\AccessRule
]
] reads as follows:
-
Allow all guest (not yet authenticated) users to access 'login' and 'signup' actions.
-
Allow authenticated users to access 'logout' action.
Rules are checked one by one from top to bottom. If rule matches, action takes place immediately. If not, next rule is
checked. If no rules matched access is denied.
[
[\yii\web\AccessRule
]
] is quite flexible and allows additionally to what was demonstrated checking IPs and request method
(i.e. POST, GET). If it's not enough you can specify your own check via anonymous function:
```
php
class
SiteController
extends
Controller
{
public
function
behaviors
()
{
return
[
'access'
=>
[
'class'
=>
\yii\web\AccessControl
::
className
(),
'only'
=>
[
'special'
],
'rules'
=>
[
[
'actions'
=>
[
'special'
],
'allow'
=>
true
,
'matchCallback'
=>
function
(
$rule
,
$action
)
{
return
date
(
'd-m'
)
===
'31-10'
;
}
],
```
Sometimes you want a custom action to be taken when access is denied. In this case you can specify
`denyCallback`
.
Role based access control (RBAC)
--------------------------------
Role based access control is very flexible approach to controlling access that is a perfect match for complex systems
where permissions are customizable.
In order to start using it some extra steps are required. First of all we need to configure
`authManager`
application
component:
```
php
```
Then create permissions hierarchy.
Specify roles from RBAC in controller's access control configuration or call
[
[User::checkAccess()
]
] where appropriate.
### How it works
TBD: write about how it works with pictures :)
### Avoiding too much RBAC
In order to keep auth hierarchy simple and efficient you should avoid creating and using too much nodes. Most of the time
simple checks could be used instead. For example such code that uses RBAC:
```
php
public
function
editArticle
(
$id
)
{
$article
=
Article
::
find
(
$id
);
if
(
!
$article
)
{
throw
new
HttpException
(
404
);
}
if
(
!
\Yii
::
$app
->
user
->
checkAccess
(
'edit_article'
,
[
'article'
=>
$article
]))
{
throw
new
HttpException
(
403
);
}
// ...
}
```
can be replaced with simpler code that doesn't use RBAC:
```
php
public
function
editArticle
(
$id
)
{
$article
=
Article
::
find
([
'id'
=>
$id
,
'author_id'
=>
\Yii
::
$app
->
user
->
id
]);
if
(
!
$article
)
{
throw
new
HttpException
(
404
);
}
// ...
}
```
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment