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

Commit a103d30

Browse files
committed
Added config option for authorization service instead of hardcoded one
1 parent 8a28d65 commit a103d30

File tree

7 files changed

+40
-6
lines changed

7 files changed

+40
-6
lines changed

docs/07. Cookbook.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -750,10 +750,8 @@ To use the authentication service from ZfcUser, just add the following alias in
750750

751751
```php
752752
return [
753-
'service_manager' => [
754-
'aliases' => [
755-
'Zend\Authentication\AuthenticationService' => 'zfcuser_auth_service'
756-
]
753+
'zfc_rbac' => [
754+
'authentication_service' => 'zfcuser_auth_service'
757755
]
758756
];
759757
```

src/ZfcRbac/Factory/AuthenticationIdentityProviderFactory.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ class AuthenticationIdentityProviderFactory implements FactoryInterface
3939
*/
4040
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
4141
{
42+
/* @var \ZfcRbac\Options\ModuleOptions $moduleOptions */
43+
$moduleOptions = $container->get('ZfcRbac\Options\ModuleOptions');
4244
/* @var \Zend\Authentication\AuthenticationService $authenticationProvider */
43-
$authenticationProvider = $container->get('Zend\Authentication\AuthenticationService');
45+
$authenticationProvider = $container->get($moduleOptions->getAuthenticationService());
4446

4547
return new AuthenticationIdentityProvider($authenticationProvider);
4648
}

src/ZfcRbac/Factory/RedirectStrategyFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o
4242
/* @var \ZfcRbac\Options\ModuleOptions $moduleOptions */
4343
$moduleOptions = $container->get('ZfcRbac\Options\ModuleOptions');
4444
/** @var \Zend\Authentication\AuthenticationService $authenticationService */
45-
$authenticationService = $container->get('Zend\Authentication\AuthenticationService');
45+
$authenticationService = $container->get($moduleOptions->getAuthenticationService());
4646

4747
return new RedirectStrategy($moduleOptions->getRedirectStrategy(), $authenticationService);
4848
}

src/ZfcRbac/Options/ModuleOptions.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ class ModuleOptions extends AbstractOptions
8686
*/
8787
protected $redirectStrategy;
8888

89+
/**
90+
* Authentication service name
91+
*
92+
* @var string
93+
*/
94+
protected $authenticationService = 'Zend\Authentication\AuthenticationService';
95+
8996
/**
9097
* Constructor
9198
*
@@ -284,4 +291,24 @@ public function getRedirectStrategy()
284291

285292
return $this->redirectStrategy;
286293
}
294+
295+
/**
296+
* Get authentication service name
297+
*
298+
* @return string
299+
*/
300+
public function getAuthenticationService()
301+
{
302+
return $this->authenticationService;
303+
}
304+
305+
/**
306+
* Set authentication service name
307+
*
308+
* @param string $authenticationService
309+
*/
310+
public function setAuthenticationService($authenticationService)
311+
{
312+
$this->authenticationService = $authenticationService;
313+
}
287314
}

tests/ZfcRbacTest/Factory/AuthenticationIdentityProviderFactoryTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
use Zend\ServiceManager\ServiceManager;
2222
use ZfcRbac\Factory\AuthenticationIdentityProviderFactory;
23+
use ZfcRbac\Options\ModuleOptions;
2324

2425
/**
2526
* @covers \ZfcRbac\Factory\AuthenticationIdentityProviderFactory
@@ -29,6 +30,7 @@ class AuthenticationIdentityProviderFactoryTest extends \PHPUnit_Framework_TestC
2930
public function testFactory()
3031
{
3132
$serviceManager = new ServiceManager();
33+
$serviceManager->setService('ZfcRbac\Options\ModuleOptions', new ModuleOptions());
3234
$serviceManager->setService(
3335
'Zend\Authentication\AuthenticationService',
3436
$this->getMock('Zend\Authentication\AuthenticationService')

tests/ZfcRbacTest/Factory/RedirectStrategyFactoryTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ public function testFactory()
3535
$moduleOptionsMock->expects($this->once())
3636
->method('getRedirectStrategy')
3737
->will($this->returnValue($redirectStrategyOptions));
38+
$moduleOptionsMock->expects($this->once())
39+
->method('getAuthenticationService')
40+
->will($this->returnValue('Zend\Authentication\AuthenticationService'));
3841

3942
$authenticationServiceMock = $this->getMock('Zend\Authentication\AuthenticationService');
4043

tests/ZfcRbacTest/Options/ModuleOptionsTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public function testAssertModuleDefaultOptions()
4444
public function testSettersAndGetters()
4545
{
4646
$moduleOptions = new ModuleOptions([
47+
'authentication_service' => 'authentication_service_name',
4748
'identity_provider' => 'IdentityProvider',
4849
'guest_role' => 'unknown',
4950
'guards' => [],
@@ -61,6 +62,7 @@ public function testSettersAndGetters()
6162
]
6263
]);
6364

65+
$this->assertEquals('authentication_service_name', $moduleOptions->getAuthenticationService());
6466
$this->assertEquals('IdentityProvider', $moduleOptions->getIdentityProvider());
6567
$this->assertEquals('unknown', $moduleOptions->getGuestRole());
6668
$this->assertEquals([], $moduleOptions->getGuards());

0 commit comments

Comments
 (0)