<?php
namespace App\Security\Voter;
use Oz\ApiNvl\Model\User;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
/**
*
*/
class ApiRoleVoter extends Voter
{
public const ADD_USER = 'ADD_USER';
/**
* Determines if the attribute and subject are supported by this voter.
*
* @param string $attribute An attribute
* @param mixed $subject The subject to secure, e.g. an object the user wants to access or any other PHP type
*
* @return bool
*/
protected function supports(string $attribute, $subject): bool
{
return $attribute == self::ADD_USER;
}
/**
* Perform a single access check operation on a given attribute, subject and token.
* It is safe to assume that $attribute and $subject already passed the "supports()" method check.
*
* @param string $attribute
* @param mixed $subject
* @param TokenInterface $token
*
* @return bool
*/
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof User) {
return false;
}
if ($attribute == self::ADD_USER) {
return $this->thisUserCanAddOtherUsers($user);
}
return false;
}
/**
*
* @param User $user
*
* @return bool
*/
private function thisUserCanAddOtherUsers(User $user): bool
{
return in_array($user->getApiRole(), ['admin']);
}
}