Skip to content

Commit

Permalink
Update PermissionWrapper.php
Browse files Browse the repository at this point in the history
  • Loading branch information
ArrayIterator authored Jul 18, 2024
1 parent f304610 commit 4f51b97
Showing 1 changed file with 76 additions and 10 deletions.
86 changes: 76 additions & 10 deletions src/Database/Wrapper/PermissionWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,34 @@

class PermissionWrapper implements PermissionInterface
{
/**
* @var ?CapabilityEntityFactoryInterface
*/
protected ?CapabilityEntityFactoryInterface $capabilityEntityFactory = null;

/**
* @var array<string, bool>
*/
private array $cachedInvalidEntities = [];

/**
* @var ?array<string, CapabilityInterface>
*/
private ?array $availableIdentities = null;

/**
* @var ?PermissionInterface $permission
*/
private PermissionInterface $permission;

/**
* PermissionWrapper constructor.
*
* @param Connection $connection
* @param ContainerInterface|null $container
* @param ManagerInterface|null $manager
* @param PermissionInterface|null $permission
*/
public function __construct(
protected Connection $connection,
?ContainerInterface $container = null,
Expand All @@ -54,11 +74,22 @@ public function __construct(
}
}

/**
* Get the capability entity factory.
*
* @return CapabilityEntityFactoryInterface|null
*/
public function getCapabilityEntityFactory(): ?CapabilityEntityFactoryInterface
{
return $this->capabilityEntityFactory;
}

/**
* Set the capability entity factory.
*
* @param CapabilityEntityFactoryInterface $capabilityEntityFactory
* @return void
*/
public function setCapabilityEntityFactory(CapabilityEntityFactoryInterface $capabilityEntityFactory): void
{
if ($capabilityEntityFactory !== $this->capabilityEntityFactory) {
Expand All @@ -67,16 +98,25 @@ public function setCapabilityEntityFactory(CapabilityEntityFactoryInterface $cap
$this->capabilityEntityFactory = $capabilityEntityFactory;
}

/**
* @return Connection
*/
public function getConnection(): Connection
{
return $this->connection;
}

/**
* @inheritdoc
*/
public function getManager(): ?ManagerInterface
{
return $this->permission->getManager();
}

/**
* @inheritdoc
*/
public function permitted(RoleInterface|UserRoleInterface $role, CapabilityInterface|string $capability): bool
{
$capability = $this->get($capability);
Expand All @@ -86,16 +126,25 @@ public function permitted(RoleInterface|UserRoleInterface $role, CapabilityInter
return $this->permission->permitted($role, $capability);
}

/**
* @inheritdoc
*/
public function add(CapabilityInterface $capability): CapabilityInterface
{
return $this->permission->add($capability);
}

/**
* @inheritdoc
*/
public function replace(CapabilityInterface $capability): void
{
$this->permission->replace($capability);
}

/**
* @inheritdoc
*/
public function has(CapabilityInterface|string $identity): bool
{
if ($this->permission->has($identity)) {
Expand All @@ -104,6 +153,9 @@ public function has(CapabilityInterface|string $identity): bool
return $this->get($identity) !== null;
}

/**
* @inheritdoc
*/
public function remove(string|CapabilityInterface $identity): ?CapabilityInterface
{
$identity = $this->identify($identity);
Expand All @@ -114,6 +166,9 @@ public function remove(string|CapabilityInterface $identity): ?CapabilityInterfa
return $this->permission->remove($identity);
}

/**
* @inheritdoc
*/
public function get(CapabilityInterface|string $identity): ?CapabilityInterface
{
if (!$this->permission->has($identity)) {
Expand All @@ -128,13 +183,7 @@ public function get(CapabilityInterface|string $identity): ?CapabilityInterface
}

$em = $this->getConnection()->getEntityManager();
$this->availableIdentities ??= IterableHelper::each(
$factory->getCapabilityIdentities($em),
static function (&$key, $value) {
$key = strtolower($value);
return false;
}
);
$this->availableIdentities ??= $this->getCapabilities();

if (!isset($this->availableIdentities[$identity])) {
return null;
Expand All @@ -155,22 +204,39 @@ static function (&$key, $value) {
return $this->permission->get($identity);
}

/**
* @inheritdoc
*/
public function identify(CapabilityInterface|string $capability): string
{
return strtolower($this->permission->identify($capability));
}

/**
* @inheritdoc
*/
public function getCapabilities(): array
{
if ($this->availableIdentities === null
|| !$this->capabilityEntityFactory
) {
$factory = $this->getCapabilityEntityFactory();
if ($this->availableIdentities === null && $factory) {
IterableHelper::each(
$factory->getCapabilityIdentities($this->getConnection()->getEntityManager()),
function (&$key, $value) {
$key = strtolower($value);
$this->availableIdentities[$key] = $value;
return false;
}
);
}

if ($this->availableIdentities === null || !$factory) {
return $this->permission->getCapabilities();
}

if (empty($this->availableIdentities)) {
return [];
}

$total = count($this->availableIdentities);
$count = count(array_filter($this->availableIdentities));
if ($total !== $count) {
Expand Down

0 comments on commit 4f51b97

Please sign in to comment.