src/Modules/Contract/Security/ContractVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Modules\Contract\Security;
  3. use App\Modules\Contract\Entity\Contract;
  4. use App\Modules\Contract\Service\ContractAccessService;
  5. use App\Modules\User\Entity\User;
  6. use App\Modules\User\Entity\UserContract;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. use Symfony\Component\Security\Core\Security;
  10. class ContractVoter extends Voter
  11. {
  12.     const CREATE_CONTRACT 'create_contract';
  13.     const UPDATE_CONTRACT 'update_contract';
  14.     const GET_CONTRACT 'get_contract';
  15.     static $access = [
  16.         self::CREATE_CONTRACT,
  17.         self::UPDATE_CONTRACT,
  18.         self::GET_CONTRACT,
  19.     ];
  20.     private $security;
  21.     /**
  22.      * @var ContractAccessService
  23.      */
  24.     private $contractAccessService;
  25.     public function __construct(Security $securityContractAccessService $contractAccessService)
  26.     {
  27.         $this->security $security;
  28.         $this->contractAccessService $contractAccessService;
  29.     }
  30.     /**
  31.      * Determines if the attribute and subject are supported by this voter.
  32.      *
  33.      * @param string $attribute An attribute
  34.      * @param mixed  $subject   The subject to secure, e.g. an object the user wants to access or any other PHP type
  35.      *
  36.      * @return bool True if the attribute and subject are supported, false otherwise
  37.      */
  38.     protected function supports($attribute$subject)
  39.     {
  40.         if (!in_array($attributeself::$access)) {
  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.      *
  52.      * @param TokenInterface $token
  53.      *
  54.      * @return bool
  55.      */
  56.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  57.     {
  58.         $loginedUser $token->getUser();
  59.         if (!$loginedUser instanceof UserContract) {
  60.             // the user must be logged in; if not, deny access
  61.             return false;
  62.         }
  63.         $contract $subject;
  64.         switch ($attribute) {
  65.             case self::CREATE_CONTRACT:
  66.                 return $this->canCreate();
  67.             case self::UPDATE_CONTRACT:
  68.                 return $this->canUpdate($contract$loginedUser);
  69.             case self::GET_CONTRACT:
  70.                 return $this->canGet($contract$loginedUser);
  71.         }
  72.     }
  73.     private function canCreate()
  74.     {
  75.         if ($this->security->isGranted('ROLE_CIRCLE_MEMBER_ADMINISTRATOR') ||
  76.             $this->security->isGranted('ROLE_PARTNER_ADMINISTRATOR')
  77.         ) {
  78.             return true;
  79.         }
  80.         return false;
  81.     }
  82.     private function canUpdate(Contract $contractUser $loggedInUser)
  83.     {
  84.         if ($this->contractAccessService->userHasAccess($contract$loggedInUser)) {
  85.             return true;
  86.         }
  87.         return false;
  88.     }
  89.     private function canGet(Contract $contractUser $loggedInUser)
  90.     {
  91.         if ($this->security->isGranted('ROLE_ADMIN') ||
  92.             $this->contractAccessService->userHasAccess($contract$loggedInUser)
  93.         ) {
  94.             return true;
  95.         }
  96.         return false;
  97.     }
  98. }