<?php
namespace App\Security\Voter\User;
use App\Entity\Address;
use App\Security\ApiUser;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class AddressVoter extends Voter
{
public const PERMISSION_READ = 'read';
public const PERMISSION_EDIT = 'edit';
protected function supports(string $attribute, $subject): bool
{
if (!$subject instanceof Address) {
return false;
}
return in_array($attribute, [self::PERMISSION_READ, self::PERMISSION_EDIT], true);
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof ApiUser) {
return false;
}
// admin should be able to do anything
if ($user->getIsAdmin()) {
return true;
}
/*
* @var $subject Address
*/
return $user->getMember()->getId() == $subject->getAccount()->getMember()->getId();
}
}