src/Security/ProjectBoardChatVoter.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use App\Entity\Assignment;
  4. use App\Modules\Chat\Entity\Project\Board;
  5. use App\Entity\User;
  6. use App\Repository\AssignmentRepository;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. class ProjectBoardChatVoter extends Voter
  10. {
  11.     const ATTRIBUTES = [self::ATTR_ASSIGNEDself::ATTR_WRITE];
  12.     const ATTR_ASSIGNED 'CHAT_ASSIGNED';
  13.     const ATTR_WRITE    'CHAT_WRITE';
  14.     /**
  15.      * @var AssignmentRepository
  16.      */
  17.     private $assignmentRepository;
  18.     public function __construct(AssignmentRepository $assignmentRepository)
  19.     {
  20.         $this->assignmentRepository $assignmentRepository;
  21.     }
  22.     /**
  23.      * Determines if the attribute and subject are supported by this voter.
  24.      *
  25.      * @param string $attribute An attribute
  26.      * @param mixed $subject The subject to secure, e.g. an object the user wants to access or any other PHP type
  27.      *
  28.      * @return bool True if the attribute and subject are supported, false otherwise
  29.      */
  30.     protected function supports($attribute$subject)
  31.     {
  32.         return $subject instanceof Board && in_array($attribute, static::ATTRIBUTES);
  33.     }
  34.     /**
  35.      * Perform a single access check operation on a given attribute, subject and token.
  36.      * It is safe to assume that $attribute and $subject already passed the "supports()" method check.
  37.      *
  38.      * @param string $attribute
  39.      * @param mixed $subject
  40.      * @param TokenInterface $token
  41.      *
  42.      * @return bool
  43.      */
  44.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  45.     {
  46.         /**
  47.          * @var Board $subject
  48.          * @var User                                   $user
  49.          */
  50.         $user $token->getUser();
  51.         switch ($attribute) {
  52.             case static::ATTR_ASSIGNED:
  53.                 $vote = ($user === $subject->getClient() or $this->isAssigned($subject$user));
  54.                 break;
  55.             case static::ATTR_WRITE:
  56. //                $vote = ($user === $subject->getClient());
  57.                 $vote = ($user === $subject->getClient() or $this->isAssigned($subject$user));
  58.                 break;
  59.             default:
  60.                 $vote false;
  61.         }
  62.         return $vote;
  63.     }
  64.     /**
  65.      * @param Board $chat
  66.      * @param User                                   $user
  67.      *
  68.      * @return bool
  69.      */
  70.     private function isAssigned(Board $chatUser $user): bool
  71.     {
  72.         $assignment $this->assignmentRepository->findOneBy(
  73.             [
  74.                 'task'   => $chat->getProject()
  75.                     ->getTasks()
  76.                     ->toArray(),
  77.                 'user'   => $user,
  78.                 'status' => [
  79.                     Assignment::STATUS_COMPLETED,
  80.                     Assignment::STATUS_PROGRESS,
  81.                     Assignment::STATUS_REQUEST,
  82.                     Assignment::STATUS_REJECTED,
  83.                     Assignment::STATUS_REVIEW,
  84.                 ],
  85.             ]
  86.         );
  87.         return !empty($assignment);
  88.     }
  89. }