Skip to content

Commit

Permalink
[Security] performance improvements of PermissionGrantingStrategy
Browse files Browse the repository at this point in the history
  • Loading branch information
schmittjoh authored and fabpot committed Feb 12, 2011
1 parent 19bbafc commit 9749da6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/Symfony/Component/Security/Acl/Domain/Acl.php
Expand Up @@ -232,7 +232,7 @@ public function isGranted(array $masks, array $securityIdentities, $administrati
*/
public function isSidLoaded($sids)
{
if (0 === count($this->loadedSids)) {
if (!$this->loadedSids) {
return true;
}

Expand Down
Expand Up @@ -30,8 +30,16 @@ class PermissionGrantingStrategy implements PermissionGrantingStrategyInterface
const ALL = 'all';
const ANY = 'any';

protected static $noAceException;
protected $auditLogger;

public function __construct()
{
if (null === static::$noAceException) {
static::$noAceException = new NoAceFoundException('No ACE.');
}
}

/**
* Sets the audit logger
*
Expand Down Expand Up @@ -62,16 +70,16 @@ public function isGranted(AclInterface $acl, array $masks, array $sids, $adminis
try {
$aces = $acl->getObjectAces();

if (0 === count($aces)) {
throw new NoAceFoundException('No applicable ACE was found.');
if (!$aces) {
throw static::$noAceException;
}

return $this->hasSufficientPermissions($acl, $aces, $masks, $sids, $administrativeMode);
} catch (NoAceFoundException $noObjectAce) {
$aces = $acl->getClassAces();

if (0 === count($aces)) {
throw new NoAceFoundException('No applicable ACE was found.');
if (!$aces) {
throw static::$noAceException;
}

return $this->hasSufficientPermissions($acl, $aces, $masks, $sids, $administrativeMode);
Expand All @@ -93,15 +101,15 @@ public function isFieldGranted(AclInterface $acl, $field, array $masks, array $s
try {
try {
$aces = $acl->getObjectFieldAces($field);
if (0 === count($aces)) {
throw new NoAceFoundException('No applicable ACE was found.');
if (!$aces) {
throw static::$noAceException;
}

return $this->hasSufficientPermissions($acl, $aces, $masks, $sids, $administrativeMode);
} catch (NoAceFoundException $noObjectAces) {
$aces = $acl->getClassFieldAces($field);
if (0 === count($aces)) {
throw new NoAceFoundException('No applicable ACE was found.');
if (!$aces) {
throw static::$noAceException;
}

return $this->hasSufficientPermissions($acl, $aces, $masks, $sids, $administrativeMode);
Expand Down Expand Up @@ -151,12 +159,8 @@ protected function hasSufficientPermissions(AclInterface $acl, array $aces, arra

foreach ($masks as $requiredMask) {
foreach ($sids as $sid) {
if (!$acl->isSidLoaded($sid)) {
throw new SidNotLoadedException(sprintf('The SID "%s" has not been loaded.', $sid));
}

foreach ($aces as $ace) {
if ($this->isAceApplicable($requiredMask, $sid, $ace)) {
if ($sid->equals($ace->getSecurityIdentity()) && $this->isAceApplicable($requiredMask, $ace)) {
if ($ace->isGranting()) {
if (!$administrativeMode && null !== $this->auditLogger) {
$this->auditLogger->logIfNeeded(true, $ace);
Expand All @@ -183,7 +187,7 @@ protected function hasSufficientPermissions(AclInterface $acl, array $aces, arra
return false;
}

throw new NoAceFoundException('No applicable ACE was found.');
throw static::$noAceException;
}

/**
Expand All @@ -203,17 +207,12 @@ protected function hasSufficientPermissions(AclInterface $acl, array $aces, arra
* Strategy EQUAL:
* The ACE will be considered applicable when the bitmasks are equal.
*
* @param SecurityIdentityInterface $sid
* @param integer $requiredMask
* @param EntryInterface $ace
* @param int $requiredMask
* @return Boolean
*/
protected function isAceApplicable($requiredMask, SecurityIdentityInterface $sid, EntryInterface $ace)
protected function isAceApplicable($requiredMask, EntryInterface $ace)
{
if (false === $ace->getSecurityIdentity()->equals($sid)) {
return false;
}

$strategy = $ace->getStrategy();
if (self::ALL === $strategy) {
return $requiredMask === ($ace->getMask() & $requiredMask);
Expand Down

0 comments on commit 9749da6

Please sign in to comment.