Feat: Add voter to prevent applications to fully equipped assignments

This commit is contained in:
Björn Fromme
2023-11-03 10:31:51 +01:00
parent d016ee043a
commit 0d7f9afdfe
2 changed files with 44 additions and 27 deletions
+23 -5
View File
@@ -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;
}
+21 -22
View File
@@ -64,32 +64,31 @@
{% if assignment.applications|length %}
{% set application = assignment.applications[0] %}
{% if application.status == constant('App\\Entity\\Application::STATUS_REJECTED') %}
{% set icon = 'close' %}
{% set class = 'bg-gray-100 text-gray-800 hover:bg-gray-50' %}
{% set url = path('app_teamer_assignment', { 'uuid': assignment.uuid, 'r': return_url() }) %}
{% set label = 'Bewerbung abgelehnt' %}
<a href="{{ path('app_teamer_assignment', { 'uuid': assignment.uuid, 'r': return_url() }) }}"
class="btn btn--small flex items-center space-x-2 bg-gray-100 text-gray-800 hover:bg-gray-50">
{{ icon('close', 'w-4 h-4 shrink-0') }}
<span>Bewerbung abgelehnt</span>
</a>
{% else %}
{% set icon = 'hourglass' %}
{% set class = 'bg-yellow-100 text-yellow-800 hover:bg-yellow-50' %}
{% set url = path('app_teamer_assignment', { 'uuid': assignment.uuid, 'r': return_url() }) %}
{% set label = 'in Bearbeitung' %}
<a href="{{ path('app_teamer_assignment', { 'uuid': assignment.uuid, 'r': return_url() }) }}"
class="btn btn--small flex items-center space-x-2 bg-yellow-100 text-yellow-800 hover:bg-yellow-50">
{{ icon('hourglass', 'w-4 h-4 shrink-0') }}
<span>in Bearbeitung</span>
</a>
{% endif %}
{% elseif assignment.dispositions|length %}
{% set icon = 'check' %}
{% set class = 'bg-green-100 text-green-700 hover:bg-green-50' %}
{% set url = path('app_teamer_disposition_detail', { 'uuid': assignment.dispositions[0].uuid }) %}
{% set label = 'eingeteilt' %}
{% else %}
{% set icon = 'hand' %}
{% set class = '' %}
{% set url = path('app_teamer_assignment', { 'uuid': assignment.uuid, 'r': return_url() }) %}
{% set label = 'bewerben' %}
<a href="{{ path('app_teamer_disposition_detail', { 'uuid': assignment.dispositions[0].uuid }) }}"
class="btn btn--small flex items-center space-x-2 bg-green-100 text-green-700 hover:bg-green-50">
{{ icon('check', 'w-4 h-4 shrink-0') }}
<span>eingeteilt</span>
</a>
{% elseif is_granted('APPLY', assignment) %}
<a href="{{ path('app_teamer_assignment', { 'uuid': assignment.uuid, 'r': return_url() }) }}"
class="btn btn--small flex items-center space-x-2">
{{ icon('hand', 'w-4 h-4 shrink-0') }}
<span>bewerben</span>
</a>
{% endif %}
<a href="{{ url }}"
class="btn btn--small flex items-center space-x-2 {{ class }}">
{{ icon(icon, 'w-4 h-4 shrink-0') }}
<span>{{ label }}</span>
</a>
</div>
{% set isBookmarked = teamer.bookmarks.contains(assignment) %}
<a href="{{ path('app_teamer_bookmark_toggle', { 'uuid': assignment.uuid, 'r': return_url() }) }}"