Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cache for the ACL entity rule check #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion Bundles/Acl/src/Spryker/Zed/Acl/Business/Model/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ class Rule implements RuleInterface
*/
protected array $aclAccessCheckerStrategyPlugins;

/**
* @var array<mixed>
*/
protected static array $cache = [];

/**
* @param \Spryker\Zed\Acl\Business\Model\GroupInterface $group
* @param \Spryker\Zed\Acl\Persistence\AclQueryContainerInterface $queryContainer
Expand Down Expand Up @@ -308,6 +313,15 @@ public function registerSystemUserRules(UserTransfer $userTransfer)
}
}

public function isAllowed(UserTransfer $userTransfer, $bundle, $controller, $action): bool
{
if (!isset(static::$cache[$userTransfer->getIdUser()][$bundle][$controller][$action])) {
static::$cache[$userTransfer->getIdUser()][$bundle][$controller][$action] = $this->executeIsAllowed($userTransfer, $bundle, $controller, $action);
}

return static::$cache[$userTransfer->getIdUser()][$bundle][$controller][$action];
}

/**
* @param \Generated\Shared\Transfer\UserTransfer $userTransfer
* @param string $bundle
Expand All @@ -316,7 +330,7 @@ public function registerSystemUserRules(UserTransfer $userTransfer)
*
* @return bool
*/
public function isAllowed(UserTransfer $userTransfer, $bundle, $controller, $action)
protected function executeIsAllowed(UserTransfer $userTransfer, $bundle, $controller, $action)
{
if ($this->userFacade->isSystemUser($userTransfer)) {
$this->registerSystemUserRules($userTransfer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ class AclEntityMetadataConfigReader implements AclEntityMetadataConfigReaderInte
*/
protected $aclEntityMetadataConfigFilter;

/**
* @var array<\Generated\Shared\Transfer\AclEntityMetadataConfigTransfer>
*/
protected static array $cache = [];

/**
* @param array<\Spryker\Zed\AclEntityExtension\Dependency\Plugin\AclEntityMetadataConfigExpanderPluginInterface> $aclEntityMetadataCollectionExpandPlugins
* @param \Spryker\Zed\AclEntity\Business\Validator\AclEntityMetadataConfigValidatorInterface $aclEntityMetadataConfigValidator
Expand All @@ -54,6 +59,24 @@ public function __construct(
public function getAclEntityMetadataConfig(
bool $runValidation = true,
?AclEntityMetadataConfigRequestTransfer $aclEntityMetadataConfigRequestTransfer = null
): AclEntityMetadataConfigTransfer {
$modelName = $aclEntityMetadataConfigRequestTransfer?->getModelName() ?? '';
if (!isset(static::$cache[$runValidation][$modelName])) {
static::$cache[$runValidation][$modelName] = $this->executeGetAclEntityMetadataConfig($runValidation, $aclEntityMetadataConfigRequestTransfer);
}

return static::$cache[$runValidation][$modelName];
}

/**
* @param bool $runValidation
* @param \Generated\Shared\Transfer\AclEntityMetadataConfigRequestTransfer|null $aclEntityMetadataConfigRequestTransfer
*
* @return \Generated\Shared\Transfer\AclEntityMetadataConfigTransfer
*/
protected function executeGetAclEntityMetadataConfig(
bool $runValidation = true,
?AclEntityMetadataConfigRequestTransfer $aclEntityMetadataConfigRequestTransfer = null
): AclEntityMetadataConfigTransfer {
$aclEntityMetadataConfigTransfer = new AclEntityMetadataConfigTransfer();
$aclEntityMetadataConfigTransfer->setAclEntityMetadataCollection(new AclEntityMetadataCollectionTransfer());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ class AclEntityRuleProvider implements AclEntityRuleProviderInterface
*/
protected $aclEntityRepository;

/**
* @var AclEntityRuleCollectionTransfer|null
*/
protected static ?AclEntityRuleCollectionTransfer $aclEntityRuleCollectionTransfer = null;

/**
* @param \Spryker\Zed\AclEntity\Persistence\Propel\Provider\AclRoleProviderInterface $aclRoleProvider
* @param \Spryker\Zed\AclEntity\Persistence\AclEntityRepositoryInterface $aclEntityRepository
Expand All @@ -38,6 +43,18 @@ public function __construct(
* @return \Generated\Shared\Transfer\AclEntityRuleCollectionTransfer
*/
public function getCurrentUserAclEntityRules(): AclEntityRuleCollectionTransfer
{
if (static::$aclEntityRuleCollectionTransfer === null) {
static::$aclEntityRuleCollectionTransfer = $this->executeGetCurrentUserAclEntityRules();
}

return static::$aclEntityRuleCollectionTransfer;
}

/**
* @return \Generated\Shared\Transfer\AclEntityRuleCollectionTransfer
*/
public function executeGetCurrentUserAclEntityRules(): AclEntityRuleCollectionTransfer
{
return $this->aclEntityRepository->getAclEntityRulesByRoles(
$this->aclRoleProvider->getCurrentUserAclRoles(),
Expand Down