64 lines
2.4 KiB
PHP
64 lines
2.4 KiB
PHP
<?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';
|
|
public const WITHDRAW = 'WITHDRAW';
|
|
public const DISPOSE = 'DISPOSE';
|
|
public const STATUS = 'STATUS';
|
|
|
|
protected function supports(string $attribute, mixed $subject): bool
|
|
{
|
|
if (!$subject instanceof Application) {
|
|
return false;
|
|
}
|
|
|
|
return in_array($attribute, [static::VIEW, static::WITHDRAW, static::DELETE, static::DISPOSE, static::STATUS]);
|
|
}
|
|
|
|
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
|
|
{
|
|
/** @var User $user */
|
|
$user = $token->getUser();
|
|
|
|
/** @var Application $application */
|
|
$application = $subject;
|
|
$status = $application->getStatus();
|
|
|
|
$adminAttributes = [static::VIEW, static::DELETE, static::DISPOSE, static::STATUS];
|
|
$teamerAttributes = [static::VIEW, static::WITHDRAW, static::DELETE, static::STATUS];
|
|
|
|
if ($user->hasRole('ROLE_ADMINISTRATIVE') && in_array($attribute, $adminAttributes)) {
|
|
$assignment = $application->getAssignment();
|
|
return match ($attribute) {
|
|
static::VIEW, static::STATUS => Application::STATUS_REJECTED !== $status,
|
|
static::DELETE => Application::STATUS_REJECTED === $status,
|
|
static::DISPOSE => Application::STATUS_REJECTED !== $status
|
|
&& $assignment->getAvailableDispositions() > $assignment->getDispositions()->count(),
|
|
default => false,
|
|
};
|
|
}
|
|
|
|
if ($user->hasRole('ROLE_TEAMER') && in_array($attribute, $teamerAttributes)) {
|
|
$teamer = $user->getTeamer();
|
|
return match ($attribute) {
|
|
static::WITHDRAW => $teamer === $application->getTeamer(),
|
|
static::DELETE => $teamer === $application->getTeamer()
|
|
&& Application::STATUS_REJECTED === $status,
|
|
static::VIEW => $teamer === $application->getTeamer()
|
|
&& Application::STATUS_REJECTED !== $status,
|
|
default => false,
|
|
};
|
|
}
|
|
|
|
return false;
|
|
}
|
|
} |