<?php
/*
* This file is part of CustomerGroup
*
* Copyright(c) Akira Kurozumi <info@a-zumi.net>
*
* https://a-zumi.net
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Plugin\CustomerGroup42\Security\Authorization\Voter;
use Eccube\Entity\Product;
use Plugin\CustomerGroup42\Service\Gate\GateContext;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class ProductVoter extends Voter
{
public const VIEW = 'view';
public const CART = 'cart';
/**
* @var GateContext
*/
private GateContext $gateContext;
public function __construct(GateContext $gateContext)
{
$this->gateContext = $gateContext;
}
/**
* @param string $attribute
* @param $subject
*
* @return bool
*/
protected function supports(string $attribute, $subject): bool
{
return $subject instanceof Product && \in_array($attribute, [self::VIEW, self::CART], true);
}
/**
* @param string $attribute
* @param $subject
* @param TokenInterface $token
*
* @return bool
*/
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
return $this->gateContext->permit($attribute, $subject, $token);
}
}