Navigation Menu

Skip to content

Commit

Permalink
Merge pull request #8704 from Phillaf/master
Browse files Browse the repository at this point in the history
BaseAuth now pass the username to custom finder
  • Loading branch information
markstory committed Apr 27, 2016
2 parents ef60e7e + 3088cc1 commit 024985e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Auth/BaseAuthenticate.php
Expand Up @@ -153,6 +153,10 @@ protected function _query($username)
$finder = key($finder);
}

if (!isset($options['username'])) {
$options['username'] = $username;
}

$query = $table->find($finder, $options);

return $query;
Expand Down
37 changes: 37 additions & 0 deletions tests/TestCase/Auth/FormAuthenticateTest.php
Expand Up @@ -319,6 +319,43 @@ public function testFinder()
$this->assertEquals($expected, $result);
}

/**
* Test using custom finder
*
* @return void
*/
public function testFinderOptions()
{
$request = new Request('posts/index');
$request->data = [
'username' => 'mariano',
'password' => 'password'
];

$this->auth->config([
'userModel' => 'AuthUsers',
'finder' => 'username'
]);

$result = $this->auth->authenticate($request, $this->response);
$expected = [
'id' => 1,
'username' => 'mariano',
];
$this->assertEquals($expected, $result);

$this->auth->config([
'finder' => ['username' => ['username' => 'nate']]
]);

$result = $this->auth->authenticate($request, $this->response);
$expected = [
'id' => 5,
'username' => 'nate',
];
$this->assertEquals($expected, $result);
}

/**
* test password hasher settings
*
Expand Down
21 changes: 21 additions & 0 deletions tests/test_app/TestApp/Model/Table/AuthUsersTable.php
Expand Up @@ -12,6 +12,7 @@
*/
namespace TestApp\Model\Table;

use Cake\Core\Exception\Exception;
use Cake\ORM\Query;
use Cake\ORM\Table;

Expand All @@ -38,4 +39,24 @@ public function findAuth(Query $query, array $options)

return $query;
}

/**
* Custom finder
*
* @param \Cake\ORM\Query $query The query to find with
* @param array $options The options to find with
* @return \Cake\ORM\Query The query builder
*/
public function findUsername(Query $query, array $options)
{
if (empty($options['username'])) {
throw new Exception(__('Username not defined'));
}

$query = $this->find()
->where(['username' => $options['username']])
->select(['id', 'username', 'password']);

return $query;
}
}

0 comments on commit 024985e

Please sign in to comment.