<?php
declare(strict_types=1);
namespace App\Platform\Security;
use App\Bundles\PaymentBundle\Entity\Subscription;
use App\Bundles\UserBundle\Entity\User;
use LogicException;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class SubscriptionVoter extends Voter
{
public const VIEW = 'view';
public const CREATE = 'create';
protected function supports(string $attribute, $subject): bool
{
// if the attribute isn't one we support, return false
if (!in_array($attribute, [self::VIEW, self::CREATE])) {
return false;
}
// only vote on `SavedSearch` objects
if (!$subject instanceof Subscription) {
return false;
}
return true;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof User) {
// the user must be logged in; if not, deny access
return false;
}
// you know $subject is a SavedSearch object, thanks to `supports()`
/** @var Subscription $subscription */
$subscription = $subject;
switch ($attribute) {
case self::VIEW:
return $this->canView($subscription, $user);
case self::CREATE:
return $this->canCreate($subscription, $user);
}
throw new LogicException('This code should not be reached!');
}
private function canView(Subscription $subscription, User $user): bool
{
return $this->canCreate($subscription, $user);
}
private function canCreate(Subscription $subscription, User $user): bool
{
// TODO: Add check user
return true;
}
}