Completed
Push — master ( d69358...27ffc6 )
by Dmitry
04:53 queued 53s
created

AuthManager::getIdentity()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 7
ccs 0
cts 7
cp 0
crap 6
rs 9.4285
1
<?php
2
3
/*
4
 * HiPanel core package
5
 *
6
 * @link      https://hipanel.com/
7
 * @package   hipanel-core
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hipanel\components;
13
14
use hipanel\helpers\ArrayHelper;
15
use Yii;
16
use yii\base\Component;
17
use yii\base\InvalidParamException;
18
use yii\helpers\Inflector;
19
20
class AuthManager extends Component
21
{
22
    protected $_permissions;
23
24
    public function init()
25
    {
26
        parent::init();
27
    }
28
29
    public function getPermissions()
30
    {
31
        if ($this->_permissions === null) {
32
            $this->_permissions = Yii::$app->params['permissions'];
33
        }
34
35
        return $this->_permissions;
36
    }
37
38
    public function hasPermission($name, $params = [])
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
39
    {
40
        $allowed = $this->getPermissions()[$name];
41
        if (is_array($allowed)) {
42
            foreach ($allowed as $k) {
43
                $k = trim($k);
44
                if ($k === $this->type || $k === $this->username) {
0 ignored issues
show
Bug introduced by
The property username does not seem to exist. Did you mean _username?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Documentation introduced by
The property type does not exist on object<hipanel\components\AuthManager>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
45
                    return true;
46
                }
47
            }
48
        }
49
        return false;
50
    }
51
52
    public function canSupport()
53
    {
54
        static $types = ['support' => 1, 'admin' => 1, 'manager' => 1, 'reseller' => 1, 'owner' => 1];
55
        return $this->isType($types);
56
    }
57
58
    public function canAdmin()
59
    {
60
        static $types = ['admin' => 1, 'owner' => 1];
61
        return $this->isType($types);
62
    }
63
64
    public function canManage()
65
    {
66
        static $types = ['manager' => 1, 'reseller' => 1, 'owner' => 1];
67
        return $this->isType($types);
68
    }
69
70
    public function canResell()
71
    {
72
        static $types = ['reseller' => 1, 'owner' => 1];
73
        return $this->isType($types);
74
    }
75
76
    public function canOwn()
77
    {
78
        static $types = ['owner' => 1];
79
        return $this->isType($types);
80
    }
81
82
    /**
83
     * Current user.
84
     */
85
    protected $_user;
86
87
    public function getUser()
88
    {
89
        if (!$this->_user) {
90
            $this->_user = Yii::$app->user;
91
        }
92
        return $this->_user;
93
    }
94
95
    /**
96
     * Current user.
97
     */
98
    protected $_identity;
99
100
    public function getIdentity()
101
    {
102
        if (!$this->_identity) {
103
            $this->_identity = $this->getUser()->identity;
104
        }
105
        return $this->_identity;
106
    }
107
108
    /**
109
     * Current user id.
110
     */
111
    protected $_id;
112
113
    public function getId()
114
    {
115
        if (!$this->_id) {
116
            $this->_id = $this->getIdentity()->id;
117
        }
118
        return $this->_id;
119
    }
120
121
    /**
122
     * Current user username.
123
     */
124
    protected $_username;
125
126
    public function getUsername()
127
    {
128
        if (!$this->_username) {
129
            $this->_username = $this->getIdentity()->username;
130
        }
131
        return $this->_username;
132
    }
133
134
    /**
135
     * Current user type.
136
     */
137
    protected $_type;
138
139
    public function getType()
140
    {
141
        if (!$this->_type) {
142
            $this->_type = $this->getIdentity()->type;
143
        }
144
        return $this->_type;
145
    }
146
147
    public function isType($list, $type = null)
148
    {
149
        $type = is_null($type) ? $this->getType() : $type;
150
        return ArrayHelper::ksplit($list)[$type];
151
    }
152
153
    public function checkAccess($userId, $permission, $params = [])
154
    {
155
        if ($userId !== $this->id) {
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<hipanel\components\AuthManager>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
156
            throw new InvalidParamException('only current user check access is available for the moment');
157
        }
158
        return $this->hasPermission($permission, $params) || $this->canDo($permission, $params);
159
    }
160
161
    public function canDo($permission, $params = [])
162
    {
163
        $checker = 'can' . Inflector::id2camel($permission);
164
        return method_exists($this, $checker) ? $this->$checker($params) : false;
165
    }
166
}
167