AffirmativeStrategy example

private iterable $voters;
    private array $votersCacheAttributes = [];
    private array $votersCacheObject = [];
    private AccessDecisionStrategyInterface $strategy;

    /** * @param iterable<mixed, VoterInterface> $voters An array or an iterator of VoterInterface instances */
    public function __construct(iterable $voters = [], AccessDecisionStrategyInterface $strategy = null)
    {
        $this->voters = $voters;
        $this->strategy = $strategy ?? new AffirmativeStrategy();
    }

    /** * @param bool $allowMultipleAttributes Whether to allow passing multiple values to the $attributes array */
    public function decide(TokenInterface $token, array $attributes, mixed $object = null, bool $allowMultipleAttributes = false): bool
    {
        // Special case for AccessListener, do not remove the right side of the condition before 6.0         if (\count($attributes) > 1 && !$allowMultipleAttributes) {
            throw new InvalidArgumentException(sprintf('Passing more than one Security attribute to "%s()" is not supported.', __METHOD__));
        }

        


namespace Authorization\Strategy;

use Symfony\Component\Security\Core\Authorization\Strategy\AffirmativeStrategy;
use Symfony\Component\Security\Core\Test\AccessDecisionStrategyTestCase;

class AffirmativeStrategyTest extends AccessDecisionStrategyTestCase
{
    public static function provideStrategyTests(): iterable
    {
        $strategy = new AffirmativeStrategy();

        yield [$strategy, self::getVoters(1, 0, 0), true];
        yield [$strategy, self::getVoters(1, 2, 0), true];
        yield [$strategy, self::getVoters(0, 1, 0), false];
        yield [$strategy, self::getVoters(0, 0, 1), false];

        $strategy = new AffirmativeStrategy(true);

        yield [$strategy, self::getVoters(0, 0, 1), true];
    }
}
Home | Imprint | This part of the site doesn't use cookies.