lib/Ntl/User/src/Security/UserVoter.php line 19

  1. <?php
  2. /**
  3.  * This file is part of the NautilePlus package.
  4.  *
  5.  * (c) Nicolas SOBOLEV <nicolas.sobolev@nautile.sarl>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Ntl\UserBundle\Security;
  11. use Ntl\UserBundle\Entity\NtlApiUser as User;
  12. use Ntl\UserBundle\Repository\NtlApiUserRepository as UserRepository;
  13. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  14. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  15. class UserVoter extends Voter
  16. {
  17.     // these strings are just invented: you can use anything
  18.     const VIEW 'view';
  19.     const EDIT 'edit';
  20.     const CHANGE_PWD 'change_password';
  21.     const RESET_PWD 'reset_password';
  22.     /**
  23.      * User repository     
  24.      */
  25.     private $userRepository;
  26.     /**
  27.      * User repository injection
  28.      *
  29.      * @param UserRepository $userRepository
  30.      */
  31.     public function __construct(UserRepository $userRepository)
  32.     {
  33.         $this->userRepository $userRepository;
  34.     }
  35.     protected function supports(string $attribute$nic): bool
  36.     {
  37.         // if the attribute isn't one we support, return false
  38.         if (!in_array($attribute, [self::VIEWself::EDITself::CHANGE_PWD])) {
  39.             return false;
  40.         }
  41.         // check user identifier
  42.         if (!$nic) {
  43.             return false;
  44.         }
  45.         return true;
  46.     }
  47.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  48.     {
  49.         $loggedUser $token->getUser();
  50.         if (!$loggedUser instanceof User) {
  51.             // the user must be logged in; if not, deny access
  52.             return false;
  53.         }
  54.         // $subject is a nic, thanks to `supports()`                
  55.         switch ($attribute) {
  56.             case self::VIEW:
  57.                 return $this->canView($subject$loggedUser);
  58.             case self::EDIT:
  59.                 return $this->canEdit($subject$loggedUser);
  60.             case self::CHANGE_PWD:
  61.                 return $this->canChangePassword($subject$loggedUser);
  62.             case self::RESET_PWD:
  63.                 return $this->canResetPassword($subject$loggedUser);
  64.         }
  65.         throw new \LogicException('This code should not be reached!');
  66.     }
  67.     private function canView(string $nicUser $loggedUser): bool
  68.     {
  69.         // if one can edit the view permission goes naturally
  70.         if ($this->canEdit($nic$loggedUser)) {
  71.             return true;
  72.         }
  73.         return ($loggedUser->getUserIdentifier() === $nic);
  74.     }
  75.     private function canEdit(string $nicUser $loggedUser): bool
  76.     {
  77.         // $emailConfirmed = $this->userRepository->isEmailConfirmed($nic, $loggedUser->getEmail());
  78.         // $mobileConfirmed = $this->userRepository->isMobileConfirmed($nic, $loggedUser->getMobile());
  79.         return (($loggedUser->getUserIdentifier() === $nic/*&& $emailConfirmed*/ /*&& $mobileConfirmed*/);
  80.     }
  81.     private function canResetPassword(string $nicUser $loggedUser): bool
  82.     {
  83.         $emailConfirmed $this->userRepository->isEmailConfirmed($nic$loggedUser->getEmail());
  84.         return ($this->canView($nic$loggedUser) && $emailConfirmed);
  85.     }
  86.     private function canChangePassword(string $nicUser $loggedUser): bool
  87.     {
  88.         // $emailConfirmed = $this->userRepository->isEmailConfirmed($nic, $loggedUser->getEmail());
  89.         return ($this->canView($nic$loggedUser)/* && $emailConfirmed*/);
  90.     }
  91. }