/** * @Route("/register", name="registration") */ public function registerAction(Request $request) { // 1) build the form $user = new User(); $user->setIsActive(false); $user->setIsAdmin(false); $form = $this->createForm(new UserType(), $user); // 2) handle the submit (will only happen on POST) $form->handleRequest($request); if (!$form->isValid()) { echo $form->getErrorsAsString(); } if ($form->isValid() && $form->isSubmitted()) { $user->setPassword($user->getPlainPassword()); $user->setIsActive(false); // 4) save the User! $em = $this->getDoctrine()->getManager(); $em->persist($user); $em->flush(); // ... do any other work - like send them an email, etc // maybe set a "flash" success message for the user return new Response("<html>USUARIO REGISTRADO CORRECTAMENTE</html>"); } return $this->render('registration/register.html.twig', array('form' => $form->createView())); }
/** * @Route("/register", name="user_registration") */ public function registerAction(Request $request) { // 1) build the form $user = new User(); $user->setIsActive(true); $user->setIsAdmin(false); $form = $this->createForm(UserType::class, $user); // 2) handle the submit (will only happen on POST) $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { // 3) Encode the password (you could also do this via Doctrine listener) $password = $this->get('security.password_encoder')->encodePassword($user, $user->getPlainPassword()); $user->setPassword($password); // 4) save the User! $em = $this->getDoctrine()->getManager(); $em->persist($user); $em->flush(); // ... do any other work - like send them an email, etc // maybe set a "flash" success message for the user return $this->redirectToRoute('homepage'); } return $this->render('security/register.html.twig', array('form' => $form->createView())); }
protected function execute(InputInterface $input, OutputInterface $output) { $em = $this->getContainer()->get('doctrine')->getManager(); $users = $em->getRepository('AppBundle:User')->findAll(); if ($users) { foreach ($users as $user) { if ($user->getIsAdmin()) { throw new \RuntimeException('Admin already exists!'); } } } $name = $input->getArgument('name'); $email = $input->getArgument('email'); $plainPassword = $input->getArgument('password'); $userExists = $em->getRepository('AppBundle:User')->findOneBy(array('username' => $name)); $userWithEmailExists = $em->getRepository('AppBundle:User')->findOneBy(array('email' => $email)); if ($userExists) { throw new \RuntimeException('User already exists: ' . $name); } if ($userWithEmailExists) { throw new \RuntimeException('User with this email already exists: ' . $email); } $admin = new User(); $admin->setIsAdmin(true); $admin->setUsername($name); $admin->setEmail($email); $admin->setPlainPassword($plainPassword); $password = $this->getContainer()->get('security.password_encoder')->encodePassword($admin, $admin->getPlainPassword()); $admin->setPassword($password); $em->persist($admin); $em->flush(); $output->writeln('Admin created!'); }