Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
75.00% |
3 / 4 |
CRAP | |
90.48% |
19 / 21 |
| UserVoter | |
0.00% |
0 / 1 |
|
75.00% |
3 / 4 |
14.17 | |
90.48% |
19 / 21 |
| __construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
| supports | |
100.00% |
1 / 1 |
3 | |
100.00% |
5 / 5 |
|||
| voteOnAttribute | |
0.00% |
0 / 1 |
4.18 | |
77.78% |
7 / 9 |
|||
| checkAuthorization | |
100.00% |
1 / 1 |
6 | |
100.00% |
5 / 5 |
|||
| <?php | |
| namespace App\Security\Voter; | |
| use App\Entity\Usertodo; | |
| // use Symfony\Component\Security\Core\User\UserInterface; | |
| use Symfony\Component\Security\Core\Authorization\Voter\Voter; | |
| use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | |
| use Symfony\Component\Security\Core\Security; | |
| class UserVoter extends Voter | |
| { | |
| const UPDATE = 'update'; | |
| const DELETE = 'delete'; | |
| /** | |
| * @var Security | |
| */ | |
| private $security; | |
| public function __construct(Security $security) | |
| { | |
| $this->security = $security; | |
| } | |
| protected function supports($attribute, $subject): bool | |
| { | |
| // if the attribute isn't one we support, return false | |
| if (!in_array($attribute, [self::UPDATE, self::DELETE])) { | |
| return false; | |
| } | |
| // only vote on `Usertodo` objects | |
| if (!$subject instanceof Usertodo) { | |
| return false; | |
| } | |
| return true; | |
| } | |
| protected function voteOnAttribute($attribute, $subject, TokenInterface $token) | |
| { | |
| $user = $token->getUser(); | |
| if (!$user instanceof Usertodo) { | |
| // the user must be logged in; if not, deny access | |
| return false; | |
| } | |
| // $subject is a Usertodo object | |
| /** @var Usertodo $user */ | |
| $user = $subject; | |
| switch ($attribute) { | |
| case self::UPDATE: | |
| // return $this->canUpdate($user); | |
| return $this->checkAuthorization($user); | |
| case self::DELETE: | |
| // return $this->canDelete($user); | |
| return $this->checkAuthorization($user); | |
| } | |
| throw new \LogicException('This code should not be reached!'); | |
| } | |
| private function checkAuthorization(Usertodo $user) | |
| { | |
| // AJOUTÉ POUR EMPÊCHER UN ADMINISTRATEUR OU UN MANAGER DE MODIFIER SON PROPRE COMPTE | |
| // AJOUTÉ POUR EMPÊCHER UN ADMINISTRATEUR DE MODIFIER LE COMPTE ANONYME | |
| // AJOUTÉ POUR EMPÊCHER UN ADMINISTRATEUR DE MODIFIER UN COMPTE SUPER ADMIN | |
| if (($this->security->getUser()->getId() === $user->getId()) || | |
| (!$this->security->isGranted('ROLE_SUPER_ADMIN') && $user->getRole() === 'ROLE_ANONYMOUS') || | |
| (!$this->security->isGranted('ROLE_SUPER_ADMIN') && $user->getRole() === 'ROLE_SUPER_ADMIN')) | |
| { | |
| return false; | |
| // throw new \Exception('Operation denied'); | |
| } else { | |
| return true; | |
| } | |
| } | |
| /* private function canUpdate(Usertodo $user) | |
| { | |
| // AJOUTÉ POUR EMPÊCHER UN ADMINISTRATEUR OU UN MANAGER DE MODIFIER SON PROPRE COMPTE | |
| // AJOUTÉ POUR EMPÊCHER UN ADMINISTRATEUR DE MODIFIER LE COMPTE ANONYME | |
| // AJOUTÉ POUR EMPÊCHER UN ADMINISTRATEUR DE MODIFIER UN COMPTE SUPER ADMIN | |
| if (($this->security->getUser()->getId() === $user->getId()) || | |
| (!$this->security->isGranted('ROLE_SUPER_ADMIN') && $user->getRole() === 'ROLE_ANONYMOUS') || | |
| (!$this->security->isGranted('ROLE_SUPER_ADMIN') && $user->getRole() === 'ROLE_SUPER_ADMIN')) | |
| { | |
| return false; | |
| // throw new \Exception('Operation denied'); | |
| } else { | |
| return true; | |
| } | |
| } */ | |
| /* private function canDelete(Usertodo $user) | |
| { | |
| // AJOUTÉ POUR EMPÊCHER UN ADMINISTRATEUR OU UN MANAGER DE SUPPRIMER SON PROPRE COMPTE | |
| // AJOUTÉ POUR EMPÊCHER UN ADMINISTRATEUR DE SUPPRIMER LE COMPTE ANONYME | |
| // AJOUTÉ POUR EMPÊCHER UN ADMINISTRATEUR DE SUPPRIMER UN COMPTE SUPER ADMIN | |
| // if($this->security->getUser()->getId() === $user->getId()) | |
| if (($this->security->getUser()->getId() === $user->getId()) || | |
| (!$this->security->isGranted('ROLE_SUPER_ADMIN') && $user->getRole() === 'ROLE_ANONYMOUS') || | |
| (!$this->security->isGranted('ROLE_SUPER_ADMIN') && $user->getRole() === 'ROLE_SUPER_ADMIN')) | |
| { | |
| return false; | |
| // throw new \Exception('Operation denied'); | |
| } else { | |
| return true; | |
| } | |
| } */ | |
| } |