<?php
namespace App\Controller;
use App\Form\PasswordChangeType;
use App\Services\ActivityLogger;
use App\Services\MailNotificator;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use App\Form\RegistrationType;
use App\Entity\User;
use App\Controller\ActivityCheckController;
use App\Entity\LastActivity;
class SecurityController extends AbstractController implements ActivityCheckController {
/**
* @Route("/login", name="login")
*/
public function loginAction(AuthenticationUtils $authenticationUtils) {
$error = $authenticationUtils->getLastAuthenticationError();
$lastUsername = $authenticationUtils->getLastUsername();
if ($error && $error->getMessage() == 'Bad credentials.') {
$this->addFlash('danger', 'security-bad-credentials');
}
return $this->render('user/login.html.twig', [
'last_username' => $lastUsername,
'error' => $error,
]);
}
/**
* @Route("/login-message", name="login-message")
*/
public function loginMessageAction() {
//$this->addFlash('info', 'Úspešne ste sa prihlásili, aj ked nebolo treba!');
return $this->redirectToRoute('user-account');
}
/**
* @Route("/logout", name="logout")
*/
public function logoutAction() {
//$this->addFlash('warning', 'W. - logout, presmerovanie na login.');
return $this->redirectToRoute('login');
}
/**
* @Route("/logout-message", name="logout-message")
*/
public function logoutMessageAction() {
$this->addFlash('warning', 'security-log-out');
return $this->redirectToRoute('login');
}
/**
* @Route("/registration", name="registration")
*/
public function registrationAction(
Request $request,
MailNotificator $mailNotificator,
ActivityLogger $activityLogger,
UserPasswordEncoderInterface $encoder
) {
if ($this->getUser()) {
return $this->redirectToRoute('user-account');
}
$user = new User();
$form = $this->createForm(RegistrationType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$user = $form->getData();
if ($form->get('address')->getData()) {
$this->addFlash('warning', 'security-registration-failed');
return $this->redirectToRoute('homepage');
}
/*
if ($form->get('address')->getData() || !$form->get('gdpr')->getData()) {
$this->addFlash('warning', 'security-registration-failed');
return $this->redirectToRoute('homepage');
}
*/
$encoded = $encoder->encodePassword($user, $user->getPassword());
$user->setPassword($encoded);
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
$this->addFlash('success', 'security-registration-success');
if (!$this->getUser()) {
$this->manualAuthenticateUser($em, $user, $activityLogger);
// $this->container->get('mailNotificator')->sendMail('ID:' . $user->getId() . ', Meno: ' . $user->getUsername() . ', e-mail: ' . $user->getEmail(), 'registrácia');
$mailNotificator->sendMail('ID:' . $user->getId() . ', Meno: ' . $user->getUsername() . ', e-mail: ' . $user->getEmail(), 'registrácia');
}
return $this->redirectToRoute('user-account');
}
return $this->render('user/registration.html.twig', [
'user' => $user,
'form' => $form->createView(),
]);
}
private function manualAuthenticateUser($em, $user, ActivityLogger $activityLogger) {
$token = new UsernamePasswordToken($user, null, 'main', array('ROLE_USER'));
$this->get('security.token_storage')->setToken($token);
$this->get('session')->set('_security_main', serialize($token));
$activityLogger->logActivity($em, $user, 'login-now');
}
/**
* @Route("/my-password-change", name="my-password-change")
*/
public function passwordChangeAction(
Request $request,
UserPasswordEncoderInterface $encoder,
ActivityLogger $activityLogger
) {
$user = $this->getUser();
$form = $this->createForm(PasswordChangeType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$user = $form->getData();
if ($form->get('password1')->getData() != $form->get('password2')->getData()) {
$this->addFlash('danger', 'Zmena hesla zlyhala. Heslo a potvrdenie hesla sa nezhodujú.');
return $this->redirectToRoute('my-password-change');
}
// if arent strong enough length 8 special characters, number, upper, lower
if (!preg_match('/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*(_|[^\w])).+$/', $form->get('password1')->getData())) {
$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.');
return $this->redirectToRoute('my-password-change');
}
$encoded = $encoder->encodePassword($user, $form->get('password1')->getData());
$user->setPassword($encoded);
// $user->setPasswordChangedAt(new \DateTime());
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
$this->addFlash('success', 'Zmena hesla bola úspešná');
$activityLogger->logActivity($em, $user, 'my-password-change');
return $this->redirectToRoute('user-account');
}
return $this->render('user/password_change.html.twig', [
'user' => $user,
'form' => $form->createView(),
]);
}
}