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 | 2 | $this->setLoaded(true); |
|
69 | 2 | } |
|
70 | |||
71 | 2 | private function initSession() { |
|
72 | 2 | if (PHP_SESSION_NONE === session_status() && !headers_sent()) { |
|
73 | session_start(); |
||
74 | } |
||
75 | // Fix for phpunits |
||
76 | 2 | if(!isset($_SESSION)) { |
|
77 | 1 | $_SESSION = []; |
|
78 | } |
||
79 | 2 | } |
|
80 | |||
81 | /** |
||
82 | * @return array |
||
83 | */ |
||
84 | 2 | public static function getProfiles() |
|
85 | { |
||
86 | return array( |
||
87 | 2 | self::ADMIN_ID_TOKEN => _('Administrador'), |
|
88 | 2 | self::MANAGER_ID_TOKEN => _('Gestor'), |
|
89 | 2 | self::USER_ID_TOKEN => _('Usuario'), |
|
90 | ); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * @return array |
||
95 | */ |
||
96 | 1 | public function getAdminProfiles() |
|
97 | { |
||
98 | 1 | return static::getProfiles(); |
|
99 | } |
||
100 | |||
101 | /** |
||
102 | * @return array |
||
103 | */ |
||
104 | 2 | public static function getCleanProfiles() |
|
105 | { |
||
106 | return array( |
||
107 | 2 | '__SUPER_ADMIN__' => self::ADMIN_ID_TOKEN, |
|
108 | 2 | '__ADMIN__' => self::MANAGER_ID_TOKEN, |
|
109 | 2 | '__USER__' => self::USER_ID_TOKEN, |
|
110 | ); |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * @return array |
||
115 | */ |
||
116 | 1 | public function getAdminCleanProfiles() |
|
117 | { |
||
118 | 1 | return static::getCleanProfiles(); |
|
119 | } |
||
120 | |||
121 | /** |
||
122 | * @param mixed $user |
||
123 | * @return bool |
||
124 | * @throws exception\GeneratorException |
||
125 | * @throws ConfigException |
||
126 | */ |
||
127 | 1 | public static function save($user) |
|
128 | { |
||
129 | 1 | $saved = true; |
|
130 | 1 | $admins = Cache::getInstance()->getDataFromFile(CONFIG_DIR . DIRECTORY_SEPARATOR . 'admins.json', Cache::JSONGZ, true) ?: []; |
|
131 | 1 | $admins[$user['username']]['hash'] = sha1($user['username'] . $user['password']); |
|
132 | 1 | $admins[$user['username']]['profile'] = $user['profile']; |
|
133 | |||
134 | 1 | Cache::getInstance()->storeData(CONFIG_DIR . DIRECTORY_SEPARATOR . 'admins.json', $admins, Cache::JSONGZ, true); |
|
135 | 1 | return $saved; |
|
136 | } |
||
137 | |||
138 | /** |
||
139 | * @param mixed $user |
||
140 | * @return bool |
||
141 | * @throws exception\GeneratorException |
||
142 | */ |
||
143 | 1 | public function saveUser($user) |
|
144 | { |
||
145 | 1 | $saved = false; |
|
146 | 1 | if (!empty($user)) { |
|
147 | 1 | $saved = static::save($user); |
|
148 | } |
||
149 | 1 | return $saved; |
|
150 | } |
||
151 | |||
152 | /** |
||
153 | * @param mixed $user |
||
154 | */ |
||
155 | 1 | public function updateUser($user) |
|
159 | |||
160 | /** |
||
161 | * @param $alias |
||
162 | * @param $profile |
||
163 | */ |
||
164 | 1 | public function updateAdmin($alias, $profile) { |
|
165 | 1 | $this->admin = array( |
|
166 | 1 | 'alias' => $alias, |
|
167 | 1 | 'profile' => $profile, |
|
168 | ); |
||
169 | 1 | $this->setSessionKey(self::ADMIN_ID_TOKEN, serialize($this->admin)); |
|
170 | 1 | } |
|
171 | |||
172 | /** |
||
173 | * @return array|null |
||
174 | */ |
||
175 | 4 | public function getAdmins() |
|
176 | { |
||
177 | 4 | 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 | 3 | public function checkAdmin($user = NULL, $pass = NULL, $force = false) |
|
188 | { |
||
189 | 3 | Logger::log('Checking admin session'); |
|
190 | 3 | if ((!$this->authorized && !$this->checked) || $force) { |
|
191 | 3 | $admins = $this->getAdmins(); |
|
192 | 3 | if (null !== $admins) { |
|
193 | 1 | $request = Request::getInstance(); |
|
194 | //Sacamos las credenciales de la petición |
||
195 | 1 | $user = $user ?: $request->getServer('PHP_AUTH_USER'); |
|
196 | 1 | $pass = $pass ?: $request->getServer('PHP_AUTH_PW'); |
|
197 | 1 | if (NULL === $user || (array_key_exists($user, $admins) && empty($admins[$user]))) { |
|
198 | 1 | list($user, $pass) = $this->getAdminFromCookie(); |
|
199 | } |
||
200 | 1 | if (!empty($user) && !empty($admins[$user])) { |
|
201 | 1 | $auth = $admins[$user]['hash']; |
|
202 | 1 | $this->authorized = ($auth === sha1($user . $pass)); |
|
203 | 1 | if ($this->authorized) { |
|
204 | 1 | $this->updateAdmin($user , $admins[$user]['profile']); |
|
205 | 1 | ResponseHelper::setCookieHeaders([ |
|
206 | [ |
||
207 | 1 | 'name' => $this->getHash(), |
|
208 | 1 | 'value' => base64_encode("$user:$pass"), |
|
209 | 'http' => true, |
||
210 | 1 | 'domain' => '', |
|
211 | ] |
||
212 | ]); |
||
213 | 1 | $this->setSessionKey(self::LOGGED_USER_TOKEN, base64_encode("{$user}:{$pass}")); |
|
214 | } |
||
215 | } else { |
||
216 | 1 | $this->admin = null; |
|
217 | 1 | $this->setSessionKey(self::ADMIN_ID_TOKEN, null); |
|
218 | } |
||
219 | 1 | $this->checked = true; |
|
220 | } |
||
221 | } |
||
222 | |||
223 | 3 | 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 | 1 | protected function getAdminFromCookie() |
|
231 | { |
||
232 | 1 | $auth_cookie = Request::getInstance()->getCookie($this->getHash()); |
|
233 | 1 | $user = $pass = array(); |
|
234 | 1 | if (!empty($auth_cookie)) { |
|
235 | 1 | list($user, $pass) = explode(':', base64_decode($auth_cookie)); |
|
236 | } |
||
237 | |||
238 | 1 | 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 | 1 | public function getHash() |
|
246 | { |
||
247 | 1 | return substr(self::MANAGER_ID_TOKEN, 0, 8); |
|
248 | } |
||
249 | |||
250 | /** |
||
251 | * Método que devuelve el usuario logado |
||
252 | * @return array |
||
253 | */ |
||
254 | 2 | public function getUser() |
|
255 | { |
||
256 | 2 | return $this->user; |
|
257 | } |
||
258 | |||
259 | /** |
||
260 | * Método que devuelve el usuario administrador logado |
||
261 | * @return array |
||
262 | */ |
||
263 | 2 | public function getAdmin() |
|
264 | { |
||
265 | 2 | 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 | 3 | public function canAccessRestrictedAdmin() |
|
273 | { |
||
274 | 3 | 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) |
||
285 | { |
||
286 | return Template::getInstance()->render('notauthorized.html.twig', array( |
||
287 | 'route' => $route, |
||
288 | )); |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * @return bool |
||
293 | */ |
||
294 | 1 | public function isSuperAdmin() |
|
295 | { |
||
296 | 1 | $users = $this->getAdmins(); |
|
297 | 1 | $logged = $this->getAdmin(); |
|
298 | 1 | if ($users[$logged['alias']]) { |
|
299 | 1 | $security = $users[$logged['alias']]['profile']; |
|
300 | 1 | return self::ADMIN_ID_TOKEN === $security; |
|
301 | } |
||
302 | |||
303 | return FALSE; |
||
304 | } |
||
305 | |||
306 | /** |
||
307 | * |
||
308 | * @param string $key |
||
309 | * |
||
310 | * @return mixed |
||
311 | */ |
||
312 | 7 | public function getSessionKey($key) |
|
313 | { |
||
314 | 7 | $data = NULL; |
|
315 | 7 | if (array_key_exists($key, $this->session)) { |
|
316 | 5 | $data = $this->session[$key]; |
|
317 | } |
||
318 | |||
319 | 7 | return $data; |
|
320 | } |
||
321 | |||
322 | /** |
||
323 | * |
||
324 | * @param string $key |
||
325 | * @param mixed $data |
||
326 | * |
||
327 | * @return Security |
||
328 | */ |
||
329 | 6 | public function setSessionKey($key, $data = NULL) |
|
330 | { |
||
331 | 6 | $this->session[$key] = $data; |
|
332 | |||
333 | 6 | return $this; |
|
334 | } |
||
335 | |||
336 | /** |
||
337 | * @return mixed |
||
338 | */ |
||
339 | 2 | public function getFlashes() |
|
340 | { |
||
341 | 2 | $flashes = $this->getSessionKey(self::FLASH_MESSAGE_TOKEN); |
|
342 | |||
343 | 2 | return (NULL !== $flashes) ? $flashes : array(); |
|
344 | } |
||
345 | |||
346 | /** |
||
347 | * @return $this |
||
348 | */ |
||
349 | 3 | public function clearFlashes() |
|
355 | |||
356 | /** |
||
357 | * |
||
358 | * @param string $key |
||
359 | * @param mixed $data |
||
360 | */ |
||
361 | 1 | public function setFlash($key, $data = NULL) |
|
362 | { |
||
363 | 1 | $flashes = $this->getFlashes(); |
|
364 | 1 | if (!is_array($flashes)) { |
|
365 | $flashes = []; |
||
366 | } |
||
367 | 1 | $flashes[$key] = $data; |
|
368 | 1 | $this->setSessionKey(self::FLASH_MESSAGE_TOKEN, $flashes); |
|
369 | 1 | } |
|
370 | |||
371 | /** |
||
372 | * |
||
373 | * @param string $key |
||
374 | * |
||
375 | * @return mixed |
||
376 | */ |
||
377 | 2 | public function getFlash($key) |
|
383 | |||
384 | /** |
||
385 | * |
||
386 | * @param boolean $closeSession |
||
387 | * |
||
388 | * @return Security |
||
389 | */ |
||
390 | 2 | public function updateSession($closeSession = FALSE) |
|
404 | |||
405 | 1 | public function closeSession() |
|
412 | |||
413 | } |
||
414 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: