<?php
/**
* Eventfix - SecurityController.php
* --------------------------------------------------------------------------
* Created by: mhs
* Created on: 12.10.2020
* --------------------------------------------------------------------------
* Copyright (c) 2020 | Michael Hack Software e.K. | www.mh-s.de
*/
namespace App\Controller;
use App\Base\Controller;
use App\Entity\Token;
use App\Entity\User;
use App\Form;
use App\Service\Messaging;
use App\Service\UserManager;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
/**
* An-/Abmeldung
* @Route("/", name="security.")
*/
class SecurityController extends Controller {
private Messaging $messaging;
private UserManager $user_manager;
public function __construct(Messaging $messaging, UserManager $user_manager) {
$this->messaging = $messaging;
$this->user_manager = $user_manager;
}
/**
* Login
* @Route("/", name="login")
*
* @param AuthenticationUtils $authenticationUtils
*
* @return Response
*/
public function login(AuthenticationUtils $authenticationUtils) {
$error = $authenticationUtils->getLastAuthenticationError();
$username = $authenticationUtils->getLastUsername();
if ($this->user()) {
return $this->redirectToRoute("backend.main.dashboard");
}
return $this->renderTemplate('backend/security/login.html.twig', compact('error', 'username'));
}
/**
* Gesperrtes Benutzerkonto
* @Route("/locked", name="locked")
*
* @return Response
*/
public function locked() {
return $this->renderTemplate('backend/security/locked.html.twig');
}
/**
* Logout
* @Route("/logout", name="logout")
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
public function logout() {
return $this->redirectToRoute("security.login");
}
/**
* Passwort vergessen
* @Route("/passwort/vergessen", name="password_forgotten")
*
* @param Request $request
*
* @return Response
*/
public function password_forgotten(Request $request) {
$form = $this->createForm(Form\PasswordForgotten::class, []);
$form->handleRequest($request);
// Formular verarbeiten
if ($form->isSubmitted() && $form->isValid()) {
$values = $form->getData();
// E-Mail prüfen
if (!filter_var($values['mail'], FILTER_VALIDATE_EMAIL)) {
$this->addFlash('warning', 'Fehlerhafte E-Mail Adresse');
} else {
// Benutzer suchen und Passwort zusenden
if ($user = $this->db()->Users()->byMail($values['mail'])->get()) {
$this->user_manager->resetPasswort($user);
}
$this->addFlash('confirmation', 'Wenn uns die E-Mail Adresse bekannt ist, erhalten Sie in den nächsten Minuten ein neues Passwort.');
}
}
return $this->renderTemplate('backend/security/password_forgotten.html.twig', ['form' => $form->createView()]);
}
/**
* Passwort wiederherstellen
* @Route("passwort/reset/{token}", name="password_reset")
*
* @param Request $request
* @param string $token
*
* @return Response
*/
public function password_reset(Request $request, string $token) : Response {
// Token ermitteln
if (!$reset = $this->db()->Tokens()->search(Token::TYPE_PASSWORD, $token)) {
return $this->showError(404);
}
// Benutzer prüfen
$user = $reset->getUser();
if (!$user || $user->getState() != User::STATE_ACTIVE) {
return $this->showError(403);
}
// Formular erstellen
$form = $this->createForm(Form\PasswordReset::class, []);
$form->handleRequest($request);
// Formular verarbeiten
if ($form->isSubmitted() && $form->isValid()) {
$values = $form->getData();
// Passwort prüfen
if ($this->user_manager->checkPassword($values['pwd_1'], $values['pwd_2'])) {
// Passwort übernehmen
$user->setPassword($values['pwd_1']);
$user->removeToken($reset);
$this->db()->flush();
$this->addFlash('success', 'Das Passwort wurde erfolgreich geändert. Du kannst Dich jetzt anmelden.');
// Loginseite
return $this->redirectToRoute('security.login');
} else {
$this->addFlash('danger', 'Dein Passwort konnte nicht geändert werden. Bitte beachte die Sicherheitsrichtlinien (min. 8 Zeichen).');
}
}
return $this->renderTemplate('backend/security/password_reset.html.twig', ['form' => $form->createView()]);
}
}