<?php
namespace MedBrief\MSR\Security\Voter;
use InvalidArgumentException;
use MedBrief\MSR\Entity\Project;
use Override;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
use Symfony\Component\Security\Core\User\UserInterface;
class ProjectLicenseLevelVoter implements VoterInterface
{
public const FEATURE_VIEW_RADIOLOGY = 'FEATURE_VIEW_RADIOLOGY';
public function supportsAttribute($attribute): bool
{
return $attribute == self::FEATURE_VIEW_RADIOLOGY;
}
public function supportsClass($class): bool
{
$supportedClass = Project::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;
}
// In the case of this voter, MedBrief Admin and Super Admin should not see the feature either,
// as the functionality is effectively removed.
// if ($this->authorizationChecker->isGranted('ROLE_ADMIN'))
// {
// return VoterInterface::ACCESS_GRANTED;
// }
/**
* END: Common code for all Voter:vote() methods. Put custom logic below.
*/
// An array containing the features we can check with this voter, and the corresponding license levels that
// grant access to them.
$featureLicenses = [];
// FEATURE_VIEW_RADIOLOGY
$featureLicenses[self::FEATURE_VIEW_RADIOLOGY] = $this->allLicenseLevelsExcept([
Project::LICENSE_LEVEL_BASIC,
]);
// If we have a set of allowed license levels for this attribute, and the Project's license level falls in that set,
// we grant access.
if (isset($featureLicenses[$attribute]) && in_array($entity->getLicenseLevel(), $featureLicenses[$attribute], true)) {
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;
}
/**
* A convenience method for specifying all Project License Levels,
* except the specified excluded ones.
*
* @param array $excludeLicenseLevels
*/
private function allLicenseLevelsExcept(array $excludeLicenseLevels): array
{
return array_filter($this->allLicenseLevels(), fn ($licenseLevel): bool => !in_array($licenseLevel, $excludeLicenseLevels));
}
/**
* A convenience method for specifying all Project License Levels.
*/
private function allLicenseLevels(): array
{
return array_keys(Project::getILicenseLevelOptions());
}
}