Skip to content
Permalink
Browse files

Allow AuthComponent and friends to work with user object implementing…

… ArrayAccess.

Refs #8240
  • Loading branch information...
ADmad committed Feb 19, 2016
1 parent 65a8468 commit ee0e57df1fc767e54ee42ce1911a863264f46c90
@@ -57,7 +57,7 @@ public function __construct(ComponentRegistry $registry, array $config = [])
/**
* Checks user authorization.
*
* @param array $user Active user data
* @param array|\ArrayAccess $user Active user data
* @param \Cake\Network\Request $request Request instance.
* @return bool
*/
@@ -83,7 +83,7 @@ public function controller(Controller $controller = null)
/**
* Checks user authorization using a controller callback.
*
* @param array $user Active user data
* @param array|\ArrayAccess $user Active user data
* @param \Cake\Network\Request $request Request instance.
* @return bool
*/
@@ -48,7 +48,7 @@ public function read()
/**
* {@inheritDoc}
*/
public function write(array $user)
public function write($user)
{
$this->_user = $user;
}
@@ -91,10 +91,10 @@ public function read()
*
* The session id is also renewed to help mitigate issues with session replays.
*
* @param array $user User record.
* @param array|\ArrayAccess $user User record.
* @return void
*/
public function write(array $user)
public function write($user)
{
$this->_user = $user;
@@ -30,10 +30,10 @@ public function read();
/**
* Write user record.
*
* @param array $user User record.
* @param array|\ArrayAccess $user User record.
* @return void
*/
public function write(array $user);
public function write($user);
/**
* Delete user record.
@@ -470,7 +470,7 @@ protected function _setDefaults()
* Each adapter will be checked in sequence, if any of them return true, then the user will
* be authorized for the request.
*
* @param array|null $user The user to check the authorization of.
* @param array|\ArrayAccess|null $user The user to check the authorization of.
* If empty the user fetched from storage will be used.
* @param \Cake\Network\Request|null $request The request to authenticate for.
* If empty, the current request will be used.
@@ -622,11 +622,11 @@ public function deny($actions = null)
* The storage class is configured using `storage` config key or passing
* instance to AuthComponent::storage().
*
* @param array $user Array of user data.
* @param array|\ArrayAccess $user User data.
* @return void
* @link http://book.cakephp.org/3.0/en/controllers/components/authentication.html#identifying-users-and-logging-them-in
*/
public function setUser(array $user)
public function setUser($user)
{
$this->storage()->write($user);
}
@@ -767,7 +767,7 @@ public function identify()
}
foreach ($this->_authenticateObjects as $auth) {
$result = $auth->authenticate($this->request, $this->response);
if (!empty($result) && is_array($result)) {
if (!empty($result)) {
$this->_authenticationProvider = $auth;
$event = $this->dispatchEvent('Auth.afterIdentify', [$result, $auth]);
if ($event->result !== null) {
@@ -166,6 +166,50 @@ public function testIdentify()
$this->assertSame($AuthLoginFormAuthenticate, $this->Auth->authenticationProvider());
}
/**
* Test identify with user record as ArrayObject instance.
*
* @return void
*/
public function testIdentifyArrayAccess()
{
$AuthLoginFormAuthenticate = $this->getMock(
'Cake\Controller\Component\Auth\FormAuthenticate',
['authenticate'],
[],
'',
false
);
$this->Auth->authenticate = [
'AuthLoginForm' => [
'userModel' => 'AuthUsers'
]
];
$this->Auth->setAuthenticateObject(0, $AuthLoginFormAuthenticate);
$this->Auth->request->data = [
'AuthUsers' => [
'username' => 'mark',
'password' => Security::hash('cake', null, true)
]
];
$user = new \ArrayObject([
'id' => 1,
'username' => 'mark'
]);
$AuthLoginFormAuthenticate->expects($this->once())
->method('authenticate')
->with($this->Auth->request)
->will($this->returnValue($user));
$result = $this->Auth->identify();
$this->assertEquals($user, $result);
$this->assertSame($AuthLoginFormAuthenticate, $this->Auth->authenticationProvider());
}
/**
* testRedirectVarClearing method
*
@@ -281,6 +325,35 @@ public function testIsAuthorizedDelegation()
$this->assertSame($AuthMockTwoAuthorize, $this->Auth->authorizationProvider());
}
/**
* test isAuthorized passing it an ArrayObject instance.
*
* @return void
*/
public function testIsAuthorizedWithArrayObject()
{
$AuthMockOneAuthorize = $this->getMock(
'Cake\Controller\Component\BaseAuthorize',
['authorize'],
[],
'',
false
);
$this->Auth->setAuthorizeObject(0, $AuthMockOneAuthorize);
$request = $this->Auth->request;
$user = new \ArrayObject(['User']);
$AuthMockOneAuthorize->expects($this->once())
->method('authorize')
->with($user, $request)
->will($this->returnValue(true));
$this->assertTrue($this->Auth->isAuthorized($user, $request));
$this->assertSame($AuthMockOneAuthorize, $this->Auth->authorizationProvider());
}
/**
* test that isAuthorized will use the session user if none is given.
*

0 comments on commit ee0e57d

Please sign in to comment.
You can’t perform that action at this time.