46 lines
1.3 KiB
PHP
46 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Security\Voter;
|
|
|
|
use App\Entity\Feedback;
|
|
use App\Entity\User;
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
|
use Symfony\Component\Security\Core\Authorization\Voter\Vote;
|
|
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
|
|
|
|
class FeedbackVoter extends Voter
|
|
{
|
|
public const VIEW = 'VIEW';
|
|
public const PUBLISH = 'PUBLISH';
|
|
public const DELETE = 'DELETE';
|
|
|
|
protected function supports(string $attribute, mixed $subject): bool
|
|
{
|
|
return $subject instanceof Feedback && in_array($attribute, [static::VIEW, static::PUBLISH, static::DELETE]);
|
|
}
|
|
|
|
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token, ?Vote $vote = null): bool
|
|
{
|
|
/** @var Feedback $feedback */
|
|
$feedback = $subject;
|
|
|
|
if (in_array('ROLE_TEAM_ADMIN', $token->getRoleNames())) {
|
|
return true;
|
|
}
|
|
|
|
if (in_array('ROLE_HOUSE_MANAGER', $token->getRoleNames())) {
|
|
return static::VIEW === $attribute;
|
|
}
|
|
|
|
if (in_array('ROLE_TEAMER', $token->getRoleNames())) {
|
|
/** @var User $user */
|
|
$user = $token->getUser();
|
|
$teamer = $user->getTeamer();
|
|
|
|
return $feedback->getTeamer() === $teamer;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|