1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the O2System Framework package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @author Steeve Andrian Salim |
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------ |
13
|
|
|
|
14
|
|
|
namespace O2System\Framework\Libraries\AccessControl; |
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
17
|
|
|
|
18
|
|
|
use O2System\Framework\Http\Message\ServerRequest; |
19
|
|
|
use O2System\Security\Authentication\User\Account; |
20
|
|
|
use O2System\Security\Authentication\User\Authorities; |
21
|
|
|
use O2System\Security\Authentication\User\Authority; |
22
|
|
|
use O2System\Security\Authentication\User\Role; |
23
|
|
|
use O2System\Spl\Exceptions\RuntimeException; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Class User |
27
|
|
|
* @package O2System\Framework\Libraries\AccessControl |
28
|
|
|
*/ |
29
|
|
|
class User extends \O2System\Security\Authentication\User |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* User::__construct |
33
|
|
|
* |
34
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException |
35
|
|
|
*/ |
36
|
|
|
public function __construct() |
37
|
|
|
{ |
38
|
|
|
parent::__construct(); |
39
|
|
|
|
40
|
|
|
if ($config = config()->loadFile('AccessControl', true)) { |
|
|
|
|
41
|
|
|
$this->setConfig($config->getArrayCopy()); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if ( ! models('users')) { |
45
|
|
|
throw new RuntimeException('ACL_E_UNDEFINED_USERS_MODEL'); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
// ------------------------------------------------------------------------ |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* User::authenticate |
53
|
|
|
* |
54
|
|
|
* @param string $username |
55
|
|
|
* @param string $password |
56
|
|
|
* |
57
|
|
|
* @return bool |
58
|
|
|
*/ |
59
|
|
|
public function authenticate($username, $password) |
60
|
|
|
{ |
61
|
|
|
if ($user = $this->find($username)) { |
62
|
|
|
if ($user->account) { |
|
|
|
|
63
|
|
|
if ($this->passwordVerify($password, $user->account->password)) { |
64
|
|
|
if ($this->passwordRehash($password)) { |
65
|
|
|
$user->account->update([ |
66
|
|
|
'id' => $user->id, |
|
|
|
|
67
|
|
|
'password' => $this->passwordHash($password), |
68
|
|
|
]); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$account = $user->account->getArrayCopy(); |
72
|
|
|
} |
73
|
|
|
} elseif ($this->passwordVerify($password, $user->password)) { |
|
|
|
|
74
|
|
|
$account = $user; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (isset($account)) { |
78
|
|
|
foreach ($account as $key => $value) { |
79
|
|
|
if (strpos($key, 'record') !== false) { |
80
|
|
|
unset($account[ $key ]); |
81
|
|
|
} elseif (in_array($key, |
82
|
|
|
['password', 'pin', 'token', 'sso', 'id_sys_user', 'id_sys_module', 'id_sys_module_role'])) { |
83
|
|
|
unset($account[ $key ]); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$this->login($account); |
88
|
|
|
|
89
|
|
|
return true; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return false; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
// ------------------------------------------------------------------------ |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* User::find |
100
|
|
|
* |
101
|
|
|
* @param string $username |
102
|
|
|
* |
103
|
|
|
* @return bool|mixed|\O2System\Database\DataObjects\Result|\O2System\Framework\Models\Sql\DataObjects\Result |
104
|
|
|
*/ |
105
|
|
|
public function find($username) |
106
|
|
|
{ |
107
|
|
|
$column = 'username'; |
108
|
|
|
if (is_numeric($username)) { |
109
|
|
|
$column = 'id'; |
110
|
|
|
} elseif (filter_var($username, FILTER_VALIDATE_EMAIL)) { |
111
|
|
|
$column = 'email'; |
112
|
|
|
} elseif (preg_match($this->config[ 'msisdnRegex' ], $username)) { |
113
|
|
|
$column = 'msisdn'; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if ($user = models('users')->findWhere([$column => $username], 1)) { |
|
|
|
|
117
|
|
|
return $user; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return false; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
// ------------------------------------------------------------------------ |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* User::loggedIn |
127
|
|
|
* |
128
|
|
|
* @return bool |
129
|
|
|
* @throws \O2System\Psr\Cache\InvalidArgumentException |
130
|
|
|
*/ |
131
|
|
|
public function loggedIn() |
132
|
|
|
{ |
133
|
|
|
if (parent::loggedIn()) { |
134
|
|
|
$account = new Account($_SESSION[ 'account' ]); |
135
|
|
|
|
136
|
|
|
if ($user = models('users')->findWhere(['username' => $account->username], 1)) { |
137
|
|
|
// Store Account Profile |
138
|
|
|
if ($profile = $user->profile) { |
|
|
|
|
139
|
|
|
$account->store('profile', $profile); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
// Store Account Role |
143
|
|
|
if ($role = $user->role) { |
|
|
|
|
144
|
|
|
$account->store('role', new Role([ |
145
|
|
|
'label' => $role->label, |
146
|
|
|
'description' => $role->description, |
147
|
|
|
'code' => $role->code, |
148
|
|
|
'authorities' => $role->authorities, |
149
|
|
|
])); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
// Store Globals Account |
154
|
|
|
globals()->store('account', $account); |
155
|
|
|
|
156
|
|
|
// Store Presenter Account |
157
|
|
|
if (services()->has('view')) { |
158
|
|
|
presenter()->store('account', $account); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return true; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return false; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
// ------------------------------------------------------------------------ |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* User::forceLogin |
171
|
|
|
* |
172
|
|
|
* @param string $username |
173
|
|
|
* @param string $column |
174
|
|
|
* |
175
|
|
|
* @return bool |
176
|
|
|
*/ |
177
|
|
|
public function forceLogin($username, $column = 'username') |
178
|
|
|
{ |
179
|
|
|
if (is_numeric($username)) { |
180
|
|
|
$column = 'id'; |
181
|
|
|
} elseif (filter_var($username, FILTER_VALIDATE_EMAIL)) { |
182
|
|
|
$column = 'email'; |
183
|
|
|
} elseif (preg_match($this->config[ 'msisdnRegex' ], $username)) { |
184
|
|
|
$column = 'msisdn'; |
185
|
|
|
} elseif (strpos($username, 'token-') !== false) { |
186
|
|
|
$username = str_replace('token-', '', $username); |
187
|
|
|
$column = 'token'; |
188
|
|
|
} elseif (strpos($username, 'sso-') !== false) { |
189
|
|
|
$username = str_replace('sso-', '', $username); |
190
|
|
|
$column = 'sso'; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
if ($account = models('users')->findWhere([$column => $username], 1)) { |
194
|
|
|
$account = $account->getArrayCopy(); |
195
|
|
|
|
196
|
|
|
foreach ($account as $key => $value) { |
197
|
|
|
if (strpos($key, 'record') !== false) { |
198
|
|
|
unset($account[ $key ]); |
199
|
|
|
} elseif (in_array($key, ['password', 'pin', 'token', 'sso'])) { |
200
|
|
|
unset($account[ $key ]); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
if ($column === 'token') { |
205
|
|
|
models('users')->update([ |
|
|
|
|
206
|
|
|
'id' => $account[ 'id' ], |
207
|
|
|
'token' => null, |
208
|
|
|
]); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
$this->login($account); |
212
|
|
|
|
213
|
|
|
return true; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
return false; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
// ------------------------------------------------------------------------ |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* User::authorize |
223
|
|
|
* |
224
|
|
|
* @param \O2System\Framework\Http\Message\ServerRequest $request |
225
|
|
|
* |
226
|
|
|
* @return bool |
227
|
|
|
*/ |
228
|
|
|
public function authorize(ServerRequest $request) |
229
|
|
|
{ |
230
|
|
|
if (isset($GLOBALS[ 'account' ][ 'role' ])) { |
231
|
|
|
$uriSegments = $request->getUri()->getSegments()->getString(); |
232
|
|
|
$role = $GLOBALS[ 'account' ][ 'role' ]; |
233
|
|
|
if (in_array($role->code, ['DEVELOPER', 'ADMINISTRATOR'])) { |
234
|
|
|
globals()->store('authority', new Authority([ |
235
|
|
|
'permission' => 'GRANTED', |
236
|
|
|
'privileges' => '11111111', |
237
|
|
|
])); |
238
|
|
|
|
239
|
|
|
return true; |
240
|
|
|
} elseif ($role->authorities instanceof Authorities) { |
241
|
|
|
if ($role->authorities->exists($uriSegments)) { |
242
|
|
|
$authority = $role->authorities->getAuthority($uriSegments); |
243
|
|
|
|
244
|
|
|
globals()->store('authority', $authority); |
245
|
|
|
|
246
|
|
|
return $authority->getPermission(); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
globals()->store('authority', new Authority([ |
250
|
|
|
'permission' => 'DENIED', |
251
|
|
|
'privileges' => '00000000', |
252
|
|
|
])); |
253
|
|
|
|
254
|
|
|
return false; |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
return false; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
// ------------------------------------------------------------------------ |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* User::getIframeCode |
265
|
|
|
* |
266
|
|
|
* @return string |
267
|
|
|
* @throws \O2System\Psr\Cache\InvalidArgumentException |
268
|
|
|
*/ |
269
|
|
|
public function getIframeCode() |
270
|
|
|
{ |
271
|
|
|
if ($this->signedOn() && $this->loggedIn() === false) { |
272
|
|
|
return '<iframe id="sign-on-iframe" width="1" height="1" src="' . rtrim($this->config[ 'sso' ][ 'server' ], |
273
|
|
|
'/') . '" style="display: none; visibility: hidden;"></iframe>'; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
return ''; |
277
|
|
|
} |
278
|
|
|
} |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.