<?php
namespace MedBrief\MSR\Security\Voter;
use Doctrine\ORM\EntityManagerInterface;
use InvalidArgumentException;
use MedBrief\MSR\Entity\Collection;
use MedBrief\MSR\Entity\ProjectUser;
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 CollectionVoter implements VoterInterface
{
public const CREATE = 'CREATE';
public const READ = 'READ';
public const UPDATE = 'UPDATE';
public const DELETE = 'DELETE';
public const ADMINISTRATION = 'ADMINISTRATION';
public const DOWNLOAD = 'DOWNLOAD';
public const ANNOTATION_VISIBILITY_DOWNLOAD_OPTIONS = 'ANNOTATION_VISIBILITY_DOWNLOAD_OPTIONS';
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::DOWNLOAD,
self::ANNOTATION_VISIBILITY_DOWNLOAD_OPTIONS,
]);
}
public function supportsClass($class): bool
{
$supportedClass = Collection::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
$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;
}
/**
* END: Common code for all Voter:vote() methods. Put custom logic below.
*/
// If this user has ADMINISTRATION rights for the project to which this
// collection belongs
if ($this->authorizationChecker->isGranted('ADMINISTRATION', $entity->getProject())) {
// then they have access to do anything
return VoterInterface::ACCESS_GRANTED;
}
// Decide if we allow the advanced annotation download options to display in the form for this collection.
if ($attribute === self::ANNOTATION_VISIBILITY_DOWNLOAD_OPTIONS && ($entity->getProject() && $this->authorizationChecker->isGranted('ROLE_PROJECT_' . $entity->getProject()->getId() . '_PROJECTMANAGER'))) {
return VoterInterface::ACCESS_GRANTED;
}
if ($attribute == self::UPDATE || $attribute == self::DELETE) {
// Retrieve the collection's root collection
$rootCollection = $this->entityManager->getRepository(Collection::class)->find($entity->getRoot());
// Get the project from the collection's root collection.
$project = $rootCollection->getProject();
// MEDICAL_RECORDS_ADMINISTRATION can update and delete any medical records Collection.
if ($this->authorizationChecker->isGranted('MEDICAL_RECORDS_ADMINISTRATION', $project)) {
$isGranted
// Is the Collection in the public medical records Collection?
= ($project->getMedicalRecordsCollection() && $project->getMedicalRecordsCollection()->containsCollectionRecursive($entity))
// Or Private Collections?
|| ($project->getPrivateCollection() && $project->getPrivateCollection()->containsCollectionRecursive($entity))
// Or PrivateSandbox Collections?
|| ($project->getPrivateSandboxCollection() && $project->getPrivateSandboxCollection()->containsCollectionRecursive($entity))
// Or Unsorted Records collection?
|| ($project->getUnsortedRecordsCollection() && $project->getUnsortedRecordsCollection()->containsCollectionRecursive($entity));
if ($isGranted) {
return VoterInterface::ACCESS_GRANTED;
}
}
// Grab the ProjectUser, so we can do some checks against the user's private collections.
$projectUser = $this->entityManager->getRepository(ProjectUser::class)->findOneBy(['user' => $user, 'project' => $project]);
// A user can update and delete collections that they created, contained in their PrivateSandbox collection.
if ($projectUser) {
$isGranted
// Is the collection in the user's PrivateSandbox Collection?
= $projectUser->getPrivateSandboxCollection()
&& $projectUser->getPrivateSandboxCollection()->containsCollectionRecursive($entity)
// And did they create the Collection?
&& $entity->getCreator()
&& $entity->getCreator()->getId() == $user->getId();
if ($isGranted) {
// If we're updating, no problem...
if ($attribute == self::UPDATE) {
return VoterInterface::ACCESS_GRANTED;
}
// If we're deleting, then we need to check there are no Collections/Documents in the Collection that the user
// does not have permission to delete.
if ($attribute == self::DELETE) {
// Check the user has access to delete the Documents contained directly in the collection.
foreach ($entity->getDocuments() as $document) {
if (!$this->authorizationChecker->isGranted('DELETE', $document)) {
return VoterInterface::ACCESS_DENIED;
}
}
// Grab all child Collections of the Collection
$children = $this->entityManager->getRepository(Collection::class)->getChildren($entity);
foreach ($children as $childCollection) {
// Check the user has access to delete each of the child Collections, if one fails, we deny access.
if (!$this->authorizationChecker->isGranted('DELETE', $childCollection)) {
return VoterInterface::ACCESS_DENIED;
}
// Check the user has access to delete the Documents in the child Collection, If one fails, we deny access.
foreach ($childCollection->getDocuments() as $document) {
if (!$this->authorizationChecker->isGranted('DELETE', $document)) {
return VoterInterface::ACCESS_DENIED;
}
}
}
// All clear for DELETE, grant access
return VoterInterface::ACCESS_GRANTED;
}
}
}
}
if ($attribute == self::DOWNLOAD) {
// Get the Project from the Collection's root collection.
$rootCollection = $this->entityManager->getRepository(Collection::class)->find($entity->getRoot());
$project = $rootCollection->getProject();
// MEDICAL_RECORDS_ADMINISTRATION can download all medical record Collections.
if ($this->authorizationChecker->isGranted('MEDICAL_RECORDS_ADMINISTRATION', $project)) {
$isGranted
// Is the Collection in the Public Medical Record Collection?
= ($project->getMedicalRecordsCollection() && $project->getMedicalRecordsCollection()->containsCollectionRecursive($entity))
// Or, Private Medical Record collection?
|| ($project->getPrivateCollection() && $project->getPrivateCollection()->containsCollectionRecursive($entity))
// Or, PrivateSandbox Medical Record collection?
|| ($project->getPrivateSandboxCollection() && $project->getPrivateSandboxCollection()->containsCollectionRecursive($entity))
// Or, Unsorted Records collection?
|| ($project->getUnsortedRecordsCollection() && $project->getUnsortedRecordsCollection()->containsCollectionRecursive($entity));
if ($isGranted) {
return VoterInterface::ACCESS_GRANTED;
}
}
// Grab the ProjectUser, so we can do some checks against the user's private Collections.
$projectUser = $this->entityManager->getRepository(ProjectUser::class)->findOneBy(['user' => $user, 'project' => $project]);
// Users with MEDICAL_RECORD_DOWNLOAD can download collections in the public medical records folder, as well as collections in their
// private collection. They can also download records from the Unsorted Records folder.
if ($projectUser && $this->authorizationChecker->isGranted('MEDICAL_RECORD_DOWNLOAD', $project)) {
$isGranted
// Is the Collection in the Public Medical Record Collection?
= ($project->getMedicalRecordsCollection() && $project->getMedicalRecordsCollection()->containsCollectionRecursive($entity))
// Or, is it in user's own Private Medical Record Collection?
|| ($projectUser->getPrivateCollection() && $projectUser->getPrivateCollection()->containsCollectionRecursive($entity))
// Or, is it in the Unsorted Records collection?
|| ($project->getUnsortedRecordsCollection() && $project->getUnsortedRecordsCollection()->containsCollectionRecursive($entity));
if ($isGranted) {
return VoterInterface::ACCESS_GRANTED;
}
}
// All users can download collections in their PrivateSandbox collections.
if ($projectUser) {
$isGranted
// Is in user's PrivateSandbox Medical Record Collection?
= $projectUser->getPrivateSandboxCollection() && $projectUser->getPrivateSandboxCollection()->containsCollectionRecursive($entity);
if ($isGranted) {
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;
}
}