Merge pull request #1 from gnieark/registerform

Registerform
pull/3/head
Gnieark 4 years ago committed by GitHub
commit d6db462065
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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

@ -4,16 +4,20 @@ namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
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
@ -24,11 +28,70 @@ 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');
}
public function showRegisterForm()
{
return $this->render('security/register.html.twig', [
'form' => $this->getRegisterForm()->createView()
]);
}
private function getRegisterForm()
{
return $this->createFormBuilder()
->add('email', EmailType::class)
->add('password', PasswordType::class, [])
->add('display_name', TextType::class,[])
->add('save', SubmitType::class)
->getForm();
}
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->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',[]);
}
}
}

@ -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();
}
}

@ -33,6 +33,16 @@ class User implements UserInterface
*/
private $password;
/**
* @ORM\Column(type="boolean")
*/
private $active;
/**
* @ORM\Column(type="string", length=255)
*/
private $display_name;
public function getId(): ?int
{
return $this->id;
@ -110,4 +120,28 @@ class User implements UserInterface
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
public function getActive(): ?bool
{
return $this->active;
}
public function setActive(bool $active): self
{
$this->active = $active;
return $this;
}
public function getDisplayName(): ?string
{
return $this->display_name;
}
public function setDisplayName(string $display_name): self
{
$this->display_name = $display_name;
return $this;
}
}

@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20191226142335 extends AbstractMigration
{
public function getDescription() : string
{
return '';
}
public function up(Schema $schema) : void
{
// this up() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');
$this->addSql('ALTER TABLE user ADD active TINYINT(1) NOT NULL, ADD display_name VARCHAR(255) NOT NULL');
}
public function down(Schema $schema) : void
{
// this down() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');
$this->addSql('ALTER TABLE user DROP active, DROP display_name');
}
}

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

@ -6,6 +6,7 @@
{% block stylesheets %}{% endblock %}
</head>
<body>
<header>{% block header %}<h1>Resources booking manager</h1>{% endblock %}</header>
{% block body %}{% endblock %}
{% block javascripts %}{% endblock %}
</body>

@ -0,0 +1,8 @@
{% extends 'base.html.twig' %}
{% block title %}Register{% endblock %}
{% block header %}<h1>Register a new user</h1>{% endblock %}
{% block body %}
{{ form(form) }}
{% endblock %}
Loading…
Cancel
Save