2018-09-24 18:42:29 +02:00
|
|
|
<?php
|
2018-10-29 19:01:00 +01:00
|
|
|
|
2018-09-24 18:42:29 +02:00
|
|
|
namespace App\Security;
|
|
|
|
|
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
|
|
|
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
|
|
|
|
use App\DBAL\Types\RightType;
|
2018-10-03 16:14:15 +02:00
|
|
|
use App\Entity\Source\SourceInterface;
|
2018-09-24 18:42:29 +02:00
|
|
|
use App\DBAL\Types\LayerType;
|
2018-10-03 15:50:46 +02:00
|
|
|
use App\Entity\UserInterface;
|
2018-09-24 18:42:29 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author kevinfrantz
|
2018-10-29 19:01:00 +01:00
|
|
|
*
|
2018-09-24 18:42:29 +02:00
|
|
|
* @see https://symfony.com/doc/current/security/voters.html
|
|
|
|
*/
|
|
|
|
class SourceVoter extends Voter
|
|
|
|
{
|
|
|
|
/**
|
2018-10-29 19:01:00 +01:00
|
|
|
* @var string[]
|
2018-09-24 18:42:29 +02:00
|
|
|
* @var SourceInterface $subject
|
2018-10-29 19:01:00 +01:00
|
|
|
* {@inheritdoc}
|
|
|
|
*
|
2018-09-24 18:42:29 +02:00
|
|
|
* @see \Symfony\Component\Security\Core\Authorization\Voter\Voter::supports()
|
|
|
|
*/
|
|
|
|
protected function supports($attribute, $subject)
|
|
|
|
{
|
2018-10-29 19:01:00 +01:00
|
|
|
return $this->checkInstance($subject) && $this->checkRight($attribute);
|
2018-09-24 18:42:29 +02:00
|
|
|
}
|
2018-10-29 19:01:00 +01:00
|
|
|
|
2018-09-24 18:42:29 +02:00
|
|
|
//private function checkLayer(string $layer):bool{
|
|
|
|
// return (in_array($right, array_keys(LayerType::getChoices())));
|
|
|
|
//}
|
2018-10-29 19:01:00 +01:00
|
|
|
|
|
|
|
private function checkRight(string $right): bool
|
|
|
|
{
|
|
|
|
return in_array($right, array_keys(RightType::getChoices()));
|
2018-09-24 18:42:29 +02:00
|
|
|
}
|
2018-10-29 19:01:00 +01:00
|
|
|
|
|
|
|
private function checkInstance($subject): bool
|
|
|
|
{
|
|
|
|
return $subject instanceof SourceInterface;
|
2018-09-24 18:42:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @todo add if father, that it should have all rights!
|
2018-10-29 19:01:00 +01:00
|
|
|
*
|
|
|
|
* @param string[] $attribute
|
2018-09-24 18:42:29 +02:00
|
|
|
* @param SourceInterface $subject
|
2018-10-29 19:01:00 +01:00
|
|
|
* @param TokenInterface $token
|
|
|
|
* {@inheritdoc}
|
|
|
|
*
|
2018-09-24 18:42:29 +02:00
|
|
|
* @see \Symfony\Component\Security\Core\Authorization\Voter\Voter::voteOnAttribute()
|
|
|
|
*/
|
|
|
|
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
|
|
|
|
{
|
|
|
|
/**
|
2018-10-29 19:01:00 +01:00
|
|
|
* @var UserInterface
|
2018-09-24 18:42:29 +02:00
|
|
|
*/
|
|
|
|
$user = $token->getUser();
|
2018-10-29 19:01:00 +01:00
|
|
|
|
2018-09-24 18:42:29 +02:00
|
|
|
return $subject->getNode()
|
|
|
|
->getLaw()
|
|
|
|
->isGranted($user->getSource()->getNode(), LayerType::SOURCE, $attribute);
|
|
|
|
}
|
|
|
|
}
|