Create everything required to login and register
This commit is contained in:
0
src/Controller/.gitignore
vendored
0
src/Controller/.gitignore
vendored
19
src/Controller/HomeController.php
Normal file
19
src/Controller/HomeController.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
|
||||
class HomeController extends AbstractController
|
||||
{
|
||||
#[Route('/', name: 'home')]
|
||||
public function home(): Response
|
||||
{
|
||||
return $this->redirectToRoute('task_view');
|
||||
// return $this->render('simple.html.twig', [
|
||||
// 'text' => 'Welcome!'
|
||||
// ]);
|
||||
}
|
||||
}
|
72
src/Controller/SecurityController.php
Normal file
72
src/Controller/SecurityController.php
Normal file
@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Entity\User;
|
||||
use App\Form\RegistrationFormType;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Exception;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
|
||||
|
||||
class SecurityController extends AbstractController
|
||||
{
|
||||
#[Route('/login', name: 'login')]
|
||||
public function login(AuthenticationUtils $authenticationUtils): Response
|
||||
{
|
||||
$error = $authenticationUtils->getLastAuthenticationError();
|
||||
$lastUsername = $authenticationUtils->getLastUsername();
|
||||
|
||||
return $this->render('security/login.html.twig', [
|
||||
'last_username' => $lastUsername,
|
||||
'error' => $error,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/logout', name: 'logout')]
|
||||
public function logout(): void
|
||||
{
|
||||
// controller can be blank: it will never be called!
|
||||
throw new Exception('Don\'t forget to activate logout in security.yaml');
|
||||
}
|
||||
|
||||
#[Route('/register', name: 'register')]
|
||||
public function register(
|
||||
Request $request,
|
||||
UserPasswordHasherInterface $userPasswordHasher,
|
||||
EntityManagerInterface $entityManager,
|
||||
): Response
|
||||
{
|
||||
$user = new User();
|
||||
$form = $this->createForm(RegistrationFormType::class, $user);
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
if ($form->get('plainPassword')->getData() !== $form->get('plainPasswordRepeated')->getData()) {
|
||||
$this->addFlash('error', 'Password and password repeated must be the same');
|
||||
} else {
|
||||
$user->setPassword(
|
||||
$userPasswordHasher->hashPassword(
|
||||
$user,
|
||||
$form->get('plainPassword')->getData()
|
||||
)
|
||||
);
|
||||
|
||||
$entityManager->persist($user);
|
||||
$entityManager->flush();
|
||||
// do anything else you need here, like send an email
|
||||
$this->addFlash('success', sprintf('Successfully registered user %s', $user->getUsername()));
|
||||
|
||||
return $this->redirectToRoute('register');
|
||||
}
|
||||
}
|
||||
|
||||
return $this->render('security/register.html.twig', [
|
||||
'registrationForm' => $form->createView(),
|
||||
]);
|
||||
}
|
||||
}
|
63
src/Controller/UserController.php
Normal file
63
src/Controller/UserController.php
Normal file
@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Entity\User;
|
||||
use App\Form\ProfileType;
|
||||
use App\Form\UserSettingsType;
|
||||
use App\Service\LastRelease;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use Symfony\Component\Uid\Uuid;
|
||||
|
||||
#[Route('/user', name: 'user')]
|
||||
class UserController extends AbstractController
|
||||
{
|
||||
public function __construct(
|
||||
private EntityManagerInterface $em,
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
#[Route('/profile', name: '_profile')]
|
||||
public function profile(
|
||||
Request $request,
|
||||
UserPasswordHasherInterface $userPasswordHasher,
|
||||
LastRelease $lastRelease,
|
||||
): Response
|
||||
{
|
||||
$user = $this->getUser();
|
||||
$form = $this->createForm(ProfileType::class, $user);
|
||||
|
||||
$form->handleRequest($request);
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
if (!empty($form->get('plainPassword')->getData())) {
|
||||
if ($form->get('plainPassword')->getData() !== $form->get('plainPasswordRepeated')->getData()) {
|
||||
$this->addFlash('error', 'Password and password repeated must be the same, password not changed');
|
||||
} else {
|
||||
$this->addFlash('success', 'Password updated successfully');
|
||||
$user
|
||||
->setPassword(
|
||||
$userPasswordHasher->hashPassword(
|
||||
$user,
|
||||
$form->get('plainPassword')->getData()
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
$this->addFlash('success', 'Profile updated successfully');
|
||||
|
||||
$this->em->persist($user);
|
||||
$this->em->flush();
|
||||
}
|
||||
|
||||
return $this->render('user/profile.html.twig', [
|
||||
'form' => $form->createView(),
|
||||
'release' => $lastRelease,
|
||||
]);
|
||||
}
|
||||
}
|
49
src/Form/ProfileType.php
Normal file
49
src/Form/ProfileType.php
Normal file
@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Form;
|
||||
|
||||
use App\Entity\User;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
use Symfony\Component\Validator\Constraints\Length;
|
||||
|
||||
class ProfileType extends AbstractType
|
||||
{
|
||||
public function buildForm(FormBuilderInterface $builder, array $options): void
|
||||
{
|
||||
$builder
|
||||
->add('email')
|
||||
->add('plainPassword', PasswordType::class, [
|
||||
'required' => false,
|
||||
'mapped' => false,
|
||||
'attr' => ['autocomplete' => 'new-password'],
|
||||
'label' => 'Password',
|
||||
'constraints' => [
|
||||
new Length([
|
||||
'min' => 8,
|
||||
'minMessage' => 'Your password should be at least {{ limit }} characters',
|
||||
// max length allowed by Symfony for security reasons
|
||||
'max' => 4096,
|
||||
]),
|
||||
],
|
||||
])
|
||||
->add('plainPasswordRepeated', PasswordType::class, [
|
||||
'required' => false,
|
||||
'mapped' => false,
|
||||
'attr' => ['autocomplete' => 'new-password'],
|
||||
'label' => 'Password repeated',
|
||||
])
|
||||
->add('save', SubmitType::class)
|
||||
;
|
||||
}
|
||||
|
||||
public function configureOptions(OptionsResolver $resolver): void
|
||||
{
|
||||
$resolver->setDefaults([
|
||||
'data_class' => User::class,
|
||||
]);
|
||||
}
|
||||
}
|
64
src/Form/RegistrationFormType.php
Normal file
64
src/Form/RegistrationFormType.php
Normal file
@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace App\Form;
|
||||
|
||||
use App\Entity\User;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\EmailType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
use Symfony\Component\Validator\Constraints\IsTrue;
|
||||
use Symfony\Component\Validator\Constraints\Length;
|
||||
use Symfony\Component\Validator\Constraints\NotBlank;
|
||||
|
||||
class RegistrationFormType extends AbstractType
|
||||
{
|
||||
public function buildForm(FormBuilderInterface $builder, array $options): void
|
||||
{
|
||||
$builder
|
||||
->add('username')
|
||||
->add('name')
|
||||
->add('plainPassword', PasswordType::class, [
|
||||
// instead of being set onto the object directly,
|
||||
// this is read and encoded in the controller
|
||||
'mapped' => false,
|
||||
'attr' => ['autocomplete' => 'new-password'],
|
||||
'label' => 'Password',
|
||||
'constraints' => [
|
||||
new NotBlank([
|
||||
'message' => 'Please enter a password',
|
||||
]),
|
||||
new Length([
|
||||
'min' => 8,
|
||||
'minMessage' => 'Your password should be at least {{ limit }} characters',
|
||||
// max length allowed by Symfony for security reasons
|
||||
'max' => 4096,
|
||||
]),
|
||||
],
|
||||
])
|
||||
->add('plainPasswordRepeated', PasswordType::class, [
|
||||
'mapped' => false,
|
||||
'label' => 'Password repeated',
|
||||
])
|
||||
->add('email', EmailType::class)
|
||||
// ->add('agreeTerms', CheckboxType::class, [
|
||||
// 'mapped' => false,
|
||||
// 'constraints' => [
|
||||
// new IsTrue([
|
||||
// 'message' => 'You should agree to our terms.',
|
||||
// ]),
|
||||
// ],
|
||||
// ])
|
||||
->add('register', SubmitType::class);
|
||||
}
|
||||
|
||||
public function configureOptions(OptionsResolver $resolver): void
|
||||
{
|
||||
$resolver->setDefaults([
|
||||
'data_class' => User::class,
|
||||
]);
|
||||
}
|
||||
}
|
68
src/Service/LastRelease.php
Normal file
68
src/Service/LastRelease.php
Normal file
@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use JetBrains\PhpStorm\ArrayShape;
|
||||
|
||||
class LastRelease
|
||||
{
|
||||
#[ArrayShape([
|
||||
"branch" => "string",
|
||||
"date" => "string",
|
||||
"commitHashShort" => "string",
|
||||
"commitHashLong" => "string",
|
||||
"commitDate" => "string",
|
||||
"branchUrl" => "string",
|
||||
"projectUrl" => "string",
|
||||
"commitUrl" => "string",
|
||||
])]
|
||||
private array $lastRelease = [];
|
||||
|
||||
public function __construct(string $jsonFile)
|
||||
{
|
||||
if (file_exists($jsonFile)) {
|
||||
$this->lastRelease = json_decode(file_get_contents($jsonFile), true);
|
||||
}
|
||||
}
|
||||
|
||||
public function getBranch(): string
|
||||
{
|
||||
return $this->lastRelease['branch'] ?? '-';
|
||||
}
|
||||
|
||||
public function getBranchUrl(): string
|
||||
{
|
||||
return $this->lastRelease['branchUrl'] ?? '#';
|
||||
}
|
||||
|
||||
public function getProjectUrl(): string
|
||||
{
|
||||
return $this->lastRelease['projectUrl'] ?? '#';
|
||||
}
|
||||
|
||||
public function getCommitUrl(): string
|
||||
{
|
||||
return $this->lastRelease['commitUrl'] ?? '#';
|
||||
}
|
||||
|
||||
public function getDate(): string
|
||||
{
|
||||
return $this->lastRelease['date'] ?? '-';
|
||||
}
|
||||
|
||||
public function getCommitHashShort(): string
|
||||
{
|
||||
return $this->lastRelease['commitHashShort'] ?? '-';
|
||||
}
|
||||
|
||||
public function getCommitHashLong(): string
|
||||
{
|
||||
return $this->lastRelease['commitHashLong'] ?? '-';
|
||||
}
|
||||
|
||||
public function getCommitDate(): string
|
||||
{
|
||||
return $this->lastRelease['commitDate'] ?? '-';
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user