<?php
/**
* This file is part of the educat package.
*
* (c) Solvee
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace App\School\Application\Security;
use App\Common\Model\Core\UserInterface;
use App\Common\Model\School\TutoringScopeInterface;
use App\Common\Security\ObjectOwnerVoterInterface;
use App\Common\Security\ObjectOwnerVoterTrait;
use App\Common\Security\VoterTrait;
use LogicException;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use function in_array;
/**
* Class TutoringScopeVoter
*
* @author Kamil KozaczyĆski <kozaczynski.kamil@gmail.com>
*/
class TutoringScopeVoter extends Voter implements ObjectOwnerVoterInterface
{
use VoterTrait, ObjectOwnerVoterTrait;
/**
* @inheritDoc
*/
protected function supports(string $attribute, $subject): bool
{
if (!in_array(
$attribute,
[
self::OBJECT_OWNER,
],
true
)) {
return false;
}
return $subject instanceof TutoringScopeInterface;
}
/**
* @param string $attribute
* @param TutoringScopeInterface $subject
* @param TokenInterface $token
*
* @return bool
*/
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof UserInterface) {
return false;
}
return match ($attribute) {
self::OBJECT_OWNER => $this->isObjectOwner($subject, $user->getAccount()),
default => throw new LogicException('This code should not be reached.')
};
}
}