lib/Ntl/User/src/Security/UserVoter.php line 19
<?php/*** This file is part of the NautilePlus package.** (c) Nicolas SOBOLEV <nicolas.sobolev@nautile.sarl>** For the full copyright and license information, please view the LICENSE* file that was distributed with this source code.*/namespace Ntl\UserBundle\Security;use Ntl\UserBundle\Entity\NtlApiUser as User;use Ntl\UserBundle\Repository\NtlApiUserRepository as UserRepository;use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;use Symfony\Component\Security\Core\Authorization\Voter\Voter;class UserVoter extends Voter{// these strings are just invented: you can use anythingconst VIEW = 'view';const EDIT = 'edit';const CHANGE_PWD = 'change_password';const RESET_PWD = 'reset_password';/*** User repository*/private $userRepository;/*** User repository injection** @param UserRepository $userRepository*/public function __construct(UserRepository $userRepository){$this->userRepository = $userRepository;}protected function supports(string $attribute, $nic): bool{// if the attribute isn't one we support, return falseif (!in_array($attribute, [self::VIEW, self::EDIT, self::CHANGE_PWD])) {return false;}// check user identifierif (!$nic) {return false;}return true;}protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool{$loggedUser = $token->getUser();if (!$loggedUser instanceof User) {// the user must be logged in; if not, deny accessreturn false;}// $subject is a nic, thanks to `supports()`switch ($attribute) {case self::VIEW:return $this->canView($subject, $loggedUser);case self::EDIT:return $this->canEdit($subject, $loggedUser);case self::CHANGE_PWD:return $this->canChangePassword($subject, $loggedUser);case self::RESET_PWD:return $this->canResetPassword($subject, $loggedUser);}throw new \LogicException('This code should not be reached!');}private function canView(string $nic, User $loggedUser): bool{// if one can edit the view permission goes naturallyif ($this->canEdit($nic, $loggedUser)) {return true;}return ($loggedUser->getUserIdentifier() === $nic);}private function canEdit(string $nic, User $loggedUser): bool{// $emailConfirmed = $this->userRepository->isEmailConfirmed($nic, $loggedUser->getEmail());// $mobileConfirmed = $this->userRepository->isMobileConfirmed($nic, $loggedUser->getMobile());return (($loggedUser->getUserIdentifier() === $nic) /*&& $emailConfirmed*/ /*&& $mobileConfirmed*/);}private function canResetPassword(string $nic, User $loggedUser): bool{$emailConfirmed = $this->userRepository->isEmailConfirmed($nic, $loggedUser->getEmail());return ($this->canView($nic, $loggedUser) && $emailConfirmed);}private function canChangePassword(string $nic, User $loggedUser): bool{// $emailConfirmed = $this->userRepository->isEmailConfirmed($nic, $loggedUser->getEmail());return ($this->canView($nic, $loggedUser)/* && $emailConfirmed*/);}}