forked from CakeDC/users
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRememberMeComponent.php
More file actions
239 lines (211 loc) · 5.78 KB
/
Copy pathRememberMeComponent.php
File metadata and controls
239 lines (211 loc) · 5.78 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php
/**
* Copyright 2010 - 2014, Cake Development Corporation (http://cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2010 - 2014, Cake Development Corporation (http://cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
App::uses('Component', 'Controller');
/**
* RememberMe Component
*
* Logs an user back in if the cookie with the credentials is found
*
* @property CookieComponent $Cookie
* @property AuthComponent $Auth
*/
class RememberMeComponent extends Component {
/**
* Components
*
* @var array
*/
public $components = array(
'Cookie',
'Auth'
);
/**
* Request object
*
* @var CakeRequest
*/
public $request = null;
/**
* Settings
*
* @var array
*/
public $settings = array();
/**
* Default settings
*
* @var array
*/
protected $_defaults = array(
'autoLogin' => true,
'userModel' => 'User',
'cookieKey' => 'rememberMe',
'cookieLifeTime' => '+1 year',
'cookie' => array(
'name' => 'User'
),
'fields' => array(
'email',
'username',
'password'
)
);
/**
* Constructor
*
* @param ComponentCollection $collection A ComponentCollection for this component
* @param array $settings Array of settings.
* @return RememberMeComponent
*/
public function __construct(ComponentCollection $collection, $settings = array()) {
parent::__construct($collection, $settings);
$this->_checkAndSetCookieLifeTime();
$this->settings = Hash::merge($this->_defaults, $settings);
$this->configureCookie($this->settings['cookie']);
}
/**
* Check if the system is 32bit and uses DateTime() instead strtotime() to get
* an integer instead of a string that is passed on to CookieComponent::write()
* due to problems with strtotime() in CookieComponent::_expire(). See
* the link in this doc block.
*
* This method needs to be called in the constructor before the default config
* values are merged!
*
* @link https://cakephp.lighthouseapp.com/projects/42648-cakephp/tickets/3868-cookiecomponent_expires-fails-on-dates-set-far-in-the-future-on-32bit-systems
* @link http://stackoverflow.com/questions/3266077/php-strtotime-is-returning-false-for-a-future-date
* @return void
*/
protected function _checkAndSetCookieLifeTime() {
$lifeTime = $this->_defaults['cookieLifeTime'];
if (is_string($lifeTime) && strtotime($lifeTime) === false) {
$Date = new DateTime($lifeTime);
$this->_defaults['cookieLifeTime'] = $Date->format('U');
}
}
/**
* Initializes RememberMeComponent for use in the controller
*
* @param Controller $controller A reference to the instantiating controller object
* @return void
*/
public function initialize(Controller $controller) {
$this->request = $controller->request;
$this->Auth = $controller->Auth;
}
/**
* startup
*
* @param Controller $controller
* @return void
*/
public function startup(Controller $controller) {
if ($this->settings['autoLogin'] == true && !$this->Auth->loggedIn()) {
$this->restoreLoginFromCookie();
}
}
/**
* Logs the user again in based on the cookie data
*
* @param boolean $checkLoginStatus
* @return boolean True on login success, false on failure
*/
public function restoreLoginFromCookie($checkLoginStatus = true) {
if ($checkLoginStatus && $this->Auth->loggedIn()) {
return true;
}
if ($this->cookieIsSet()) {
extract($this->settings);
$cookie = $this->Cookie->read($cookieKey);
$request = $this->request->data;
foreach ($fields as $field) {
if (!empty($cookie[$field])) {
$this->request->data[$userModel][$field] = $cookie[$field];
}
}
$result = $this->Auth->login();
if (!$result) {
$this->request->data = $request;
}
return $result;
}
return false;
}
/**
* Sets the cookie with the specified fields
*
* @param array Optional, login credentials array in the form of Model.field, if empty this->request['<model>'] will be used
* @return boolean
*/
public function setCookie($data = array()) {
extract($this->settings);
if (empty($data)) {
$data = $this->request->data;
if (empty($data)) {
$data = $this->Auth->user();
}
}
if (empty($data)) {
return false;
}
$cookieData = array();
foreach ($fields as $field) {
if (isset($data[$userModel][$field]) && !empty($data[$userModel][$field])) {
$cookieData[$field] = $data[$userModel][$field];
}
}
$this->Cookie->write($cookieKey, $cookieData, true, $cookieLifeTime);
return true;
}
/**
* Checks if the remember me cookie is set
*
* @return boolean
*/
public function cookieIsSet() {
extract($this->settings);
$cookie = $this->Cookie->read($cookieKey);
return (!empty($cookie));
}
/**
* Destroys the remember me cookie
*
* @return void
*/
public function destroyCookie() {
extract($this->settings);
if (isset($_COOKIE[$cookie['name']])) {
$this->Cookie->name = $cookie['name'];
$this->Cookie->destroy();
}
}
/**
* Configures the cookie component instance
*
* @param array $options
* @throws InvalidArgumentException Thrown if an invalid option key was passed
* @return void
*/
public function configureCookie($options = array()) {
$validProperties = array('domain', 'key', 'name', 'path', 'secure', 'time');
$defaults = array(
'time' => '1 month',
'name' => 'User');
$options = array_merge($defaults, $options);
foreach ($options as $key => $value) {
if (in_array($key, $validProperties)) {
$this->Cookie->{$key} = $value;
} else {
throw new InvalidArgumentException(__d('users', 'Invalid options %s', $key));
}
}
}
}