src/Modules/User/Security/CircleMemberVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Modules\User\Security;
  3. use App\Modules\User\Entity\CircleMember;
  4. use App\Modules\User\Entity\UserContract;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. use Symfony\Component\Security\Core\Security;
  8. class CircleMemberVoter extends Voter
  9. {
  10.     const CREATE_CIRCLE_MEMBER 'create_circle_member';
  11.     const UPDATE_CIRCLE_MEMBER 'update_circle_member';
  12.     const DELETE_CIRCLE_MEMBER 'delete_circle_member';
  13.     static $access = [
  14.         self::CREATE_CIRCLE_MEMBER,
  15.         self::UPDATE_CIRCLE_MEMBER,
  16.         self::DELETE_CIRCLE_MEMBER,
  17.     ];
  18.     private $security;
  19.     public function __construct(Security $security)
  20.     {
  21.         $this->security $security;
  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 (!in_array($attributeself::$access)) {
  34.             return false;
  35.         }
  36.         return true;
  37.     }
  38.     /**
  39.      * Perform a single access check operation on a given attribute, subject and token.
  40.      * It is safe to assume that $attribute and $subject already passed the "supports()" method check.
  41.      *
  42.      * @param string         $attribute
  43.      * @param mixed          $subject
  44.      *
  45.      * @param TokenInterface $token
  46.      *
  47.      * @return bool
  48.      */
  49.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  50.     {
  51.         $loginedUser $token->getUser();
  52.         if (!$loginedUser instanceof UserContract) {
  53.             // the user must be logged in; if not, deny access
  54.             return false;
  55.         }
  56.         $circleMember $subject;
  57.         switch ($attribute) {
  58.             case self::CREATE_CIRCLE_MEMBER:
  59.                 return $this->canCreate();
  60.             case self::UPDATE_CIRCLE_MEMBER:
  61.                 return $this->canUpdate($circleMember$loginedUser);
  62.             case self::DELETE_CIRCLE_MEMBER:
  63.                 return $this->canDelete();
  64.         }
  65.     }
  66.     private function canCreate()
  67.     {
  68.         if ($this->security->isGranted('ROLE_ADMIN')) {
  69.             return true;
  70.         }
  71.         return false;
  72.     }
  73.     private function canUpdate(CircleMember $circleMemberUserContract $loginedUser)
  74.     {
  75.         if ($this->security->isGranted('ROLE_ADMIN') ||
  76.             $circleMember->hasAdministrator($loginedUser)
  77.         ) {
  78.             return true;
  79.         }
  80.         return false;
  81.     }
  82.     private function canDelete()
  83.     {
  84.         if ($this->security->isGranted('ROLE_ADMIN')) {
  85.             return true;
  86.         }
  87.         return false;
  88.     }
  89. }