Skip to content

Commit b2d8536

Browse files
committed
Fixing Authcomponent tests. Updating the component + tests to use the new request object.
Deprecating a number of Authcomponent properties.
1 parent 36a7158 commit b2d8536

File tree

2 files changed

+187
-207
lines changed

2 files changed

+187
-207
lines changed

cake/libs/controller/components/auth.php

Lines changed: 40 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,20 @@ class AuthComponent extends Object {
3737
* Maintains current user login state.
3838
*
3939
* @var boolean
40-
* @access private
4140
*/
4241
protected $_loggedIn = false;
4342

4443
/**
4544
* Other components utilized by AuthComponent
4645
*
4746
* @var array
48-
* @access public
4947
*/
5048
public $components = array('Session', 'RequestHandler');
5149

5250
/**
5351
* A reference to the object used for authentication
5452
*
5553
* @var object
56-
* @access public
5754
* @link http://book.cakephp.org/view/1278/authenticate
5855
*/
5956
public $authenticate = null;
@@ -67,7 +64,6 @@ class AuthComponent extends Object {
6764
* 'object' will validate Controller::action against object::isAuthorized(user, controller, action)
6865
*
6966
* @var mixed
70-
* @access public
7167
* @link http://book.cakephp.org/view/1275/authorize
7268
*/
7369
public $authorize = false;
@@ -77,7 +73,6 @@ class AuthComponent extends Object {
7773
* with an invalid or expired session
7874
*
7975
* @var string
80-
* @access public
8176
* @link http://book.cakephp.org/view/1277/ajaxLogin
8277
*/
8378
public $ajaxLogin = null;
@@ -86,15 +81,13 @@ class AuthComponent extends Object {
8681
* The name of the element used for SessionComponent::setFlash
8782
*
8883
* @var string
89-
* @access public
9084
*/
9185
public $flashElement = 'default';
9286

9387
/**
9488
* The name of the model that represents users which will be authenticated. Defaults to 'User'.
9589
*
9690
* @var string
97-
* @access public
9891
* @link http://book.cakephp.org/view/1266/userModel
9992
*/
10093
public $userModel = 'User';
@@ -104,7 +97,6 @@ class AuthComponent extends Object {
10497
* i.e. array('User.is_active' => 1).
10598
*
10699
* @var array
107-
* @access public
108100
* @link http://book.cakephp.org/view/1268/userScope
109101
*/
110102
public $userScope = array();
@@ -114,7 +106,6 @@ class AuthComponent extends Object {
114106
* $userModel, i.e. array('username' => 'login_name', 'password' => 'passwd').
115107
*
116108
* @var array
117-
* @access public
118109
* @link http://book.cakephp.org/view/1267/fields
119110
*/
120111
public $fields = array('username' => 'username', 'password' => 'password');
@@ -124,7 +115,6 @@ class AuthComponent extends Object {
124115
* unspecified, it will be "Auth.{$userModel name}".
125116
*
126117
* @var string
127-
* @access public
128118
* @link http://book.cakephp.org/view/1276/sessionKey
129119
*/
130120
public $sessionKey = null;
@@ -136,7 +126,6 @@ class AuthComponent extends Object {
136126
* "Controllers/".
137127
*
138128
* @var string
139-
* @access public
140129
* @link http://book.cakephp.org/view/1279/actionPath
141130
*/
142131
public $actionPath = null;
@@ -146,7 +135,6 @@ class AuthComponent extends Object {
146135
* logins.
147136
*
148137
* @var mixed
149-
* @access public
150138
* @link http://book.cakephp.org/view/1269/loginAction
151139
*/
152140
public $loginAction = null;
@@ -158,7 +146,6 @@ class AuthComponent extends Object {
158146
* set, the user will be redirected to the page specified in $loginRedirect.
159147
*
160148
* @var mixed
161-
* @access public
162149
* @link http://book.cakephp.org/view/1270/loginRedirect
163150
*/
164151
public $loginRedirect = null;
@@ -169,7 +156,6 @@ class AuthComponent extends Object {
169156
* Defaults to AuthComponent::$loginAction.
170157
*
171158
* @var mixed
172-
* @access public
173159
* @see AuthComponent::$loginAction
174160
* @see AuthComponent::logout()
175161
* @link http://book.cakephp.org/view/1271/logoutRedirect
@@ -180,7 +166,6 @@ class AuthComponent extends Object {
180166
* The name of model or model object, or any other object has an isAuthorized method.
181167
*
182168
* @var string
183-
* @access public
184169
*/
185170
public $object = null;
186171

@@ -189,7 +174,6 @@ class AuthComponent extends Object {
189174
* login failures, so as not to expose information on why the login failed.
190175
*
191176
* @var string
192-
* @access public
193177
* @link http://book.cakephp.org/view/1272/loginError
194178
*/
195179
public $loginError = null;
@@ -199,7 +183,6 @@ class AuthComponent extends Object {
199183
* acccess.
200184
*
201185
* @var string
202-
* @access public
203186
* @link http://book.cakephp.org/view/1273/authError
204187
*/
205188
public $authError = null;
@@ -208,7 +191,6 @@ class AuthComponent extends Object {
208191
* Determines whether AuthComponent will automatically redirect and exit if login is successful.
209192
*
210193
* @var boolean
211-
* @access public
212194
* @link http://book.cakephp.org/view/1274/autoRedirect
213195
*/
214196
public $autoRedirect = true;
@@ -217,7 +199,6 @@ class AuthComponent extends Object {
217199
* Controller actions for which user validation is not required.
218200
*
219201
* @var array
220-
* @access public
221202
* @see AuthComponent::allow()
222203
* @link http://book.cakephp.org/view/1251/Setting-Auth-Component-Variables
223204
*/
@@ -227,7 +208,6 @@ class AuthComponent extends Object {
227208
* Maps actions to CRUD operations. Used for controller-based validation ($validate = 'controller').
228209
*
229210
* @var array
230-
* @access public
231211
* @see AuthComponent::mapActions()
232212
*/
233213
public $actionMap = array(
@@ -238,27 +218,33 @@ class AuthComponent extends Object {
238218
'remove' => 'delete'
239219
);
240220

221+
/**
222+
* Request object
223+
*
224+
* @var CakeRequest
225+
*/
226+
public $request;
227+
241228
/**
242229
* Form data from Controller::$data
243230
*
231+
* @deprecated Use $this->request->data instead
244232
* @var array
245-
* @access public
246233
*/
247234
public $data = array();
248235

249236
/**
250237
* Parameter data from Controller::$params
251238
*
239+
* @deprecated Use $this->request instead
252240
* @var array
253-
* @access public
254241
*/
255242
public $params = array();
256243

257244
/**
258245
* Method list for bound controller
259246
*
260247
* @var array
261-
* @access protected
262248
*/
263249
protected $_methods = array();
264250

@@ -268,8 +254,10 @@ class AuthComponent extends Object {
268254
* @param object $controller A reference to the instantiating controller object
269255
* @return void
270256
*/
271-
public function initialize(&$controller, $settings = array()) {
272-
$this->params = $controller->params;
257+
public function initialize(Controller $controller, $settings = array()) {
258+
$this->request = $controller->request;
259+
$this->params = $this->request;
260+
273261
$crud = array('create', 'read', 'update', 'delete');
274262
$this->actionMap = array_merge($this->actionMap, array_combine($crud, $crud));
275263
$this->_methods = $controller->methods;
@@ -314,7 +302,7 @@ public function startup(&$controller) {
314302
}
315303

316304
$methods = array_flip($controller->methods);
317-
$action = strtolower($controller->params['action']);
305+
$action = strtolower($controller->request->params['action']);
318306
$isMissingAction = (
319307
$controller->scaffold === false &&
320308
!isset($methods[$action])
@@ -327,12 +315,13 @@ public function startup(&$controller) {
327315
if (!$this->__setDefaults()) {
328316
return false;
329317
}
330-
331-
$this->data = $controller->data = $this->hashPasswords($controller->data);
318+
$request =& $controller->request;
319+
320+
$this->request->data = $controller->request->data = $this->hashPasswords($request->data);
332321
$url = '';
333322

334-
if (isset($controller->params['url']['url'])) {
335-
$url = $controller->params['url']['url'];
323+
if (isset($request->query['url'])) {
324+
$url = $request->query['url'];
336325
}
337326
$url = Router::normalize($url);
338327
$loginAction = Router::normalize($this->loginAction);
@@ -348,20 +337,20 @@ public function startup(&$controller) {
348337
}
349338

350339
if ($loginAction == $url) {
351-
$model =& $this->getModel();
352-
if (empty($controller->data) || !isset($controller->data[$model->alias])) {
340+
$model = $this->getModel();
341+
if (empty($request->data) || !isset($request->data[$model->alias])) {
353342
if (!$this->Session->check('Auth.redirect') && !$this->loginRedirect && env('HTTP_REFERER')) {
354343
$this->Session->write('Auth.redirect', $controller->referer(null, true));
355344
}
356345
return false;
357346
}
358347

359-
$isValid = !empty($controller->data[$model->alias][$this->fields['username']]) &&
360-
!empty($controller->data[$model->alias][$this->fields['password']]);
348+
$isValid = !empty($request->data[$model->alias][$this->fields['username']]) &&
349+
!empty($request->data[$model->alias][$this->fields['password']]);
361350

362351
if ($isValid) {
363-
$username = $controller->data[$model->alias][$this->fields['username']];
364-
$password = $controller->data[$model->alias][$this->fields['password']];
352+
$username = $request->data[$model->alias][$this->fields['username']];
353+
$password = $request->data[$model->alias][$this->fields['password']];
365354

366355
$data = array(
367356
$model->alias . '.' . $this->fields['username'] => $username,
@@ -377,14 +366,14 @@ public function startup(&$controller) {
377366
}
378367

379368
$this->Session->setFlash($this->loginError, $this->flashElement, array(), 'auth');
380-
$controller->data[$model->alias][$this->fields['password']] = null;
369+
$request->data[$model->alias][$this->fields['password']] = null;
381370
return false;
382371
} else {
383372
if (!$this->user()) {
384373
if (!$this->RequestHandler->isAjax()) {
385374
$this->Session->setFlash($this->authError, $this->flashElement, array(), 'auth');
386-
if (!empty($controller->params['url']) && count($controller->params['url']) >= 2) {
387-
$query = $controller->params['url'];
375+
if (!empty($request->query) && count($request->query) >= 2) {
376+
$query = $request->query;
388377
unset($query['url'], $query['ext']);
389378
$url .= Router::queryString($query, array());
390379
}
@@ -409,12 +398,12 @@ public function startup(&$controller) {
409398
extract($this->__authType());
410399
switch ($type) {
411400
case 'controller':
412-
$this->object =& $controller;
401+
$this->object = $controller;
413402
break;
414403
case 'crud':
415404
case 'actions':
416405
if (isset($controller->Acl)) {
417-
$this->Acl =& $controller->Acl;
406+
$this->Acl = $controller->Acl;
418407
} else {
419408
trigger_error(__('Could not find AclComponent. Please include Acl in Controller::$components.'), E_USER_WARNING);
420409
}
@@ -525,22 +514,22 @@ public function isAuthorized($type = null, $object = null, $user = null) {
525514
break;
526515
case 'crud':
527516
$this->mapActions();
528-
if (!isset($this->actionMap[$this->params['action']])) {
517+
if (!isset($this->actionMap[$this->request['action']])) {
529518
trigger_error(
530-
sprintf(__('Auth::startup() - Attempted access of un-mapped action "%1$s" in controller "%2$s"'), $this->params['action'], $this->params['controller']),
519+
sprintf(__('Auth::startup() - Attempted access of un-mapped action "%1$s" in controller "%2$s"'), $this->request['action'], $this->request['controller']),
531520
E_USER_WARNING
532521
);
533522
} else {
534523
$valid = $this->Acl->check(
535524
$user,
536525
$this->action(':controller'),
537-
$this->actionMap[$this->params['action']]
526+
$this->actionMap[$this->request['action']]
538527
);
539528
}
540529
break;
541530
case 'model':
542531
$this->mapActions();
543-
$action = $this->params['action'];
532+
$action = $this->request['action'];
544533
if (isset($this->actionMap[$action])) {
545534
$action = $this->actionMap[$action];
546535
}
@@ -717,7 +706,7 @@ public function user($key = null) {
717706
}
718707

719708
if ($key == null) {
720-
$model =& $this->getModel();
709+
$model = $this->getModel();
721710
return array($model->alias => $this->Session->read($this->sessionKey));
722711
} else {
723712
$user = $this->Session->read($this->sessionKey);
@@ -783,10 +772,10 @@ public function validate($object, $user = null, $action = null) {
783772
* @link http://book.cakephp.org/view/1256/action
784773
*/
785774
public function action($action = ':plugin/:controller/:action') {
786-
$plugin = empty($this->params['plugin']) ? null : Inflector::camelize($this->params['plugin']) . '/';
775+
$plugin = empty($this->request['plugin']) ? null : Inflector::camelize($this->request['plugin']) . '/';
787776
return str_replace(
788777
array(':controller', ':action', ':plugin/'),
789-
array(Inflector::camelize($this->params['controller']), $this->params['action'], $plugin),
778+
array(Inflector::camelize($this->request['controller']), $this->request['action'], $plugin),
790779
$this->actionPath . $action
791780
);
792781
}
@@ -804,11 +793,7 @@ public function &getModel($name = null) {
804793
$name = $this->userModel;
805794
}
806795

807-
if (PHP5) {
808-
$model = ClassRegistry::init($name);
809-
} else {
810-
$model =& ClassRegistry::init($name);
811-
}
796+
$model = ClassRegistry::init($name);
812797

813798
if (empty($model)) {
814799
trigger_error(__('Auth::getModel() - Model is not set or could not be found'), E_USER_WARNING);
@@ -834,7 +819,7 @@ public function identify($user = null, $conditions = null) {
834819
} else {
835820
$conditions = $this->userScope;
836821
}
837-
$model =& $this->getModel();
822+
$model = $this->getModel();
838823
if (empty($user)) {
839824
$user = $this->user();
840825
if (empty($user)) {
@@ -908,7 +893,7 @@ public function hashPasswords($data) {
908893
}
909894

910895
if (is_array($data)) {
911-
$model =& $this->getModel();
896+
$model = $this->getModel();
912897

913898
if(isset($data[$model->alias])) {
914899
if (isset($data[$model->alias][$this->fields['username']]) && isset($data[$model->alias][$this->fields['password']])) {

0 commit comments

Comments
 (0)