src/Security/ChangeLogVoter.php line 39

Open in your IDE?
  1. <?php
  2. //------------------------------------------------------------------------------
  3. // src/Security/ChangeLogVoter.php
  4. //------------------------------------------------------------------------------
  5. namespace App\Security;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. use Doctrine\Persistence\ManagerRegistry;
  10. use App\Entity\Access;
  11. use App\Entity\Security\Acl;
  12. use App\Entity\Log\ChangeLog;
  13. class ChangeLogVoter extends Voter
  14. {
  15.     //--------------------------------------------------------------------------------
  16.     // is_granted constants
  17.     const ADD "add_change_log";
  18.     const LISTING "list_change_logs";
  19.     const VIEW "view_change_log";
  20.     const EDIT "edit_change_log";
  21.     const IS_GRANTED_CONSTANTS = array(
  22.         self::ADD,
  23.         self::LISTING,
  24.         self::VIEW,
  25.         self::EDIT,
  26.     );
  27.     //--------------------------------------------------------------------------------
  28.     // acl constants
  29.     // none
  30.     //--------------------------------------------------------------------------------
  31.     public function __construct(AccessDecisionManagerInterface $accessDecisionManagerManagerRegistry $doctrine)
  32.     {
  33.         $this->accessDecisionManager $accessDecisionManager;
  34.         $this->em $doctrine->getManager();
  35.     }
  36.     // Plan.io Task #4453 [See AccessVoter for details]
  37.     public function supportsAttribute(string $attribute): bool
  38.     {
  39.         return in_array($attributeself::IS_GRANTED_CONSTANTStrue);
  40.     }
  41.     protected function supports(string $attribute$subject): bool
  42.     {
  43.         // if the attribute isn't one we support, return false
  44.         if (!in_array($attributeself::IS_GRANTED_CONSTANTS))
  45.         {
  46.             return false;
  47.         }
  48.         // only vote on Bug objects inside this voter
  49.         if ($subject !== null && !$subject instanceof ChangeLog)
  50.         {
  51.             return false;
  52.         }
  53.         return true;
  54.     }
  55.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  56.     {
  57.         // ROLE_USER and ROLE_ADMIN
  58.         $user $token->getUser();
  59.         if (!$user instanceof Access)
  60.         {
  61.             // the user must be logged in; if not, deny access
  62.             return false;
  63.         }
  64.         $changeLog $subject;
  65.         switch ($attribute)
  66.         {
  67.             case self::ADD:
  68.             {
  69.                 return $this->accessDecisionManager->decide($token, ['is_admin']);
  70.             }
  71.             case self::LISTING:
  72.             {
  73.                 return $this->accessDecisionManager->decide($token, ['IS_AUTHENTICATED_FULLY']);
  74.             }
  75.             case self::VIEW:
  76.             {
  77.                 return $this->canView($changeLog$token);
  78.             }
  79.             case self::EDIT:
  80.             {
  81.                 return $this->accessDecisionManager->decide($token, ['is_admin']);
  82.             }
  83.         }
  84.         throw new \LogicException('This code should not be reached!');
  85.     }
  86.     private function canView(ChangeLog $changeLog$token)
  87.     {
  88.         if ($this->accessDecisionManager->decide($token, ['is_admin']))
  89.         {
  90.             return true;
  91.         }
  92.         // For ROLE_USER check if the change log is published
  93.         if ($this->accessDecisionManager->decide($token, ['ROLE_USER']))
  94.         {
  95.             if ($changeLog->isPublished())
  96.             {
  97.                 return true;
  98.             }
  99.         }
  100.         // All hope is lost
  101.         return false;
  102.     }
  103. }