Bladeren bron

tweak AclSecurityHandler to return false on AuthenticationCredentialsNotFoundException

Thomas Rabaix 14 jaren geleden
bovenliggende
commit
ba17ba305b
2 gewijzigde bestanden met toevoegingen van 53 en 18 verwijderingen
  1. 8 1
      Security/Handler/AclSecurityHandler.php
  2. 45 17
      Tests/Security/Handler/AclSecurityHandlerTest.php

+ 8 - 1
Security/Handler/AclSecurityHandler.php

@@ -12,6 +12,7 @@
 namespace Sonata\AdminBundle\Security\Handler;
 
 use Symfony\Component\Security\Core\SecurityContextInterface;
+use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException;
 use Sonata\AdminBundle\Admin\AdminInterface;
 
 class AclSecurityHandler implements SecurityHandlerInterface
@@ -26,7 +27,13 @@ class AclSecurityHandler implements SecurityHandlerInterface
      */
     public function isGranted($attributes, $object = null)
     {
-        return $this->securityContext->isGranted($attributes, $object);
+        try {
+            return $this->securityContext->isGranted($attributes, $object);
+        } catch (AuthenticationCredentialsNotFoundException $e) {
+            return false;
+        } catch (\Exception $e) {
+            throw $e;
+        }
     }
 
     /**

+ 45 - 17
Tests/Security/Handler/AclSecurityHandlerTest.php

@@ -11,6 +11,7 @@
 namespace Sonata\AdminBundle\Tests\Admin\Security\Acl\Permission;
 
 use Sonata\AdminBundle\Security\Handler\AclSecurityHandler;
+use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException;
 
 class AclSecurityHandlerTest extends \PHPUnit_Framework_TestCase
 {
@@ -37,26 +38,53 @@ class AclSecurityHandlerTest extends \PHPUnit_Framework_TestCase
         $this->assertFalse($handler->isGranted('TOTO'));
     }
 
-  public function testBuildInformation()
-  {
-      $informations = array(
-          'EDIT' => array('EDIT')
-      );
+    public function testBuildInformation()
+    {
+        $informations = array(
+            'EDIT' => array('EDIT')
+        );
+
+        $securityContext = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
+        $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
+        $admin->expects($this->once())
+            ->method('getCode')
+            ->will($this->returnValue('test'));
+
+        $admin->expects($this->once())
+            ->method('getSecurityInformation')
+            ->will($this->returnValue($informations));
+
+        $handler = new AclSecurityHandler($securityContext);
+
+        $results = $handler->buildSecurityInformation($admin);
+
+        $this->assertArrayHasKey('ROLE_TEST_EDIT', $results);
+    }
+
+    public function testWithAuthenticationCredentialsNotFoundException()
+    {
+        $securityContext = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
+        $securityContext->expects($this->any())
+            ->method('isGranted')
+            ->will($this->throwException(new AuthenticationCredentialsNotFoundException('FAIL')));
 
-      $securityContext = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
-      $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
-      $admin->expects($this->once())
-          ->method('getCode')
-          ->will($this->returnValue('test'));
+        $handler = new AclSecurityHandler($securityContext);
 
-      $admin->expects($this->once())
-          ->method('getSecurityInformation')
-          ->will($this->returnValue($informations));
+        $this->assertFalse($handler->isGranted('raise exception'));
+    }
 
-      $handler = new AclSecurityHandler($securityContext);
+    /**
+     * @expectedException RuntimeException
+     */
+    public function testWithNonAuthenticationCredentialsNotFoundException()
+    {
+        $securityContext = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
+        $securityContext->expects($this->any())
+            ->method('isGranted')
+            ->will($this->throwException(new \RunTimeException('FAIL')));
 
-      $results = $handler->buildSecurityInformation($admin);
+        $handler = new AclSecurityHandler($securityContext);
 
-      $this->assertArrayHasKey('ROLE_TEST_EDIT', $results);
-  }
+        $this->assertFalse($handler->isGranted('raise exception'));
+    }
 }