src/Security/UserVoter.php line 14

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\Board;
  7. use App\Entity\HousingUnit;
  8. use App\Entity\Attachment;
  9. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  10. use Symfony\Component\Security\Core\Authorization\Voter\Voter as BaseVoter;
  11. use Symfony\Component\Security\Core\Security;
  12. class UserVoter extends BaseVoter
  13. {
  14.     // attributes for users
  15.     const VIEW 'userView';
  16.     const EDIT 'userEdit';
  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 === Voter::ATTACHMENT && $subject instanceof Attachment) return true;
  27.         if (!in_array($attribute, [self::VIEWself::EDIT])) {
  28.             return false;
  29.         }
  30.         if ($subject instanceof Chat) {return true;}
  31.         if ($subject instanceof HousingUnit) {return true;}
  32.         if ($subject instanceof Structure) {return true;}
  33.         if ($subject instanceof Board) {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 Chat) {return $this->voteOnChat($attribute$subject$user);}
  47.         if ($subject instanceof HousingUnit) {return $this->voteOnHousingUnit($attribute$subject$user);}
  48.         if ($subject instanceof Structure) {return $this->voteOnStructure($attribute$subject$user);}
  49.         if ($subject instanceof Attachment) {return $this->voteOnAttachment($attribute$subject$user);}
  50.         if ($subject instanceof Board) {return $this->voteOnBoard($attribute$subject$user);}
  51.     }
  52.     
  53.     private function voteOnChat($attributeChat $chatUser $user) : bool
  54.     {
  55.         if ($chat->isOwnerChat()) $users $chat->getHousingUnit()->getOwners();
  56.         else $users $chat->getHousingUnit()->getTenants();
  57.         return $users->contains($user);
  58.     }
  59.     private function voteOnBoard($attributeBoard $boardUser $user) : bool
  60.     {
  61.         return $this->voteOnStructure($attribute$board->getStructure(), $user);
  62.     }
  63.     private function voteOnAttachment($attributeAttachment $attachUser $user) : bool
  64.     {
  65.         if ($attach->getBoardMessage() !== null) return $this->voteOnBoard($attribute$attach->getBoardMessage()->getBoard(), $user);
  66.         if ($attach->getChatMessage() !== null) return $this->voteOnChat($attribute$attach->getChatMessage()->getChat(), $user);
  67.         return false;
  68.     }
  69.     
  70.     private function voteOnHousingUnit($attributeHousingUnit $huUser $user) : bool
  71.     {
  72.         return $hu->getUsers()->contains($user);
  73.     }
  74.     private function voteOnStructure($attributeStructure $structureUser $user) : bool
  75.     {
  76.         return $structure->getUsers()->contains($user);
  77.     }
  78.     
  79. }