src/Security/Voter/ForumMessageVoter.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Forum;
  4. use App\Entity\ForumAccess;
  5. use App\Entity\ForumMessage;
  6. use App\Entity\User;
  7. use App\EntityManager\ForumAccessManager;
  8. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  9. use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
  10. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  11. use Symfony\Component\Security\Core\Security;
  12. class ForumMessageVoter extends Voter
  13. {
  14.     const VIEW       'view';
  15.     const EDIT       'edit';
  16.     const REPLY      'reply';
  17.     const VOTE       'vote';
  18.     const MODERATE   'moderate';
  19.     const REPORT     'report';
  20.     const DELETE     'delete';
  21.     const TRACK      'track';
  22.     const VIEW_LIKES 'viewLikes';
  23.     // 5 minutes
  24.     const EDIT_TIME_LIMIT 3600 5;
  25.     protected $security;
  26.     protected $forumAccessManager;
  27.     protected $userForumAccess null;
  28.     protected $forumAccess null;
  29.     public function __construct(Security $securityForumAccessManager $forumAccessManager)
  30.     {
  31.         $this->security $security;
  32.         $this->forumAccessManager $forumAccessManager;
  33.     }
  34.     protected function supports($attribute$subject)
  35.     {
  36.         if (!in_array(
  37.             $attribute,
  38.             [
  39.                 self::VIEW,
  40.                 self::EDIT,
  41.                 self::REPLY,
  42.                 self::VOTE,
  43.                 self::MODERATE,
  44.                 self::REPORT,
  45.                 self::DELETE,
  46.                 self::TRACK,
  47.                 self::VIEW_LIKES,
  48.             ]
  49.         )) {
  50.             return false;
  51.         }
  52.         if (!$subject instanceof ForumMessage) {
  53.             return false;
  54.         }
  55.         return true;
  56.     }
  57.     /**
  58.      * @param string             $attribute
  59.      * @param mixed|ForumMessage $subject
  60.      * @param TokenInterface     $token
  61.      * @return bool
  62.      */
  63.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  64.     {
  65.         $user $token->getUser();
  66.         if (!$user instanceof User) {
  67.             return false;
  68.         }
  69.         if (is_null($this->forumAccess)) {
  70.             $this->userForumAccess $this->forumAccessManager->getForumAccessForCurrentUser($subject->getForum());
  71.             $this->forumAccess $this->forumAccessManager->getForumAccess($subject->getForum());
  72.         }
  73.         if ($this->security->isGranted(User::ROLE_SUPER_ADMIN$user)) {
  74.             return true;
  75.         }
  76.         switch ($attribute) {
  77.             case self::VIEW:
  78.                 return $this->canView($subject$user$token);
  79.             case self::EDIT:
  80.                 return $this->canEdit($subject$user$token);
  81.             case self::REPLY:
  82.                 return $this->canReply($subject$user$token);
  83.             case self::VOTE:
  84.                 return $this->canVote($subject$user$token);
  85.             case self::MODERATE:
  86.                 return $this->canModerate($subject$user$token);
  87.             case self::REPORT:
  88.                 return $this->canReport($subject$user$token);
  89.             case self::DELETE:
  90.                 return $this->canDelete($subject$user$token);
  91.             case self::TRACK:
  92.                 return $this->canTrack($subject$user$token);
  93.             case self::VIEW_LIKES:
  94.                 return $this->canViewLikes($subject$user$token);
  95.         }
  96.         throw new \LogicException('This code should not be reached!');
  97.     }
  98.     private function canView(ForumMessage $messageUser $userTokenInterface $token)
  99.     {
  100.         return (
  101.             ($this->forumAccess instanceof ForumAccess && $this->forumAccess->getListMessage()) ||
  102.             ($this->userForumAccess instanceof ForumAccess && $this->userForumAccess->getListMessage())
  103.         );
  104.     }
  105.     private function canEdit(ForumMessage $messageUser $userTokenInterface $token)
  106.     {
  107.         return (
  108.             (
  109.                 $message->getCreatedBy() == $user &&
  110.                 $message->getCreatedAt()->getTimestamp() + self::EDIT_TIME_LIMIT time()
  111.             ) ||
  112.             ($this->userForumAccess instanceof ForumAccess && $this->userForumAccess->getModMessage())
  113.         );
  114.     }
  115.     private function canReply(ForumMessage $messageUser $userTokenInterface $token)
  116.     {
  117.         $topic $message->getTopicMessage();
  118.         if ($topic->isLocked() && !$this->security->isGranted(User::ROLE_SUPER_ADMIN)) {
  119.             return false;
  120.         }
  121.         return (
  122.             ($this->forumAccess instanceof ForumAccess && $this->forumAccess->getAddMessage()) ||
  123.             ($this->userForumAccess instanceof ForumAccess && $this->userForumAccess->getAddMessage())
  124.         );
  125.     }
  126.     private function canModerate(ForumMessage $messageUser $userTokenInterface $token)
  127.     {
  128.         return $this->userForumAccess instanceof ForumAccess && $this->userForumAccess->getModMessage();
  129.     }
  130.     private function canReport(ForumMessage $messageUser $userTokenInterface $token)
  131.     {
  132.         return $this->canReply($message$user$token);
  133.     }
  134.     private function canVote(ForumMessage $messageUser $userTokenInterface $token)
  135.     {
  136.         return $this->canReply($message$user$token);
  137.     }
  138.     private function canDelete(ForumMessage $messageUser $userTokenInterface $token)
  139.     {
  140.         return $this->userForumAccess instanceof ForumAccess && $this->userForumAccess->getDeleteMessage();
  141.     }
  142.     private function canTrack(ForumMessage $messageUser $userTokenInterface $token)
  143.     {
  144.         return true;
  145.     }
  146.     private function canViewLikes(ForumMessage $messageUser $userTokenInterface $token)
  147.     {
  148.         // only super admin can do that, if that code is reached, then the user can't.
  149.         return false;
  150.     }
  151. }