Skip to content
This repository has been archived by the owner on Jul 3, 2020. It is now read-only.

Added config option for authorization service instead of hard-coded one #395

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 2 additions & 4 deletions docs/07. Cookbook.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
]
];
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/ZfcRbac/Factory/RedirectStrategyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
27 changes: 27 additions & 0 deletions src/ZfcRbac/Options/ModuleOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ class ModuleOptions extends AbstractOptions
*/
protected $redirectStrategy;

/**
* Authentication service name
*
* @var string
*/
protected $authenticationService = 'Zend\Authentication\AuthenticationService';

/**
* Constructor
*
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

use Zend\ServiceManager\ServiceManager;
use ZfcRbac\Factory\AuthenticationIdentityProviderFactory;
use ZfcRbac\Options\ModuleOptions;

/**
* @covers \ZfcRbac\Factory\AuthenticationIdentityProviderFactory
Expand All @@ -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')
Expand Down
3 changes: 3 additions & 0 deletions tests/ZfcRbacTest/Factory/RedirectStrategyFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
2 changes: 2 additions & 0 deletions tests/ZfcRbacTest/Options/ModuleOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [],
Expand All @@ -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());
Expand Down