forked from CakeDC/users
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusers_auth.php
More file actions
214 lines (194 loc) · 6.19 KB
/
Copy pathusers_auth.php
File metadata and controls
214 lines (194 loc) · 6.19 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
<?php
/**
* Copyright 2010, Cake Development Corporation (http://cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2010, Cake Development Corporation (http://cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
App::import('Component', 'Auth');
/**
* Users plugin Auth component
*
* Provides additional functionality over the core Auth component such as cookie
* logins.
*
* @package users
* @subpackage users.controllers.components
*/
class UsersAuthComponent extends AuthComponent {
/**
* Component dependencies
*
* @var array
*/
public $components = array(
'Auth',
'Cookie',
'RequestHandler',
'Session',
);
/**
* Options for cookie storage of authentication information.
*
* @var array
*/
public $cookieOptions = null;
/**
* Override for Auth::initialize to provide Users plugin defaults.
*
* For all settings other than "cookieOptions", please refer to the core Auth component
*
* ### Settings:
*
* - `cookieOptions` array Array of cookie settings:
* - `domain` string Domain name to restrict cookie. Use '.domain.com' to
* allow all subdomains to access the cookie, or just 'domain.com' for the
* main domain.
* - `name` string Cookie name to use
* - `key` string Cypher key for encrypting cookie data
* - `time` string Can be a Unix timestamp or a string time, eg: '1 Month'
* - `path` string Path to limit cookie use, defaults to the controller
* 'base' which is derived from the CakePHP app install location.
* - `secure` boolean True if values are to be secured / crypted.
*
* @param AppController $controller
* @param array $settings
* @return void
*/
public function initialize(Controller $controller, array $settings = array()) {
// For legacy support, add the 'Auth' variable on the controller, so
// apps can still reference this auth component as if it were the core
// auth component.
$controller->Auth = $this;
$loginRedirect = $this->Session->read('Auth.redirect');
if (empty($loginRedirect)) {
$loginRedirect = array(
'admin' => false,
'prefix' => 'admin',
'plugin' => 'users',
'controller' => 'users',
'action' => 'dashboard');
}
$defaults = array(
'loginAction' => array(
'admin' => false,
'prefix' => 'admin',
'plugin' => 'users',
'controller' => 'users',
'action' => 'login'),
'authorize' => 'controller',
'fields' => array(
'username' => 'email',
'password' => 'passwd'),
'loginRedirect' => $loginRedirect,
'logoutRedirect' => '/',
'authError' => __d('users', 'Sorry, but you need to login to access this location.', true),
'loginError' => __d('users', 'Invalid e-mail / password combination. Please try again', true),
'autoRedirect' => true,
'userModel' => 'Users.User',
'userScope' => array(
$controller->modelClass . '.active' => 1,
$controller->modelClass . '.email_authenticated' => 1),
'cookieOptions' => array(
'domain' => env('HTTP_HOST'),
'name' => 'Users',
'keyname' => 'rememberMe',
'time' => '1 Month',
'path' => '/' . $controller->base),
);
parent::initialize($controller, array_merge($defaults, $settings));
$this->controller = $controller;
}
/**
* Override for Auth::login to provide last login tracking and inject cookie checks and storage
*
* @param array $data Form data
* @return boolean True if login is successful
*/
public function login($data = null, $skipCookies = false) {
if (empty($data)) {
$data = $this->data;
}
$loggedIn =
parent::login($data) || (
!$skipCookies &&
$this->_getCookie()
);
if ($loggedIn) {
$User = $this->getModel();
$User->id = $this->user($User->primaryKey);
$User->saveField('last_login', date('Y-m-d H:i:s'));
$this->Session->setFlash(sprintf(__d('users', '%s, you have successfully logged in.', true), $this->user('username')));
if (!empty($this->data)) {
!$skipCookies && $this->_setCookie();
}
}
return $loggedIn;
}
/**
* Destroy session and cookie information, and return the logoutRedirect URL
*
* @return string logoutRedirect url
*/
public function logout() {
$this->_deleteCookie();
return parent::logout();
}
/**
* Setup the cookies with options provided the the UsersAuth setup.
*
* @return void
*/
protected function _setupCookies() {
// Allow only specified values set on the cookie
$validProperties = array('domain', 'key', 'name', 'path', 'secure', 'time');
$options = array_intersect_key($this->cookieOptions, array_flip($validProperties));
foreach ($options as $key => $value) {
$this->Cookie->{$key} = $value;
}
}
/**
* Sets the cookie to remember the user
*
* @param array Cookie component properties as array, like array('domain' => 'yourdomain.com')
* @param string Cookie data keyname for the userdata, its default is "User". This is set to User and NOT using the model alias to make sure it works with different apps with different user models across different (sub)domains.
* @return void
* @link http://api13.cakephp.org/class/cookie-component
*/
protected function _setCookie() {
$this->_setupCookies();
list($plugin, $model) = pluginSplit($this->userModel);
if (!isset($this->data[$model]['remember_me']) || !$this->data[$model]['remember_me']) {
$this->Cookie->delete($this->cookieOptions['keyname']);
return false;
}
$cookieData = array_intersect_key($this->data[$model], array_flip(array($this->fields['username'], $this->fields['password'])));
$this->Cookie->write($this->cookieOptions['keyname'], $cookieData);
}
/**
* Attempts to automatically login the user if a valid cookie exists
*
* @return boolean True if successfully logged in
*/
protected function _getCookie() {
$this->_setupCookies();
$cookieData = $this->Cookie->read($this->cookieOptions['keyname']);
if ($cookieData === null) {
return false;
}
list($plugin, $model) = pluginSplit($this->userModel);
return $this->login(array($model => $cookieData), true);
}
/**
* Delete the remember cookie.
*
* @return void
*/
protected function _deleteCookie() {
$this->_setupCookies();
$this->Cookie->delete($this->cookieOptions['keyname']);
}
}