<?php
namespace App\Security\Core;
use App\Entity\Core\Publication;
use App\Entity\Core\PublisherPermission;
use App\Entity\Core\PublisherPermissionRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
class PublicationVoter extends Voter
{
const PERMISSION = 'publicationEntityPermission';
const INDEX_ACTION = 'publicationIndexAction';
const NEW_ACTION = 'publicationNewAction';
const EDIT_ACTION = 'publicationEditAction';
const DELETE_ACTION = 'publicationDeleteAction';
private EntityManagerInterface $em;
private Security $security;
public function __construct(EntityManagerInterface $em, Security $security)
{
$this->em = $em;
$this->security = $security;
}
protected function supports(string $attribute, $subject): bool
{
// For index and new, $subject will always be null. For permission, it will be null when trying to create a new entity.
if (in_array($attribute, [self::INDEX_ACTION, self::NEW_ACTION, self::PERMISSION])) {
return true;
}
if (in_array($attribute, [self::EDIT_ACTION, self::DELETE_ACTION])) {
return $subject instanceof Publication;
}
return false;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
if ($attribute === self::INDEX_ACTION) {
// Allow everyone to list - the entity permissions will still apply and hide entities you are not allowed
// to access.
return true;
}
if ($attribute === self::NEW_ACTION || $attribute === self::PERMISSION && $subject === null) {
// Includes ROLE_SUPER_ADMIN by inheritance.
// Authors should not be allowed to create new publications.
return $this->security->isGranted('ROLE_ADMIN') ||
$this->security->isGranted('ROLE_EDITOR');
}
if (!$subject instanceof Publication) {
throw new \LogicException("Invalid type for voter and attribute.");
}
return $this->checkEntityPermissions($attribute, $subject, $token);
}
public function checkEntityPermissions(string $attribute, Publication $subject, TokenInterface $token): bool
{
if ($subject->isDeleted() || $subject->isHidden() || $subject->getPublisher()->isDeleted()
|| $subject->getPublisher()->isHidden() || !$subject->isAvailableForBackendUserCreation()) {
return false;
}
// ROLE_SUPER_ADMIN inherits ROLE_ADMIN, and will also be included here.
if ($this->security->isGranted('ROLE_ADMIN')) {
return true;
}
/** @var PublisherPermissionRepository $permissionRepo */
$permissionRepo = $this->em->getRepository(PublisherPermission::class);
if ($this->security->isGranted('ROLE_EDITOR')) {
return $permissionRepo->hasEditorPermission($token->getUser(), $subject->getPublisher()) ||
$permissionRepo->hasAuthorPermission($token->getUser(), $subject);
}
if ($this->security->isGranted('ROLE_AUTHOR')) {
if ($attribute !== self::PERMISSION) {
// Don't allow authors to edit or delete publications.
return false;
}
return $permissionRepo->hasAuthorPermission($token->getUser(), $subject);
}
// Should not get here due to EasyAdmin firewall on any other roles.
return false;
}
}