1 | <?php |
||
11 | class CookieAuthenticate extends BaseAuthenticate |
||
12 | { |
||
13 | |||
14 | /** |
||
15 | * Constructor. |
||
16 | * |
||
17 | * @param \Cake\Controller\ComponentRegistry $registry The Component registry used on this request. |
||
18 | * @param array $config Array of config to use. |
||
19 | */ |
||
20 | public function __construct(ComponentRegistry $registry, array $config = []) |
||
21 | { |
||
22 | $this->_registry = $registry; |
||
23 | $this->config($config); |
||
24 | } |
||
25 | |||
26 | /** |
||
27 | * Authenticate a user based on the cookies information. |
||
28 | * |
||
29 | * @param \Cake\Network\Request $request The request instance. |
||
30 | * @param \Cake\Network\Response $response The response instance. |
||
31 | * |
||
32 | * @return mixed |
||
33 | * |
||
34 | * @throws \RuntimeException When the CookieComponent is not loaded. |
||
35 | */ |
||
36 | public function authenticate(Request $request, Response $response) |
||
37 | { |
||
38 | if (!isset($this->_registry->Cookie) || !$this->_registry->Cookie instanceof CookieComponent) { |
||
39 | throw new \RuntimeException('You need to load the CookieComponent.'); |
||
40 | } |
||
41 | |||
42 | $cookies = $this->_registry->Cookie->read('CookieAuth'); |
||
43 | if (empty($cookies)) { |
||
44 | return false; |
||
45 | } |
||
46 | |||
47 | extract($this->_config['fields']); |
||
48 | if (empty($cookies[$username])) { |
||
49 | return false; |
||
50 | } |
||
51 | |||
52 | $user = $this->_findUser($cookies[$username], $cookies[$password]); |
||
53 | if ($user) { |
||
54 | return $user; |
||
55 | } |
||
56 | |||
57 | return false; |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * Returns a list of all events that this authenticate class will listen to. |
||
62 | * |
||
63 | * @return array |
||
64 | */ |
||
65 | public function implementedEvents() |
||
66 | { |
||
67 | return [ |
||
68 | 'Auth.logout' => 'logout' |
||
69 | ]; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Delete cookies when an user logout. |
||
74 | * |
||
75 | * @param \Cake\Event\Event $event The logout Event. |
||
76 | * @param array $user The user about to be logged out. |
||
77 | * |
||
78 | * @return void |
||
79 | */ |
||
80 | public function logout(Event $event, array $user) |
||
84 | } |
||
85 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.