register OK

This commit is contained in:
Gnieark 2019-12-27 11:14:34 +01:00
parent 8a0e27a287
commit c486d83d3f
4 changed files with 46 additions and 52 deletions

View File

@ -1,16 +1,16 @@
index: index:
path: / path: /
controller: App\Controller\HomeController::index controller: App\Controller\HomeController::index
login: login:
path: /login path: /login
controller: App\Controller\SecurityController::login controller: App\Controller\SecurityController::login
methods: GET|POST methods: GET|POST
register: registerForm:
path: /register path: /register
controller: App\Controller\SecurityController::show_register_form controller: App\Controller\SecurityController::showRegisterForm
methods: GET|POST methods: GET
register: register:
path: /register path: /register
controller: App\Controller\SecurityController::register controller: App\Controller\SecurityController::register

View File

@ -11,14 +11,13 @@ use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils; use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use App\Entity\User; use App\Entity\User;
class SecurityController extends AbstractController class SecurityController extends AbstractController
{ {
/**
* @Route("/login", name="app_login")
*/
public function login(AuthenticationUtils $authenticationUtils): Response public function login(AuthenticationUtils $authenticationUtils): Response
{ {
// get the login error if there is one // get the login error if there is one
@ -29,26 +28,19 @@ class SecurityController extends AbstractController
return $this->render('security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error]); return $this->render('security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error]);
} }
/**
* @Route("/logout", name="app_logout")
*/
public function logout() public function logout()
{ {
throw new \Exception('This method can be blank - it will be intercepted by the logout key on your firewall'); throw new \Exception('This method can be blank - it will be intercepted by the logout key on your firewall');
} }
/** public function showRegisterForm()
* @Route("/register", name="register")
*/
public function show_register_form()
{ {
return $this->render('security/register.html.twig', [ return $this->render('security/register.html.twig', [
'form' => $this->get_registerform()->createView() 'form' => $this->getRegisterForm()->createView()
]); ]);
} }
private function get_registerform() private function getRegisterForm()
{ {
return $this->createFormBuilder() return $this->createFormBuilder()
->add('email', EmailType::class) ->add('email', EmailType::class)
@ -59,11 +51,46 @@ class SecurityController extends AbstractController
} }
public function getNbUsersActives() {
$em = $this->getDoctrine()->getManager();
$repoUser = $em->getRepository(User::class);
$totalUsers = $repoUser->createQueryBuilder('u')
->select('count(u.id)')
->where('u.active= 1')
->getQuery()
->getSingleScalarResult();
return $totalUsers;
}
public function register(Request $request, UserPasswordEncoderInterface $passwordEncoder) public function register(Request $request, UserPasswordEncoderInterface $passwordEncoder)
{ {
$form = $this->get_registerform(); $form = $this->getRegisterForm();
$form->handleRequest($request); $form->handleRequest($request);
echo "hey";
if ($form->isSubmitted() && $form->isValid())
{
$data = $form->getData();
$entityManager = $this->getDoctrine()->getManager();
$user = new User();
$user ->setEmail($data["email"])
->setPassword( $passwordEncoder->encodePassword($user,$data["password"]) )
->setDisplayName( $data["display_name"] );
if( $this->getNbUsersActives() == 0 )
{
//it's the first user, he will be activated and added to group SUPER_ADMIN
$user->setActive(true)
->setRoles( array('SUPER_ADMIN'));
}else{
$user->setActive(false);
}
$entityManager->persist($user);
$entityManager->flush();
return $this->redirectToRoute('index',[]);
}
} }

View File

@ -1,33 +0,0 @@
<?php
namespace App\DataFixtures;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
class UserFixtures extends Fixture
{
private $passwordEncoder;
public function __construct(UserPasswordEncoderInterface $passwordEncoder)
{
$this->passwordEncoder = $passwordEncoder;
}
public function load(ObjectManager $manager)
{
$user = new User();
$user->setPassword($this->passwordEncoder->encodePassword(
$user,
'the_new_password'
));
$manager->flush();
}
}

View File

@ -99,6 +99,6 @@ class LoginFormAuthentificatorAuthenticator extends AbstractFormLoginAuthenticat
protected function getLoginUrl() protected function getLoginUrl()
{ {
return $this->urlGenerator->generate('app_login'); return $this->urlGenerator->generate('login');
} }
} }