AccessBehavior   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 12
eloc 20
c 1
b 0
f 1
dl 0
loc 69
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A events() 0 4 1
A accessAction() 0 4 3
A checkRole() 0 8 3
A checkPermission() 0 8 3
A processLogout() 0 7 2
1
<?php
2
3
namespace modules\rbac\components\behavior;
4
5
use Yii;
6
use yii\base\Behavior;
7
use yii\console\Controller;
8
use modules\rbac\Module;
9
10
/**
11
 * Class AccessBehavior
12
 * @package modules\rbac\components\behavior
13
 */
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()
31
    {
32
        return [
33
            Controller::EVENT_BEFORE_ACTION => 'accessAction'
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)) {
0 ignored issues
show
Bug introduced by
The method can() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

53
            if (Yii::$app->user->/** @scrutinizer ignore-call */ can($this->permission)) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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()
77
    {
78
        if (!Yii::$app->user->isGuest) {
79
            Yii::$app->user->logout();
80
            Yii::$app->session->setFlash('error', Module::t(
0 ignored issues
show
Bug introduced by
The method setFlash() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

80
            Yii::$app->session->/** @scrutinizer ignore-call */ 
81
                                setFlash('error', Module::t(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
81
                'module',
82
                'You are not allowed access!'
83
            ));
84
        }
85
    }
86
}
87