user2819732
user2819732

Reputation: 11

Customizing AuthenticationService to ZfcUser

I have a custom implementation of AuthenticationService that I would like to use in ZfcUser Module but I am able to set this class into the module. The implementation seems to be fixed.

vendor\zf-commons\zfc-user\Module.php

'zfcuser_auth_service' => function ($sm) {
    return new \Zend\Authentication\AuthenticationService(
         $sm->get('ZfcUser\Authentication\Storage\Db'),
         $sm->get('ZfcUser\Authentication\Adapter\AdapterChain')
      );
 }

The original requirement is to keep a unique active session per user that is implemented in my CustomAuthenticationService. Any ideas to solve this problem?

Upvotes: 1

Views: 447

Answers (2)

user2819732
user2819732

Reputation: 11

The auth adapter is just trigged when the login action is performed. To handle each request you can override the storage adapter that allow validate the identify in every request. In your configuration file add a 'ZfcUser\Authentication\Storage\Db' attribute poiting to your custom storage class.

'service_manager' => array(
    'invokables' => array(
    'ZfcUser\Authentication\Storage\Db' => 'MyCustom\Authentication\Storage'),
...

Upvotes: 0

AlexP
AlexP

Reputation: 9857

Your use case is unclear; normally the authentication adapter is the class that you would normally customise, rather than the actual authentication service.

Nevertheless, you can override the default service with your own providing you register the service with the same name and the module is loaded after the ZfcUser module.

Say your custom authentication service is in your own Auth namespace/module, with the class Auth\Service\CustomAuthenticationService.

Register the service in Auth\Module.php (or depending on the type of factory the module.config.php of that module).

class Module
{
    public function getServiceConfig()
    {
        return [
            'aliases' => [
                'MyAuthenticationService' => 'zfcuser_auth_service',
            ],
            'factories' => [
                'zfcuser_auth_service' => function($sm) {
                    return new \Auth\Service\CustomAuthenticationService(
                        $sm->get('ZfcUser\Authentication\Storage\Db'),
                        $sm->get('ZfcUser\Authentication\Adapter\AdapterChain')
                    );
                },
            ],
        ];
    }
}

Lastly, ensure the module is loaded after ZfcUser in application.config.php.

return [
    'modules' => [
        //...
        'ZfcUser',
        'Auth',
        // ...
    ],
];

Upvotes: 1

Related Questions