wip-feat: reduces env vars, adds getting-started page
This commit is contained in:
16
src/User/Action/Command/RegisterLdapUserCommand.php
Normal file
16
src/User/Action/Command/RegisterLdapUserCommand.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\User\Action\Command;
|
||||
|
||||
use OneToMany\RichBundle\Contract\CommandInterface;
|
||||
|
||||
/** @implements CommandInterface<RegisterUserCommand> */
|
||||
class RegisterLdapUserCommand implements CommandInterface
|
||||
{
|
||||
public function __construct(
|
||||
public string $name,
|
||||
public string $email,
|
||||
public string $username,
|
||||
public string $password,
|
||||
) {}
|
||||
}
|
||||
16
src/User/Action/Command/RegisterUserCommand.php
Normal file
16
src/User/Action/Command/RegisterUserCommand.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\User\Action\Command;
|
||||
|
||||
use OneToMany\RichBundle\Contract\CommandInterface;
|
||||
|
||||
/** @implements CommandInterface<RegisterUserCommand> */
|
||||
class RegisterUserCommand implements CommandInterface
|
||||
{
|
||||
public function __construct(
|
||||
public string $name,
|
||||
public string $email,
|
||||
public string $username,
|
||||
public string $password,
|
||||
) {}
|
||||
}
|
||||
57
src/User/Action/Handler/RegisterLdapUserHandler.php
Normal file
57
src/User/Action/Handler/RegisterLdapUserHandler.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace App\User\Action\Handler;
|
||||
|
||||
use App\User\Action\Command\RegisterLdapUserCommand;
|
||||
use App\User\Action\Result\RegisterLdapUserResult;
|
||||
use App\User\Framework\Entity\User;
|
||||
use App\User\Framework\Entity\UserPreference;
|
||||
use App\User\Framework\Repository\PreferencesRepository;
|
||||
use App\User\Framework\Repository\UserRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use OneToMany\RichBundle\Contract\CommandInterface as C;
|
||||
use OneToMany\RichBundle\Contract\HandlerInterface;
|
||||
use OneToMany\RichBundle\Contract\ResultInterface as R;
|
||||
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
|
||||
|
||||
/** @implements HandlerInterface<RegisterLdapUserCommand, RegisterLdapUserResult> */
|
||||
class RegisterLdapUserHandler implements HandlerInterface
|
||||
{
|
||||
public function __construct(
|
||||
private readonly EntityManagerInterface $entityManager,
|
||||
private readonly PreferencesRepository $preferenceRepository,
|
||||
private readonly UserPasswordHasherInterface $userPasswordHasher,
|
||||
private readonly UserRepository $userRepository,
|
||||
) {}
|
||||
|
||||
public function handle(C $command): R
|
||||
{
|
||||
$user = $this->userRepository->findOneBy(['username' => $command->username]);
|
||||
if (null === $user) {
|
||||
$user = new User();
|
||||
$user->setPassword($this->userPasswordHasher->hashPassword($user, $command->password));
|
||||
}
|
||||
|
||||
$user->setUsername($command->username);
|
||||
$user->setEmail($command->email);
|
||||
$user->setName($command->name);
|
||||
$this->entityManager->persist($user);
|
||||
$this->entityManager->flush();
|
||||
|
||||
$this->setUserPreferences($user, $this->preferenceRepository->findEnabled());
|
||||
$this->entityManager->flush();
|
||||
|
||||
return new RegisterLdapUserResult($user);
|
||||
}
|
||||
|
||||
private function setUserPreferences(User $user, array $preferences): void
|
||||
{
|
||||
foreach ($preferences as $preference) {
|
||||
$user->addUserPreference((new UserPreference())
|
||||
->setUser($user)
|
||||
->setPreference($preference)
|
||||
->setPreferenceValue(null)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
53
src/User/Action/Handler/RegisterUserHandler.php
Normal file
53
src/User/Action/Handler/RegisterUserHandler.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace App\User\Action\Handler;
|
||||
|
||||
use App\User\Action\Command\RegisterUserCommand;
|
||||
use App\User\Action\Result\RegisterUserResult;
|
||||
use App\User\Action\Result\SaveUserMediaPreferencesResult;
|
||||
use App\User\Framework\Entity\User;
|
||||
use App\User\Framework\Entity\UserPreference;
|
||||
use App\User\Framework\Repository\PreferencesRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use OneToMany\RichBundle\Contract\CommandInterface as C;
|
||||
use OneToMany\RichBundle\Contract\HandlerInterface;
|
||||
use OneToMany\RichBundle\Contract\ResultInterface as R;
|
||||
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
|
||||
|
||||
/** @implements HandlerInterface<RegisterUserCommand> */
|
||||
class RegisterUserHandler implements HandlerInterface
|
||||
{
|
||||
public function __construct(
|
||||
private readonly EntityManagerInterface $entityManager,
|
||||
private readonly PreferencesRepository $preferenceRepository,
|
||||
private readonly UserPasswordHasherInterface $userPasswordHasher,
|
||||
) {}
|
||||
|
||||
public function handle(C $command): R
|
||||
{
|
||||
$user = new User();
|
||||
|
||||
$user->setUsername($command->username);
|
||||
$user->setEmail($command->email);
|
||||
$user->setPassword($this->userPasswordHasher->hashPassword($user, $command->password));
|
||||
$user->setName($command->name);
|
||||
$this->entityManager->persist($user);
|
||||
$this->entityManager->flush();
|
||||
|
||||
$this->setUserPreferences($user, $this->preferenceRepository->findEnabled());
|
||||
$this->entityManager->flush();
|
||||
|
||||
return new RegisterUserResult($user);
|
||||
}
|
||||
|
||||
private function setUserPreferences(User $user, array $preferences): void
|
||||
{
|
||||
foreach ($preferences as $preference) {
|
||||
$user->addUserPreference((new UserPreference())
|
||||
->setUser($user)
|
||||
->setPreference($preference)
|
||||
->setPreferenceValue(null)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
14
src/User/Action/Result/RegisterLdapUserResult.php
Normal file
14
src/User/Action/Result/RegisterLdapUserResult.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace App\User\Action\Result;
|
||||
|
||||
use App\User\Framework\Entity\User;
|
||||
use OneToMany\RichBundle\Contract\ResultInterface;
|
||||
|
||||
/** @implements ResultInterface */
|
||||
class RegisterLdapUserResult implements ResultInterface
|
||||
{
|
||||
public function __construct(
|
||||
public User $user,
|
||||
) {}
|
||||
}
|
||||
14
src/User/Action/Result/RegisterUserResult.php
Normal file
14
src/User/Action/Result/RegisterUserResult.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace App\User\Action\Result;
|
||||
|
||||
use App\User\Framework\Entity\User;
|
||||
use OneToMany\RichBundle\Contract\ResultInterface;
|
||||
|
||||
/** @implements ResultInterface */
|
||||
class RegisterUserResult implements ResultInterface
|
||||
{
|
||||
public function __construct(
|
||||
public User $user,
|
||||
) {}
|
||||
}
|
||||
@@ -2,16 +2,31 @@
|
||||
|
||||
namespace App\User\Framework\Controller\Web;
|
||||
|
||||
use App\User\Action\Command\RegisterUserCommand;
|
||||
use App\User\Action\Handler\RegisterUserHandler;
|
||||
use App\User\Framework\Entity\User;
|
||||
use App\User\Framework\Form\RegistrationFormType;
|
||||
use App\User\Framework\Repository\UserRepository;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
|
||||
|
||||
class LoginController extends AbstractController
|
||||
{
|
||||
#[Route(path: '/login', name: 'app_login')]
|
||||
public function login(AuthenticationUtils $authenticationUtils): Response
|
||||
public function __construct(private readonly RegisterUserHandler $registerUserHandler)
|
||||
{
|
||||
}
|
||||
|
||||
#[Route(path: '/login', name: 'app_login')]
|
||||
public function login(AuthenticationUtils $authenticationUtils, UserRepository $userRepository): Response
|
||||
{
|
||||
if ((new ArrayCollection($userRepository->findAll()))->count() === 0) {
|
||||
return $this->redirectToRoute('app_getting_started');
|
||||
}
|
||||
|
||||
// get the login error if there is one
|
||||
$error = $authenticationUtils->getLastAuthenticationError();
|
||||
|
||||
@@ -29,4 +44,26 @@ class LoginController extends AbstractController
|
||||
{
|
||||
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
|
||||
}
|
||||
|
||||
#[Route(path: '/getting-started', name: 'app_getting_started')]
|
||||
public function gettingStarted(Request $request): Response
|
||||
{
|
||||
$form = $this->createForm(RegistrationFormType::class, new User());
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$this->registerUserHandler->handle(new RegisterUserCommand(
|
||||
name: $form->get('name')->getData(),
|
||||
email: $form->get('email')->getData(),
|
||||
username: $form->get('username')->getData(),
|
||||
password: $form->get('plainPassword')->getData(),
|
||||
));
|
||||
|
||||
return $this->redirectToRoute('app_index');
|
||||
}
|
||||
|
||||
return $this->render('user/getting-started.html.twig', [
|
||||
'registrationForm' => $form,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace App\User\Framework\Controller\Web;
|
||||
|
||||
use App\User\Action\Command\RegisterUserCommand;
|
||||
use App\User\Action\Handler\RegisterUserHandler;
|
||||
use App\User\Framework\Entity\User;
|
||||
use App\User\Framework\Entity\UserPreference;
|
||||
use App\User\Framework\Form\RegistrationFormType;
|
||||
@@ -15,30 +17,24 @@ use Symfony\Component\Routing\Attribute\Route;
|
||||
|
||||
class RegistrationController extends AbstractController
|
||||
{
|
||||
public function __construct(private readonly RegisterUserHandler $registerUserHandler)
|
||||
{
|
||||
}
|
||||
|
||||
#[Route('/register', name: 'app_register')]
|
||||
public function register(
|
||||
Request $request,
|
||||
UserPasswordHasherInterface $userPasswordHasher,
|
||||
EntityManagerInterface $entityManager,
|
||||
PreferencesRepository $preferencesRepository,
|
||||
): Response {
|
||||
$user = new User();
|
||||
$form = $this->createForm(RegistrationFormType::class, $user);
|
||||
$form = $this->createForm(RegistrationFormType::class, new User());
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
/** @var string $plainPassword */
|
||||
$plainPassword = $form->get('plainPassword')->getData();
|
||||
|
||||
// encode the plain password
|
||||
$user->setPassword($userPasswordHasher->hashPassword($user, $plainPassword));
|
||||
|
||||
$entityManager->persist($user);
|
||||
$entityManager->flush();
|
||||
|
||||
$this->setUserPreferences($user, $preferencesRepository->findEnabled());
|
||||
|
||||
$preferencesRepository->getEntityManager()->flush();
|
||||
$this->registerUserHandler->handle(new RegisterUserCommand(
|
||||
name: $form->get('name')->getData(),
|
||||
email: $form->get('email')->getData(),
|
||||
username: $form->get('username')->getData(),
|
||||
password: $form->get('plainPassword')->getData(),
|
||||
));
|
||||
|
||||
return $this->redirectToRoute('app_index');
|
||||
}
|
||||
@@ -47,15 +43,4 @@ class RegistrationController extends AbstractController
|
||||
'registrationForm' => $form,
|
||||
]);
|
||||
}
|
||||
|
||||
private function setUserPreferences(User $user, array $preferences): void
|
||||
{
|
||||
foreach ($preferences as $preference) {
|
||||
$user->addUserPreference((new UserPreference())
|
||||
->setUser($user)
|
||||
->setPreference($preference)
|
||||
->setPreferenceValue(null)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ class RegistrationFormType extends AbstractType
|
||||
{
|
||||
$builder
|
||||
->add('email')
|
||||
->add('username')
|
||||
->add('name')
|
||||
->add('plainPassword', PasswordType::class, [
|
||||
// instead of being set onto the object directly,
|
||||
|
||||
@@ -11,6 +11,8 @@
|
||||
|
||||
namespace App\User\Framework\Security;
|
||||
|
||||
use App\User\Action\Command\RegisterLdapUserCommand;
|
||||
use App\User\Action\Handler\RegisterLdapUserHandler;
|
||||
use App\User\Framework\Entity\User;
|
||||
use App\User\Framework\Repository\UserRepository;
|
||||
use Symfony\Component\Ldap\Entry;
|
||||
@@ -45,6 +47,7 @@ class LdapUserProvider implements UserProviderInterface, PasswordUpgraderInterfa
|
||||
private string $displayNameAttribute;
|
||||
|
||||
public function __construct(
|
||||
private RegisterLdapUserHandler $registerLdapUserHandler,
|
||||
private UserRepository $userRepository,
|
||||
private LdapInterface $ldap,
|
||||
private string $baseDn,
|
||||
@@ -159,21 +162,24 @@ class LdapUserProvider implements UserProviderInterface, PasswordUpgraderInterfa
|
||||
$dbUser = $this->getDbUser($identifier, $entry);
|
||||
|
||||
if (null === $dbUser) {
|
||||
$dbUser = new User();
|
||||
$dbUser->setPassword("test");
|
||||
return $this->registerLdapUserHandler->handle(new RegisterLdapUserCommand(
|
||||
name:$this->getAttributeValue($entry, $this->displayNameAttribute)[0] ?? null,
|
||||
email:$this->getAttributeValue($entry, $this->emailAttribute)[0] ?? null,
|
||||
username:$this->getAttributeValue($entry, $this->usernameAttribute) ?? null,
|
||||
password: uniqid(),
|
||||
))->user;
|
||||
} else {
|
||||
$dbUser
|
||||
->setName($this->getAttributeValue($entry, $this->displayNameAttribute)[0] ?? null)
|
||||
->setEmail($this->getAttributeValue($entry, $this->emailAttribute)[0] ?? null)
|
||||
->setUsername($this->getAttributeValue($entry, $this->usernameAttribute) ?? null);
|
||||
$this->userRepository->getEntityManager()->flush();
|
||||
}
|
||||
|
||||
$dbUser
|
||||
->setName( $this->getAttributeValue($entry, $this->displayNameAttribute)[0] ?? null)
|
||||
->setEmail($this->getAttributeValue($entry, $this->emailAttribute)[0] ?? null)
|
||||
->setUsername($this->getAttributeValue($entry, $this->usernameAttribute) ?? null);
|
||||
|
||||
$this->userRepository->getEntityManager()->persist($dbUser);
|
||||
$this->userRepository->getEntityManager()->flush();
|
||||
|
||||
return $dbUser;
|
||||
}
|
||||
|
||||
/** @return User */
|
||||
private function getDbUser(string $identifier, Entry $entry): ?UserInterface
|
||||
{
|
||||
if (in_array($this->uidKey, ['mail', 'email'])) {
|
||||
|
||||
Reference in New Issue
Block a user