Просмотр исходного кода

added extra exception if only a partial result is found

Johannes M. Schmitt 14 лет назад
Родитель
Сommit
0449dbdc5d

+ 9 - 1
src/Symfony/Component/Security/Acl/Dbal/AclProvider.php

@@ -11,6 +11,7 @@ use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
 use Symfony\Component\Security\Acl\Domain\RoleSecurityIdentity;
 use Symfony\Component\Security\Acl\Domain\UserSecurityIdentity;
 use Symfony\Component\Security\Acl\Exception\AclNotFoundException;
+use Symfony\Component\Security\Acl\Exception\NotAllAclsFoundException;
 use Symfony\Component\Security\Acl\Model\AclCacheInterface;
 use Symfony\Component\Security\Acl\Model\AclProviderInterface;
 use Symfony\Component\Security\Acl\Model\ObjectIdentityInterface;
@@ -184,7 +185,14 @@ class AclProvider implements AclProviderInterface
         // check that we got ACLs for all the identities
         foreach ($oids as $oid) {
             if (!$result->contains($oid)) {
-                throw new AclNotFoundException(sprintf('No ACL found for %s.', $oid));
+                if (1 === count($oids)) {
+                    throw new AclNotFoundException(sprintf('No ACL found for %s.', $oid));
+                }
+
+                $partialResultException = new NotAllAclsFoundException('The provider could not find ACLs for all object identities.');
+                $partialResultException->setPartialResult($result);
+
+                throw $partialResultException;
             }
         }
 

+ 38 - 0
src/Symfony/Component/Security/Acl/Exception/NotAllAclsFoundException.php

@@ -0,0 +1,38 @@
+<?php
+
+namespace Symfony\Component\Security\Acl\Exception;
+
+/**
+ * This exception is thrown when you have requested ACLs for multiple object
+ * identities, but the AclProvider implementation failed to find ACLs for all
+ * identities.
+ *
+ * This exception contains the partial result.
+ *
+ * @author Johannes M. Schmitt <schmittjoh@gmail.com>
+ */
+class NotAllAclsFoundException extends AclNotFoundException
+{
+    protected $partialResult;
+
+    /**
+     * Sets the partial result
+     *
+     * @param \SplObjectStorage $result
+     * @return void
+     */
+    public function setPartialResult(\SplObjectStorage $result)
+    {
+        $this->partialResult = $result;
+    }
+
+    /**
+     * Returns the partial result
+     *
+     * @return \SplObjectStorage
+     */
+    public function getPartialResult()
+    {
+        return $this->partialResult;
+    }
+}

+ 12 - 4
tests/Symfony/Tests/Component/Security/Acl/Dbal/AclProviderTest.php

@@ -26,16 +26,24 @@ class AclProviderTest extends \PHPUnit_Framework_TestCase
         $this->getProvider()->findAcl(new ObjectIdentity('foo', 'foo'));
     }
 
-    /**
-     * @expectedException Symfony\Component\Security\Acl\Exception\AclNotFoundException
-     */
     public function testFindAclsThrowsExceptionUnlessAnACLIsFoundForEveryOID()
     {
         $oids = array();
         $oids[] = new ObjectIdentity('1', 'foo');
         $oids[] = new ObjectIdentity('foo', 'foo');
 
-        $this->getProvider()->findAcls($oids);
+        try {
+            $this->getProvider()->findAcls($oids);
+
+            $this->fail('Provider did not throw an expected exception.');
+        } catch (\Exception $ex) {
+            $this->assertInstanceOf('Symfony\Component\Security\Acl\Exception\AclNotFoundException', $ex);
+            $this->assertInstanceOf('Symfony\Component\Security\Acl\Exception\NotAllAclsFoundException', $ex);
+
+            $partialResult = $ex->getPartialResult();
+            $this->assertTrue($partialResult->contains($oids[0]));
+            $this->assertFalse($partialResult->contains($oids[1]));
+        }
     }
 
     public function testFindAcls()