<?php
namespace MedBrief\MSR\Security\Voter;
use InvalidArgumentException;
use MedBrief\MSR\Entity\Project;
use MedBrief\MSR\Entity\SortingSession;
use MedBrief\MSR\Entity\SortingSessionMemo;
use MedBrief\MSR\Entity\User;
use MedBrief\MSR\Traits\Security\Authorization\Voter\ClientSortingSessionTrait;
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 SortingSessionMemoVoter implements VoterInterface
{
use ClientSortingSessionTrait;
public const READ = 'READ';
public const DELETE = 'DELETE';
public const SYNC = 'SYNC';
public function __construct(private AuthorizationCheckerInterface $authorizationChecker)
{
}
public function supportsAttribute($attribute): bool
{
return in_array($attribute, [
self::READ,
self::DELETE,
self::SYNC,
]);
}
public function supportsClass($class): bool
{
$supportedClass = SortingSessionMemo::class;
return $supportedClass === $class || is_subclass_of($class, $supportedClass);
}
/**
*
* @param mixed $entity
*/
#[Override]
public function vote(TokenInterface $token, $entity, array $attributes)
{
/**
* START: This is common code for all Voter::vote() methods
*/
// 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
// this isn't a requirement, it's just one easy way for you to
// design your voter
if (1 !== count($attributes)) {
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;
}
/** @var SortingSessionMemo $sortingSessionMemo */
$sortingSessionMemo = $entity;
// get current logged in user
/** @var User $user */
$user = $token->getUser();
// get current project
/** @var Project $project */
$project = $sortingSessionMemo->getProject();
// get current sorting session
/** @var SortingSession $sortingSession */
$sortingSession = $sortingSessionMemo->getSortingSession();
// 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;
}
/**
* END: Common code for all Voter:vote() methods. Put custom logic below.
*/
// For all permissions on a memo/file note, if it is a client sorting session,
// and client is ADMIN, ProjectManager for the Account, or is granted an invite
// as an external ProjectManager to the specific project.... grant full access
if ($this->hasClientSessionAccess($project, $user, $sortingSession)) {
return VoterInterface::ACCESS_GRANTED;
};
// If we get to the end of this function, then no decisions have been
// made so we deny access
return VoterInterface::ACCESS_DENIED;
}
}