<?php
namespace App\Security\Voter;
use App\Entity\Client\Client;
use App\Entity\Event\Action;
use App\Entity\Event\Event;
use App\Entity\Event\Notice;
use App\Entity\User;
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 EventVoter extends Voter
{
// these strings are just invented: you can use anything
const VIEW = 'view';
const EDIT = 'edit';
const DELETE = 'delete';
/**
* @var Security
*/
private Security $security;
/**
* @var EntityManagerInterface
*/
private EntityManagerInterface $entityManager;
public function __construct(Security $security, EntityManagerInterface $entityManager)
{
$this->security = $security;
$this->entityManager = $entityManager;
}
protected function supports(string $attribute, $subject): bool
{
// if the attribute isn't one we support, return false
if (!in_array($attribute, [self::VIEW, self::EDIT, self::DELETE], true)) {
return false;
}
// only vote on `File` objects
if (!$subject instanceof Event) {
return false;
}
return true;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$loggedUser = $token->getUser();
if (!$loggedUser instanceof User) {
// the user must be logged in; if not, deny access
return false;
}
// you know $subject is a Post object, thanks to `supports()`
/** @var Event $targetEvent */
$targetEvent = $subject;
switch ($attribute) {
case self::VIEW:
return $this->canSee($targetEvent, $loggedUser);
case self::EDIT:
return $this->canEdit($targetEvent, $loggedUser);
case self::DELETE:
return $this->canDelete($targetEvent, $loggedUser);
}
throw new \LogicException('This code should not be reached!');
}
/**
* @param Event $targetEvent
* @param User $loggedUser
* @return bool
*/
private function canSee(Event $targetEvent, User $loggedUser): bool
{
// sanity check (different client than logged user client)
if ($targetEvent->getClient()->getId() !== $loggedUser->getClient()->getId()) {
return false;
}
if ($targetEvent instanceof Notice && !$this->security->isGranted('ROLE_CAN_SEE_NOTICE_DETAIL')) {
return false;
}
if ($targetEvent instanceof Action && !$this->security->isGranted('ROLE_CAN_SEE_ACTION_DETAIL')) {
return false;
}
return true;
}
/**
* @param Event $targetEvent
* @param User $loggedUser
* @return bool
*/
private function canEdit(Event $targetEvent, User $loggedUser): bool
{
if (!$this->canSee($targetEvent, $loggedUser)) {
return false;
}
if ($targetEvent instanceof Notice) {
// Check specific permission for custom messages
if ($targetEvent->isCustomMessage()) {
if (!$this->security->isGranted('ROLE_CAN_EDIT_TYPE_CUSTOM_MESSAGE')) {
return false;
}
} else {
// Regular notices need standard edit permission
if (!$this->security->isGranted('ROLE_CAN_EDIT_NOTICE')) {
return false;
}
}
}
if ($targetEvent instanceof Action && !$this->security->isGranted('ROLE_CAN_EDIT_ACTION')) {
return false;
}
return true;
}
/**
* @param Event $targetEvent
* @param User $loggedUser
* @return bool
*/
private function canDelete(Event $targetEvent, User $loggedUser): bool
{
if (!$this->canSee($targetEvent, $loggedUser)) {
return false;
}
if ($targetEvent instanceof Notice && !$this->security->isGranted('ROLE_CAN_DELETE_NOTICE')) {
return false;
}
if ($targetEvent instanceof Action && !$this->security->isGranted('ROLE_CAN_DELETE_ACTION')) {
return false;
}
return true;
}
}