| Total Complexity | 41 |
| Total Lines | 230 |
| 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 | $column = 'username'; |
||
| 62 | if (is_numeric($username)) { |
||
| 63 | $column = 'id'; |
||
| 64 | } elseif (filter_var($username, FILTER_VALIDATE_EMAIL)) { |
||
| 65 | $column = 'email'; |
||
| 66 | } elseif (preg_match($this->config[ 'msisdnRegex' ], $username)) { |
||
| 67 | $column = 'msisdn'; |
||
| 68 | } |
||
| 69 | |||
| 70 | if ($user = models('users')->findWhere([$column => $username], 1)) { |
||
| 71 | if ($user->account) { |
||
| 72 | if ($this->passwordVerify($password, $user->account->password)) { |
||
| 73 | if ($this->passwordRehash($password)) { |
||
| 74 | $user->account->update([ |
||
| 75 | 'id' => $user->id, |
||
| 76 | 'password' => $this->passwordHash($password), |
||
| 77 | ]); |
||
| 78 | } |
||
| 79 | |||
| 80 | $account = $user->account->getArrayCopy(); |
||
| 81 | } |
||
| 82 | } elseif ($this->passwordVerify($password, $user->password)) { |
||
| 83 | $account = $user; |
||
| 84 | } |
||
| 85 | |||
| 86 | if (isset($account)) { |
||
| 87 | foreach ($account as $key => $value) { |
||
| 88 | if (strpos($key, 'record') !== false) { |
||
| 89 | unset($account[ $key ]); |
||
| 90 | } elseif (in_array($key, |
||
| 91 | ['password', 'pin', 'token', 'sso', 'id_sys_user', 'id_sys_module', 'id_sys_module_role'])) { |
||
| 92 | unset($account[ $key ]); |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | $this->login($account); |
||
| 97 | |||
| 98 | return true; |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | return false; |
||
| 103 | } |
||
| 104 | |||
| 105 | // ------------------------------------------------------------------------ |
||
| 106 | |||
| 107 | /** |
||
| 108 | * User::loggedIn |
||
| 109 | * |
||
| 110 | * @return bool |
||
| 111 | * @throws \O2System\Psr\Cache\InvalidArgumentException |
||
| 112 | */ |
||
| 113 | public function loggedIn() |
||
| 114 | { |
||
| 115 | if (parent::loggedIn()) { |
||
| 116 | $account = new Account($_SESSION[ 'account' ]); |
||
| 117 | |||
| 118 | if ($user = models('users')->findWhere(['username' => $account->username], 1)) { |
||
| 119 | // Store Account Profile |
||
| 120 | if ($profile = $user->profile) { |
||
| 121 | $account->store('profile', $profile); |
||
| 122 | } |
||
| 123 | |||
| 124 | // Store Account Role |
||
| 125 | if ($role = $user->role) { |
||
| 126 | $account->store('role', new Role([ |
||
| 127 | 'label' => $role->label, |
||
| 128 | 'description' => $role->description, |
||
| 129 | 'code' => $role->code, |
||
| 130 | 'authorities' => $role->authorities, |
||
| 131 | ])); |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | // Store Globals Account |
||
| 136 | globals()->store('account', $account); |
||
| 137 | |||
| 138 | // Store Presenter Account |
||
| 139 | if (services()->has('view')) { |
||
| 140 | presenter()->store('account', $account); |
||
| 141 | } |
||
| 142 | |||
| 143 | return true; |
||
| 144 | } |
||
| 145 | |||
| 146 | return false; |
||
| 147 | } |
||
| 148 | |||
| 149 | // ------------------------------------------------------------------------ |
||
| 150 | |||
| 151 | /** |
||
| 152 | * User::forceLogin |
||
| 153 | * |
||
| 154 | * @param string $username |
||
| 155 | * @param string $column |
||
| 156 | * |
||
| 157 | * @return bool |
||
| 158 | */ |
||
| 159 | public function forceLogin($username, $column = 'username') |
||
| 160 | { |
||
| 161 | if (is_numeric($username)) { |
||
| 162 | $column = 'id'; |
||
| 163 | } elseif (filter_var($username, FILTER_VALIDATE_EMAIL)) { |
||
| 164 | $column = 'email'; |
||
| 165 | } elseif (preg_match($this->config[ 'msisdnRegex' ], $username)) { |
||
| 166 | $column = 'msisdn'; |
||
| 167 | } elseif (strpos($username, 'token-') !== false) { |
||
| 168 | $username = str_replace('token-', '', $username); |
||
| 169 | $column = 'token'; |
||
| 170 | } elseif (strpos($username, 'sso-') !== false) { |
||
| 171 | $username = str_replace('sso-', '', $username); |
||
| 172 | $column = 'sso'; |
||
| 173 | } |
||
| 174 | |||
| 175 | if ($account = models('users')->findWhere([$column => $username], 1)) { |
||
| 176 | $account = $account->getArrayCopy(); |
||
| 177 | |||
| 178 | foreach ($account as $key => $value) { |
||
| 179 | if (strpos($key, 'record') !== false) { |
||
| 180 | unset($account[ $key ]); |
||
| 181 | } elseif (in_array($key, ['password', 'pin', 'token', 'sso'])) { |
||
| 182 | unset($account[ $key ]); |
||
| 183 | } |
||
| 184 | } |
||
| 185 | |||
| 186 | if ($column === 'token') { |
||
| 187 | models('users')->update([ |
||
| 188 | 'id' => $account[ 'id' ], |
||
| 189 | 'token' => null, |
||
| 190 | ]); |
||
| 191 | } |
||
| 192 | |||
| 193 | $this->login($account); |
||
| 194 | |||
| 195 | return true; |
||
| 196 | } |
||
| 197 | |||
| 198 | return false; |
||
| 199 | } |
||
| 200 | |||
| 201 | // ------------------------------------------------------------------------ |
||
| 202 | |||
| 203 | /** |
||
| 204 | * User::authorize |
||
| 205 | * |
||
| 206 | * @param \O2System\Framework\Http\Message\ServerRequest $request |
||
| 207 | * |
||
| 208 | * @return bool |
||
| 209 | */ |
||
| 210 | public function authorize(ServerRequest $request) |
||
| 211 | { |
||
| 212 | if (isset($GLOBALS[ 'account' ][ 'role' ])) { |
||
| 213 | $uriSegments = $request->getUri()->getSegments()->getString(); |
||
| 214 | $role = $GLOBALS[ 'account' ][ 'role' ]; |
||
| 215 | if (in_array($role->code, ['DEVELOPER', 'ADMINISTRATOR'])) { |
||
| 216 | globals()->store('authority', new Authority([ |
||
| 217 | 'permission' => 'GRANTED', |
||
| 218 | 'privileges' => '11111111', |
||
| 219 | ])); |
||
| 220 | |||
| 221 | return true; |
||
| 222 | } elseif ($role->authorities instanceof Authorities) { |
||
| 223 | if ($role->authorities->exists($uriSegments)) { |
||
| 224 | $authority = $role->authorities->getAuthority($uriSegments); |
||
| 225 | |||
| 226 | globals()->store('authority', $authority); |
||
| 227 | |||
| 228 | return $authority->getPermission(); |
||
| 229 | } |
||
| 230 | |||
| 231 | globals()->store('authority', new Authority([ |
||
| 232 | 'permission' => 'DENIED', |
||
| 233 | 'privileges' => '00000000', |
||
| 234 | ])); |
||
| 235 | |||
| 236 | return false; |
||
| 237 | } |
||
| 238 | } |
||
| 239 | |||
| 240 | return false; |
||
| 241 | } |
||
| 242 | |||
| 243 | // ------------------------------------------------------------------------ |
||
| 244 | |||
| 245 | /** |
||
| 246 | * User::getIframeCode |
||
| 247 | * |
||
| 248 | * @return string |
||
| 249 | * @throws \O2System\Psr\Cache\InvalidArgumentException |
||
| 250 | */ |
||
| 251 | public function getIframeCode() |
||
| 259 | } |
||
| 260 | } |
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.