<?php
namespace App\Controller;
use App\Entity\SystemUser;
use App\Entity\User;
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\TextType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
class LoginController extends AbstractController
{
private UserPasswordHasherInterface $passwordHasher;
public function __construct(UserPasswordHasherInterface $passwordHasher){
$this->passwordHasher = $passwordHasher;
}
/**
* @Route("/web/login", name="app_login")
* @param AuthenticationUtils $authenticationUtils
* @return Response
*/
public function login(AuthenticationUtils $authenticationUtils): 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();
return $this->render('security/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("/web/update", name="app_login_update")
* @return Response
*/
public function updateUser(): Response
{
$em = $this->getDoctrine()->getManager();
$user = $em->getRepository(User::class)->findOneBy([
'phone' => '0716308459'
]);
$user->setPassword($this->passwordHasher->hashPassword(
$user,
'mirage'
));
/** @var array $array */
$array = $user->getRoles();
// dump($array);
// dump(array_push($array, 'ROLE_WEB'));
// $user->setRoles(array_push($array, array('ROLE_WEB')));
$user->setIsActive(true);
$em->flush();
// return new Response('update complete', Response::HTTP_OK);
return $this->render('security/update.html.twig');
}
/**
* @Route("/web/sign_up", name="web_signup")
*/
public function webSignUp(Request $request)
{
$em = $this->getDoctrine()->getManager();
$signUpForm = $this->signUpForm();
$signUpForm->handleRequest($request);
if($signUpForm->isSubmitted() && $signUpForm->isValid()){
$phone = $signUpForm->get('phone')->getData();
$password = $signUpForm->get('password')->getData();
$systemUser = $em->getRepository(SystemUser::class)->findOneBy([
'phone' => $phone
]);
if(!$systemUser){
$this->addFlash('error', 'Your account is not web ready - Contact ADMIN');
return $this->redirectToRoute('web_signup');
}
$user = $em->getRepository('App:User')->findOneBy([
'systemUser' => $systemUser
]);
if(!$user){
$this->addFlash('error', 'Your account is not web ready - Contact ADMIN');
return $this->redirectToRoute('web_signup');
}
if($user->getIsActive()){
$this->addFlash('info', 'User already activated: please login!');
return $this->redirectToRoute('app_login');
}
if(!in_array('ROLE_WEB', $user->getRoles())){
$this->addFlash('warning', 'Your account is not web ready - Contact ADMIN');
return $this->redirectToRoute('web_signup');
}
$pass = $this->passwordHasher->hashPassword($user, $password);
$user->setPassword($pass);
$user->setIsActive(true);
$em->flush();
$this->addFlash('info', 'Sign Up successful :-)');
return $this->redirectToRoute('app_login');
}
return $this->render('security/username.html.twig', [
'form' => $signUpForm->createView()
]);
}
private function signUpForm(): FormInterface
{
$sF = $this->get('form.factory')->createNamedBuilder('sign_up_form');
return $sF
->add('phone', TextType::class,[
'constraints' => [
new NotBlank(),
new Length(['min' => 10, 'minMessage'=> 'Enter a valid phone number'])
]
])
->add('password', RepeatedType::class,[
'constraints' => [
new NotBlank(['message' => 'Please enter a password']),
new Length(['min' => 8,'minMessage' => 'Password should be at least 8 characters'])
],
'type' => PasswordType::class,
'invalid_message' => 'The Password fields must match',
'required' => true,
'first_options' => [
'label' => 'Password'
],
'second_options' => [
'label' => 'Repeat Password'
]
])
->setAction($this->generateUrl('web_signup'))
->setMethod('POST')
->getForm();
}
}