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