WIP: Implement application process

This commit is contained in:
Björn Fromme
2023-10-09 16:59:35 +02:00
parent abced8c924
commit 3a458df94f
17 changed files with 441 additions and 219 deletions
+42
View File
@@ -0,0 +1,42 @@
<?php
namespace App\Security\Voter;
use App\Entity\Application;
use App\Entity\User;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class ApplicationVoter extends Voter
{
public const VIEW = 'VIEW';
public const DELETE = 'DELETE';
protected function supports(string $attribute, mixed $subject): bool
{
if (!$subject instanceof Application) {
return false;
}
return in_array($attribute, [static::VIEW, static::DELETE]);
}
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
{
/** @var User $user */
$user = $token->getUser();
/** @var Application $application */
$application = $subject;
if ($user->hasRole('ROLE_ADMINISTRATIVE')) {
return true;
}
if ($user->hasRole('ROLE_TEAMER')) {
return $user->getTeamer() === $application->getTeamer();
}
return false;
}
}
+55
View File
@@ -0,0 +1,55 @@
<?php
namespace App\Security\Voter;
use App\Entity\Assignment;
use App\Entity\User;
use App\Repository\ApplicationRepository;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class AssignmentVoter extends Voter
{
public const VIEW = 'VIEW';
public const EDIT = 'EDIT';
public const APPLY = 'APPLY';
public function __construct(private readonly ApplicationRepository $applicationRepository)
{}
protected function supports(string $attribute, mixed $subject): bool
{
if (!$subject instanceof Assignment) {
return false;
}
return in_array($attribute, [static::VIEW, static::EDIT, static::APPLY]);
}
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
{
// All authenticated users may view assignments
if (static::VIEW === $attribute && null !== $token->getUser()) {
return true;
}
// Only users with administrative role may edit assignments
if (static::EDIT === $attribute && in_array('ROLE_ADMINISTRATIVE', $token->getRoleNames())) {
return true;
}
// 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]);
return null === $application;
}
return false;
}
}