src/AppBundle/Security/Voters/ProposalVoter.php line 18

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