vendor/emprel/login-bundle/Security/Voter/ResourceVoter.php line 21

Open in your IDE?
  1. <?php
  2. /*
  3.  * Este arquivo é parte do package Login Bundle.
  4.  *
  5.  * (c) Laino Santos <lainosantos@recife.pe.gov.br>
  6.  *
  7.  * Para informações completas sobre copyright e licença, por favor ver o
  8.  * arquivo LICENCE distribuído juntamente com este código.
  9.  */
  10. namespace Emprel\Login\Bundle\Security\Voter;
  11. use Emprel\OpenIdUmaClient\Exceptions\NotAuthorizedException;
  12. use Emprel\OpenIdUmaClient\Uma\Representation\ResourceRepresentation;
  13. use Emprel\Login\Bundle\Security\User\LoginUser;
  14. use Emprel\Login\Bundle\Security\Voter\Exception\ResourceNotFoundException;
  15. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  16. use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
  17. class ResourceVoter implements VoterInterface
  18. {
  19.     /**
  20.      * @var array
  21.      */
  22.     protected static $cache = [];
  23.     public function vote(TokenInterface $token$subject, array $attributes)
  24.     {
  25.         if (!$token->getUser() instanceof LoginUser || !$subject instanceof ResourceVoterSubject) {
  26.             return self::ACCESS_ABSTAIN;
  27.         } elseif (($resource $this->getResource($token$subject)) === null) {
  28.             return self::ACCESS_DENIED;
  29.         } elseif (!$this->supports($token$subject$attributes$resource)) {
  30.             return self::ACCESS_ABSTAIN;
  31.         }
  32.         if ($this->voteOnResource($token$subject$attributes$resource)) {
  33.             return self::ACCESS_GRANTED;
  34.         } else {
  35.             return self::ACCESS_DENIED;
  36.         }
  37.     }
  38.     protected function getResource(TokenInterface $tokenResourceVoterSubject $subject): ResourceRepresentation
  39.     {
  40.         $cacheKey md5($subject->getId() . ':' $subject->getLocalId() . ':' $subject->getUri() . ':' $subject->getType());
  41.         if (!$subject->isAllowCache() || !isset(static::$cache[$cacheKey])) {
  42.             $resources $subject->getUmaClient()->getResources(
  43.                 true$subject->getId(), $subject->getLocalId(), $subject->getUri(), null$subject->getType()
  44.             );
  45.             static::$cache[$cacheKey] = $resources;
  46.         } else {
  47.             $resources = static::$cache[$cacheKey];
  48.         }
  49.         if (count($resources) !== 1) {
  50.             throw new ResourceNotFoundException();
  51.         }
  52.         return $resources[0];
  53.     }
  54.     protected function supports(TokenInterface $tokenResourceVoterSubject $subject$scopesResourceRepresentation $resource): bool
  55.     {
  56.         return true;
  57.     }
  58.     protected function voteOnResource(TokenInterface $tokenResourceVoterSubject $subject$scopesResourceRepresentation $resource): bool
  59.     {
  60.         $context $subject->getPolicyContext();
  61.         array_multisort($context);
  62.         array_multisort($scopes);
  63.         $cacheKey md5(serialize($context));
  64.         try {
  65.             if (!$subject->isAllowCache() || !isset(static::$cache[$cacheKey])) {
  66.                 $permissions $subject->getUmaClient()->getPermissions($token->getUser()->getTokenWrapper()->getAccessToken(), $context);
  67.                 static::$cache[$cacheKey] = $permissions;
  68.             } else {
  69.                 $permissions = static::$cache[$cacheKey];
  70.             }
  71.         } catch (NotAuthorizedException $exception) {
  72.             static::$cache[$cacheKey] = false;
  73.             return false;
  74.         }
  75.         if ($permissions === false) {
  76.             return false;
  77.         }
  78.         foreach ($permissions as $permission) {
  79.             if ($permission->resourceId === $resource->id) {
  80.                 foreach ($scopes as $scope) {
  81.                     if ($scope !== null && in_array($scope$permission->scopes)) {
  82.                         return true;
  83.                     }
  84.                 }
  85.                 // Decisão unânime faz com que as chamadas com atributos vazios não chegem ao voter, por isso passar null
  86.                 return empty($scopes) || (count($scopes) === && $scopes[0] === null);
  87.             }
  88.         }
  89.         return false;
  90.     }
  91.     
  92.     public static function clearCache(): void {
  93.         static::$cache = [];
  94.     }
  95. }