src/Modules/User/Security/PartnerVoter.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Modules\User\Security;
  3. use App\Modules\User\Entity\Partner;
  4. use App\Modules\User\Entity\User;
  5. use App\Modules\User\Entity\UserContract;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. use Symfony\Component\Security\Core\Security;
  9. class PartnerVoter extends Voter
  10. {
  11.     const CREATE_PARTNER 'create_partner';
  12.     const UPDATE_PARTNER 'update_partner';
  13.     const DELETE_PARTNER 'delete_partner';
  14.     static $access = [
  15.         self::CREATE_PARTNER,
  16.         self::UPDATE_PARTNER,
  17.         self::DELETE_PARTNER,
  18.     ];
  19.     private $security;
  20.     public function __construct(Security $security)
  21.     {
  22.         $this->security $security;
  23.     }
  24.     /**
  25.      * Determines if the attribute and subject are supported by this voter.
  26.      *
  27.      * @param string $attribute An attribute
  28.      * @param mixed  $subject   The subject to secure, e.g. an object the user wants to access or any other PHP type
  29.      *
  30.      * @return bool True if the attribute and subject are supported, false otherwise
  31.      */
  32.     protected function supports($attribute$subject)
  33.     {
  34.         if (!in_array($attributeself::$access)) {
  35.             return false;
  36.         }
  37.         return true;
  38.     }
  39.     /**
  40.      * Perform a single access check operation on a given attribute, subject and token.
  41.      * It is safe to assume that $attribute and $subject already passed the "supports()" method check.
  42.      *
  43.      * @param string         $attribute
  44.      * @param mixed          $subject
  45.      *
  46.      * @param TokenInterface $token
  47.      *
  48.      * @return bool
  49.      */
  50.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  51.     {
  52.         $loginedUser $token->getUser();
  53.         if (!$loginedUser instanceof UserContract) {
  54.             // the user must be logged in; if not, deny access
  55.             return false;
  56.         }
  57.         
  58.         $partner $subject;
  59.         switch ($attribute) {
  60.             case self::CREATE_PARTNER:
  61.                 return $this->canCreate();
  62.             case self::UPDATE_PARTNER:
  63.                 return $this->canUpdate($partner$loginedUser);
  64.             case self::DELETE_PARTNER:
  65.                 return $this->canDelete();
  66.         }
  67.     }
  68.     private function canCreate()
  69.     {
  70.         if ($this->security->isGranted('ROLE_ADMIN')) {
  71.             return true;
  72.         }
  73.         return false;
  74.     }
  75.     private function canUpdate(Partner $partnerUser $loggedInUser)
  76.     {
  77.         if ($this->security->isGranted('ROLE_ADMIN') ||
  78.             $partner->hasAdministrator($loggedInUser)
  79.         ) {
  80.             return true;
  81.         }
  82.         return false;
  83.     }
  84.     private function canDelete()
  85.     {
  86.         if ($this->security->isGranted('ROLE_ADMIN')) {
  87.             return true;
  88.         }
  89.         return false;
  90.     }
  91. }