src/Security/Voter.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use App\Entity\Structure;
  4. use App\Entity\User;
  5. use App\Entity\Chat;
  6. use App\Entity\HousingUnit;
  7. use App\Entity\Attachment;
  8. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  9. use Symfony\Component\Security\Core\Authorization\Voter\Voter as BaseVoter;
  10. use Symfony\Component\Security\Core\Security;
  11. class Voter extends BaseVoter
  12. {
  13.     // attributes for administrators
  14.     const VIEW 'view';
  15.     const EDIT 'edit';
  16.     const ATTACHMENT 'attachment';
  17.     private $security;
  18.     public function __construct(Security $security)
  19.     {
  20.         $this->security $security;
  21.     }
  22.     
  23.     protected function supports(string $attribute$subject): bool
  24.     {
  25.         // if the attribute isn't one we support, return false
  26.         if ($attribute === self::ATTACHMENT && $subject instanceof Attachment) return true;
  27.         if (!in_array($attribute, [self::VIEWself::EDIT])) {
  28.             return false;
  29.         }
  30.         if ($subject instanceof Structure) {return true;}
  31.         if ($subject instanceof Chat) {return true;}
  32.         if ($subject instanceof HousingUnit) {return true;}
  33.         if ($subject instanceof Attachment) {return true;}
  34.         return false;
  35.     }
  36.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  37.     
  38.         $user $token->getUser();
  39.         if (!$user instanceof User) {
  40.             // the user must be logged in; if not, deny access
  41.             return false;
  42.         }
  43.         if ($this->security->isGranted('ROLE_SUPER_ADMIN')) {
  44.             return true;
  45.         }
  46.         if ($subject instanceof Structure) {return $this->voteOnStructure($attribute$subject$user);}
  47.         if ($subject instanceof Chat) {return $this->voteOnChat($attribute$subject$user);}
  48.         if ($subject instanceof HousingUnit) {return $this->voteOnHousingUnit($attribute$subject$user);}
  49.         if ($subject instanceof Attachment) {return $this->voteOnAttachment($attribute$subject$user);}
  50.     }
  51.     
  52.     private function voteOnStructure($attributeStructure $structureUser $user) : bool
  53.     {
  54.         switch ($attribute) {
  55.             case self::VIEW:
  56.                 return $this->canViewStructure($structure$user);
  57.             case self::EDIT:
  58.                 return $this->canEditStructure($structure$user);
  59.         }
  60.         throw new \LogicException('This code should not be reached!');
  61.     }
  62.     private function canViewStructure(Structure $structureUser $user): bool
  63.     {
  64.         // if they can edit, they can view
  65.         if ($this->canEditStructure($structure$user)) {
  66.             return true;
  67.         }
  68.         // the object could have, for example, a method `isPrivate()`
  69.         //return !$post->isPrivate();
  70.         return false;
  71.     }
  72.     private function canEditStructure(Structure $structureUser $user): bool
  73.     {
  74.         $userFirm $user->getFirm();
  75.         $structureFirm $structure->getFirm();
  76.         return $userFirm === $structureFirm;
  77.     }
  78.     
  79.     private function voteOnChat($attributeChat $chatUser $user) : bool
  80.     {
  81.         $userFirm $user->getFirm();
  82.         $chatFirm $chat->getHousingUnit()->getStructure()->getFirm();
  83.         return $userFirm === $chatFirm;
  84.     }
  85.     private function voteOnHousingUnit($attributeHousingUnit $huUser $user) : bool
  86.     {
  87.         $userFirm $user->getFirm();
  88.         $huFirm $hu->getStructure()->getFirm();
  89.         return $userFirm === $huFirm;
  90.     }
  91.     private function voteOnAttachment($attributeAttachment $attachUser $user) : bool
  92.     {
  93.         if ($attach->getBoardMessage() !== null) return $this->canViewStructure($attach->getBoardMessage()->getBoard()->getStructure(), $user);
  94.         if ($attach->getChatMessage() !== null) return $this->voteOnChat($attribute$attach->getChatMessage()->getChat(), $user);
  95.         return false;
  96.     }
  97.     
  98. }