<?php
namespace App\Security\Voter\Webmaster;
use App\Entity\Webmaster;
use App\Security\ApiUser;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class WebmasterVoter extends Voter
{
public const STATS = 'stats';
public const TRAFFIC = 'traffic';
public const ADS = 'ads';
public const CAMPAIGNS = 'campaigns';
public const EDIT = 'edit';
public const PERMISSION_READ_SENSITIVE = 'read_sensitive';
public const PERMISSION_WRITE_SENSITIVE = 'write_sensitive';
protected function supports(string $attribute, $subject): bool
{
if (!$subject instanceof Webmaster) {
return false;
}
return in_array($attribute, [
self::STATS, self::TRAFFIC,
self::ADS,
self::CAMPAIGNS,
self::EDIT,
self::PERMISSION_READ_SENSITIVE,
self::PERMISSION_WRITE_SENSITIVE,
], 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 Webmaster
*/
return $user->getIsWebmaster() && $user->getWebmaster()->getId() === $subject->getId();
}
}