src/AppBundle/Security/Voters/ProposalCommentVoter.php line 20

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by SAWIT Mateusz Miklewski.
  4.  * User: Mateusz
  5.  * Date: 2018-03-13
  6.  * Time: 13:10
  7.  */
  8. namespace AppBundle\Security\Voters;
  9. use AppBundle\Entity\Policy\Proposal;
  10. use AppBundle\Entity\Policy\ProposalComment;
  11. use AppBundle\Entity\User\User;
  12. use LogicException;
  13. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  14. use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
  15. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  16. class ProposalCommentVoter extends Voter
  17. {
  18.     const VIEW 'view';
  19.     const EDIT 'edit';
  20.     private $decisionManager;
  21.     public function __construct(AccessDecisionManagerInterface $decisionManager)
  22.     {
  23.         $this->decisionManager $decisionManager;
  24.     }
  25.     /**
  26.      * Determines if the attribute and subject are supported by this voter.
  27.      *
  28.      * @param string $attribute An attribute
  29.      * @param mixed $subject The subject to secure, e.g. an object the user wants to access or any other PHP type
  30.      *
  31.      * @return bool True if the attribute and subject are supported, false otherwise
  32.      */
  33.     protected function supports($attribute$subject)
  34.     {
  35.         // if the attribute isn't one we support, return false
  36.         if (!in_array($attribute, array(self::VIEWself::EDIT))) {
  37.             return false;
  38.         }
  39.         // only vote on ProposalComment objects inside this voter
  40.         if (!$subject instanceof ProposalComment) {
  41.             return false;
  42.         }
  43.         return true;
  44.     }
  45.     /**
  46.      * Perform a single access check operation on a given attribute, subject and token.
  47.      * It is safe to assume that $attribute and $subject already passed the "supports()" method check.
  48.      *
  49.      * @param string $attribute
  50.      * @param mixed $subject
  51.      * @param TokenInterface $token
  52.      *
  53.      * @return bool
  54.      */
  55.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  56.     {
  57.         if ($this->decisionManager->decide($token, ['ROLE_ADMIN'])) {
  58.             return true;
  59.         }
  60.         $user $token->getUser();
  61.         if (!$user instanceof User) {
  62.             // the user must be logged in; if not, deny access
  63.             return false;
  64.         }
  65.         // you know $subject is a ProposalComment object, thanks to supports
  66.         /** @var ProposalComment $comment */
  67.         $comment $subject;
  68.         switch ($attribute) {
  69.             case self::VIEW:
  70.                 return $this->canView($comment$token);
  71.             case self::EDIT:
  72.                 return $this->canEdit($comment$token);
  73.         }
  74.         throw new LogicException('This code should not be reached!');
  75.     }
  76.     private function canView(ProposalComment $commentTokenInterface $token) {
  77.         if ($this->decisionManager->decide($token, [ProposalVoter::VIEW], $comment->getProposal())) {
  78.             return true;
  79.         }
  80.         return false;
  81.     }
  82.     private function canEdit(ProposalComment $commentTokenInterface $token) {
  83.         if ($comment->getCreatedBy()->getId() == $token->getUser()->getId()) {
  84.             return true;
  85.         }
  86.         return false;
  87.     }
  88. }