vendor/kematjaya/user-bundle/src/Controller/KmjSecurityController.php line 43

Open in your IDE?
  1. <?php
  2. namespace Kematjaya\UserBundle\Controller;
  3. use Kematjaya\UserBundle\Config\RoutingConfigurationFactoryInterface;
  4. use Kematjaya\UserBundle\Form\LoginType;
  5. use Kematjaya\UserBundle\Exception\UserNotFoundException;
  6. use Kematjaya\UserBundle\Entity\ClientChangePassword;
  7. use Kematjaya\UserBundle\Form\ChangePasswordType;
  8. use Kematjaya\UserBundle\Repo\KmjUserRepoInterface;
  9. use Kematjaya\UserBundle\Controller\AbstractKmjController;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  13. class KmjSecurityController extends AbstractKmjController
  14. {
  15.     /**
  16.      * 
  17.      * @var AuthenticationUtils
  18.      */
  19.     protected $authenticationUtils;
  20.     
  21.     /**
  22.      * 
  23.      * @var KmjUserRepoInterface
  24.      */
  25.     protected $userRepo;
  26.     
  27.     public function __construct(RoutingConfigurationFactoryInterface $routingConfigurationFactoryAuthenticationUtils $authenticationUtilsKmjUserRepoInterface $userRepo
  28.     {
  29.         $this->authenticationUtils $authenticationUtils;
  30.         $this->userRepo $userRepo;
  31.         
  32.         parent::__construct($routingConfigurationFactory);
  33.     }
  34.     
  35.     /**
  36.      * Login page
  37.      * 
  38.      * @return Response
  39.      */
  40.     public function login(): Response
  41.     {
  42.         if ($this->getUser()) {
  43.             $this->addFlash("info"'wellcome back : '$this->getUser()->getUsername());
  44.             
  45.             return $this->redirectToRoute($this->getRoutingConfiguration()->getLoginSuccessRedirectPath($this->getUser()->getRoles()));
  46.         }
  47.         $error $this->authenticationUtils->getLastAuthenticationError();
  48.         $lastUsername $this->authenticationUtils->getLastUsername();
  49.         $form $this->createForm(LoginType::class, [
  50.             'username' => $lastUsername
  51.         ]);
  52.         
  53.         return $this->render('@User/security/login.html.twig', [
  54.             'last_username' => $lastUsername
  55.             'error' => $error,
  56.             'form' => $form->createView()
  57.         ]);
  58.     }
  59.     
  60.     public function logout()
  61.     {
  62.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  63.     }
  64.     
  65.     /**
  66.      * Change password page
  67.      * 
  68.      * @param  Request $request
  69.      * @return Response
  70.      * @throws UserNotFoundException
  71.      */
  72.     public function changePassword(Request $request) : Response
  73.     {
  74.         if (!$this->getUser()) {
  75.             
  76.             return $this->redirectToRoute('kmj_user_logout');
  77.         }
  78.         
  79.         $user $this->userRepo->find($this->getUser()->getId());
  80.         if (!$user) {
  81.             throw new UserNotFoundException($this->getUser()->getId());
  82.         }
  83.         
  84.         $redirect $this->getRoutingConfiguration()->getLoginSuccessRedirectPath($this->getUser()->getRoles());
  85.         $form $this->createForm(ChangePasswordType::class, new ClientChangePassword($user), ["action" => $this->generateUrl("kmj_user_change_password")]);
  86.         $object parent::processForm($request$form);
  87.         if ($object) {
  88.             
  89.             return $this->redirectToRoute($redirect);
  90.         }
  91.         
  92.         return $this->render(
  93.             '@User/security/change_password.html.twig', array(
  94.             'title' => 'change_password',
  95.             'form' => $form->createView(),
  96.             'back_path' => $redirect
  97.             )
  98.         );
  99.     }
  100.     
  101.     /**
  102.      * Saving object into database
  103.      * 
  104.      * @param  ClientChangePassword   $object
  105.      * @param  EntityManagerInterface $manager
  106.      * @return type
  107.      */
  108.     protected function saveObject($object, \Doctrine\ORM\EntityManagerInterface $manager
  109.     {
  110.         if (!$object instanceof ClientChangePassword) {
  111.             throw new \Exception(sprintf("object type not allowed: %s"ClientChangePassword::class));
  112.         }
  113.         
  114.         return parent::saveObject($object->getUser(), $manager);
  115.     }
  116. }