<?php
namespace App\Controller;
use App\Entity\User;
use App\Service\SocketlabsService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class SecurityController extends AbstractController
{
/**
* @Route("/login", name="app_login")
*/
public function login(AuthenticationUtils $authenticationUtils): Response
{
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('theme_' . config_theme . '/login.html.twig', ['last_username' => $lastUsername, 'error' => $error]);
}
/**
* @Route("/logout", name="app_logout")
*/
public function logout()
{
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
/**
* @Route("/register", name="app_register")
*/
public function register(Request $request, UserPasswordEncoderInterface $encoder, SocketlabsService $socketlabs)
{
$message = '';
$p = [];
if ($request->getMethod() == 'POST') {
$p = $_POST;
#pre($p,1);
$username = $p['email'];
if (filter_var($username,FILTER_VALIDATE_EMAIL)) {
$em = $this->getDoctrine()->getManager();
$user_exists = $em->getRepository(User::class)->findBy(['email' => $username]);
if (!$user_exists) {
$password = $p['password'];
$firstname = $p['firstname'];
$lastname = $p['lastname'];
$phone = $p['phone'];
$address = $p['address'];
$postalnumber = $p['postalnumber'];
$postalname = $p['postalname'];
$user = new User();
$user->setEmail($username);
$user->setPassword($encoder->encodePassword($user,$password));
$user->setFirstname($firstname);
$user->setLastname($lastname);
$user->setPhone($phone);
$user->setAddress($address);
$user->setPostalnumber($postalnumber);
$user->setPostalname($postalname);
$em->persist($user);
$em->flush();
$html = '<b>Hei,</b><br>';
$html .= '<br>';
$html .= 'Du er registrert som bruker på Etikettbutikken!<br><br>';
$html .= 'Brukernavn : ' . $username . '<br>';
$html .= 'Passord : ' . $password . '<br>';
$html .= '<br>';
$html .= '<a href="https://web2print.raaness.no/login">Klikk her for å logge inn!</a>';
$socketlabs->sendEmail($username,$firstname . ' ' . $lastname,'Takk for registrering',$html);
$message = 'Bruker "' . $username . '" opprettet! Bekreftelse er sendt på mail. <a class="text-bold" href="/login">Logg inn!</a>';
} else {
// User exists...
$message = 'Bruker eksisterer allerede...';
}
} else {
// Invalid email address
$message = 'Ulovlig email-adresse...';
}
}
return $this->render('theme_' . config_theme . '/register.html.twig', [
'message' => $message,
'post' => $p,
]);
}
/**
* @Route("/forgotpasswd", name="app_forgotpasswd")
*/
public function forgotpasswd(Request $request, UserPasswordEncoderInterface $encoder, SocketlabsService $socketlabs)
{
$new_password = '';
if ($request->getMethod() == 'POST') {
$new_password = generateRandomString(8);
$em = $this->getDoctrine()->getManager();
$user = $em->getRepository(User::class)->findOneBy(['email' => $_POST['email']]);
$user->setPassword($encoder->encodePassword($user,$new_password));
$em->persist($user);
$em->flush();
$html = 'Ditt nye passord er : ' . $new_password;
$mail_response = $socketlabs->sendEmail($user->getEmail(),$user->getFirstname() . ' ' . $user->getLastname(),'Nytt passord',$html);
}
return $this->render('theme_' . config_theme . '/forgotpasswd.html.twig', [
'new_password' => $new_password,
]);
}
}