<?php
namespace MedBrief\MSR\Security\Voter;
use InvalidArgumentException;
use MedBrief\MSR\Entity\User;
use MedBrief\MSR\Entity\UserPreference;
use Override;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
use Symfony\Component\Security\Core\User\UserInterface;
class UserPreferenceVoter implements VoterInterface
{
public const READ = 'READ';
public const UPDATE = 'UPDATE';
public const DELETE = 'DELETE';
public function __construct(private readonly AuthorizationCheckerInterface $authorizationChecker)
{
}
/**
* Check if the attribute is supported by this voter.
*
* @param string $attribute The attribute to check.
*
* @return bool
*/
public function supportsAttribute($attribute): bool
{
return in_array($attribute, [
self::READ,
self::UPDATE,
self::DELETE,
]);
}
/**
* Check if the class is supported by this voter.
*
* @param string $class The class name to check.
*
* @return bool
*/
public function supportsClass($class): bool
{
return $class === UserPreference::class || is_subclass_of($class, UserPreference::class);
}
/**
* @param mixed $entity
*/
#[Override]
public function vote(TokenInterface $token, $entity, array $attributes): int
{
// check if class of this object is supported by this voter
if (!$this->supportsClass($entity && !is_array($entity) ? $entity::class : '')) {
return VoterInterface::ACCESS_ABSTAIN;
}
// check if the voter is used correct, only allow one attribute
if (count($attributes) !== 1) {
throw new InvalidArgumentException('Only one attribute is allowed for MedBrief voters.');
}
// set the attribute to check against
$attribute = $attributes[0];
// check if the given attribute is covered by this voter
if (!$this->supportsAttribute($attribute)) {
return VoterInterface::ACCESS_ABSTAIN;
}
// get current logged in user
/** @var User $user */
$user = $token->getUser();
// make sure there is a user object (i.e. that the user is logged in)
if (!$user instanceof UserInterface) {
return VoterInterface::ACCESS_DENIED;
}
// Admin users can do everything
if ($this->authorizationChecker->isGranted('ROLE_ADMIN')) {
return VoterInterface::ACCESS_GRANTED;
}
// Only allow access to own preferences
return $entity->getUser()?->getId() === $user->getId()
? VoterInterface::ACCESS_GRANTED
: VoterInterface::ACCESS_DENIED;
}
}