Skip to content

Commit 8e5b2c7

Browse files
committed
Remove use of request info to identify user in AuthComponent::login().
In your controller you now have to directly call identify() to verify user credentials passed in request against db record. login() now just takes a user array as param and writes the data to sesssion.
1 parent 7edcea3 commit 8e5b2c7

File tree

2 files changed

+42
-51
lines changed

2 files changed

+42
-51
lines changed

src/Controller/Component/AuthComponent.php

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -584,26 +584,19 @@ public function mapActions(array $map = array()) {
584584
/**
585585
* Log a user in.
586586
*
587-
* If a $user is provided that data will be stored as the logged in user. If `$user` is empty or not
588-
* specified, the request will be used to identify a user. If the identification was successful,
589-
* the user record is written to the session key specified in AuthComponent::$sessionKey. Logging in
590-
* will also change the session id in order to help mitigate session replays.
587+
* The provided user data will be stored as the logged in user. The user record
588+
* is written to the session key specified in AuthComponent::$sessionKey. Logging
589+
* in will also change the session id in order to help mitigate session replays.
591590
*
592-
* @param array $user Either an array of user data, or null to identify a user using the current request.
593-
* @return bool True on login success, false on failure
591+
* @param array $user Array of user data.
592+
* @return void
594593
* @link http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#identifying-users-and-logging-them-in
595594
*/
596-
public function login($user = null) {
595+
public function login(array $user) {
597596
$this->_setDefaults();
598597

599-
if (empty($user)) {
600-
$user = $this->identify($this->request, $this->response);
601-
}
602-
if ($user) {
603-
$this->session->renew();
604-
$this->session->write($this->sessionKey, $user);
605-
}
606-
return (bool)$this->user();
598+
$this->session->renew();
599+
$this->session->write($this->sessionKey, $user);
607600
}
608601

609602
/**
@@ -729,16 +722,16 @@ public function redirectUrl($url = null) {
729722
* Use the configured authentication adapters, and attempt to identify the user
730723
* by credentials contained in $request.
731724
*
732-
* @param \Cake\Network\Request $request The request that contains authentication data.
733-
* @param \Cake\Network\Response $response The response
734725
* @return array User record data, or false, if the user could not be identified.
735726
*/
736-
public function identify(Request $request, Response $response) {
727+
public function identify() {
728+
$this->_setDefaults();
729+
737730
if (empty($this->_authenticateObjects)) {
738731
$this->constructAuthenticate();
739732
}
740733
foreach ($this->_authenticateObjects as $auth) {
741-
$result = $auth->authenticate($request, $response);
734+
$result = $auth->authenticate($this->request, $this->response);
742735
if (!empty($result) && is_array($result)) {
743736
$this->_authenticationProvider = $auth;
744737
return $result;

tests/TestCase/Controller/Component/AuthComponentTest.php

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,11 @@ public function testIsErrorOrTests() {
129129
}
130130

131131
/**
132-
* testLogin method
132+
* testIdentify method
133133
*
134134
* @return void
135135
*/
136-
public function testLogin() {
136+
public function testIdentify() {
137137
$AuthLoginFormAuthenticate = $this->getMock(
138138
'Cake\Controller\Component\Auth\FormAuthenticate',
139139
array('authenticate'), array(), '', false
@@ -143,10 +143,6 @@ public function testLogin() {
143143
'userModel' => 'AuthUsers'
144144
)
145145
);
146-
$this->Auth->session = $this->getMock(
147-
'Cake\Network\Session',
148-
array('renew')
149-
);
150146

151147
$this->Auth->setAuthenticateObject(0, $AuthLoginFormAuthenticate);
152148

@@ -167,14 +163,8 @@ public function testLogin() {
167163
->with($this->Auth->request)
168164
->will($this->returnValue($user));
169165

170-
$this->Auth->session->expects($this->once())
171-
->method('renew');
172-
173-
$result = $this->Auth->login();
174-
$this->assertTrue($result);
175-
176-
$this->assertTrue((bool)$this->Auth->user());
177-
$this->assertEquals($user, $this->Auth->user());
166+
$result = $this->Auth->identify();
167+
$this->assertEquals($user, $result);
178168
$this->assertSame($AuthLoginFormAuthenticate, $this->Auth->authenticationProvider());
179169
}
180170

@@ -228,6 +218,8 @@ public function testAuthorizeFalse() {
228218
}
229219

230220
/**
221+
* testIsAuthorizedMissingFile function
222+
*
231223
* @expectedException \Cake\Error\Exception
232224
* @return void
233225
*/
@@ -317,6 +309,8 @@ public function testLoadAuthorizeResets() {
317309
}
318310

319311
/**
312+
* testLoadAuthenticateNoFile function
313+
*
320314
* @expectedException \Cake\Error\Exception
321315
* @return void
322316
*/
@@ -516,6 +510,11 @@ public function testAllowedActionsWithCamelCaseMethods() {
516510
$this->assertNull($result, 'startup() should return null, as action is allowed. %s');
517511
}
518512

513+
/**
514+
* testAllowedActionsSetWithAllowMethod method
515+
*
516+
* @return void
517+
*/
519518
public function testAllowedActionsSetWithAllowMethod() {
520519
$url = '/auth_test/action_name';
521520
$this->Controller->request->addParams(Router::parse($url));
@@ -822,6 +821,7 @@ public function testRedirectToUnauthorizedRedirectSuppressedAuthError() {
822821

823822
/**
824823
* Throw ForbiddenException if config `unauthorizedRedirect` is set to false
824+
*
825825
* @expectedException \Cake\Error\ForbiddenException
826826
* @return void
827827
*/
@@ -1093,36 +1093,34 @@ public function testMapActionsDelegation() {
10931093
}
10941094

10951095
/**
1096-
* test logging in with a request.
1096+
* test logging in.
10971097
*
10981098
* @return void
10991099
*/
1100-
public function testLoginWithRequestData() {
1101-
$RequestLoginMockAuthenticate = $this->getMock(
1102-
'Cake\Controller\Component\Auth\FormAuthenticate',
1103-
array('authenticate'), array(), '', false
1100+
public function testLogin() {
1101+
$this->Auth->session = $this->getMock(
1102+
'Cake\Network\Session',
1103+
array('renew', 'write')
11041104
);
1105-
$request = new Request('users/login');
1105+
11061106
$user = array('username' => 'mark', 'role' => 'admin');
11071107

1108-
$this->Auth->request = $request;
1109-
$this->Auth->authenticate = array('RequestLoginMock');
1110-
$this->Auth->setAuthenticateObject(0, $RequestLoginMockAuthenticate);
1111-
$RequestLoginMockAuthenticate->expects($this->once())
1112-
->method('authenticate')
1113-
->with($request)
1114-
->will($this->returnValue($user));
1108+
$this->Auth->session->expects($this->once())
1109+
->method('renew');
11151110

1116-
$this->assertTrue($this->Auth->login());
1117-
$this->assertEquals($user['username'], $this->Auth->user('username'));
1111+
$this->Auth->session->expects($this->once())
1112+
->method('write')
1113+
->with($this->Auth->sessionKey, $user);
1114+
1115+
$this->Auth->login($user);
11181116
}
11191117

11201118
/**
1121-
* test login() with user data
1119+
* testGettingUserAfterLogin
11221120
*
11231121
* @return void
11241122
*/
1125-
public function testLoginWithUserData() {
1123+
public function testGettingUserAfterLogin() {
11261124
$this->assertFalse((bool)$this->Auth->user());
11271125

11281126
$user = array(
@@ -1131,7 +1129,7 @@ public function testLoginWithUserData() {
11311129
'created' => new \DateTime('2007-03-17 01:16:23'),
11321130
'updated' => new \DateTime('2007-03-17 01:18:31')
11331131
);
1134-
$this->assertTrue($this->Auth->login($user));
1132+
$this->Auth->login($user);
11351133
$this->assertTrue((bool)$this->Auth->user());
11361134
$this->assertEquals($user['username'], $this->Auth->user('username'));
11371135
}

0 commit comments

Comments
 (0)