<?php
namespace MedBrief\MSR\Security\Voter;
use InvalidArgumentException;
use MedBrief\MSR\Entity\BatchRequest;
use MedBrief\MSR\Entity\Project;
use MedBrief\MSR\Entity\ServiceOption;
use MedBrief\MSR\Entity\ServiceRequest;
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 BatchRequestVoter implements VoterInterface
{
use ClientSortingSessionTrait;
public const CREATE = 'CREATE';
public const READ = 'READ';
public const UPDATE = 'UPDATE';
public const UPDATE_SIMPLE = 'UPDATE_SIMPLE';
public const DELETE = 'DELETE';
public const ADMINISTRATION = 'ADMINISTRATION';
public const DUPLICATE = 'DUPLICATE';
public const DOWNLOAD = 'DOWNLOAD';
public const UPLOAD_DOCUMENT = 'UPLOAD_DOCUMENT';
public function __construct(private AuthorizationCheckerInterface $authorizationChecker)
{
}
public function supportsAttribute($attribute): bool
{
return in_array($attribute, [
self::CREATE,
self::READ,
self::UPDATE,
self::UPDATE_SIMPLE,
self::DELETE,
self::ADMINISTRATION,
self::DUPLICATE,
self::DOWNLOAD,
self::UPLOAD_DOCUMENT,
]);
}
public function supportsClass($class): bool
{
$supportedClass = BatchRequest::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 BatchRequest $batchRequest */
$batchRequest = $entity;
// get current logged in user
/** @var User $user */
$user = $token->getUser();
// Grab Project this BatchRequest belongs to, so we can do permission checks at the Project level.
/** @var Project $project */
$project = $batchRequest->getProject();
/** @var ServiceRequest $serviceRequest */
$serviceRequest = $batchRequest->getServiceRequest();
$batchRequest->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.
*/
// If you can READ the project, you can read the BatchRequest.
if ($attribute === self::READ && $this->authorizationChecker->isGranted('READ', $project)) {
return VoterInterface::ACCESS_GRANTED;
}
$permissionGroup = [
self::UPDATE_SIMPLE,
self::UPLOAD_DOCUMENT,
];
// if user is client, grant permission to update
// Only grant permission if service option applies to simple batch request.
if (in_array($attribute, $permissionGroup) && $this->hasClientSessionAccess($project, $user) && ($serviceRequest && $serviceRequest->getServiceOption()->getAppliesTo() === ServiceOption::APPLIES_TO_BATCH_REQUEST_SIMPLE)) {
return VoterInterface::ACCESS_GRANTED;
}
if ($attribute === self::DOWNLOAD) {
$clientId = $project->getAccount()->getId();
if (
$this->authorizationChecker->isGranted('ROLE_ACCOUNT_' . $clientId . '_ADMINISTRATOR')
|| $this->authorizationChecker->isGranted('ROLE_ACCOUNT_' . $clientId . '_SUPERADMINISTRATOR')
|| $this->authorizationChecker->isGranted('ROLE_ACCOUNT_' . $clientId . '_SORTER')
|| $this->authorizationChecker->isGranted('ROLE_ACCOUNT_' . $clientId . '_PROJECTMANAGER')
// Include Project level Project Managers in this vote.
|| $this->authorizationChecker->isGranted('ROLE_PROJECT_' . $project->getId() . '_PROJECTMANAGER')
) {
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;
}
}