<?php
namespace App\Security\Voter;
use App\Entity\CrmConversation;
use App\Entity\User;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class ConversationVoter extends Voter
{
public const VIEW = 'VIEW';
public const SEND = 'SEND';
public const MANAGE = 'MANAGE';
protected function supports(string $attribute, $subject): bool
{
return in_array($attribute, [self::VIEW, self::SEND, self::MANAGE], true)
&& $subject instanceof CrmConversation;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof User) {
return false;
}
if (in_array('ROLE_SUPER_ADMIN', $user->getRoles(), true)) {
return true;
}
/** @var CrmConversation $conversation */
$conversation = $subject;
if ($attribute === self::SEND && !$conversation->getAllowReplies()) {
return $conversation->getCreatedBy() && $conversation->getCreatedBy()->getId() === $user->getId();
}
return $conversation->isParticipant($user);
}
}