vendor/easycorp/easyadmin-bundle/src/Security/AuthorizationChecker.php line 23

Open in your IDE?
  1. <?php
  2. namespace EasyCorp\Bundle\EasyAdminBundle\Security;
  3. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  4. use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException;
  5. /**
  6.  * A slightly modified authorization checker optimized for performance and which
  7.  * doesn't trigger exceptions when security is not enabled.
  8.  *
  9.  * @author Javier Eguiluz <javier.eguiluz@gmail.com>
  10.  */
  11. class AuthorizationChecker implements AuthorizationCheckerInterface
  12. {
  13.     private $authorizationChecker;
  14.     public function __construct(AuthorizationCheckerInterface $authorizationChecker)
  15.     {
  16.         $this->authorizationChecker $authorizationChecker;
  17.     }
  18.     public function isGranted($permission$subject null): bool
  19.     {
  20.         // this check is needed for performance reasons because most of the times permissions
  21.         // won't be set, so this function must return as early as possible in those cases
  22.         if (empty($permission)) {
  23.             return true;
  24.         }
  25.         try {
  26.             return $this->authorizationChecker->isGranted($permission$subject);
  27.         } catch (AuthenticationCredentialsNotFoundException $e) {
  28.             // this exception happens when there's no security configured in the application
  29.             // that's a valid scenario for EasyAdmin, where security is not required (although very common)
  30.             return true;
  31.         }
  32.     }
  33. }