UserFilter::isAllowed()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 4
nc 2
nop 0
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