<?php
namespace App\Security\Authorization\Voter;
use App\Entity\User;
use App\Service\Report\DeliveredItemsReport;
use App\Service\Report\OfferReport;
use App\Service\Report\ReportInterface;
use App\Service\Report\SupportCaseReport;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class ReportVoter extends Voter
{
const READ = 'read';
/**
* {@inheritDoc}
*/
protected function supports($attribute, $subject): bool
{
if ($attribute != self::READ) {
return false;
}
if (is_string($subject)) {
if (!(class_exists($subject) && is_a($subject, ReportInterface::class, true))) {
return false;
}
} elseif (is_object($subject)) {
if (!($subject instanceof ReportInterface)) {
return false;
}
}
return true;
}
/**
* {@inheritDoc}
*/
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
$user = $token->getUser();
if (!($user instanceof User)) {
return false;
}
if (is_object($subject)) {
$subject = get_class($subject);
}
/* @var string $reportClass */
$reportClass = $subject;
switch ($attribute) {
case self::READ:
if (in_array($reportClass, [OfferReport::class, SupportCaseReport::class, DeliveredItemsReport::class])) {
return true;
}
if ($user->isSuperAdmin()) {
return true;
}
break;
}
return false;
}
}