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

Commit

Permalink
Merge pull request #343 from basz/change/pass-context-parameter-to-ge…
Browse files Browse the repository at this point in the history
…tIdentityRoles

Change/pass context parameter to RoleService::getIdentityRoles
  • Loading branch information
danizord authored Sep 5, 2016
2 parents 7f4d0d8 + 1ea20b3 commit 4898d8e
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 8 deletions.
2 changes: 1 addition & 1 deletion config/dependencies.global.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
ZfcRbac\Options\ModuleOptions::class => ZfcRbac\Container\ModuleOptionsFactory::class,
ZfcRbac\Role\RoleProviderPluginManager::class => ZfcRbac\Container\RoleProviderPluginManagerFactory::class,
ZfcRbac\Service\AuthorizationServiceInterface::class => ZfcRbac\Container\AuthorizationServiceFactory::class,
ZfcRbac\Service\RoleService::class => ZfcRbac\Container\RoleServiceFactory::class,
ZfcRbac\Service\RoleServiceInterface::class => ZfcRbac\Container\RoleServiceFactory::class,
],
],

Expand Down
13 changes: 8 additions & 5 deletions src/Service/AuthorizationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class AuthorizationService implements AuthorizationServiceInterface
protected $rbac;

/**
* @var RoleService
* @var RoleServiceInterface
*/
protected $roleService;

Expand All @@ -57,11 +57,14 @@ class AuthorizationService implements AuthorizationServiceInterface
* Constructor
*
* @param Rbac $rbac
* @param RoleService $roleService
* @param RoleServiceInterface $roleService
* @param AssertionPluginManager $assertionPluginManager
*/
public function __construct(Rbac $rbac, RoleService $roleService, AssertionPluginManager $assertionPluginManager)
{
public function __construct(
Rbac $rbac,
RoleServiceInterface $roleService,
AssertionPluginManager $assertionPluginManager
) {
$this->rbac = $rbac;
$this->roleService = $roleService;
$this->assertionPluginManager = $assertionPluginManager;
Expand Down Expand Up @@ -106,7 +109,7 @@ public function hasAssertion($permission)
*/
public function isGranted($identity, $permission, $context = null)
{
$roles = $this->roleService->getIdentityRoles($identity);
$roles = $this->roleService->getIdentityRoles($identity, $context);

if (empty($roles)) {
return false;
Expand Down
5 changes: 3 additions & 2 deletions src/Service/RoleService.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* @author Michaël Gallego <[email protected]>
* @licence MIT
*/
class RoleService
class RoleService implements RoleServiceInterface
{

/**
Expand Down Expand Up @@ -87,9 +87,10 @@ public function getGuestRole()
* Get the identity roles from the current identity, applying some more logic
*
* @param IdentityInterface $identity
* @param null $context
* @return RoleInterface[]
*/
public function getIdentityRoles(IdentityInterface $identity = null)
public function getIdentityRoles(IdentityInterface $identity = null, $context = null)
{
if (null === $identity) {
return $this->convertRoles([$this->guestRole]);
Expand Down
40 changes: 40 additions & 0 deletions src/Service/RoleServiceInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*/

namespace ZfcRbac\Service;

use Rbac\Role\RoleInterface;
use ZfcRbac\Identity\IdentityInterface;

/**
* Role service
*
* @author Michaël Gallego <[email protected]>
* @licence MIT
*/
interface RoleServiceInterface
{
/**
* Get the identity roles from the current identity, applying some more logic
*
* @param null|IdentityInterface $identity
* @param mixed $context
* @return RoleInterface[]
*/
public function getIdentityRoles(IdentityInterface $identity = null, $context = null);
}
15 changes: 15 additions & 0 deletions test/Service/AuthorizationServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use ZfcRbac\Identity\IdentityInterface;
use ZfcRbac\Service\AuthorizationService;
use ZfcRbac\Service\RoleService;
use ZfcRbac\Service\RoleServiceInterface;
use ZfcRbacTest\Asset\FlatRole;
use ZfcRbacTest\Asset\Identity;
use ZfcRbacTest\Asset\SimpleAssertion;
Expand Down Expand Up @@ -267,4 +268,18 @@ public function testAssertionMap()

$this->assertFalse($authorizationService->hasAssertion('bar'));
}

public function testContextIsPassedToRoleService()
{
$identity = new Identity([]);
$context = 'context';

$rbac = $this->getMockBuilder(Rbac::class)->disableOriginalConstructor()->getMock();
$roleService = $this->getMockBuilder(RoleServiceInterface::class)->disableOriginalConstructor()->getMock();
$assertionPluginManager = $this->getMockBuilder(AssertionPluginManager::class)->disableOriginalConstructor()->getMock();
$authorizationService = new AuthorizationService($rbac, $roleService, $assertionPluginManager);

$roleService->expects($this->once())->method('getIdentityRoles')->with($identity, $context)->willReturn([]);
$authorizationService->isGranted($identity, 'foo', $context);
}
}

0 comments on commit 4898d8e

Please sign in to comment.