<?php
namespace App\Security\Voter;
use App\Entity\User;
use App\Security\Model\BelongUser;
use App\Traits\UserTrait;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
class BelongUserVoter extends AbstractVoter {
use UserTrait;
public const BELONG = 'BELONG';
protected function getAttributes(): array {
return [
self::BELONG
];
}
protected function getEntityClass(): string {
return BelongUser::class;
}
/**
* @param string $attribute
* @param BelongUser $subject
* @param TokenInterface $token
* @return bool
*/
protected function voteOnAttribute($attribute, $subject, TokenInterface $token) {
$user = $token->getUser();
if (
$user instanceof User &&
$subject->getUser()->getId() == $user->getId()
) {
return self::ACCESS_GRANTED;
}
return self::ACCESS_ABSTAIN;
}
}