src/Controller/SecurityController.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Form\PasswordChangeType;
  4. use App\Services\ActivityLogger;
  5. use App\Services\MailNotificator;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  10. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  11. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  12. use App\Form\RegistrationType;
  13. use App\Entity\User;
  14. use App\Controller\ActivityCheckController;
  15. use App\Entity\LastActivity;
  16. class SecurityController extends AbstractController implements ActivityCheckController {
  17.     /**
  18.      * @Route("/login", name="login")
  19.      */
  20.     public function loginAction(AuthenticationUtils $authenticationUtils) {
  21.         $error $authenticationUtils->getLastAuthenticationError();
  22.         $lastUsername $authenticationUtils->getLastUsername();
  23.         if ($error && $error->getMessage() == 'Bad credentials.') {
  24.             $this->addFlash('danger''security-bad-credentials');
  25.         }
  26.         return $this->render('user/login.html.twig', [
  27.             'last_username' => $lastUsername,
  28.             'error' => $error,
  29.         ]);
  30.     }
  31.     /**
  32.      * @Route("/login-message", name="login-message")
  33.      */
  34.     public function loginMessageAction() {
  35.         //$this->addFlash('info', 'Úspešne ste sa prihlásili, aj ked nebolo treba!');
  36.         return $this->redirectToRoute('user-account');
  37.     }
  38.     /**
  39.      * @Route("/logout", name="logout")
  40.      */
  41.     public function logoutAction() {
  42.         //$this->addFlash('warning', 'W. - logout, presmerovanie na login.');
  43.         return $this->redirectToRoute('login');
  44.     }
  45.     /**
  46.      * @Route("/logout-message", name="logout-message")
  47.      */
  48.     public function logoutMessageAction() {
  49.         $this->addFlash('warning''security-log-out');
  50.         return $this->redirectToRoute('login');
  51.     }
  52.     /**
  53.      * @Route("/registration", name="registration")
  54.      */
  55.     public function registrationAction(
  56.         Request                      $request,
  57.         MailNotificator              $mailNotificator,
  58.         ActivityLogger               $activityLogger,
  59.         UserPasswordEncoderInterface $encoder
  60.     ) {
  61.         if ($this->getUser()) {
  62.             return $this->redirectToRoute('user-account');
  63.         }
  64.         $user = new User();
  65.         $form $this->createForm(RegistrationType::class, $user);
  66.         $form->handleRequest($request);
  67.         if ($form->isSubmitted() && $form->isValid()) {
  68.             $user $form->getData();
  69.             
  70.             if ($form->get('address')->getData()) {
  71.                 $this->addFlash('warning''security-registration-failed');
  72.                 return $this->redirectToRoute('homepage');
  73.             }
  74.             /*
  75.             if ($form->get('address')->getData() || !$form->get('gdpr')->getData()) {
  76.                 $this->addFlash('warning', 'security-registration-failed');
  77.                 return $this->redirectToRoute('homepage');
  78.             }
  79.             */
  80.             $encoded $encoder->encodePassword($user$user->getPassword());
  81.             $user->setPassword($encoded);
  82.             $em $this->getDoctrine()->getManager();
  83.             $em->persist($user);
  84.             $em->flush();
  85.             $this->addFlash('success''security-registration-success');
  86.             if (!$this->getUser()) {
  87.                 $this->manualAuthenticateUser($em$user$activityLogger);
  88. //                $this->container->get('mailNotificator')->sendMail('ID:' . $user->getId() . ', Meno: ' . $user->getUsername() . ', e-mail: ' . $user->getEmail(), 'registrácia');
  89.                 $mailNotificator->sendMail('ID:' $user->getId() . ', Meno: ' $user->getUsername() . ', e-mail: ' $user->getEmail(), 'registrácia');
  90.             }
  91.             return $this->redirectToRoute('user-account');
  92.         }
  93.         return $this->render('user/registration.html.twig', [
  94.             'user' => $user,
  95.             'form' => $form->createView(),
  96.         ]);
  97.     }
  98.     private function manualAuthenticateUser($em$userActivityLogger $activityLogger) {
  99.         $token = new UsernamePasswordToken($usernull'main', array('ROLE_USER'));
  100.         $this->get('security.token_storage')->setToken($token);
  101.         $this->get('session')->set('_security_main'serialize($token));
  102.         $activityLogger->logActivity($em$user'login-now');
  103.     }
  104.     /**
  105.      * @Route("/my-password-change", name="my-password-change")
  106.      */
  107.     public function passwordChangeAction(
  108.         Request                      $request,
  109.         UserPasswordEncoderInterface $encoder,
  110.         ActivityLogger               $activityLogger
  111.     ) {
  112.         $user $this->getUser();
  113.         $form $this->createForm(PasswordChangeType::class, $user);
  114.         $form->handleRequest($request);
  115.         if ($form->isSubmitted() && $form->isValid()) {
  116.             $user $form->getData();
  117.             if ($form->get('password1')->getData() != $form->get('password2')->getData()) {
  118.                 $this->addFlash('danger''Zmena hesla zlyhala. Heslo a potvrdenie hesla sa nezhodujú.');
  119.                 return $this->redirectToRoute('my-password-change');
  120.             }
  121.             // if arent strong enough length 8 special characters, number, upper, lower
  122.             if (!preg_match('/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*(_|[^\w])).+$/'$form->get('password1')->getData())) {
  123.                 $this->addFlash('danger''Zmena hesla bola slabá. Heslo musí mať minimálne 8 znakov, obsahovať špeciálne znaky, číslice, veľké a malé písmená. Prosím, skúste použiť silnejšie heslo.');
  124.                 return $this->redirectToRoute('my-password-change');
  125.             }
  126.             $encoded $encoder->encodePassword($user$form->get('password1')->getData());
  127.             $user->setPassword($encoded);
  128. //            $user->setPasswordChangedAt(new \DateTime());
  129.             $em $this->getDoctrine()->getManager();
  130.             $em->persist($user);
  131.             $em->flush();
  132.             $this->addFlash('success''Zmena hesla bola úspešná');
  133.             $activityLogger->logActivity($em$user'my-password-change');
  134.             return $this->redirectToRoute('user-account');
  135.         }
  136.         return $this->render('user/password_change.html.twig', [
  137.             'user' => $user,
  138.             'form' => $form->createView(),
  139.         ]);
  140.     }
  141. }