src/Diplix/KMGBundle/Controller/AuthController.php line 57

Open in your IDE?
  1. <?php
  2. namespace Diplix\KMGBundle\Controller;
  3. use Diplix\Commons\DataHandlingBundle\Repository\SysLogRepository;
  4. use Diplix\KMGBundle\Entity\User;
  5. use Diplix\KMGBundle\Form\ForgotPasswordForm;
  6. use Diplix\KMGBundle\Form\ResetPasswordForm;
  7. use Diplix\KMGBundle\Helper\ClientConfigProvider;
  8. use Diplix\KMGBundle\Service\MailHelper;
  9. use Doctrine\ORM\NonUniqueResultException;
  10. use Symfony\Component\Form\FormError;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  13. use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
  14. use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
  15. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  16. class AuthController extends BaseController
  17. {
  18.     public function __construct(
  19.         protected ClientConfigProvider $ccp,
  20.         protected MailHelper $mailHelper,
  21.         protected UserPasswordHasherInterface $hasher,
  22.         private readonly \Doctrine\Persistence\ManagerRegistry $managerRegistry
  23.     )
  24.     {
  25.     }
  26.     /**
  27.      * Set encrypted pw using the default provider
  28.      * @param User $row
  29.      * @param string $newPw
  30.      * @return true
  31.      */
  32.     protected function setEncryptedUserPw(User $row$newPw)
  33.     {
  34.         $password $this->hasher->hashPassword($row,$newPw);
  35.         $row->setPassword($password);
  36.         return true;
  37.     }
  38.     public function loginAction(Request $request,AuthenticationUtils $authenticationUtils)
  39.     {
  40.         // get the login error if there is one
  41.         $error $authenticationUtils->getLastAuthenticationError();
  42.         // last username entered by the user
  43.         $lastUsername $authenticationUtils->getLastUsername();
  44.         return $this->render(
  45.             'Auth/login.html.twig',
  46.             array(
  47.                 // last username entered by the user
  48.                 'last_username' => $lastUsername,
  49.                 'error'         => $error,
  50.                 'platformSetup' => $this->ccp
  51.             )
  52.         );
  53.     }
  54.     public function forgotPasswordAction(Request $request)
  55.     {
  56.         $rep $this->managerRegistry->getManager()->getRepository(User::class);
  57.         $form $this->createForm(ForgotPasswordForm::class);
  58.         $form->handleRequest($request);
  59.         if ($form->isSubmitted())
  60.         {
  61.             $valid $form->isValid();
  62.             $email $form->get('email')->getData();
  63.             /** @var User $row */
  64.             try
  65.             {
  66.                 $row $rep->loadUserByUsername($email);
  67.             }
  68.             catch (\Throwable $x)
  69.             {
  70.                 $row false;
  71.                 $this->addFlash('danger',$x->getMessage());
  72.             }
  73.             if ( (!$valid) || (!is_object($row)) )
  74.             {
  75.                 $form->addError(new FormError("message.please-check-email"));
  76.                 return $this->render('@DiplixKMG/forgot_password.html.twig',array (
  77.                     "form" => $form->createView(),
  78.                 ));
  79.             }
  80.             $row->setMailActionMode(User::MAIL_ACTION_RESET_PASSWORD);
  81.             $row->setMailActionHashsha1(uniqid('',true)) );
  82.             $rep->flush($row);
  83.             $mh $this->mailHelper;
  84.             try {
  85.                 $mh->ResetPasswordMail($row);
  86.             }
  87.             catch (\Throwable $ex)
  88.             {
  89.                 SysLogRepository::logError($this->managerRegistry->getConnection(),$ex->getMessage(),null,0,'Mail');
  90.                 $this->addFlash('warning',$ex->getMessage());
  91.             }
  92.             $this->addFlash('success'"message.reset-instructions-by-mail");
  93.             return $this->redirect($this->generateUrl('forgot_password'));
  94.     }
  95.         else
  96.         {
  97.             $e $request->query->get("email","");
  98.             if ($e!=""$form->get('email')->setData($e);
  99.             return $this->render('@DiplixKMG/forgot_password.html.twig',array (
  100.                     "form" => $form->createView()
  101.             ));
  102.         }
  103.     }
  104.      public function resetPasswordAction(Request $request$email,$hash)
  105.     {
  106.         $rep $this->managerRegistry->getManager()->getRepository(User::class);
  107.         /** @var User $row */
  108.         $row $rep->getPendingPasswordResetUser($email,$hash);
  109.         if (is_object($row))
  110.         {
  111.             $form $this->createForm(ResetPasswordForm::class);
  112.             $form->handleRequest($request);
  113.             if ($form->isSubmitted())
  114.             {
  115.                 if ($form->isValid())
  116.                 {
  117.                     $this->setEncryptedUserPw($row$form->get('password')->getData());
  118.                     $row->setMailActionMode(User::MAIL_ACTION_NONE);
  119.                     $row->setMailActionHash("");
  120.                     $row->setLastPasswordChange(new \DateTime());
  121.                     $row->setSharedKey(''); // key was encrypted with old pw - it is not usable any more anyway - remove it
  122.                     $rep->flush($row);
  123.                     $this->addFlash('info'"message.password-was-reset");
  124.                     return $this->redirect($this->generateUrl("sys-login"));
  125.                 }
  126.             }
  127.             return $this->render('@DiplixKMG/reset_password.html.twig',array (
  128.                     "form" => $form->createView(),
  129.                     'show_key_warning' => ($row->getSharedKey()!==''),
  130.                     "row"=> $row,
  131.             ));
  132.         }
  133.         else
  134.         {
  135.             $this->addFlash("warning","message.invalid-link");
  136.             return $this->redirectToRoute("kmg_home");
  137.         }
  138.     }
  139.     public function registerAction(Request $request)
  140.     {
  141.         return $this->render('@DiplixKMG/register.html.twig');
  142.     }
  143.     public function unregisterAction(Request $request)
  144.     {
  145.         $ff $this->createForm(ForgotPasswordForm::class);
  146.         $ff->handleRequest($request);
  147.         if ($ff->isSubmitted()&&$ff->isValid())
  148.         {
  149.             $mh $this->get('diplix.mailhelper');
  150.             try {
  151.                 $mh->sendUserRemovalRequest($ff->get('email')->getData());
  152.             }
  153.             catch (\Throwable $ex)
  154.             {
  155.                 SysLogRepository::logError($this->managerRegistry->getConnection(),$ex->getMessage(),null,0,'Mail');
  156.                 //$this->addFlash('warning',$ex->getMessage());
  157.             }
  158.             $this->addFlash('success','Vielen Dank. Wir haben Ihre Anfrage erhalten und werden Sie in Kürze kontaktieren.');
  159.             return $this->redirectToRoute('unregister');
  160.         }
  161.         return $this->render('@DiplixKMG/unregister.html.twig',
  162.         [
  163.             'form'=>$ff->createView()
  164.         ]);
  165.     }
  166.     public function jsonLoginAction(Request $request)
  167.     {
  168.         /** @var User $user */
  169.         $user $this->getUser();
  170.         return $this->json([
  171.             'username' => $user->getUserIdentifier(),
  172.             'success' => true,
  173.         ]);
  174.     }
  175. }