src/Security/ProjectVoter.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use App\Entity\Assignment;
  4. use App\Entity\Project\Project;
  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 ProjectVoter extends Voter
  10. {
  11.     const ATTRIBUTES = [self::ATTR_ASSIGNED];
  12.     const ATTR_ASSIGNED 'PROJECT_ASSIGNED';
  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 Project && 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 Project $subject
  47.          * @var User $user
  48.          */
  49.         $user $token->getUser();
  50.         switch ($attribute) {
  51.             case static::ATTR_ASSIGNED:
  52.                 $vote = ($user === $subject->getClient() or $this->isAssigned($subject$user));
  53.                 break;
  54.             default:
  55.                 $vote false;
  56.         }
  57.         return $vote;
  58.     }
  59.     /**
  60.      * @param Project $project
  61.      * @param User $user
  62.      *
  63.      * @return bool
  64.      */
  65.     private function isAssigned(Project $projectUser $user): bool
  66.     {
  67.         $assignment $this->assignmentRepository->findOneBy(
  68.             [
  69.                 'task'   => $project->getTasks()
  70.                     ->toArray(),
  71.                 'user'   => $user,
  72.                 'status' => [
  73.                     Assignment::STATUS_COMPLETED,
  74.                     Assignment::STATUS_PROGRESS,
  75.                     Assignment::STATUS_REQUEST,
  76.                     Assignment::STATUS_REJECTED,
  77.                     Assignment::STATUS_REVIEW,
  78.                 ],
  79.             ]
  80.         );
  81.         return !empty($assignment);
  82.     }
  83. }