Skip to content

Commit

Permalink
Added setter/getter for assertions to AuthorizationService.php
Browse files Browse the repository at this point in the history
Signed-off-by: Eric Richer [email protected] <[email protected]>
  • Loading branch information
visto9259 committed Aug 7, 2024
1 parent f210c3b commit 8428160
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/Service/AuthorizationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

namespace Lmc\Rbac\Service;

use Lmc\Rbac\Assertion\AssertionInterface;
use Lmc\Rbac\Assertion\AssertionPluginManagerInterface;
use Lmc\Rbac\Assertion\AssertionSet;
use Lmc\Rbac\Identity\IdentityInterface;
Expand Down Expand Up @@ -59,6 +60,59 @@ public function __construct(
$this->assertions = $assertions;
}

/**
* Set assertions, either merging or replacing (default)
* @param array $assertions
* @param bool $merge
* @return void
*/
public function setAssertions(array $assertions, bool $merge = false): void
{
$this->assertions = $merge ?
array_merge($this->assertions, $assertions) :
$assertions;
}

/**
* Set assertion for a given permission
* @param PermissionInterface|string $permission
* @param AssertionInterface|callable|string $assertion
* @return void
*/
public function setAssertion(PermissionInterface|string $permission, AssertionInterface|callable|string $assertion): void
{
$this->assertions[(string) $permission] = $assertion;
}

/**
* Check if there are assertions for the permission
* @param PermissionInterface|string $permission
* @return bool
*/
public function hasAssertion(PermissionInterface|string $permission): bool
{
return isset($this->assertions[(string) $permission]);
}

/**
* Get the assertions
* @return array
*/
public function getAssertions(): array
{
return $this->assertions;
}

/**
* Get the assertions for the given permission
* @param PermissionInterface|string $permission
* @return AssertionInterface|callable|string|null
*/
public function getAssertion(PermissionInterface|string $permission): AssertionInterface|callable|string|null
{
return $this->hasAssertion(($permission)) ? $this->assertions[(string) $permission] : null;
}

public function isGranted(IdentityInterface|null $identity, string|PermissionInterface $permission, mixed $context = null): bool
{
$roles = $this->roleService->getIdentityRoles($identity, $context);
Expand Down
37 changes: 37 additions & 0 deletions src/Service/AuthorizationServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

namespace Lmc\Rbac\Service;

use Lmc\Rbac\Assertion\AssertionInterface;
use Lmc\Rbac\Identity\IdentityInterface;
use Lmc\Rbac\Permission\PermissionInterface;

Expand All @@ -41,4 +42,40 @@ interface AuthorizationServiceInterface
* @return bool
*/
public function isGranted(?IdentityInterface $identity, PermissionInterface|string $permission, mixed $context = null): bool;

/**
* Set assertions, either merging or replacing (default)
* @param array $assertions
* @param bool $merge
* @return void
*/
public function setAssertions(array $assertions, bool $merge = false): void;

/**
* Set assertion for a given permission
* @param PermissionInterface|string $permission
* @param AssertionInterface|callable|string $assertion
* @return void
*/
public function setAssertion(PermissionInterface|string $permission, AssertionInterface|callable|string $assertion): void;

/**
* Check if there are assertions for the permission
* @param PermissionInterface|string $permission
* @return bool
*/
public function hasAssertion(PermissionInterface|string $permission): bool;

/**
* Get the assertions
* @return array
*/
public function getAssertions(): array;

/**
* Get the assertions for the given permission
* @param PermissionInterface|string $permission
* @return AssertionInterface|callable|string|null
*/
public function getAssertion(PermissionInterface|string $permission): AssertionInterface|callable|string|null;
}
54 changes: 54 additions & 0 deletions test/Service/AuthorizationServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -398,4 +398,58 @@ public function testContextIsPassedToRoleService(): void
$roleService->expects($this->once())->method('getIdentityRoles')->with($identity, $context)->willReturn([]);
$authorizationService->isGranted($identity, 'foo', $context);
}

public function testGetAssertions(): void
{
$assertions = [
'foo' => 'foo',
];
$authorizationService = $this->createAuthorizationService($assertions);
$this->assertEquals($assertions, $authorizationService->getAssertions());
$this->assertEquals('foo', $authorizationService->getAssertion('foo'));
$this->assertNull($authorizationService->getAssertion('bar'));
}

public function testHasAssertion(): void
{
$assertions = [
'foo' => 'foo',
];
$authorizationService = $this->createAuthorizationService($assertions);
$this->assertTrue($authorizationService->hasAssertion('foo'));
$this->assertFalse($authorizationService->hasAssertion('bar'));
}

public function testSetAssertions(): void
{
$assertions = [
'foo' => 'foo',
];
$newAssertions = [
'bar' => 'bar',
];
$authorizationService = $this->createAuthorizationService($assertions);
$authorizationService->setAssertions($newAssertions, false);
$this->assertEquals($newAssertions, $authorizationService->getAssertions());
$authorizationService->setAssertions($assertions, true);
$this->assertEquals(array_merge($assertions, $newAssertions), $authorizationService->getAssertions());

// Reset assertions
$authorizationService->setAssertions($assertions, false);
$authorizationService->setAssertion('bar', 'bar');
$this->assertEquals(array_merge($assertions, $newAssertions), $authorizationService->getAssertions());

$authorizationService->setAssertion('bar', 'foo');
$this->assertEquals('foo', $authorizationService->getAssertion('bar'));
}

private function createAuthorizationService(array $assertions): AuthorizationService
{
return new AuthorizationService(
$this->createMock(Rbac::class),
$this->createMock(RoleServiceInterface::class),
$this->createMock(AssertionPluginManagerInterface::class),
$assertions
);
}
}

0 comments on commit 8428160

Please sign in to comment.