<?php
namespace App\Controller;
use App\Service\ProfileClass;
use App\Entity\User;
use App\Repository\UserRepository;
use Symfony\Component\Uid\Uuid;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Doctrine\ORM\EntityManagerInterface;
class SecurityController extends AbstractController
{
/**
* @Route("/login", name="app_login", schemes={"https"})
*/
public function login(Request $request, AuthenticationUtils $authenticationUtils, UserPasswordHasherInterface $passwordHasher): Response
{
// if ($this->getUser()) {
// return $this->redirectToRoute('target_path');
// }
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
$regform = $this->createFormBuilder(null, array('attr' => array('class' => 'php-email-form', 'method' => 'post', 'action' => $this->generateUrl('app_reg'))))
->setAttribute('class', 'php-email-form')
->add('username', TextType::class,[
'required' => true,
'label' => 'Benutzer',
'attr' => array(
'class' => 'form-control'),
'label_attr' => array(
'class' => 'form-label'),
])
->add('password', RepeatedType::class,[
'required' => true,
'type' => PasswordType::class,
'first_options' => [
'label' => 'Passwort',
'attr' => array(
'class' => 'form-control',
),
'label_attr' => array(
'class' => 'form-label'),
],
'second_options' => [
'label' => 'Passwort wiederholen',
'attr' => array(
'class' => 'form-control',
),
'label_attr' => array(
'class' => 'form-label'),
],
])
->add('email', TextType::class,[
'required' => true,
'label' => 'E-Mail',
'attr' => array(
'class' => 'form-control'),
'label_attr' => array(
'class' => 'form-label'),
])
->add('btnSave', SubmitType::class,[
'label' => 'Registrieren',
])
->getForm();
return $this->render('security/login.html.twig', [
'regform' => $regform->createView(),
'last_username' => $lastUsername,
'error' => $error
]);
}
/**
* @Route("/logout", name="app_logout", schemes={"https"})
*/
public function logout(): void
{
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
/**
* @Route("/reg", name="app_reg", schemes={"https"})
*/
public function reg(Request $request, UserPasswordHasherInterface $passwordHasher, UserRepository $repUser, MailerInterface $mailer):Response
{
$regform = $this->createFormBuilder(null, array('attr' => array('method' => 'post')))
->setAttribute('class', 'php-email-form')
->add('username', TextType::class,[
'required' => true,
'label' => 'Benutzer',
'attr' => array(
'class' => 'form-control'),
'label_attr' => array(
'class' => 'form-label'),
])
->add('password', RepeatedType::class,[
'required' => true,
'type' => PasswordType::class,
'first_options' => [
'label' => 'Passwort',
'attr' => array(
'class' => 'form-control',
),
'label_attr' => array(
'class' => 'form-label'),
],
'second_options' => [
'label' => 'Passwort wiederholen',
'attr' => array(
'class' => 'form-control',
),
'label_attr' => array(
'class' => 'form-label'),
],
])
->add('email', TextType::class,[
'required' => true,
'label' => 'E-Mail',
'attr' => array(
'class' => 'form-control'),
'label_attr' => array(
'class' => 'form-label'),
])
->add('btnSave', SubmitType::class,[
'label' => 'Registrieren',
])
->getForm();
$regform->handleRequest($request);
if ($regform->isSubmitted() && $regform->isValid()) {
$user = new User;
$uuid = Uuid::v6();
$user->setUid($uuid);
$user->setUsername($regform['username']->getData());
$user->setPassword($passwordHasher->hashPassword($user, $regform['password']->getData()));
$user->setEmail($regform['email']->getData());
$user->setActivated(0);
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
// Zusatzdaten erstellen
$service = new ProfileClass;
$userId = $repUser->getUserId($regform['username']->getData());
$service->registerUser($userId, $em);
$uid = $repUser->getUid($userId);
$email = (new Email())
->from('noreply@heylotte.de')
->to($regform['email']->getData())
->subject('Willkommen bei HEY:LOTTE !')
->text('
Hey '.$regform['username']->getData().'
wir heißen dich auf der Lernplattform HEY:LOTTE herzlich willkommen! Schön, dass du zusammen mit uns deinen Lernstoff erfassen und dann abfragen möchtest.
Dein Account ist erstellt und dir ist der Plan "For Free" zugewiesen worden. Du kannst also sofort völlig kostenlos loslegen und HEY:LOTTE kennenlernen.
Aktiviere nun noch deinen Account, indem du auf den folgenden Link klickst oder ihn in einen Browser kopierst.
https://heylotte.de/reg/'.$uid.'
Viel Spaß wünscht das Team von
HEY:LOTTE
');
$mailer->send($email);
return $this->redirect($this->generateUrl('app_registersuccess'));
}
//return $this->redirect($this->generateUrl('app_registersuccess'));
}
/**
* @Route("/reg/{uid}", name="app_reg2", schemes={"https"})
*/
public function reg2($uid, UserRepository $repUser)
{
$em = $this->getDoctrine()->getManager();
$service = new ProfileClass;
$optin = $service->checkOptIn($uid, $repUser, $em);
return $this->render('home/registersuccess.html.twig', [
'optin' => $optin,
]);
}
/**
* @Route("/password", name="app_password", schemes={"https"})
*/
public function password(Request $request, UserRepository $repUser, MailerInterface $mailer, UserPasswordHasherInterface $passwordHasher): Response
{
$pwform = $this->createFormBuilder(null, array('attr' => array('method' => 'post')))
->setAttribute('class', 'php-email-form')
->add('searchvalue', TextType::class,[
'required' => true,
'label' => false,
])
->getForm();
$pwform->handleRequest($request);
if ($pwform->isSubmitted() && $pwform->isValid()) {
$searchvalue = $pwform['searchvalue']->getData();
$service = new ProfileClass;
$em = $this->getDoctrine()->getManager();
$service->forgotPasswort($searchvalue, $repUser, $em, $mailer, $passwordHasher);
return $this->redirect($this->generateUrl('app_login'));
}
return $this->render('security/password.html.twig', [
'pwform' => $pwform->createView()
]);
}
/**
* @Route("/newpassword/{uid}", name="app_newpassword", schemes={"https"})
*/
public function newPassword($uid, Request $request, UserRepository $repUser, EntityManagerInterface $em, UserPasswordHasherInterface $passwordHasher): Response
{
$newpwform = $this->createFormBuilder(null, array('attr' => array('method' => 'post', 'class' => 'php-email-form',)))
->add('password', RepeatedType::class,[
'required' => true,
'type' => PasswordType::class,
'first_options' => [
'label' => 'Passwort',
'attr' => array(
'class' => 'form-control',
),
'label_attr' => array(
'class' => 'form-label'),
],
'second_options' => [
'label' => 'Passwort wiederholen',
'attr' => array(
'class' => 'form-control',
),
'label_attr' => array(
'class' => 'form-label'),
],
])
->add('btnSave', SubmitType::class,[
'label' => 'Passwort ändern',
])
->getForm();
$newpwform->handleRequest($request);
if ($newpwform->isSubmitted() && $newpwform->isValid()) {
$searchvalue = $newpwform['password']->getData();
// User ermitteln
$userId = $repUser->getUserId5ForgotPassword($uid);
$UserClass = 'App\Entity\User';
$user = $em->getReference($UserClass, $userId);
// PW verschlüsseln
$searchvalue = $passwordHasher->hashPassword($user, $searchvalue);
// neues PW speichern
$service = new ProfileClass;
$em = $this->getDoctrine()->getManager();
$service->setNewPasswort($uid, $searchvalue, $em);
return $this->redirect($this->generateUrl('app_login'));
}
return $this->render('security/newpassword.html.twig', [
'newpwform' => $newpwform->createView()
]);
}
}