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 4f51b97 commit 28fbecb
Showing 1 changed file with 13 additions and 22 deletions.
35 changes: 13 additions & 22 deletions src/Database/Wrapper/PermissionWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class PermissionWrapper implements PermissionInterface
private array $cachedInvalidEntities = [];

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

Expand Down Expand Up @@ -93,6 +93,7 @@ public function getCapabilityEntityFactory(): ?CapabilityEntityFactoryInterface
public function setCapabilityEntityFactory(CapabilityEntityFactoryInterface $capabilityEntityFactory): void
{
if ($capabilityEntityFactory !== $this->capabilityEntityFactory) {
$this->cachedInvalidEntities = [];
$this->availableIdentities = null;
}
$this->capabilityEntityFactory = $capabilityEntityFactory;
Expand Down Expand Up @@ -181,24 +182,11 @@ public function get(CapabilityInterface|string $identity): ?CapabilityInterface
if (isset($this->cachedInvalidEntities[$identity])) {
return null;
}

$em = $this->getConnection()->getEntityManager();
$this->availableIdentities ??= $this->getCapabilities();

if (!isset($this->availableIdentities[$identity])) {
return null;
}

$entity = $factory->createEntity(
$this->getConnection()->getEntityManager(),
$identity
);
if (!$entity) {
if (!isset($this->getCapabilities()[$identity])) {
$this->cachedInvalidEntities[$identity] = false;
return null;
}
$this->availableIdentities[$identity] = true;
$this->add($entity);

}

return $this->permission->get($identity);
Expand All @@ -219,12 +207,15 @@ public function getCapabilities(): array
{
$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;
$this->availableIdentities = [];
IterableHelper::every(
$this
->getCapabilityEntityFactory()
->all($this->getConnection()->getEntityManager()),
function ($e, CapabilityInterface $capability) {
$identity = $this->identify($capability);
$this->availableIdentities[$identity] = true;
$this->permission->add($capability);
}
);
}
Expand Down

0 comments on commit 28fbecb

Please sign in to comment.