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