<?php
namespace App\Security\Voter;
use App\Entity\Agent;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
use App\Entity\AbstractCollaborator;
use App\Entity\Property;
class PropertyDetailsVoter extends Voter
{
public const PROPERTY_DETAILS = 'PROPERTY_DETAILS';
protected function supports(string $attribute, $subject): bool
{
return $subject instanceof Property && $attribute === self::PROPERTY_DETAILS;
}
/**
* @param string $attribute
* @param Property $subject
* @param TokenInterface $token
*
* @return bool
*/
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
/** @var UserInterface $user */
$user = $token->getUser();
if (!$user instanceof AbstractCollaborator) {
return false;
}
if (!$subject->getIsShared()) {
return true;
}
if ($subject->getCollaborator()->getId() === $user->getId()) {
return true;
}
$agentAllowed = array_filter($subject->getAgentsSharedWith()->toArray(), function(Agent $agent) use ($user) {
return $user->getId() === $agent->getId();
});
return count($agentAllowed) > 0;
}
}