UserFilter   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 38
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 38
rs 10

2 Methods

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