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

Pass user object to preprocess function. #483

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
7 changes: 4 additions & 3 deletions src/ZfcUser/Authentication/Adapter/Db.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ public function authenticate(AuthenticationEvent $event)

$identity = $event->getRequest()->getPost()->get('identity');
$credential = $event->getRequest()->getPost()->get('credential');
$credential = $this->preProcessCredential($credential);
$userObject = null;

// Cycle through the configured identity sources and test each
Expand Down Expand Up @@ -96,6 +95,8 @@ public function authenticate(AuthenticationEvent $event)
}
}

$credential = $this->preProcessCredential($credential, $userObject);

$cryptoService = $this->getHydrator()->getCryptoService();
if (!$cryptoService->verify($credential, $userObject->getPassword())) {
// Password does not match
Expand Down Expand Up @@ -132,10 +133,10 @@ protected function updateUserPasswordHash(UserEntity $user, $password, Bcrypt $b
$this->getMapper()->update($user);
}

public function preprocessCredential($credential)
public function preprocessCredential($credential, UserEntity $user)
Copy link
Contributor

Choose a reason for hiding this comment

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

UserInterface

Copy link
Contributor Author

Choose a reason for hiding this comment

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

UserInterface is imported as UserEntity. I did not touch this, but this was already the case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could resolve this in different PR?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, sorry about that. ZfcUser\Mapper\UserInterface will be renamed to UserMapperInterface in 2.0 which will resolve this "collision".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah I see! So I guess I should leave it the way it is?

{
if (is_callable($this->credentialPreprocessor)) {
return call_user_func($this->credentialPreprocessor, $credential);
return call_user_func($this->credentialPreprocessor, $credential, $user);
}
return $credential;
}
Expand Down
21 changes: 19 additions & 2 deletions tests/ZfcUserTest/Authentication/Adapter/DbTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -425,15 +425,32 @@ public function testPreprocessCredentialWithCallable()
$this->db->setCredentialPreprocessor(function ($credential) use ($expected) {
return $expected;
});
$this->assertSame($expected, $this->db->preprocessCredential('ZfcUser'));
$this->assertSame($expected, $this->db->preprocessCredential('ZfcUser', $this->user));
}

/**
* @covers ZfcUser\Authentication\Adapter\Db::preprocessCredential
* @covers ZfcUser\Authentication\Adapter\Db::setCredentialPreprocessor
*/
public function testPreprocessCredentialWithCallableAndAdditionalUserParam()
{
$expected = 3;
$this->user->expects($this->once())
->method('getId')
->will($this->returnValue($expected));

$this->db->setCredentialPreprocessor(function ($credential, $user) {
return $user->getId();
});
$this->assertSame($expected, $this->db->preprocessCredential('ZfcUser', $this->user));
}

/**
* @covers ZfcUser\Authentication\Adapter\Db::preprocessCredential
*/
public function testPreprocessCredentialWithoutCallable()
{
$this->assertSame('zfcUser', $this->db->preprocessCredential('zfcUser'));
$this->assertSame('zfcUser', $this->db->preprocessCredential('zfcUser', $this->user));
}

/**
Expand Down