| Total Complexity | 14 |
| Total Lines | 67 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 13 | class AccessBehavior extends Behavior |
||
| 14 | {
|
||
| 15 | /** |
||
| 16 | * @var string |
||
| 17 | */ |
||
| 18 | public $permission = ''; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | public $role = ''; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @inheritdoc |
||
| 27 | * @return array |
||
| 28 | */ |
||
| 29 | public function events() |
||
| 33 | ]; |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @inheritdoc |
||
| 38 | */ |
||
| 39 | public function accessAction() |
||
| 40 | {
|
||
| 41 | if ((!empty($this->permission) && !$this->checkPermission()) || (!empty($this->role) && !$this->checkRole())) {
|
||
| 42 | $this->processLogout(); |
||
| 43 | } |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @return bool |
||
| 48 | */ |
||
| 49 | protected function checkPermission() |
||
| 50 | {
|
||
| 51 | if (!empty($this->permission)) {
|
||
| 52 | if (Yii::$app->user->can($this->permission)) {
|
||
| 53 | return true; |
||
| 54 | } |
||
| 55 | } |
||
| 56 | return false; |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @return bool |
||
| 61 | */ |
||
| 62 | protected function checkRole() |
||
| 63 | {
|
||
| 64 | if (!empty($this->role)) {
|
||
| 65 | if (Yii::$app->user->can($this->role)) {
|
||
| 66 | return true; |
||
| 67 | } |
||
| 68 | } |
||
| 69 | return false; |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Logout and set Flash message |
||
| 74 | */ |
||
| 75 | private function processLogout() |
||
| 80 | } |
||
| 81 | } |
||
| 82 | } |
||
| 83 |