Total Complexity | 43 |
Total Lines | 248 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like User often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use User, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
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() |
||
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) |
||
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() |
||
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.