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

Return error if userObject is not found #37

Open
wants to merge 5 commits 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
31 changes: 22 additions & 9 deletions src/GoalioRememberMe/Authentication/Adapter/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@

use ZfcUser\Authentication\Adapter\AbstractAdapter;
use Zend\Authentication\Result as AuthenticationResult;
use Zend\ServiceManager\ServiceManagerAwareInterface;
use Zend\ServiceManager\ServiceManager;
use Zend\Stdlib\RequestInterface as Request;
use Zend\Stdlib\ResponseInterface as Response;
use ZfcUser\Authentication\Adapter\AdapterChainEvent as AuthEvent;

class Cookie extends AbstractAdapter implements ServiceManagerAwareInterface
class Cookie extends AbstractAdapter
{
protected $userMapper;

Expand All @@ -19,6 +16,12 @@ class Cookie extends AbstractAdapter implements ServiceManagerAwareInterface
protected $serviceManager;

protected $rememberMeService;

public function __construct($serviceManager)
{

$this->serviceManager = $serviceManager;
}

public function authenticate(AuthEvent $e)
{
Expand Down Expand Up @@ -77,6 +80,16 @@ public function authenticate(AuthEvent $e)

$userObject = $this->getUserMapper()->findById($cookie[0]);

if(!$userObject) {
$this->getRememberMeMapper()->removeAll($cookie[0]);
$this->getRememberMeService()->removeCookie();
$this->setSatisfied(false);

$e->setCode(AuthenticationResult::FAILURE)
->setMessages(array('User not found.'));
Copy link

Choose a reason for hiding this comment

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

Maybe this the message needs to be an class constant as wel??

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's not in constants in the other cases, so it's not in the scope of this ticket to move it :)

Choose a reason for hiding this comment

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

Agreed.
Let's merge this change

return false;
}

$this->getRememberMeService()->updateSerie($rememberMe);

// Success!
Expand Down Expand Up @@ -122,7 +135,7 @@ public function setRememberMeMapper($rememberMeMapper)
public function getRememberMeMapper()
{
if (null === $this->rememberMeMapper) {
$this->rememberMeMapper = $this->getServiceManager()->get('goaliorememberme_rememberme_mapper');
$this->rememberMeMapper = $this->serviceManager->get('goaliorememberme_rememberme_mapper');
}
return $this->rememberMeMapper;
}
Expand All @@ -135,7 +148,7 @@ public function setUserMapper($userMapper)
public function getUserMapper()
{
if (null === $this->userMapper) {
$this->userMapper = $this->getServiceManager()->get('zfcuser_user_mapper');
$this->userMapper = $this->serviceManager->get('zfcuser_user_mapper');
}
return $this->userMapper;
}
Expand All @@ -148,7 +161,7 @@ public function setRememberMeService($rememberMeService)
public function getRememberMeService()
{
if (null === $this->rememberMeService) {
$this->rememberMeService = $this->getServiceManager()->get('goaliorememberme_rememberme_service');
$this->rememberMeService = $this->serviceManager->get('goaliorememberme_rememberme_service');
}
return $this->rememberMeService;
}
Expand All @@ -160,7 +173,7 @@ public function getRememberMeService()
*/
public function logout()
{
$authService = $this->getServiceManager()->get('zfcuser_auth_service');
$authService = $this->serviceManager->get('zfcuser_auth_service');
$user = $authService->getIdentity();

$cookie = explode("\n", $this->getRememberMeService()->getCookie());
Expand All @@ -171,4 +184,4 @@ public function logout()
}
}

}
}
12 changes: 9 additions & 3 deletions src/GoalioRememberMe/Module.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace GoalioRememberMe;

use GoalioRememberMe\Authentication\Adapter\Cookie;
use Zend\Mvc\MvcEvent;
use Zend\Http\Request as HttpRequest;
use Zend\Loader\StandardAutoloader;
Expand All @@ -26,12 +27,17 @@ public function getConfig() {
public function getServiceConfig() {
return array(
'invokables' => array(
'GoalioRememberMe\Authentication\Adapter\Cookie' => 'GoalioRememberMe\Authentication\Adapter\Cookie',
'GoalioRememberMe\Form\Login' => 'GoalioRememberMe\Form\Login',
'goaliorememberme_rememberme_service' => 'GoalioRememberMe\Service\RememberMe',
),
),

'factories' => array(
'goaliorememberme_rememberme_service' => function ($sm) {

Choose a reason for hiding this comment

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

I think the factories should move to separate file so we dion't break the caching of this.

return new\GoalioRememberMe\Service\RememberMe($sm);
},

'GoalioRememberMe\Authentication\Adapter\Cookie' => function ($sm) {
return new Cookie($sm);
},

'goaliorememberme_module_options' => function ($sm) {
$config = $sm->get('Config');
Expand Down
9 changes: 7 additions & 2 deletions src/GoalioRememberMe/Service/RememberMe.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@

namespace GoalioRememberMe\Service;

use Zend\ServiceManager\ServiceManagerAwareInterface;
use Zend\ServiceManager\ServiceManager;
use ZfcBase\EventManager\EventProvider;
use GoalioRememberMe\Options\RememberMeOptionsInterface;
use Zend\Math\Rand;

class RememberMe extends EventProvider implements ServiceManagerAwareInterface
class RememberMe extends EventProvider
{
protected $mapper, $options;

protected $serviceManager;

public function __construct($serviceManager)
{

$this->serviceManager = $serviceManager;
}

public function createToken($length = 16)
{
Expand Down