<?php
namespace MedBrief\MSR\Security\Voter;
use Doctrine\ORM\EntityManagerInterface;
use InvalidArgumentException;
use MedBrief\MSR\Entity\Annotation;
use MedBrief\MSR\Entity\Document;
use MedBrief\MSR\Entity\User;
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 PDFTronAnnotationVoter implements VoterInterface
{
public const CREATE = 'CREATE';
public const READ = 'READ';
public const UPDATE = 'UPDATE';
public const DELETE = 'DELETE';
public const ADMINISTRATION = 'ADMINISTRATION';
public const UPDATE_VISIBILITY = 'UPDATE_VISIBILITY';
public function __construct(private readonly AuthorizationCheckerInterface $authorizationChecker, private readonly EntityManagerInterface $entityManager)
{
}
public function supportsAttribute($attribute): bool
{
return in_array($attribute, [
self::CREATE,
self::READ,
self::UPDATE,
self::DELETE,
self::ADMINISTRATION,
self::UPDATE_VISIBILITY,
]);
}
public function supportsClass($class): bool
{
$supportedClass = Annotation::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;
}
// 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 and Super 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.
*/
// Check if the user has access to the Document in order to determine if they can see the annotations.
if ($entity->getDocumentId()) {
// Retrieve the Document...
/** @var Document $document */
$document = $this->entityManager->getRepository(Document::class)->find($entity->getDocumentId());
// You need to be able to read the Project the Document belongs to, and you stream the native file for the Document,
// to do anything related to the Document's annotations.
if ($document && $document->getProject() && ($this->authorizationChecker->isGranted('READ', $document->getProject())
&& $this->authorizationChecker->isGranted('STREAM_NATIVE', $document))) {
// The basic permissions for a Document is enough to allow CREATE of annotations.
if ($attribute === self::CREATE) {
return VoterInterface::ACCESS_GRANTED;
}
// Reading annotations is based on the visibility of the Annotation, unless the user has Client level access,
// or is a Project manager for the Project the annotation's document belongs to.
if ($attribute === self::READ) {
// For disclosure matters, only annotations made by the user can be seen by them.
if ($document->getProject()->isTypeDisclosure() === true) {
if ($entity->getAuthorId() == $user->getId()) {
return VoterInterface::ACCESS_GRANTED;
}
} elseif ($entity->isPubliclyVisible()) {
// If the annotation is public, everyone can see it.
return VoterInterface::ACCESS_GRANTED;
} elseif ($entity->getAuthorId() == $user->getId()) {
// If the user is the author of the annotation, they can see it.
return VoterInterface::ACCESS_GRANTED;
} elseif ($user->isAccountAdministrator()) {
// If the user is an account level administrator, they can see all annotations.
return VoterInterface::ACCESS_GRANTED;
} elseif ($this->authorizationChecker->isGranted('ROLE_PROJECT_' . $document->getProject()->getId() . '_PROJECTMANAGER')) {
// If the user is a project manager for the Matter the document belongs to, they can see all annotations.
return VoterInterface::ACCESS_GRANTED;
}
}
// In order to UPDATE or DELETE an annotation, you must be the Annotation's Author.
if (($attribute === self::UPDATE || $attribute === self::DELETE) && $entity->getAuthorId() == $user->getId()) {
return VoterInterface::ACCESS_GRANTED;
}
// Check if the user can update the visibility of an annotation.
if ($attribute === self::UPDATE_VISIBILITY && $this->authorizationChecker->isGranted('UPDATE_ANNOTATION_VISIBILITY', $document)) {
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;
}
}