src/Controller/SecurityController.php line 46

Open in your IDE?
  1. <?php
  2. /**
  3.  * Eventfix - SecurityController.php
  4.  * --------------------------------------------------------------------------
  5.  * Created by: mhs
  6.  * Created on: 12.10.2020
  7.  * --------------------------------------------------------------------------
  8.  * Copyright (c) 2020 | Michael Hack Software e.K. | www.mh-s.de
  9.  */
  10. namespace App\Controller;
  11. use App\Base\Controller;
  12. use App\Entity\Token;
  13. use App\Entity\User;
  14. use App\Form;
  15. use App\Service\Messaging;
  16. use App\Service\UserManager;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\Routing\Annotation\Route;
  20. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  21. /**
  22.  * An-/Abmeldung
  23.  * @Route("/", name="security.")
  24.  */
  25. class SecurityController extends Controller {
  26.     private Messaging   $messaging;
  27.     private UserManager $user_manager;
  28.     public function __construct(Messaging $messagingUserManager $user_manager) {
  29.         $this->messaging    $messaging;
  30.         $this->user_manager $user_manager;
  31.     }
  32.     /**
  33.      * Login
  34.      * @Route("/", name="login")
  35.      *
  36.      * @param AuthenticationUtils $authenticationUtils
  37.      *
  38.      * @return Response
  39.      */
  40.     public function login(AuthenticationUtils $authenticationUtils) {
  41.         $error    $authenticationUtils->getLastAuthenticationError();
  42.         $username $authenticationUtils->getLastUsername();
  43.         if ($this->user()) {
  44.             return $this->redirectToRoute("backend.main.dashboard");
  45.         }
  46.         return $this->renderTemplate('backend/security/login.html.twig'compact('error''username'));
  47.     }
  48.     /**
  49.      * Gesperrtes Benutzerkonto
  50.      * @Route("/locked", name="locked")
  51.      *
  52.      * @return Response
  53.      */
  54.     public function locked() {
  55.         return $this->renderTemplate('backend/security/locked.html.twig');
  56.     }
  57.     /**
  58.      * Logout
  59.      * @Route("/logout", name="logout")
  60.      *
  61.      * @return \Symfony\Component\HttpFoundation\RedirectResponse
  62.      */
  63.     public function logout() {
  64.         return $this->redirectToRoute("security.login");
  65.     }
  66.     /**
  67.      * Passwort vergessen
  68.      * @Route("/passwort/vergessen", name="password_forgotten")
  69.      *
  70.      * @param Request $request
  71.      *
  72.      * @return Response
  73.      */
  74.     public function password_forgotten(Request $request) {
  75.         $form $this->createForm(Form\PasswordForgotten::class, []);
  76.         $form->handleRequest($request);
  77.         // Formular verarbeiten
  78.         if ($form->isSubmitted() && $form->isValid()) {
  79.             $values $form->getData();
  80.             // E-Mail prüfen
  81.             if (!filter_var($values['mail'], FILTER_VALIDATE_EMAIL)) {
  82.                 $this->addFlash('warning''Fehlerhafte E-Mail Adresse');
  83.             } else {
  84.                 // Benutzer suchen und Passwort zusenden
  85.                 if ($user $this->db()->Users()->byMail($values['mail'])->get()) {
  86.                     $this->user_manager->resetPasswort($user);
  87.                 }
  88.                 $this->addFlash('confirmation''Wenn uns die E-Mail Adresse bekannt ist, erhalten Sie in den nächsten Minuten ein neues Passwort.');
  89.             }
  90.         }
  91.         return $this->renderTemplate('backend/security/password_forgotten.html.twig', ['form' => $form->createView()]);
  92.     }
  93.     /**
  94.      * Passwort wiederherstellen
  95.      * @Route("passwort/reset/{token}", name="password_reset")
  96.      *
  97.      * @param Request $request
  98.      * @param string  $token
  99.      *
  100.      * @return Response
  101.      */
  102.     public function password_reset(Request $requeststring $token) : Response {
  103.         // Token ermitteln
  104.         if (!$reset $this->db()->Tokens()->search(Token::TYPE_PASSWORD$token)) {
  105.             return $this->showError(404);
  106.         }
  107.         // Benutzer prüfen
  108.         $user $reset->getUser();
  109.         if (!$user || $user->getState() != User::STATE_ACTIVE) {
  110.             return $this->showError(403);
  111.         }
  112.         // Formular erstellen
  113.         $form $this->createForm(Form\PasswordReset::class, []);
  114.         $form->handleRequest($request);
  115.         // Formular verarbeiten
  116.         if ($form->isSubmitted() && $form->isValid()) {
  117.             $values $form->getData();
  118.             // Passwort prüfen
  119.             if ($this->user_manager->checkPassword($values['pwd_1'], $values['pwd_2'])) {
  120.                 // Passwort übernehmen
  121.                 $user->setPassword($values['pwd_1']);
  122.                 $user->removeToken($reset);
  123.                 $this->db()->flush();
  124.                 $this->addFlash('success''Das Passwort wurde erfolgreich geändert. Du kannst Dich jetzt anmelden.');
  125.                 // Loginseite
  126.                 return $this->redirectToRoute('security.login');
  127.             } else {
  128.                 $this->addFlash('danger''Dein Passwort konnte nicht geändert werden. Bitte beachte die Sicherheitsrichtlinien (min. 8 Zeichen).');
  129.             }
  130.         }
  131.         return $this->renderTemplate('backend/security/password_reset.html.twig', ['form' => $form->createView()]);
  132.     }
  133. }