Skip to content

Commit

Permalink
Removed AuthenticationIdentityProvider to make LmcRbac unopiniated on…
Browse files Browse the repository at this point in the history
… how identities are provided. Roll back isGranted methods to use identities

Signed-off-by: Eric Richer [email protected] <[email protected]>
  • Loading branch information
visto9259 committed Aug 5, 2024
1 parent 65d538b commit 74ee45c
Show file tree
Hide file tree
Showing 15 changed files with 34 additions and 467 deletions.
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
],
"require": {
"php": "^8.1 || ^8.2 || ^8.3",
"laminas/laminas-authentication": "^2.0",
"laminas/laminas-servicemanager": "^3.3",
"laminas/laminas-stdlib": "^3.1",
"doctrine/persistence": "^2.0 || ^3.0"
Expand Down
1 change: 0 additions & 1 deletion src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public function getDependencyConfig(): array
\LmcRbac\Options\ModuleOptions::class => \LmcRbac\Options\ModuleOptionsFactory::class,
\LmcRbac\Role\InMemoryRoleProvider::class => \LmcRbac\Role\InMemoryRoleProviderFactory::class,
\LmcRbac\Role\ObjectRepositoryRoleProvider::class => \LmcRbac\Role\ObjectRepositoryRoleProviderFactory::class,
\LmcRbac\Identity\AuthenticationIdentityProvider::class => \LmcRbac\Identity\AuthenticationIdentityProviderFactory::class,
\LmcRbac\Service\AuthorizationServiceInterface::class => \LmcRbac\Service\AuthorizationServiceFactory::class,
\LmcRbac\Service\RoleServiceInterface::class => \LmcRbac\Service\RoleServiceFactory::class,
\LmcRbac\Rbac::class => \Laminas\ServiceManager\Factory\InvokableFactory::class,
Expand Down
23 changes: 0 additions & 23 deletions src/Identity/AuthenticationIdentityProvider.php

This file was deleted.

26 changes: 0 additions & 26 deletions src/Identity/AuthenticationIdentityProviderFactory.php

This file was deleted.

7 changes: 4 additions & 3 deletions src/Service/AuthorizationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

use LmcRbac\Assertion\AssertionPluginManagerInterface;
use LmcRbac\Assertion\AssertionSet;
use LmcRbac\Identity\IdentityInterface;
use LmcRbac\Permission\PermissionInterface;
use LmcRbac\RbacInterface;

Expand Down Expand Up @@ -58,9 +59,9 @@ public function __construct(
$this->assertions = $assertions;
}

public function isGranted(string|PermissionInterface $permission, mixed $context = null): bool
public function isGranted(IdentityInterface|null $identity, string|PermissionInterface $permission, mixed $context = null): bool
{
$roles = $this->roleService->getIdentityRoles(null, $context);
$roles = $this->roleService->getIdentityRoles($identity, $context);

if (empty($roles)) {
return false;
Expand All @@ -82,6 +83,6 @@ public function isGranted(string|PermissionInterface $permission, mixed $context

$assertionSet = new AssertionSet($this->assertionPluginManager, $permissionAssertions);

return $assertionSet->assert($permission, $this->roleService->getIdentity(), $context);
return $assertionSet->assert($permission, $identity, $context);
}
}
4 changes: 3 additions & 1 deletion src/Service/AuthorizationServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

namespace LmcRbac\Service;

use LmcRbac\Identity\IdentityInterface;
use LmcRbac\Permission\PermissionInterface;

/**
Expand All @@ -34,9 +35,10 @@ interface AuthorizationServiceInterface
/**
* Check if the permission is granted to the current identity
*
* @param IdentityInterface|null $identity
* @param PermissionInterface|string $permission
* @param mixed|null $context
* @return bool
*/
public function isGranted(PermissionInterface|string $permission, mixed $context = null): bool;
public function isGranted(?IdentityInterface $identity, PermissionInterface|string $permission, mixed $context = null): bool;
}
66 changes: 2 additions & 64 deletions src/Service/RoleService.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,32 +35,18 @@
*/
class RoleService implements RoleServiceInterface
{
protected IdentityProviderInterface $identityProvider;

protected RoleProviderInterface $roleProvider;

protected string $guestRole = '';

public function __construct(
IdentityProviderInterface $identityProvider,
RoleProviderInterface $roleProvider,
string $guestRole
) {
$this->identityProvider = $identityProvider;
$this->roleProvider = $roleProvider;
$this->guestRole = $guestRole;
}

/**
* Get the current identity from the identity provider
*
* @return IdentityInterface|null
*/
public function getIdentity(): ?IdentityInterface
{
return $this->identityProvider->getIdentity();
}

/**
* Get the identity roles from the current identity, applying some more logic
*
Expand All @@ -70,47 +56,14 @@ public function getIdentity(): ?IdentityInterface
*/
public function getIdentityRoles(IdentityInterface $identity = null, mixed $context = null): iterable
{
// If no identity is provided, get it from the identity provider
// If no identity is provided, get the guest role
if (null === $identity) {
$identity = $this->identityProvider->getIdentity();
if (null === $identity) {
return $this->convertRoles([$this->guestRole]);
}
return $this->convertRoles([$this->guestRole]);
}

return $this->convertRoles($identity->getRoles());
}

/**
* Check if the given roles match one of the identity's roles
* @param string[]|RoleInterface[] $roles
* @param IdentityInterface|null $identity
* @return bool
*/
public function matchIdentityRoles(array $roles, IdentityInterface $identity = null): bool
{
// Get the roles for the identity
$identityRoles = $this->getIdentityRoles($identity);

// No roles
if (empty($identityRoles)) {
return false;
}

$roleNames = [];

foreach ($roles as $role) {
$roleNames[] = $role instanceof RoleInterface ? $role->getName() : $role;
}

foreach ($this->flattenRoles($identityRoles) as $role) {
$a[] = $role->getName();
}
$identityRoles = $a;

return count(array_intersect($roleNames, $identityRoles)) > 0;
}

/**
* Convert the roles (potentially strings) to concrete RoleInterface objects using role provider
*
Expand Down Expand Up @@ -140,19 +93,4 @@ private function convertRoles(iterable $roles): iterable

return array_merge($collectedRoles, $this->roleProvider->getRoles($toCollect));
}

/**
* @param RoleInterface[] $roles
* @return \Generator
*/
private function flattenRoles(array $roles): \Generator
{
foreach ($roles as $role) {
yield $role;

if ($role->hasChildren()) {
yield from $this->flattenRoles($role->getChildren());
}
}
}
}
6 changes: 2 additions & 4 deletions src/Service/RoleServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@
namespace LmcRbac\Service;

use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
use LmcRbac\Identity\IdentityProviderInterface;
use LmcRbac\Options\ModuleOptions;
use LmcRbac\Service\RoleService;
use Psr\Container\ContainerInterface;

/**
Expand All @@ -48,8 +46,8 @@ public function __invoke(ContainerInterface $container): RoleService
$roleProviderName = key($roleProvider);

return new RoleService(
$container->get($moduleOptions->getIdentityProvider()),
$container->get($roleProviderName),
$moduleOptions->getGuestRole());
$moduleOptions->getGuestRole()
);
}
}
15 changes: 0 additions & 15 deletions src/Service/RoleServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,4 @@ interface RoleServiceInterface
*/
public function getIdentityRoles(IdentityInterface $identity = null, mixed $context = null): iterable;

/**
* Get the current identity from the identity provider
*
* @return IdentityInterface|null
*/
public function getIdentity(): ?IdentityInterface;

/**
* Check if the given roles match one of the identity's roles
* @param RoleInterface[] $roles
* @param IdentityInterface|null $identity
* @return bool
*/
public function matchIdentityRoles(array $roles, IdentityInterface $identity = null): bool;

}
1 change: 0 additions & 1 deletion test/ConfigProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public function testProvidesExpectedConfiguration()
\LmcRbac\Options\ModuleOptions::class => \LmcRbac\Options\ModuleOptionsFactory::class,
\LmcRbac\Role\InMemoryRoleProvider::class => \LmcRbac\Role\InMemoryRoleProviderFactory::class,
\LmcRbac\Role\ObjectRepositoryRoleProvider::class => \LmcRbac\Role\ObjectRepositoryRoleProviderFactory::class,
\LmcRbac\Identity\AuthenticationIdentityProvider::class => \LmcRbac\Identity\AuthenticationIdentityProviderFactory::class,
\LmcRbac\Service\AuthorizationServiceInterface::class => \LmcRbac\Service\AuthorizationServiceFactory::class,
\LmcRbac\Service\RoleServiceInterface::class => \LmcRbac\Service\RoleServiceFactory::class,
\LmcRbac\Rbac::class => \Laminas\ServiceManager\Factory\InvokableFactory::class,
Expand Down
2 changes: 1 addition & 1 deletion test/Container/RoleServiceFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function testCanCreateRoleService(): void
'services' => [
ModuleOptions::class => $options,
InMemoryRoleProvider::class => new InMemoryRoleProvider([]),
\LmcRbac\Identity\AuthenticationIdentityProvider::class => $this->createMock(IdentityProviderInterface::class),
// \LmcRbac\Identity\AuthenticationIdentityProvider::class => $this->createMock(IdentityProviderInterface::class),
IdentityProviderInterface::class => $this->createMock(IdentityProviderInterface::class),
],
]);
Expand Down
44 changes: 0 additions & 44 deletions test/Identity/AuthenticationIdentityProviderFactoryTest.php

This file was deleted.

53 changes: 0 additions & 53 deletions test/Identity/AuthenticationIdentityProviderTest.php

This file was deleted.

Loading

0 comments on commit 74ee45c

Please sign in to comment.