From c486d83d3f32d7a176253da99010dcab7c4dde1c Mon Sep 17 00:00:00 2001 From: Gnieark Date: Fri, 27 Dec 2019 11:14:34 +0100 Subject: [PATCH] register OK --- config/routes.yaml | 8 +-- src/Controller/SecurityController.php | 55 ++++++++++++++----- src/DataFixtures/UserFixtures.php | 33 ----------- .../LoginFormAuthentificatorAuthenticator.php | 2 +- 4 files changed, 46 insertions(+), 52 deletions(-) delete mode 100644 src/DataFixtures/UserFixtures.php diff --git a/config/routes.yaml b/config/routes.yaml index cbfb37b..eb0ccc7 100644 --- a/config/routes.yaml +++ b/config/routes.yaml @@ -1,16 +1,16 @@ index: path: / controller: App\Controller\HomeController::index - login: path: /login controller: App\Controller\SecurityController::login methods: GET|POST -register: +registerForm: path: /register - controller: App\Controller\SecurityController::show_register_form - methods: GET|POST + controller: App\Controller\SecurityController::showRegisterForm + methods: GET + register: path: /register controller: App\Controller\SecurityController::register diff --git a/src/Controller/SecurityController.php b/src/Controller/SecurityController.php index 2f14d46..985862b 100644 --- a/src/Controller/SecurityController.php +++ b/src/Controller/SecurityController.php @@ -11,14 +11,13 @@ use Symfony\Component\Form\Extension\Core\Type\SubmitType; 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; use App\Entity\User; class SecurityController extends AbstractController { - /** - * @Route("/login", name="app_login") - */ + public function login(AuthenticationUtils $authenticationUtils): Response { // 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]); } - /** - * @Route("/logout", name="app_logout") - */ public function logout() { throw new \Exception('This method can be blank - it will be intercepted by the logout key on your firewall'); } - /** - * @Route("/register", name="register") - */ - - public function show_register_form() + public function showRegisterForm() { 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() ->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) { - $form = $this->get_registerform(); + $form = $this->getRegisterForm(); $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',[]); + } } diff --git a/src/DataFixtures/UserFixtures.php b/src/DataFixtures/UserFixtures.php deleted file mode 100644 index 9a661d5..0000000 --- a/src/DataFixtures/UserFixtures.php +++ /dev/null @@ -1,33 +0,0 @@ -passwordEncoder = $passwordEncoder; - } - - - - public function load(ObjectManager $manager) - { - $user = new User(); - $user->setPassword($this->passwordEncoder->encodePassword( - $user, - 'the_new_password' - )); - - $manager->flush(); - } -} diff --git a/src/Security/LoginFormAuthentificatorAuthenticator.php b/src/Security/LoginFormAuthentificatorAuthenticator.php index d6126b9..49362f8 100644 --- a/src/Security/LoginFormAuthentificatorAuthenticator.php +++ b/src/Security/LoginFormAuthentificatorAuthenticator.php @@ -99,6 +99,6 @@ class LoginFormAuthentificatorAuthenticator extends AbstractFormLoginAuthenticat protected function getLoginUrl() { - return $this->urlGenerator->generate('app_login'); + return $this->urlGenerator->generate('login'); } }