Skip to content

Commit

Permalink
Add registration form
Browse files Browse the repository at this point in the history
  • Loading branch information
codedmonkey committed May 27, 2024
1 parent 7135800 commit 1f2fd59
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/Controller/Dashboard/DashboardRootController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class DashboardRootController extends AbstractDashboardController
public function __construct(
#[Autowire(param: 'conductor.title')]
private readonly string $title,
#[Autowire(param: 'conductor.security.registration_enabled')]
private readonly bool $registrationEnabled,
) {
}

Expand All @@ -43,6 +45,10 @@ public function configureMenuItems(): iterable
yield MenuItem::linkToLogout('Sign out', 'fa fa-user');
} else {
yield MenuItem::linkToRoute('Sign in', 'fa fa-user', 'dashboard_login');

if ($this->registrationEnabled) {
yield MenuItem::linkToRoute('Register', 'fa fa-user-plus', 'dashboard_register');
}
}

if ($user?->isAdmin()) {
Expand Down
41 changes: 41 additions & 0 deletions src/Controller/Dashboard/DashboardSecurityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,24 @@

namespace CodedMonkey\Conductor\Controller\Dashboard;

use CodedMonkey\Conductor\Doctrine\Entity\User;
use CodedMonkey\Conductor\Doctrine\Repository\UserRepository;
use CodedMonkey\Conductor\Form\RegistrationFormType;
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;

class DashboardSecurityController extends AbstractController
{
public function __construct(
private readonly UserRepository $userRepository,
) {
}

#[Route('/login', name: 'dashboard_login')]
public function login(AuthenticationUtils $authenticationUtils): Response
{
Expand All @@ -19,4 +30,34 @@ public function login(AuthenticationUtils $authenticationUtils): Response
'username_label' => 'Email',
]);
}

#[Route('/register', name: 'dashboard_register')]
public function register(Request $request, AdminUrlGenerator $adminUrlGenerator, #[Autowire(param: 'conductor.security.registration_enabled')] bool $registrationEnabled): Response
{
$userCount = $this->userRepository->count([]);

if (!$registrationEnabled && 0 !== $userCount) {
return $this->redirectToRoute('dashboard');
}

$user = new User();

$form = $this->createForm(RegistrationFormType::class, $user);

$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
if (0 === $userCount) {
$user->setRoles(['ROLE_OWNER', 'ROLE_USER']);
}

$this->userRepository->save($user, true);

return $this->redirect($adminUrlGenerator->setRoute('dashboard_login'));
}

return $this->render('dashboard/security/register.html.twig', [
'form' => $form,
]);
}
}
7 changes: 7 additions & 0 deletions src/DependencyInjection/AppConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ public function getConfigTreeBuilder(): TreeBuilder

$rootNode->children()
->scalarNode('title')->defaultValue('My Conductor')->end()
->arrayNode('security')
->addDefaultsIfNotSet()
->children()
->booleanNode('public')->defaultFalse()->end()
->booleanNode('registration')->defaultFalse()->end()
->end()
->end()
->arrayNode('storage')
->addDefaultsIfNotSet()
->children()
Expand Down
2 changes: 2 additions & 0 deletions src/DependencyInjection/AppExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ protected function loadInternal(array $mergedConfig, ContainerBuilder $container
{
$container->setParameter('conductor.storage.path', $mergedConfig['storage']['path']);
$container->setParameter('conductor.title', $mergedConfig['title']);
$container->setParameter('conductor.security.public_access', $mergedConfig['security']['public']);
$container->setParameter('conductor.security.registration_enabled', $mergedConfig['security']['registration']);
}

public function getConfiguration(array $config, ContainerBuilder $container): ConfigurationInterface
Expand Down
2 changes: 0 additions & 2 deletions src/Form/AccountType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
use CodedMonkey\Conductor\Doctrine\Entity\User;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
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\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
Expand Down
36 changes: 36 additions & 0 deletions src/Form/RegistrationFormType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace CodedMonkey\Conductor\Form;

use CodedMonkey\Conductor\Doctrine\Entity\User;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class RegistrationFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('name', TextType::class, [
'required' => true,
])
->add('email', EmailType::class, [
'required' => true,
])
->add('plainPassword', PasswordType::class, [
'label' => 'Password',
'required' => true,
]);
}

public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => User::class,
]);
}
}
32 changes: 32 additions & 0 deletions templates/dashboard/security/register.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{% extends ea.templatePath('layout') %}

{% block page_title ea.dashboardTitle|raw %}

{% block body_class 'page-login' %}

{% block wrapper_wrapper %}
<div class="login-wrapper">
<header class="main-header">
<div id="header-logo">
<a href="{{ path('dashboard') }}" class="logo logo-long">
{{ block('page_title') }}
</a>
</div>
</header>

<section class="content">
<p class="lead">Register an account.</p>
{{ form_start(form) }}
{{ form_row(form.name) }}
{{ form_row(form.email) }}
{{ form_row(form.plainPassword) }}

<div class="form-group">
<button class="btn btn-primary">Create account</button>
</div>
{{ form_end(form) }}
</section>
</div>

<script src="{{ asset('login.js', constant('EasyCorp\\Bundle\\EasyAdminBundle\\Asset\\AssetPackage::PACKAGE_NAME')) }}"></script>
{% endblock wrapper_wrapper %}

0 comments on commit 1f2fd59

Please sign in to comment.