Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow auth adapters to return a single value instead of arrays #651

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions security/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,17 @@ public static function check($name, $credentials = null, array $options = array(

if (($credentials) && $data = $self::adapter($name)->check($credentials, $options)) {
if ($options['persist']) {
foreach ($data as $key => $value) {
if (!in_array($key, $options['persist'])) {
unset($data[$key]);
if (is_array($data)) {
foreach ($data as $key => $value) {
if (!in_array($key, $options['persist'])) {
unset($data[$key]);
}
}
}
} else {
unset($data['password']);
if (is_array($data) && isset($data['password'])) {
unset($data['password']);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, you can rewrite the entire if/else block this way:

if ($options['persist'] && is_array($data)) {
    $data = array_intersect_key($data, array_fill_keys($options['persist'], true));
} elseif (is_array($data)) {
    unset($data['password']);
}

Do that, squash the commit, and re-point this at dev, and I'll get it merged right away. Good work on the test case!

}

return ($options['writeSession']) ? $self::set($name, $data) : $data;
Expand Down
22 changes: 22 additions & 0 deletions tests/cases/security/AuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,28 @@ public function testAuthPersist() {
$result = Auth::check('test', $user, array('success' => true, 'checkSession' => false));
$this->assertEqual($expected, $result);
$this->assertEqual($expected, Session::read('test'));

Auth::reset();

Auth::config(array(
'test' => array(
'adapter' => $this->_classes['mockAuthAdapter'],
)
));

$user = array(
'id' => '123',
'username' => 'foobar',
'password' => 'not!important',
'email' => 'foo@bar.com',
'insuranceNumer' => 1234567
);

$expected = 123;

$result = Auth::check('test', $user, array('key_only' => true, 'checkSession' => false));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, also, to be consistent with coding standards, 'key_only' should be 'keyOnly'. Thanks!

$this->assertEqual($expected, $result);
$this->assertEqual($expected, Session::read('test'));
}
}

Expand Down
8 changes: 7 additions & 1 deletion tests/mocks/security/auth/adapter/MockAuthAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@
class MockAuthAdapter extends \lithium\core\Object {

public function check($credentials, array $options = array()) {
return isset($options['success']) ? $credentials : false;
switch (true) {
case isset($options['success']):
return $credentials;
case isset($options['key_only']):
return $credentials['id'];
}
return false;
}

public function set($data, array $options = array()) {
Expand Down