Feat: Add voter to prevent applications to fully equipped assignments
This commit is contained in:
@@ -2,9 +2,11 @@
|
||||
|
||||
namespace App\Security\Voter;
|
||||
|
||||
use App\Entity\Application;
|
||||
use App\Entity\Assignment;
|
||||
use App\Entity\Disposition;
|
||||
use App\Entity\User;
|
||||
use App\Repository\ApplicationRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
||||
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
|
||||
|
||||
@@ -14,7 +16,7 @@ class AssignmentVoter extends Voter
|
||||
public const EDIT = 'EDIT';
|
||||
public const APPLY = 'APPLY';
|
||||
|
||||
public function __construct(private readonly ApplicationRepository $applicationRepository)
|
||||
public function __construct(private readonly EntityManagerInterface $entityManager)
|
||||
{}
|
||||
|
||||
protected function supports(string $attribute, mixed $subject): bool
|
||||
@@ -38,14 +40,30 @@ class AssignmentVoter extends Voter
|
||||
return true;
|
||||
}
|
||||
|
||||
/** @var Assignment $assignment */
|
||||
$assignment = $subject;
|
||||
|
||||
// Only allow applications on empty slots
|
||||
$availableSlots = (int) $assignment->getAvailableDispositions();
|
||||
if (0 < $availableSlots) {
|
||||
$dispositions = $this
|
||||
->entityManager
|
||||
->getRepository(Disposition::class)
|
||||
->getCurrentByAssignment($assignment)
|
||||
;
|
||||
|
||||
return count($dispositions) < $availableSlots;
|
||||
}
|
||||
|
||||
// Only teamers without existing applications may apply to the assignment
|
||||
if (in_array('ROLE_TEAMER', $token->getRoleNames())) {
|
||||
/** @var User $user */
|
||||
$user = $token->getUser();
|
||||
$teamer = $user->getTeamer();
|
||||
/** @var Assignment $assignment */
|
||||
$assignment = $subject;
|
||||
$application = $this->applicationRepository->findOneBy(['teamer' => $teamer, 'assignment' => $assignment]);
|
||||
$application = $this
|
||||
->entityManager
|
||||
->getRepository(Application::class)
|
||||
->findOneBy(['teamer' => $teamer, 'assignment' => $assignment]);
|
||||
|
||||
return null === $application;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user