src/Controller/SecurityController.php line 49

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Domain\Password\PasswordChange;
  4. use App\Entity\AppUser;
  5. use App\Enum\Errors;
  6. use App\Enum\Events;
  7. use App\Events\SendPasswordLinkEvent;
  8. use App\Exception\PasswordException;
  9. use App\Form\PasswordChangeEmailType;
  10. use App\Form\PasswordChangeType;
  11. use App\Services\PasswordService;
  12. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  13. use Swift_Mailer;
  14. use Swift_Message;
  15. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  16. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  17. use Symfony\Component\HttpFoundation\RedirectResponse;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\HttpFoundation\Response;
  20. use Symfony\Component\Routing\Annotation\Route;
  21. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  22. /**
  23.  * Class SecurityController
  24.  * @package App\Controller
  25.  */
  26. class SecurityController extends AbstractController
  27. {
  28.     /** @var PasswordService */
  29.     private $passwordService;
  30.     /**
  31.      * SecurityController constructor.
  32.      * @param PasswordService $passwordService
  33.      */
  34.     public function __construct(PasswordService $passwordService)
  35.     {
  36.         $this->passwordService $passwordService;
  37.     }
  38.     /**
  39.      * @Route("/", name="app_root")
  40.      *
  41.      * @param AuthenticationUtils $authenticationUtils
  42.      * @return Response
  43.      */
  44.     public function redirectToLogin(AuthenticationUtils $authenticationUtils): Response
  45.     {
  46.         return $this->redirectToRoute('app_login');
  47.     }
  48.     /**
  49.      * @Route("/login", name="app_login")
  50.      *
  51.      * @param AuthenticationUtils $authenticationUtils
  52.      * @return Response
  53.      */
  54.     public function login(AuthenticationUtils $authenticationUtils): Response
  55.     {
  56.         $error $authenticationUtils->getLastAuthenticationError();
  57.         $lastUsername $authenticationUtils->getLastUsername();
  58.         return $this->render('security/login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  59.     }
  60.     /**
  61.      * @Route("/logout", name="app_logout")
  62.      */
  63.     public function logout()
  64.     {
  65.     }
  66.     /**
  67.      * @IsGranted("ROLE_EDITOR")
  68.      *
  69.      * @Route("/password/send/email/{id}", name="app_send_password_email", methods="POST|GET")
  70.      *
  71.      * @param Request $request
  72.      * @param AppUser $appUser
  73.      * @param EventDispatcherInterface $dispatcher
  74.      * @return RedirectResponse
  75.      */
  76.     public function sendPasswordReset(Request $requestAppUser $appUserEventDispatcherInterface $dispatcher)
  77.     {
  78.         try {
  79.             $passwordRequest $this->passwordService->createPasswordRequest($appUser);
  80.             $dispatcher->dispatch(new SendPasswordLinkEvent($passwordRequest), Events::SEND_PASSWORD_LINK_EMAIL);
  81.             $this->addFlash(
  82.                 'info',
  83.                 sprintf('Es wird in Kürze ein Passwort-Link an %s verschickt',
  84.                     $appUser->getFirstname() . ' ' $appUser->getLastname())
  85.             );
  86.         } catch (PasswordException $ex) {
  87.             $this->addFlash('error''Email kann nicht verschickt werden');
  88.         }
  89.         return $this->redirect($request->headers->get('referer'));
  90.     }
  91.     /**
  92.      * @Route("/password/reset", name="app_reset_password", methods="POST|GET")
  93.      *
  94.      * @param Request $request
  95.      * @param EventDispatcherInterface $dispatcher
  96.      * @return Response
  97.      */
  98.     public function resetPassword(Request $requestEventDispatcherInterface $dispatcher)
  99.     {
  100.         $form $this->createForm(PasswordChangeEmailType::class, new PasswordChange());
  101.         $form->handleRequest($request);
  102.         if ($form->isSubmitted() && $form->isValid()) {
  103.             try {
  104.                 /** @var PasswordChange $passwordChange */
  105.                 $passwordChange $form->getData();
  106.                 $passwordRequest $this->passwordService->createPasswordRequestByEmail($passwordChange->getEmail());
  107.                 $dispatcher->dispatch(new SendPasswordLinkEvent($passwordRequest), Events::SEND_PASSWORD_LINK_EMAIL);
  108.                 $this->addFlash('success''Sie erhalten in Kürze eine Email');
  109.             } catch (PasswordException $ex) {
  110.                 if ($ex->getCode() === Errors::UNKONWN_USER) {
  111.                     $this->addFlash('error''Email ist unbekannt');
  112.                 } else {
  113.                     $this->addFlash('error'$ex->getMessage());
  114.                 }
  115.             }
  116.         }
  117.         return $this->render('security/reset.password.html.twig', [
  118.             'form' => $form->createView(),
  119.         ]);
  120.     }
  121.     /**
  122.      * @Route("/password/{token}", name="app_change_password", methods="POST|GET")
  123.      *
  124.      * @param Request $request
  125.      * @param $token
  126.      * @param PasswordService $passwordService
  127.      * @return Response
  128.      */
  129.     public function changePassword(Request $request$tokenPasswordService $passwordService)
  130.     {
  131.         $form null;
  132.         try {
  133.             $passwordChange $passwordService->checkPasswordResetRequest($token);
  134.             $form $this->createForm(PasswordChangeType::class, $passwordChange, [
  135.                 'method' => 'POST',
  136.             ]);
  137.             $form->handleRequest($request);
  138.             if ($form->isSubmitted() && $form->isValid()) {
  139.                 if ($this->passwordService->changePassword($form->getData())) {
  140.                     $this->addFlash('success''Passwort wurde erfolgreich geändert');
  141.                 }
  142.             } elseif ($form->isSubmitted() && !$form->isValid()) {
  143.                 $this->passwordService->increaseTryCount($form->getData());
  144.             }
  145.         } catch (PasswordException $ex) {
  146.             if ($ex->getCode() === Errors::TO_MANY_TRYS_TO_CHANGE_PASSWORD) {
  147.                 $this->addFlash('error''Zu viele Versuche. Token ist jetzt ungültig');
  148.             } else {
  149.                 $this->addFlash('error''Token ist ungültig');
  150.             }
  151.         }
  152.         $view $form $form->createView() : null;
  153.         return $this->render('security/change.password.html.twig', [
  154.             'form' => $view,
  155.         ]);
  156.     }
  157.     /**
  158.      * @param Request $request
  159.      * @param string $email
  160.      * @param Swift_Mailer $mailer
  161.      * @return Response
  162.      *
  163.      * @Route("/send/test/mail/{email}", name="send_test_mail", methods="POST|GET")
  164.      */
  165.     public function sendMail(Request $requeststring $emailSwift_Mailer $mailer)
  166.     {
  167.         $message = (new Swift_Message('Hello Email'))
  168.             ->setFrom('info@trainee.mdm.dom')
  169.             ->setTo($email)
  170.             ->setBody('test mail',
  171.                 'text/plain'
  172.             );
  173.         $responseMessage 'Email konnte nicht gesendt werden';
  174.         $state Response::HTTP_INTERNAL_SERVER_ERROR;
  175.         if ($mailer->send($message)) {
  176.             $responseMessage 'Email wurde an: ' $email ' gesendet';
  177.             $state Response::HTTP_OK;
  178.         }
  179.         return new Response($responseMessage$state);
  180.     }
  181. }