Skip to content

Commit aa6088a

Browse files
committed
Updated AuthComponent and friends to use new ORM and other 3.x changes.
1 parent 6421ce3 commit aa6088a

15 files changed

+256
-247
lines changed

Cake/Controller/Component/Auth/BaseAuthenticate.php

Lines changed: 34 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616
namespace Cake\Controller\Component\Auth;
1717

1818
use Cake\Controller\ComponentRegistry;
19+
use Cake\Controller\Component\Auth\AbstractPasswordHasher;
20+
use Cake\Core\App;
1921
use Cake\Error;
2022
use Cake\Network\Request;
2123
use Cake\Network\Response;
22-
use Cake\Utility\ClassRegistry;
24+
use Cake\ORM\TableRegistry;
2325
use Cake\Utility\Hash;
2426
use Cake\Utility\Security;
2527

@@ -33,28 +35,26 @@ abstract class BaseAuthenticate {
3335
* Settings for this object.
3436
*
3537
* - `fields` The fields to use to identify a user by.
36-
* - `userModel` The model name of the User, defaults to User.
38+
* - `userModel` The alias for users table, defaults to Users.
3739
* - `scope` Additional conditions to use when looking up and authenticating users,
38-
* i.e. `array('User.is_active' => 1).`
39-
* - `recursive` The value of the recursive key passed to find(). Defaults to 0.
40+
* i.e. `['Users.is_active' => 1].`
4041
* - `contain` Extra models to contain and store in session.
4142
* - `passwordHasher` Password hasher class. Can be a string specifying class name
4243
* or an array containing `className` key, any other keys will be passed as
4344
* settings to the class. Defaults to 'Simple'.
4445
*
4546
* @var array
4647
*/
47-
public $settings = array(
48-
'fields' => array(
48+
public $settings = [
49+
'fields' => [
4950
'username' => 'username',
5051
'password' => 'password'
51-
),
52-
'userModel' => 'User',
53-
'scope' => array(),
54-
'recursive' => 0,
52+
],
53+
'userModel' => 'Users',
54+
'scope' => [],
5555
'contain' => null,
5656
'passwordHasher' => 'Simple'
57-
);
57+
];
5858

5959
/**
6060
* A Component registry, used to get more components.
@@ -82,56 +82,48 @@ public function __construct(ComponentRegistry $registry, $settings) {
8282
}
8383

8484
/**
85-
* Find a user record using the standard options.
86-
*
87-
* The $username parameter can be a (string)username or an array containing
88-
* conditions for Model::find('first'). If the $password param is not provided
89-
* the password field will be present in returned array.
85+
* Find a user record using the username and password provided.
9086
*
9187
* Input passwords will be hashed even when a user doesn't exist. This
9288
* helps mitigate timing attacks that are attempting to find valid usernames.
9389
*
94-
* @param string|array $username The username/identifier, or an array of find conditions.
95-
* @param string $password The password, only used if $username param is string.
90+
* @param string $username The username/identifier.
91+
* @param string $password The password, if not provide password checking is skipped
92+
* and result of find is returned.
9693
* @return boolean|array Either false on failure, or an array of user data.
9794
*/
9895
protected function _findUser($username, $password = null) {
9996
$userModel = $this->settings['userModel'];
10097
list(, $model) = pluginSplit($userModel);
10198
$fields = $this->settings['fields'];
10299

103-
if (is_array($username)) {
104-
$conditions = $username;
105-
} else {
106-
$conditions = array(
107-
$model . '.' . $fields['username'] => $username
108-
);
109-
}
100+
$conditions = [$model . '.' . $fields['username'] => $username];
110101

111102
if (!empty($this->settings['scope'])) {
112103
$conditions = array_merge($conditions, $this->settings['scope']);
113104
}
114105

115-
$result = ClassRegistry::init($userModel)->find('first', array(
116-
'conditions' => $conditions,
117-
'recursive' => $this->settings['recursive'],
118-
'contain' => $this->settings['contain'],
119-
));
120-
if (empty($result[$model])) {
121-
$this->passwordHasher()->hash($password);
106+
$table = TableRegistry::get($userModel)->find('all');
107+
if ($this->settings['contain']) {
108+
$table = $table->contain($this->settings['contain']);
109+
}
110+
$result = $table
111+
->where($conditions)
112+
->hydrate(false)
113+
->first();
114+
115+
if (empty($result)) {
122116
return false;
123117
}
124118

125-
$user = $result[$model];
126-
if ($password) {
127-
if (!$this->passwordHasher()->check($password, $user[$fields['password']])) {
119+
if ($password !== null) {
120+
if (!$this->passwordHasher()->check($password, $result[$fields['password']])) {
128121
return false;
129122
}
130-
unset($user[$fields['password']]);
123+
unset($result[$fields['password']]);
131124
}
132125

133-
unset($result[$model]);
134-
return array_merge($user, $result);
126+
return $result;
135127
}
136128

137129
/**
@@ -154,15 +146,17 @@ public function passwordHasher() {
154146
$config = $this->settings['passwordHasher'];
155147
unset($config['className']);
156148
}
149+
157150
list($plugin, $class) = pluginSplit($class, true);
158151
$className = App::classname($class, 'Controller/Component/Auth', 'PasswordHasher');
159152
if (!class_exists($className)) {
160153
throw new Error\Exception(__d('cake_dev', 'Password hasher class "%s" was not found.', $class));
161154
}
162-
if (!is_subclass_of($className, 'AbstractPasswordHasher')) {
155+
156+
$this->_passwordHasher = new $className($config);
157+
if (!($this->_passwordHasher instanceof AbstractPasswordHasher)) {
163158
throw new Error\Exception(__d('cake_dev', 'Password hasher must extend AbstractPasswordHasher class.'));
164159
}
165-
$this->_passwordHasher = new $className($config);
166160
return $this->_passwordHasher;
167161
}
168162

Cake/Controller/Component/Auth/BaseAuthorize.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ abstract class BaseAuthorize {
6565
'delete' => 'delete',
6666
'remove' => 'delete'
6767
),
68-
'userModel' => 'User'
68+
'userModel' => 'Users'
6969
);
7070

7171
/**

Cake/Controller/Component/Auth/BasicAuthenticate.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function getUser(Request $request) {
8484
*/
8585
public function unauthenticated(Request $request, Response $response) {
8686
$Exception = new Error\UnauthorizedException();
87-
$Exception->responseHeader(array($this->loginHeaders()));
87+
$Exception->responseHeader(array($this->loginHeaders($request)));
8888
throw $Exception;
8989
}
9090

Cake/Controller/Component/Auth/DigestAuthenticate.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use Cake\Controller\Component\Auth\BasicAuthenticate;
2020
use Cake\Network\Request;
2121
use Cake\Network\Response;
22-
use Cake\Utility\ClassRegistry;
2322

2423
/**
2524
* Digest Authentication adapter for AuthComponent.
@@ -107,9 +106,7 @@ public function getUser(Request $request) {
107106
}
108107

109108
list(, $model) = pluginSplit($this->settings['userModel']);
110-
$user = $this->_findUser(array(
111-
$model . '.' . $this->settings['fields']['username'] => $digest['username']
112-
));
109+
$user = $this->_findUser($digest['username']);
113110
if (empty($user)) {
114111
return false;
115112
}
@@ -207,7 +204,7 @@ public function loginHeaders(Request $request) {
207204
'qop' => $this->settings['qop'],
208205
'nonce' => $this->settings['nonce'] ?: uniqid(''),
209206
);
210-
$options['nonce'] = $this->settings['nonce'] ?: $options['realm'];
207+
$options['opaque'] = $this->settings['opaque'] ?: md5($options['realm']);
211208
$opts = array();
212209
foreach ($options as $k => $v) {
213210
$opts[] = sprintf('%s="%s"', $k, $v);

Cake/Controller/Component/Auth/FormAuthenticate.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* {{{
2727
* $this->Auth->authenticate = array(
2828
* 'Form' => array(
29-
* 'scope' => array('User.active' => 1)
29+
* 'scope' => array('Users.active' => 1)
3030
* )
3131
* )
3232
* }}}

Cake/Controller/Component/AuthComponent.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
use Cake\Core\Configure;
2222
use Cake\Error;
2323
use Cake\Event\Event;
24-
use Cake\Model\Datasource\Session;
2524
use Cake\Network\Request;
2625
use Cake\Network\Response;
26+
use Cake\Network\Session;
2727
use Cake\Routing\Router;
2828
use Cake\Utility\Debugger;
2929
use Cake\Utility\Hash;
@@ -57,7 +57,7 @@ class AuthComponent extends Component {
5757
* {{{
5858
* $this->Auth->authenticate = array(
5959
* 'Form' => array(
60-
* 'userModel' => 'Users.User'
60+
* 'userModel' => 'Users.Users'
6161
* )
6262
* );
6363
* }}}
@@ -69,8 +69,8 @@ class AuthComponent extends Component {
6969
* {{{
7070
* $this->Auth->authenticate = array(
7171
* 'all' => array(
72-
* 'userModel' => 'Users.User',
73-
* 'scope' => array('User.active' => 1)
72+
* 'userModel' => 'Users.Users',
73+
* 'scope' => ['Users.active' => 1]
7474
* ),
7575
* 'Form',
7676
* 'Basic'
@@ -351,10 +351,11 @@ protected function _unauthenticated(Controller $controller) {
351351
}
352352

353353
if ($this->_isLoginAction($controller)) {
354-
if (empty($controller->request->data)) {
355-
if (!$this->Session->check('Auth.redirect') && env('HTTP_REFERER')) {
356-
$this->Session->write('Auth.redirect', $controller->referer(null, true));
357-
}
354+
if (empty($controller->request->data) &&
355+
!$this->Session->check('Auth.redirect') &&
356+
$this->request->env('HTTP_REFERER')
357+
) {
358+
$this->Session->write('Auth.redirect', $controller->referer(null, true));
358359
}
359360
return true;
360361
}

Cake/Test/TestApp/Controller/AjaxAuthController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
namespace TestApp\Controller;
1919

2020
use Cake\Controller\Controller;
21+
use Cake\Event\Event;
2122

2223
/**
2324
* AjaxAuthController class
@@ -58,7 +59,7 @@ class AjaxAuthController extends Controller {
5859
*
5960
* @return void
6061
*/
61-
public function beforeFilter() {
62+
public function beforeFilter(Event $event) {
6263
$this->TestAuth->ajaxLogin = 'test_element';
6364
$this->TestAuth->userModel = 'AuthUser';
6465
$this->TestAuth->RequestHandler->ajaxLayout = 'ajax2';

Cake/Test/TestApp/Controller/AuthTestController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class AuthTestController extends Controller {
3838
*
3939
* @var array
4040
*/
41-
public $uses = array('AuthUser');
41+
public $uses = array('Users');
4242

4343
/**
4444
* components property
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
4+
* Copyright 2005-2013, Cake Software Foundation, Inc. (http://cakefoundation.org)
5+
*
6+
* Licensed under The MIT License
7+
* Redistributions of files must retain the above copyright notice
8+
*
9+
* @since CakePHP(tm) v 3.0
10+
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
11+
*/
12+
namespace TestApp\Model\Repository;
13+
14+
use Cake\ORM\Table;
15+
16+
/**
17+
* AuthUser class
18+
*
19+
*/
20+
class AuthUsersTable extends Table {
21+
22+
}

Cake/Test/TestCase/Controller/Component/Auth/ActionsAuthorizeTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ protected function _mockAcl() {
6262
*/
6363
public function testAuthorizeFailure() {
6464
$user = array(
65-
'User' => array(
65+
'Users' => array(
6666
'id' => 1,
6767
'user' => 'mariano'
6868
)
@@ -81,7 +81,7 @@ public function testAuthorizeFailure() {
8181
->with($user, 'controllers/Posts/index')
8282
->will($this->returnValue(false));
8383

84-
$this->assertFalse($this->auth->authorize($user['User'], $request));
84+
$this->assertFalse($this->auth->authorize($user['Users'], $request));
8585
}
8686

8787
/**
@@ -91,7 +91,7 @@ public function testAuthorizeFailure() {
9191
*/
9292
public function testAuthorizeSuccess() {
9393
$user = array(
94-
'User' => array(
94+
'Users' => array(
9595
'id' => 1,
9696
'user' => 'mariano'
9797
)
@@ -110,7 +110,7 @@ public function testAuthorizeSuccess() {
110110
->with($user, 'controllers/Posts/index')
111111
->will($this->returnValue(true));
112112

113-
$this->assertTrue($this->auth->authorize($user['User'], $request));
113+
$this->assertTrue($this->auth->authorize($user['Users'], $request));
114114
}
115115

116116
/**
@@ -128,13 +128,13 @@ public function testAuthorizeSettings() {
128128

129129
$this->_mockAcl();
130130

131-
$this->auth->settings['userModel'] = 'TestPlugin.TestPluginAuthUser';
131+
$this->auth->settings['userModel'] = 'TestPlugin.AuthUser';
132132
$user = array(
133133
'id' => 1,
134-
'user' => 'mariano'
134+
'username' => 'mariano'
135135
);
136136

137-
$expected = array('TestPlugin.TestPluginAuthUser' => array('id' => 1, 'user' => 'mariano'));
137+
$expected = array('TestPlugin.AuthUser' => array('id' => 1, 'username' => 'mariano'));
138138
$this->Acl->expects($this->once())
139139
->method('check')
140140
->with($expected, 'controllers/Posts/index')

0 commit comments

Comments
 (0)