forked from CakeDC/users
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCookieAuthenticate.php
More file actions
87 lines (77 loc) · 2.66 KB
/
Copy pathCookieAuthenticate.php
File metadata and controls
87 lines (77 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
App::uses('BaseAuthenticate', 'Controller/Component/Auth');
App::uses('AuthComponent', 'Controller/Component');
App::uses('Router', 'Routing');
/**
* An authentication adapter for AuthComponent. Provides the ability to authenticate using COOKIE
*
* {{{
* $this->Auth->authenticate = array(
* 'Authenticate.Cookie' => array(
* 'fields' => array(
* 'username' => 'username',
* 'password' => 'password'
* ),
* 'userModel' => 'User',
* 'scope' => array('User.active' => 1),
* 'crypt' => 'rijndael', // Defaults to rijndael(safest), optionally set to 'cipher' if required
* 'cookie' => array(
* 'name' => 'RememberMe',
* 'time' => '+2 weeks',
* )
* )
* )
* }}}
*
* @author Ceeram
* @copyright Ceeram
* @license MIT
* @link https://github.com/ceeram/Authenticate
*/
class CookieAuthenticate extends BaseAuthenticate {
public function __construct(ComponentCollection $collection, $settings) {
$this->settings['cookie'] = array(
'name' => 'RememberMe',
'time' => '+2 weeks',
'base' => Router::getRequest()->base
);
$this->settings['crypt'] = 'rijndael';
parent::__construct($collection, $settings);
}
/**
* Authenticates the identity contained in the cookie. Will use the `settings.userModel`, and `settings.fields`
* to find COOKIE data that is used to find a matching record in the `settings.userModel`. Will return false if
* there is no cookie data, either username or password is missing, of if the scope conditions have not been met.
*
* @param CakeRequest $request The unused request object
* @return mixed False on login failure. An array of User data on success.
* @throws CakeException
*/
public function getUser(CakeRequest $request) {
if (!isset($this->_Collection->Cookie) || !$this->_Collection->Cookie instanceof CookieComponent) {
throw new CakeException('CookieComponent is not loaded');
}
$this->_Collection->Cookie->type($this->settings['crypt']);
list(, $model) = pluginSplit($this->settings['userModel']);
$data = $this->_Collection->Cookie->read($model);
if (empty($data)) {
return false;
}
extract($this->settings['fields']);
if (empty($data[$username]) || empty($data[$password])) {
return false;
}
$user = $this->_findUser($data[$username], $data[$password]);
if ($user) {
$this->_Collection->Session->write(AuthComponent::$sessionKey, $user);
return $user;
}
return false;
}
public function authenticate(CakeRequest $request, CakeResponse $response) {
return $this->getUser($request);
}
public function logout($user) {
$this->_Collection->Cookie->destroy();
}
}