RoleFilter   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isAllowed() 0 10 5
A init() 0 7 2
1
<?php
2
3
namespace dominus77\maintenance\filters;
4
5
use Yii;
6
use yii\web\User;
7
use dominus77\maintenance\Filter;
8
9
/**
10
 * Class RoleFilter
11
 * @package dominus77\maintenance\filters
12
 */
13
class RoleFilter extends Filter
14
{
15
    /**
16
     * @var array|string
17
     */
18
    public $roles;
19
    /**
20
     * @var User
21
     */
22
    protected $user;
23
24
    /**
25
     * @inheritdoc
26
     */
27
    public function init()
28
    {
29
        $this->user = Yii::$app->user;
30
        if (is_string($this->roles)) {
31
            $this->roles = [$this->roles];
32
        }
33
        parent::init();
34
    }
35
36
    /**
37
     * @return bool
38
     */
39
    public function isAllowed()
40
    {
41
        if (is_array($this->roles) && !empty($this->roles)) {
42
            foreach ($this->roles as $role) {
43
                if ($this->user->can($role)) {
44
                    return true;
45
                }
46
            }
47
        }
48
        return false;
49
    }
50
}
51