Total Complexity | 69 |
Total Lines | 372 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like AccessControl 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 AccessControl, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class AccessControl extends \O2System\Security\Authentication\User |
||
27 | { |
||
28 | /** |
||
29 | * User::$app |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $app = 'app'; |
||
34 | |||
35 | /** |
||
36 | * User::__construct |
||
37 | * |
||
38 | * @throws \O2System\Spl\Exceptions\RuntimeException |
||
39 | */ |
||
40 | public function __construct() |
||
41 | { |
||
42 | parent::__construct(); |
||
43 | |||
44 | if ($config = config()->loadFile('AccessControl', true)) { |
||
|
|||
45 | $this->setConfig($config->getArrayCopy()); |
||
46 | } |
||
47 | |||
48 | if ( ! models('users')) { |
||
49 | throw new RuntimeException('ACL_E_UNDEFINED_USERS_MODEL'); |
||
50 | } |
||
51 | } |
||
52 | |||
53 | // ------------------------------------------------------------------------ |
||
54 | |||
55 | /** |
||
56 | * User::setApp |
||
57 | * |
||
58 | * @param string $app |
||
59 | * |
||
60 | * @return static |
||
61 | */ |
||
62 | public function setApp($app) |
||
63 | { |
||
64 | if ($app = modules()->getApp($app)) { |
||
65 | $this->app = $app; |
||
66 | } |
||
67 | |||
68 | return $this; |
||
69 | } |
||
70 | |||
71 | // ------------------------------------------------------------------------ |
||
72 | |||
73 | /** |
||
74 | * User::authenticate |
||
75 | * |
||
76 | * @param string $username |
||
77 | * @param string $password |
||
78 | * |
||
79 | * @return bool |
||
80 | */ |
||
81 | public function authenticate($username, $password) |
||
82 | { |
||
83 | if ($user = $this->find($username)) { |
||
84 | if ($user->account) { |
||
85 | if ($this->passwordVerify($password, $user->account->password)) { |
||
86 | if ($this->passwordRehash($password)) { |
||
87 | $user->account->update([ |
||
88 | 'id' => $user->id, |
||
89 | 'password' => $this->passwordHash($password), |
||
90 | ]); |
||
91 | } |
||
92 | |||
93 | $account = $user->account->getArrayCopy(); |
||
94 | } |
||
95 | } elseif ($this->passwordVerify($password, $user->password)) { |
||
96 | $account = $user->getArrayCopy(); |
||
97 | } |
||
98 | |||
99 | if (isset($account)) { |
||
100 | foreach ($account as $key => $value) { |
||
101 | if (strpos($key, 'record') !== false) { |
||
102 | unset($account[ $key ]); |
||
103 | } elseif (in_array($key, |
||
104 | ['password', 'pin', 'token', 'sso', 'id_sys_user', 'id_sys_module', 'id_sys_module_role'])) { |
||
105 | unset($account[ $key ]); |
||
106 | } |
||
107 | } |
||
108 | |||
109 | $this->login($account); |
||
110 | |||
111 | return true; |
||
112 | } |
||
113 | } |
||
114 | |||
115 | return false; |
||
116 | } |
||
117 | |||
118 | // ------------------------------------------------------------------------ |
||
119 | |||
120 | /** |
||
121 | * User::find |
||
122 | * |
||
123 | * @param string $username |
||
124 | * |
||
125 | * @return bool|mixed|\O2System\Database\DataObjects\Result|\O2System\Framework\Models\Sql\DataObjects\Result |
||
126 | */ |
||
127 | public function find($username) |
||
128 | { |
||
129 | $column = 'username'; |
||
130 | if (is_numeric($username)) { |
||
131 | $column = 'id'; |
||
132 | } elseif (filter_var($username, FILTER_VALIDATE_EMAIL)) { |
||
133 | $column = 'email'; |
||
134 | } elseif (preg_match($this->config[ 'msisdnRegex' ], $username)) { |
||
135 | $column = 'msisdn'; |
||
136 | } |
||
137 | |||
138 | if ($user = models('users')->findWhere([ |
||
139 | $column => $username, |
||
140 | ], 1)) { |
||
141 | return $user; |
||
142 | } |
||
143 | |||
144 | return false; |
||
145 | } |
||
146 | |||
147 | // ------------------------------------------------------------------------ |
||
148 | |||
149 | /** |
||
150 | * User::loggedIn |
||
151 | * |
||
152 | * @return bool |
||
153 | * @throws \Psr\Cache\InvalidArgumentException |
||
154 | */ |
||
155 | public function loggedIn() |
||
156 | { |
||
157 | if (parent::loggedIn()) { |
||
158 | if(is_object($_SESSION['account'])) { |
||
159 | $account = new Account($_SESSION['account']->getArrayCopy()); |
||
160 | $username = $account->user->username; |
||
161 | } else { |
||
162 | $account = new Account(); |
||
163 | $username = $_SESSION['account']['username']; |
||
164 | } |
||
165 | |||
166 | if ($user = models('users')->findWhere(['username' => $username], 1)) { |
||
167 | if ($profile = $user->profile) { |
||
168 | $account->store('profile', $profile); |
||
169 | } |
||
170 | |||
171 | if ($employee = $user->employee) { |
||
172 | $account->store('employee', $employee); |
||
173 | } |
||
174 | |||
175 | if ($member = $user->member) { |
||
176 | $account->store('member', $member); |
||
177 | } |
||
178 | |||
179 | if ($role = $user->role) { |
||
180 | $user->store('role', $role); |
||
181 | } |
||
182 | |||
183 | $account->store('user', $user); |
||
184 | |||
185 | session()->set('account', $account); |
||
186 | } |
||
187 | |||
188 | // Store Globals Account |
||
189 | globals()->store('account', $account); |
||
190 | |||
191 | // Store Presenter Account |
||
192 | if (services()->has('view')) { |
||
193 | presenter()->store('account', $account); |
||
194 | } |
||
195 | |||
196 | return true; |
||
197 | } |
||
198 | |||
199 | return false; |
||
200 | } |
||
201 | |||
202 | // ------------------------------------------------------------------------ |
||
203 | |||
204 | /** |
||
205 | * User::forceLogin |
||
206 | * |
||
207 | * @param string $username |
||
208 | * @param string $column |
||
209 | * |
||
210 | * @return bool |
||
211 | */ |
||
212 | public function forceLogin($username, $column = 'username') |
||
252 | } |
||
253 | |||
254 | // ------------------------------------------------------------------------ |
||
255 | |||
256 | /** |
||
257 | * User::hasAccess |
||
258 | * |
||
259 | * @param array $segments |
||
260 | * |
||
261 | * @return bool |
||
262 | */ |
||
263 | public function hasAccess(array $segments) |
||
334 | } |
||
335 | |||
336 | // ------------------------------------------------------------------------ |
||
337 | |||
338 | /** |
||
339 | * User::hasWriteAccess |
||
340 | * |
||
341 | * @return bool |
||
342 | * @throws \Psr\Cache\InvalidArgumentException |
||
343 | */ |
||
344 | public function hasWriteAccess() |
||
380 | } |
||
381 | |||
382 | // ------------------------------------------------------------------------ |
||
383 | |||
384 | /** |
||
385 | * User::getIframeCode |
||
386 | * |
||
387 | * @return string |
||
388 | * @throws \Psr\Cache\InvalidArgumentException |
||
389 | */ |
||
390 | public function getIframeCode() |
||
398 | } |
||
399 | } |
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.