diff --git a/docs/07. Cookbook.md b/docs/07. Cookbook.md index 7065f74d..7ab3f4cd 100644 --- a/docs/07. Cookbook.md +++ b/docs/07. Cookbook.md @@ -750,10 +750,8 @@ To use the authentication service from ZfcUser, just add the following alias in ```php return [ - 'service_manager' => [ - 'aliases' => [ - 'Zend\Authentication\AuthenticationService' => 'zfcuser_auth_service' - ] + 'zfc_rbac' => [ + 'authentication_service' => 'zfcuser_auth_service' ] ]; ``` diff --git a/src/ZfcRbac/Factory/AuthenticationIdentityProviderFactory.php b/src/ZfcRbac/Factory/AuthenticationIdentityProviderFactory.php index 604ba1f5..863c45f8 100644 --- a/src/ZfcRbac/Factory/AuthenticationIdentityProviderFactory.php +++ b/src/ZfcRbac/Factory/AuthenticationIdentityProviderFactory.php @@ -39,8 +39,10 @@ class AuthenticationIdentityProviderFactory implements FactoryInterface */ public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { + /* @var \ZfcRbac\Options\ModuleOptions $moduleOptions */ + $moduleOptions = $container->get('ZfcRbac\Options\ModuleOptions'); /* @var \Zend\Authentication\AuthenticationService $authenticationProvider */ - $authenticationProvider = $container->get('Zend\Authentication\AuthenticationService'); + $authenticationProvider = $container->get($moduleOptions->getAuthenticationService()); return new AuthenticationIdentityProvider($authenticationProvider); } diff --git a/src/ZfcRbac/Factory/RedirectStrategyFactory.php b/src/ZfcRbac/Factory/RedirectStrategyFactory.php index e9e28365..125bf420 100644 --- a/src/ZfcRbac/Factory/RedirectStrategyFactory.php +++ b/src/ZfcRbac/Factory/RedirectStrategyFactory.php @@ -42,7 +42,7 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o /* @var \ZfcRbac\Options\ModuleOptions $moduleOptions */ $moduleOptions = $container->get('ZfcRbac\Options\ModuleOptions'); /** @var \Zend\Authentication\AuthenticationService $authenticationService */ - $authenticationService = $container->get('Zend\Authentication\AuthenticationService'); + $authenticationService = $container->get($moduleOptions->getAuthenticationService()); return new RedirectStrategy($moduleOptions->getRedirectStrategy(), $authenticationService); } diff --git a/src/ZfcRbac/Options/ModuleOptions.php b/src/ZfcRbac/Options/ModuleOptions.php index 648f4107..6f056a83 100644 --- a/src/ZfcRbac/Options/ModuleOptions.php +++ b/src/ZfcRbac/Options/ModuleOptions.php @@ -86,6 +86,13 @@ class ModuleOptions extends AbstractOptions */ protected $redirectStrategy; + /** + * Authentication service name + * + * @var string + */ + protected $authenticationService = 'Zend\Authentication\AuthenticationService'; + /** * Constructor * @@ -284,4 +291,24 @@ public function getRedirectStrategy() return $this->redirectStrategy; } + + /** + * Get authentication service name + * + * @return string + */ + public function getAuthenticationService() + { + return $this->authenticationService; + } + + /** + * Set authentication service name + * + * @param string $authenticationService + */ + public function setAuthenticationService($authenticationService) + { + $this->authenticationService = $authenticationService; + } } diff --git a/tests/ZfcRbacTest/Factory/AuthenticationIdentityProviderFactoryTest.php b/tests/ZfcRbacTest/Factory/AuthenticationIdentityProviderFactoryTest.php index 0908f36d..92e2511f 100644 --- a/tests/ZfcRbacTest/Factory/AuthenticationIdentityProviderFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/AuthenticationIdentityProviderFactoryTest.php @@ -20,6 +20,7 @@ use Zend\ServiceManager\ServiceManager; use ZfcRbac\Factory\AuthenticationIdentityProviderFactory; +use ZfcRbac\Options\ModuleOptions; /** * @covers \ZfcRbac\Factory\AuthenticationIdentityProviderFactory @@ -29,6 +30,7 @@ class AuthenticationIdentityProviderFactoryTest extends \PHPUnit_Framework_TestC public function testFactory() { $serviceManager = new ServiceManager(); + $serviceManager->setService('ZfcRbac\Options\ModuleOptions', new ModuleOptions()); $serviceManager->setService( 'Zend\Authentication\AuthenticationService', $this->getMock('Zend\Authentication\AuthenticationService') diff --git a/tests/ZfcRbacTest/Factory/RedirectStrategyFactoryTest.php b/tests/ZfcRbacTest/Factory/RedirectStrategyFactoryTest.php index b871534b..8b195770 100644 --- a/tests/ZfcRbacTest/Factory/RedirectStrategyFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/RedirectStrategyFactoryTest.php @@ -35,6 +35,9 @@ public function testFactory() $moduleOptionsMock->expects($this->once()) ->method('getRedirectStrategy') ->will($this->returnValue($redirectStrategyOptions)); + $moduleOptionsMock->expects($this->once()) + ->method('getAuthenticationService') + ->will($this->returnValue('Zend\Authentication\AuthenticationService')); $authenticationServiceMock = $this->getMock('Zend\Authentication\AuthenticationService'); diff --git a/tests/ZfcRbacTest/Options/ModuleOptionsTest.php b/tests/ZfcRbacTest/Options/ModuleOptionsTest.php index 85d39d7f..c44591d2 100644 --- a/tests/ZfcRbacTest/Options/ModuleOptionsTest.php +++ b/tests/ZfcRbacTest/Options/ModuleOptionsTest.php @@ -44,6 +44,7 @@ public function testAssertModuleDefaultOptions() public function testSettersAndGetters() { $moduleOptions = new ModuleOptions([ + 'authentication_service' => 'authentication_service_name', 'identity_provider' => 'IdentityProvider', 'guest_role' => 'unknown', 'guards' => [], @@ -61,6 +62,7 @@ public function testSettersAndGetters() ] ]); + $this->assertEquals('authentication_service_name', $moduleOptions->getAuthenticationService()); $this->assertEquals('IdentityProvider', $moduleOptions->getIdentityProvider()); $this->assertEquals('unknown', $moduleOptions->getGuestRole()); $this->assertEquals([], $moduleOptions->getGuards());