-
Notifications
You must be signed in to change notification settings - Fork 140
/
RbacWebModule.php
71 lines (62 loc) · 1.57 KB
/
RbacWebModule.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
/*
* This file is part of the Dektrium project.
*
* (c) Dektrium project <http://github.com/dektrium>
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/
namespace dektrium\rbac;
use yii\base\Module as BaseModule;
use yii\filters\AccessControl;
/**
* @author Dmitry Erofeev <[email protected]>
*/
class RbacWebModule extends BaseModule
{
/**
* @var string
*/
public $defaultRoute = 'role/index';
/**
* @var array
*/
public $admins = [];
/**
* @var string The Administrator permission name.
*/
public $adminPermission;
/** @inheritdoc */
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'allow' => true,
'roles' => ['@'],
'matchCallback' => [$this, 'checkAccess'],
]
],
],
];
}
/**
* Checks access.
*
* @return bool
*/
public function checkAccess()
{
$user = \Yii::$app->user->identity;
if (method_exists($user, 'getIsAdmin')) {
return $user->getIsAdmin();
} else if ($this->adminPermission) {
return \Yii::$app->user->can($this->adminPermission);
} else {
return isset($user->username) ? in_array($user->username, $this->admins) : false;
}
}
}