<?php
namespace App\Controller;
use App\Entity\User;
use App\Helpers\AppHelper;
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;
class SecurityController extends AbstractController
{
public static function getSubscribedServices(){
return array_merge(parent::getSubscribedServices(), [
'Basecamp' => \App\Helper\Basecamp::class,
'settings' => \App\Helper\Settings::class
]);
}
/**
* @Route("/login", name="login")
*/
public function login(Request $request, AuthenticationUtils $authenticationUtils)
{
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
// var_dump($request);exit;
return $this->render('security/login.html.twig', array(
'last_username' => $lastUsername,
'error' => $error,
));
}
/**
* @Route("/remind_password", name="remind_password")
*/
public function remind_password(Request $request, \Swift_Mailer $mailer)
{
$error = false;
$msg = "Jeżeli istnieje użytkownik z podanym adresem email, na podany adres zostanie wysłany link do zmiany hasła.";
$sended = false;
if ($request->isMethod('POST') ) {
$email = $request->request->get('email');
if( $email != '' ){
$user = $this->getDoctrine()->getRepository(User::class)->findOneBy(['email' => $email]);
if( $user ){
$this->sendRemindPasswordEmail($user, $mailer);
$sended = true;
}else{
// $error = 'Użytkownik o podanym adresie nie istnieje.';
$sended = true;
}
}else{
$error = 'Podaj poprawny adres email';
}
}
return $this->render('security/remind.html.twig', array(
'error' => $error,
'sended' => $sended,
'msg' => $msg
));
}
/**
* @Route("/reset_password", name="reset_password")
*/
public function reset_password(Request $request, UserPasswordEncoderInterface $encoder)
{
$hash = $request->query->get('hash', false);
if( !$hash ){
return $this->redirectToRoute('home');
}
$user = $this->getDoctrine()->getRepository(User::class)->findOneBy(['hash' => $hash]);
$changed = false;
$error = false;
$username = '';
if ($request->isMethod('POST') ) {
$password = $request->request->get('_password');
$passwordConfirm = $request->request->get('_password_confirm');
if( $password == $passwordConfirm ){
try {
$password = $encoder->encodePassword($user, $password);
$user->setPassword($password);
$user->setHash(NULL);
$this->getDoctrine()->getManager()->persist($user);
$this->getDoctrine()->getManager()->flush();
$changed = true;
}catch(\Exception $e){
$error = $e->getMessage();
}
}else{
$error = "Podane hasła muszą być identyczne";
}
}
return $this->render('security/reset.html.twig', array(
'user' => $user,
'changed' => $changed,
'error' => $error,
'username' => $username
));
}
public function sendRemindPasswordEmail($user, $mailer){
$hash = md5(time() . '-/-' . $user->getEmail() . '-/-' . $user->__toString() );
$user->setHash($hash);
$this->getDoctrine()->getManager()->persist($user);
$this->getDoctrine()->getManager()->flush();
$emailSubject = '['.$this->container->get('settings')->get('appTitle').'] ' . "Reset hasła";
$emailBody = $this->renderView(
'_emails/remind_password.html.twig',
array( 'user' => $user, 'hash' => $hash )
);
$emailMessage = (new \Swift_Message($emailSubject))
// ->setFrom( [$this->container->getParameter('mailer_user') => $this->container->getParameter('mailer_sender')] )
->setFrom($this->container->get('settings')->get('mailSender'), $this->container->get('settings')->get('appTitle'))
->setTo( $user->getEmail() )
->setBody($emailBody, 'text/html');
$mailer->send($emailMessage);
}
/**
* @Route("/login_check", name="login_check")
*/
public function loginAction()
{
// The security layer will intercept this request, else redirect to login page
$this->addFlash('warning', $this->get('translator')->trans('login_expired'));
return $this->redirect($this->generateUrl('home'));
// die("LOL");
}
/**
* @Route("/logout", name="logout")
*/
public function logoutAction()
{
// The security layer will intercept this request, else redirect to login page
$this->addFlash('warning', $this->get('translator')->trans('login_expired'));
return $this->redirect($this->generateUrl('login'));
}
/**
* @Route("/access_denied", name="forbidden")
*/
public function forbidden($parent = false)
{
if( $parent )
return $this->render('security/forbidden_parent.html.twig', ['parent' => $parent]);
else
return $this->render('security/forbidden.html.twig', array());
}
/**
* @Route("/not_found", name="notfound")
*/
public function notfound()
{
return $this->render('security/notfound.html.twig', array());
}
}