src/Security/ProjectPersonalChatVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use App\Modules\Chat\Entity\Project\Personal;
  4. use App\Entity\User;
  5. use App\Repository\AssignmentRepository;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. class ProjectPersonalChatVoter extends Voter
  9. {
  10.     const ATTRIBUTES = [self::ATTR_ASSIGNEDself::ATTR_WRITE];
  11.     const ATTR_ASSIGNED 'CHAT_ASSIGNED';
  12.     const ATTR_WRITE    'CHAT_WRITE';
  13.     /**
  14.      * @var AssignmentRepository
  15.      */
  16.     private $assignmentRepository;
  17.     public function __construct(AssignmentRepository $assignmentRepository)
  18.     {
  19.         $this->assignmentRepository $assignmentRepository;
  20.     }
  21.     /**
  22.      * Determines if the attribute and subject are supported by this voter.
  23.      *
  24.      * @param string $attribute An attribute
  25.      * @param mixed $subject The subject to secure, e.g. an object the user wants to access or any other PHP type
  26.      *
  27.      * @return bool True if the attribute and subject are supported, false otherwise
  28.      */
  29.     protected function supports($attribute$subject)
  30.     {
  31.         return $subject instanceof Personal && in_array($attribute, static::ATTRIBUTES);
  32.     }
  33.     /**
  34.      * Perform a single access check operation on a given attribute, subject and token.
  35.      * It is safe to assume that $attribute and $subject already passed the "supports()" method check.
  36.      *
  37.      * @param string $attribute
  38.      * @param mixed $subject
  39.      * @param TokenInterface $token
  40.      *
  41.      * @return bool
  42.      */
  43.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  44.     {
  45.         /**
  46.          * @var Personal $subject
  47.          * @var User                                      $user
  48.          */
  49.         $user $token->getUser();
  50.         switch ($attribute) {
  51.             case static::ATTR_ASSIGNED:
  52.             case static::ATTR_WRITE:
  53.                 $vote = ($user === $subject->getUser());
  54.                 break;
  55.             default:
  56.                 $vote false;
  57.         }
  58.         return $vote;
  59.     }
  60. }