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 = []) |
|
|
|
|
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) { |
|
|
|
|
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) { |
|
|
|
|
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
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.