53 lines
1.6 KiB
PHP
53 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Security\Voter;
|
|
|
|
use App\Entity\Disposition;
|
|
use App\Entity\User;
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
|
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
|
|
|
|
class DispositionVoter extends Voter
|
|
{
|
|
public const VIEW = 'VIEW';
|
|
public const EDIT = 'EDIT';
|
|
public const DELETE = 'DELETE';
|
|
public const CONTRACT = 'CONTRACT';
|
|
public const INVOICE = 'INVOICE';
|
|
|
|
protected function supports(string $attribute, mixed $subject): bool
|
|
{
|
|
if (!$subject instanceof Disposition) {
|
|
return false;
|
|
}
|
|
|
|
return in_array($attribute, [static::VIEW, static::EDIT, static::DELETE, static::CONTRACT, static::INVOICE]);
|
|
}
|
|
|
|
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
|
|
{
|
|
// Administrative users have full access to all dispositions
|
|
if (in_array('ROLE_ADMINISTRATIVE', $token->getRoleNames())) {
|
|
return true;
|
|
}
|
|
|
|
// Teamers may only view or edit their own dispositions
|
|
if (in_array('ROLE_TEAMER', $token->getRoleNames())) {
|
|
/** @var User $user */
|
|
$user = $token->getUser();
|
|
$teamer = $user->getTeamer();
|
|
/** @var Disposition $disposition */
|
|
$disposition = $subject;
|
|
|
|
switch ($attribute) {
|
|
case static::VIEW:
|
|
case static::EDIT:
|
|
case static::CONTRACT:
|
|
case static::INVOICE:
|
|
return $teamer === $disposition->getTeamer();
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
} |