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::$app |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $app = 'app'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* User::__construct |
40
|
|
|
* |
41
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException |
42
|
|
|
*/ |
43
|
|
|
public function __construct() |
44
|
|
|
{ |
45
|
|
|
parent::__construct(); |
46
|
|
|
|
47
|
|
|
if ($config = config()->loadFile('AccessControl', true)) { |
|
|
|
|
48
|
|
|
$this->setConfig($config->getArrayCopy()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if ( ! models('users')) { |
52
|
|
|
throw new RuntimeException('ACL_E_UNDEFINED_USERS_MODEL'); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
// ------------------------------------------------------------------------ |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* User::setApp |
60
|
|
|
* |
61
|
|
|
* @param string $app |
62
|
|
|
* |
63
|
|
|
* @return static |
64
|
|
|
*/ |
65
|
|
|
public function setApp($app) |
66
|
|
|
{ |
67
|
|
|
if($app = modules()->getApp($app)) { |
|
|
|
|
68
|
|
|
$this->app = $app; |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// ------------------------------------------------------------------------ |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* User::authenticate |
78
|
|
|
* |
79
|
|
|
* @param string $username |
80
|
|
|
* @param string $password |
81
|
|
|
* |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
|
|
public function authenticate($username, $password) |
85
|
|
|
{ |
86
|
|
|
if ($user = $this->find($username)) { |
87
|
|
|
if ($user->account) { |
|
|
|
|
88
|
|
|
if ($this->passwordVerify($password, $user->account->password)) { |
89
|
|
|
if ($this->passwordRehash($password)) { |
90
|
|
|
$user->account->update([ |
91
|
|
|
'id' => $user->id, |
|
|
|
|
92
|
|
|
'password' => $this->passwordHash($password), |
93
|
|
|
]); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$account = $user->account->getArrayCopy(); |
97
|
|
|
} |
98
|
|
|
} elseif ($this->passwordVerify($password, $user->password)) { |
|
|
|
|
99
|
|
|
$account = $user->getArrayCopy(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if (isset($account)) { |
103
|
|
|
foreach ($account as $key => $value) { |
104
|
|
|
if (strpos($key, 'record') !== false) { |
105
|
|
|
unset($account[ $key ]); |
106
|
|
|
} elseif (in_array($key, |
107
|
|
|
['password', 'pin', 'token', 'sso', 'id_sys_user', 'id_sys_module', 'id_sys_module_role'])) { |
108
|
|
|
unset($account[ $key ]); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$this->login($account); |
113
|
|
|
|
114
|
|
|
return true; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return false; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
// ------------------------------------------------------------------------ |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* User::find |
125
|
|
|
* |
126
|
|
|
* @param string $username |
127
|
|
|
* |
128
|
|
|
* @return bool|mixed|\O2System\Database\DataObjects\Result|\O2System\Framework\Models\Sql\DataObjects\Result |
129
|
|
|
*/ |
130
|
|
|
public function find($username) |
131
|
|
|
{ |
132
|
|
|
$column = 'username'; |
133
|
|
|
if (is_numeric($username)) { |
134
|
|
|
$column = 'id'; |
135
|
|
|
} elseif (filter_var($username, FILTER_VALIDATE_EMAIL)) { |
136
|
|
|
$column = 'email'; |
137
|
|
|
} elseif (preg_match($this->config[ 'msisdnRegex' ], $username)) { |
138
|
|
|
$column = 'msisdn'; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
if ($user = models('users')->findWhere([ |
|
|
|
|
142
|
|
|
$column => $username |
143
|
|
|
], 1)) { |
144
|
|
|
return $user; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return false; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
// ------------------------------------------------------------------------ |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* User::loggedIn |
154
|
|
|
* |
155
|
|
|
* @return bool |
156
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
157
|
|
|
*/ |
158
|
|
|
public function loggedIn() |
159
|
|
|
{ |
160
|
|
|
if (parent::loggedIn()) { |
161
|
|
|
$account = new Account($_SESSION[ 'account' ]); |
162
|
|
|
|
163
|
|
|
if ($user = models('users')->findWhere(['username' => $account->username], 1)) { |
164
|
|
|
// Store Account Profile |
165
|
|
|
if ($profile = $user->profile) { |
|
|
|
|
166
|
|
|
$account->store('profile', $profile); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
// Store Account Role |
170
|
|
|
if ($role = $user->role) { |
|
|
|
|
171
|
|
|
$account->store('role', new Role([ |
172
|
|
|
'label' => $role->label, |
173
|
|
|
'description' => $role->description, |
174
|
|
|
'code' => $role->code, |
175
|
|
|
'authorities' => $role->authorities, |
176
|
|
|
])); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
// Store Globals Account |
181
|
|
|
globals()->store('account', $account); |
182
|
|
|
|
183
|
|
|
// Store Presenter Account |
184
|
|
|
if (services()->has('view')) { |
185
|
|
|
presenter()->store('account', $account); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
return true; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return false; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
// ------------------------------------------------------------------------ |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* User::forceLogin |
198
|
|
|
* |
199
|
|
|
* @param string $username |
200
|
|
|
* @param string $column |
201
|
|
|
* |
202
|
|
|
* @return bool |
203
|
|
|
*/ |
204
|
|
|
public function forceLogin($username, $column = 'username') |
205
|
|
|
{ |
206
|
|
|
if (is_numeric($username)) { |
207
|
|
|
$column = 'id'; |
208
|
|
|
} elseif (filter_var($username, FILTER_VALIDATE_EMAIL)) { |
209
|
|
|
$column = 'email'; |
210
|
|
|
} elseif (preg_match($this->config[ 'msisdnRegex' ], $username)) { |
211
|
|
|
$column = 'msisdn'; |
212
|
|
|
} elseif (strpos($username, 'token-') !== false) { |
213
|
|
|
$username = str_replace('token-', '', $username); |
214
|
|
|
$column = 'token'; |
215
|
|
|
} elseif (strpos($username, 'sso-') !== false) { |
216
|
|
|
$username = str_replace('sso-', '', $username); |
217
|
|
|
$column = 'sso'; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
if ($account = models('users')->findWhere([$column => $username], 1)) { |
221
|
|
|
$account = $account->getArrayCopy(); |
222
|
|
|
|
223
|
|
|
foreach ($account as $key => $value) { |
224
|
|
|
if (strpos($key, 'record') !== false) { |
225
|
|
|
unset($account[ $key ]); |
226
|
|
|
} elseif (in_array($key, ['password', 'pin', 'token', 'sso'])) { |
227
|
|
|
unset($account[ $key ]); |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
if ($column === 'token') { |
232
|
|
|
models('users')->update([ |
|
|
|
|
233
|
|
|
'id' => $account[ 'id' ], |
234
|
|
|
'token' => null, |
235
|
|
|
]); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
$this->login($account); |
239
|
|
|
|
240
|
|
|
return true; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
return false; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
// ------------------------------------------------------------------------ |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* User::getIframeCode |
250
|
|
|
* |
251
|
|
|
* @return string |
252
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
253
|
|
|
*/ |
254
|
|
|
public function getIframeCode() |
255
|
|
|
{ |
256
|
|
|
if ($this->signedOn() && $this->loggedIn() === false) { |
257
|
|
|
return '<iframe id="sign-on-iframe" width="1" height="1" src="' . rtrim($this->config[ 'sso' ][ 'server' ], |
258
|
|
|
'/') . '" style="display: none; visibility: hidden;"></iframe>'; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
return ''; |
262
|
|
|
} |
263
|
|
|
} |
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.