Execute code before BjyAuthorize

140 views Asked by At

I have created a "Remember me" cookie-based login system, and I'm using ZF2 with ZfcUser and BjyAuthorize. Well, after logged in, if the cache expire, BjyAuthorize trigger before my "Remember me" system and the user is not recognized as logged in.

This is where I call the "Remember Me" cookie system in my AbstractController: https://gist.github.com/Gamempire/2b6b6388cedca9491d5f#file-abstractcontroller-php-L18 (every controller extend my AbstractController)

How can I call it before BjyAuthorize, so BjyAuthorize knows that someone is logged in?

Thanks

1

There are 1 answers

2
Otto Sandström On BEST ANSWER

To do this properly i would not just try to inject myself before the bjyAuthorize it would be better to add to ZfcUser functionality and use a storageChain to get the identity of the user.

to do this start by overriding the service name of the ZfcUser

'service_manager' => array(
    'factories' => array(
        'zfcuser_auth_service' => 'myNamespace\Factory\AuthenticationServiceFactory',
    ),
),

After this you need to create your own factory basicly you could copy the ZfcUser and change it too

public function createService(ServiceLocatorInterface $serviceLocator)
{
    $storageChain = new \Zend\Authentication\Storage\Chain();

    // dummy storage interface
    $nonPersistent = new \Zend\Authentication\Storage\NonPersistent(); // replace this with your own storage interface.
    $nonPersistent->write(1); // just to test 

    /* @var $authStorage Storage\StorageInterface */
    $authStorage = $serviceLocator->get('ZfcUser\Authentication\Storage\Db');

    $storageChain->add($authStorage, 10); // Check this interface first.
    $storageChain->add($nonPersistent, 0); // Check this interface Second.

    /* @var $authAdapter Adapter\AdapterInterface */
    $authAdapter = $serviceLocator->get('ZfcUser\Authentication\Adapter\AdapterChain');

    return new AuthenticationService(
        $storageChain,
        $authAdapter
    );
}

With this example code doing $this->zfcUserAuthentication()->getIdentity() will return 1 when there is no Session logged in user.

After this you will need to create your own storage interface. which implements Zend\Authentication\Storage\StorageInterface. I dont know how your cookie implementation is and you would need to adapt it to this. also you would need to implement on when to use cookies and when not to. There is also a module made for zfcuser that implements a remember me function named GoalioRememberMe. I hope this helps you.