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 support for filtering by sids #1

Merged
merged 1 commit into from
May 17, 2023
Merged
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
36 changes: 33 additions & 3 deletions Dbal/AclProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
use Symfony\Component\Security\Acl\Model\AclProviderInterface;
use Symfony\Component\Security\Acl\Model\ObjectIdentityInterface;
use Symfony\Component\Security\Acl\Model\PermissionGrantingStrategyInterface;
use Symfony\Component\Security\Acl\Model\SecurityIdentityInterface;
use Symfony\Component\Security\Core\Role\Role;

/**
* An ACL provider implementation.
Expand All @@ -37,6 +39,7 @@
class AclProvider implements AclProviderInterface
{
public const MAX_BATCH_SIZE = 30;
private const TOKEN_FILTER_PREFIX = 'IS_AUTHENTICATED_';

/**
* @var AclCacheInterface|null
Expand Down Expand Up @@ -219,9 +222,12 @@ public function findAcls(array $oids, array $sids = [])
*
* @return string
*/
protected function getLookupSql(array $ancestorIds)
protected function getLookupSql(array $ancestorIds/*, array $identityIds = []*/)
{
// FIXME: add support for filtering by sids (right now we select all sids)
$identityIds = [];
if (\func_num_args() > 1) {
$identityIds = \func_get_arg(1);
}

$sql = <<<SELECTCLAUSE
SELECT
Expand Down Expand Up @@ -255,6 +261,9 @@ protected function getLookupSql(array $ancestorIds)
SELECTCLAUSE;

$sql .= implode(' OR o.id = ', $ancestorIds).')';
if ($identityIds !== []) {
$sql .= ' AND (s.identifier = "' . implode('" OR s.identifier = "', $identityIds) . '")';
}

return $sql;
}
Expand Down Expand Up @@ -445,6 +454,8 @@ private function doUpdateAceIdentityMap(array &$aces)
* This method is called for object identities which could not be retrieved
* from the cache, and for which thus a database query is required.
*
* @param array<SecurityIdentityInterface> $sids
*
* @return \SplObjectStorage<ObjectIdentityInterface,AclInterface> mapping object identities to ACL instances
*
* @throws AclNotFoundException
Expand All @@ -456,7 +467,7 @@ private function lookupObjectIdentities(array $batch, array $sids, array $oidLoo
throw new AclNotFoundException('There is no ACL for the given object identity.');
}

$sql = $this->getLookupSql($ancestorIds);
$sql = $this->getLookupSql($ancestorIds, $this->getIdentityIds($sids));
$stmt = $this->connection->executeQuery($sql);

return $this->hydrateObjectIdentities($stmt, $oidLookup, $sids);
Expand Down Expand Up @@ -669,4 +680,23 @@ private function hydrateObjectIdentities(Result $stmt, array $oidLookup, array $

return $result;
}


/**
* Retrieves all the security identity ids which need to be queried from the database
*
* @param array<SecurityIdentityInterface> $sids
*
* @return array<string|Role>
*/
private function getIdentityIds(array $sids)
{
$filteredSids = \array_filter($sids, function ($sid) {
return false === \strpos($sid->getRole(), self::TOKEN_FILTER_PREFIX);
});

return \array_map(function ($sid) {
return $sid->getRole();
}, $filteredSids);
}
}
23 changes: 6 additions & 17 deletions Dbal/MutableAclProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -839,9 +839,7 @@ private function updateNewFieldAceProperty($name, array $changes)
$sids = new \SplObjectStorage();
$classIds = new \SplObjectStorage();
foreach ($changes[1] as $field => $new) {
for ($i = 0, $c = \count($new); $i < $c; ++$i) {
$ace = $new[$i];

foreach ($new as $i => $ace) {
if (null === $ace->getId()) {
if ($sids->contains($ace->getSecurityIdentity())) {
$sid = $sids->offsetGet($ace->getSecurityIdentity());
Expand Down Expand Up @@ -879,19 +877,15 @@ private function updateOldFieldAceProperty($name, array $changes)
{
$currentIds = [];
foreach ($changes[1] as $field => $new) {
for ($i = 0, $c = \count($new); $i < $c; ++$i) {
$ace = $new[$i];

foreach($new as $ace) {
if (null !== $ace->getId()) {
$currentIds[$ace->getId()] = true;
}
}
}

foreach ($changes[0] as $old) {
for ($i = 0, $c = \count($old); $i < $c; ++$i) {
$ace = $old[$i];

foreach($old as $ace) {
if (!isset($currentIds[$ace->getId()])) {
$this->connection->executeStatement($this->getDeleteAccessControlEntrySql($ace->getId()));
unset($this->loadedAces[$ace->getId()]);
Expand All @@ -911,8 +905,7 @@ private function updateNewAceProperty($name, array $changes)

$sids = new \SplObjectStorage();
$classIds = new \SplObjectStorage();
for ($i = 0, $c = \count($new); $i < $c; ++$i) {
$ace = $new[$i];
foreach ($new as $i => $ace) {

if (null === $ace->getId()) {
if ($sids->contains($ace->getSecurityIdentity())) {
Expand Down Expand Up @@ -951,17 +944,13 @@ private function updateOldAceProperty($name, array $changes)
[$old, $new] = $changes;
$currentIds = [];

for ($i = 0, $c = \count($new); $i < $c; ++$i) {
$ace = $new[$i];

foreach ($new as $ace) {
if (null !== $ace->getId()) {
$currentIds[$ace->getId()] = true;
}
}

for ($i = 0, $c = \count($old); $i < $c; ++$i) {
$ace = $old[$i];

foreach($old as $ace) {
if (!isset($currentIds[$ace->getId()])) {
$this->connection->executeStatement($this->getDeleteAccessControlEntrySql($ace->getId()));
unset($this->loadedAces[$ace->getId()]);
Expand Down