Complex classes like Security 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Security, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class Security |
||
14 | { |
||
15 | // sha1('user') |
||
16 | const USER_ID_TOKEN = '12dea96fec20593566ab75692c9949596833adc9'; |
||
17 | // sha1('admin') |
||
18 | const MANAGER_ID_TOKEN = 'd033e22ae348aeb5660fc2140aec35850c4da997'; |
||
19 | // sha1('superadmin') |
||
20 | const ADMIN_ID_TOKEN = '889a3a791b3875cfae413574b53da4bb8a90d53e'; |
||
21 | // sha1('FLASHES') |
||
22 | const FLASH_MESSAGE_TOKEN = '4680c68435db1bfbf17c3fcc4f7b39d2c6122504'; |
||
23 | const LOGGED_USER_TOKEN = '__U_T_L__'; |
||
24 | |||
25 | use SecureTrait; |
||
26 | use SingletonTrait; |
||
27 | /** |
||
28 | * @var array $user |
||
29 | */ |
||
30 | private $user = null; |
||
31 | |||
32 | /** |
||
33 | * @var array $admin |
||
34 | */ |
||
35 | private $admin = null; |
||
36 | |||
37 | /** |
||
38 | * @var bool $authorized |
||
39 | */ |
||
40 | private $authorized = FALSE; |
||
41 | |||
42 | /** |
||
43 | * @var bool $checked |
||
44 | */ |
||
45 | private $checked = false; |
||
46 | |||
47 | /** |
||
48 | * @var array $session |
||
49 | */ |
||
50 | protected $session; |
||
51 | |||
52 | /** |
||
53 | * Constructor por defecto |
||
54 | */ |
||
55 | 2 | public function init() |
|
56 | { |
||
57 | 2 | $this->initSession(); |
|
58 | 2 | $this->session = null === $_SESSION ? array() : $_SESSION; |
|
59 | 2 | if (NULL === $this->getSessionKey('__FLASH_CLEAR__')) { |
|
60 | 2 | $this->clearFlashes(); |
|
61 | 2 | $this->setSessionKey('__FLASH_CLEAR__', microtime(TRUE)); |
|
62 | } |
||
63 | 2 | $this->user = array_key_exists(self::USER_ID_TOKEN, $this->session) ? unserialize($this->session[self::USER_ID_TOKEN]) : NULL; |
|
64 | 2 | $this->admin = array_key_exists(self::ADMIN_ID_TOKEN, $this->session) ? unserialize($this->session[self::ADMIN_ID_TOKEN]) : NULL; |
|
65 | 2 | if (null === $this->admin) { |
|
66 | 2 | $this->checkAdmin(); |
|
67 | } |
||
68 | $this->setLoaded(true); |
||
69 | } |
||
70 | |||
71 | 2 | private function initSession() { |
|
80 | |||
81 | /** |
||
82 | * @return array |
||
83 | */ |
||
84 | public static function getProfiles() |
||
85 | { |
||
86 | return array( |
||
87 | self::ADMIN_ID_TOKEN => _('Administrador'), |
||
88 | self::MANAGER_ID_TOKEN => _('Gestor'), |
||
89 | self::USER_ID_TOKEN => _('Usuario'), |
||
90 | ); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * @return array |
||
95 | */ |
||
96 | public function getAdminProfiles() |
||
97 | { |
||
98 | return static::getProfiles(); |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * @return array |
||
|
|||
103 | */ |
||
104 | public static function getCleanProfiles() |
||
105 | { |
||
106 | return array( |
||
107 | '__SUPER_ADMIN__' => self::ADMIN_ID_TOKEN, |
||
108 | '__ADMIN__' => self::MANAGER_ID_TOKEN, |
||
109 | '__USER__' => self::USER_ID_TOKEN, |
||
110 | ); |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * @return array |
||
115 | */ |
||
116 | public function getAdminCleanProfiles() |
||
117 | { |
||
118 | return static::getCleanProfiles(); |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * @param mixed $user |
||
123 | * @return bool |
||
124 | * @throws exception\GeneratorException |
||
125 | * @throws ConfigException |
||
126 | */ |
||
127 | public static function save($user) |
||
128 | { |
||
129 | $saved = true; |
||
130 | $admins = Cache::getInstance()->getDataFromFile(CONFIG_DIR . DIRECTORY_SEPARATOR . 'admins.json', Cache::JSONGZ, true) ?: []; |
||
131 | $admins[$user['username']]['hash'] = sha1($user['username'] . $user['password']); |
||
132 | $admins[$user['username']]['profile'] = $user['profile']; |
||
133 | |||
134 | Cache::getInstance()->storeData(CONFIG_DIR . DIRECTORY_SEPARATOR . 'admins.json', $admins, Cache::JSONGZ, true); |
||
135 | return $saved; |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * @param mixed $user |
||
140 | * @return bool |
||
141 | * @throws exception\GeneratorException |
||
142 | */ |
||
143 | public function saveUser($user) |
||
144 | { |
||
145 | $saved = false; |
||
146 | if (!empty($user)) { |
||
147 | $saved = static::save($user); |
||
148 | } |
||
149 | return $saved; |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * @param mixed $user |
||
154 | */ |
||
155 | public function updateUser($user) |
||
156 | { |
||
157 | $this->user = $user; |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * @param $alias |
||
162 | * @param $profile |
||
163 | */ |
||
164 | public function updateAdmin($alias, $profile) { |
||
165 | $this->admin = array( |
||
166 | 'alias' => $alias, |
||
167 | 'profile' => $profile, |
||
168 | ); |
||
169 | $this->setSessionKey(self::ADMIN_ID_TOKEN, serialize($this->admin)); |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * @return array|null |
||
174 | */ |
||
175 | public function getAdmins() |
||
176 | { |
||
177 | return Cache::getInstance()->getDataFromFile(CONFIG_DIR . DIRECTORY_SEPARATOR . 'admins.json', Cache::JSONGZ, true); |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * @param string $user |
||
182 | * @param string $pass |
||
183 | * @param boolean $force |
||
184 | * |
||
185 | * @return bool |
||
186 | */ |
||
187 | 2 | public function checkAdmin($user = NULL, $pass = NULL, $force = false) |
|
188 | { |
||
189 | 2 | Logger::log('Checking admin session'); |
|
190 | if ((!$this->authorized && !$this->checked) || $force) { |
||
191 | $admins = $this->getAdmins(); |
||
192 | if (null !== $admins) { |
||
193 | $request = Request::getInstance(); |
||
194 | //Sacamos las credenciales de la petición |
||
195 | $user = $user ?: $request->getServer('PHP_AUTH_USER'); |
||
196 | $pass = $pass ?: $request->getServer('PHP_AUTH_PW'); |
||
197 | if (NULL === $user || (array_key_exists($user, $admins) && empty($admins[$user]))) { |
||
198 | list($user, $pass) = $this->getAdminFromCookie(); |
||
199 | } |
||
200 | if (!empty($user) && !empty($admins[$user])) { |
||
201 | $auth = $admins[$user]['hash']; |
||
202 | $this->authorized = ($auth === sha1($user . $pass)); |
||
203 | if ($this->authorized) { |
||
204 | $this->updateAdmin($user , $admins[$user]['profile']); |
||
205 | ResponseHelper::setCookieHeaders([ |
||
206 | [ |
||
207 | 'name' => $this->getHash(), |
||
208 | 'value' => base64_encode("$user:$pass"), |
||
209 | 'http' => true, |
||
210 | 'domain' => '', |
||
211 | ] |
||
212 | ]); |
||
213 | $this->setSessionKey(self::LOGGED_USER_TOKEN, base64_encode("{$user}:{$pass}")); |
||
214 | } |
||
215 | } else { |
||
216 | $this->admin = null; |
||
217 | $this->setSessionKey(self::ADMIN_ID_TOKEN, null); |
||
218 | } |
||
219 | $this->checked = true; |
||
220 | } |
||
221 | } |
||
222 | |||
223 | return $this->authorized; |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * Método que obtiene el usuario y contraseña de la cookie de sesión de administración |
||
228 | * @return array |
||
229 | */ |
||
230 | protected function getAdminFromCookie() |
||
231 | { |
||
232 | $auth_cookie = Request::getInstance()->getCookie($this->getHash()); |
||
233 | $user = $pass = array(); |
||
234 | if (!empty($auth_cookie)) { |
||
235 | list($user, $pass) = explode(':', base64_decode($auth_cookie)); |
||
236 | } |
||
237 | |||
238 | return array($user, $pass); |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * Método privado para la generación del hash de la cookie de administración |
||
243 | * @return string |
||
244 | */ |
||
245 | public function getHash() |
||
246 | { |
||
247 | return substr(self::MANAGER_ID_TOKEN, 0, 8); |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * Método que devuelve el usuario logado |
||
252 | * @return array |
||
253 | */ |
||
254 | public function getUser() |
||
255 | { |
||
256 | return $this->user; |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * Método que devuelve el usuario administrador logado |
||
261 | * @return array |
||
262 | */ |
||
263 | public function getAdmin() |
||
264 | { |
||
265 | return $this->admin; |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * Método que calcula si se está logado o para acceder a administración |
||
270 | * @return bool |
||
271 | */ |
||
272 | public function canAccessRestrictedAdmin() |
||
273 | { |
||
274 | return null !== $this->admin && !preg_match('/^\/admin\/login/i', Request::requestUri()); |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * Servicio que devuelve una pantalla de error porque se necesita estar authenticado |
||
279 | * |
||
280 | * @param string|null $route |
||
281 | * |
||
282 | * @return string|null |
||
283 | */ |
||
284 | public function notAuthorized($route) |
||
290 | |||
291 | /** |
||
292 | * @return bool |
||
293 | */ |
||
294 | public function isSuperAdmin() |
||
295 | { |
||
296 | $users = $this->getAdmins(); |
||
297 | $logged = $this->getAdmin(); |
||
298 | if (is_array($logged) |
||
299 | && array_key_exists('alias', $logged) |
||
300 | && array_key_exists($logged['alias'], $users)) { |
||
301 | $security = $users[$logged['alias']]['profile']; |
||
302 | return self::ADMIN_ID_TOKEN === $security; |
||
303 | } |
||
304 | |||
305 | return FALSE; |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * |
||
310 | * @param string $key |
||
311 | * |
||
312 | * @return mixed |
||
313 | */ |
||
314 | 4 | public function getSessionKey($key) |
|
323 | |||
324 | /** |
||
325 | * |
||
326 | * @param string $key |
||
327 | * @param mixed $data |
||
328 | * |
||
329 | * @return Security |
||
330 | */ |
||
331 | 4 | public function setSessionKey($key, $data = NULL) |
|
337 | |||
338 | /** |
||
339 | * @return mixed |
||
340 | */ |
||
341 | public function getFlashes() |
||
342 | { |
||
343 | $flashes = $this->getSessionKey(self::FLASH_MESSAGE_TOKEN); |
||
344 | |||
345 | return (NULL !== $flashes) ? $flashes : array(); |
||
346 | } |
||
347 | |||
348 | /** |
||
349 | * @return $this |
||
350 | */ |
||
351 | 2 | public function clearFlashes() |
|
357 | |||
358 | /** |
||
359 | * |
||
360 | * @param string $key |
||
361 | * @param mixed $data |
||
362 | */ |
||
363 | public function setFlash($key, $data = NULL) |
||
364 | { |
||
365 | $flashes = $this->getFlashes(); |
||
366 | if (!is_array($flashes)) { |
||
367 | $flashes = []; |
||
368 | } |
||
369 | $flashes[$key] = $data; |
||
370 | $this->setSessionKey(self::FLASH_MESSAGE_TOKEN, $flashes); |
||
371 | } |
||
372 | |||
373 | /** |
||
374 | * |
||
375 | * @param string $key |
||
376 | * |
||
377 | * @return mixed |
||
378 | */ |
||
379 | public function getFlash($key) |
||
385 | |||
386 | /** |
||
387 | * |
||
388 | * @param boolean $closeSession |
||
389 | * |
||
390 | * @return Security |
||
391 | */ |
||
392 | public function updateSession($closeSession = FALSE) |
||
406 | |||
407 | public function closeSession() |
||
414 | |||
415 | } |
||
416 |
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.