wip-feat: reduces env vars, adds getting-started page

This commit is contained in:
2025-05-15 23:25:12 -05:00
parent ce6fda257b
commit 5d5d66bd79
22 changed files with 500 additions and 62 deletions

5
.env
View File

@@ -15,7 +15,7 @@
# https://symfony.com/doc/current/best_practices.html#use-environment-variables-for-infrastructure-configuration # https://symfony.com/doc/current/best_practices.html#use-environment-variables-for-infrastructure-configuration
###> symfony/framework-bundle ### ###> symfony/framework-bundle ###
APP_ENV=dev APP_ENV=prod
APP_SECRET= APP_SECRET=
###< symfony/framework-bundle ### ###< symfony/framework-bundle ###
@@ -26,7 +26,8 @@ APP_SECRET=
# DATABASE_URL="sqlite:///%kernel.project_dir%/var/data.db" # DATABASE_URL="sqlite:///%kernel.project_dir%/var/data.db"
# DATABASE_URL="mysql://app:!ChangeMe!@127.0.0.1:3306/app?serverVersion=8.0.32&charset=utf8mb4" # DATABASE_URL="mysql://app:!ChangeMe!@127.0.0.1:3306/app?serverVersion=8.0.32&charset=utf8mb4"
# DATABASE_URL="mysql://app:!ChangeMe!@127.0.0.1:3306/app?serverVersion=10.11.2-MariaDB&charset=utf8mb4" # DATABASE_URL="mysql://app:!ChangeMe!@127.0.0.1:3306/app?serverVersion=10.11.2-MariaDB&charset=utf8mb4"
DATABASE_URL="postgresql://app:!ChangeMe!@127.0.0.1:5432/app?serverVersion=16&charset=utf8" # DATABASE_URL="postgresql://app:!ChangeMe!@127.0.0.1:5432/app?serverVersion=16&charset=utf8"
###< doctrine/doctrine-bundle ### ###< doctrine/doctrine-bundle ###
###> symfony/messenger ### ###> symfony/messenger ###

View File

@@ -1,3 +1,4 @@
APP_URL="https://dev.caldwell.digital"
DATABASE_URL="mysql://root:password@database:3306/app?serverVersion=10.6.19.2-MariaDB&charset=utf8mb4" DATABASE_URL="mysql://root:password@database:3306/app?serverVersion=10.6.19.2-MariaDB&charset=utf8mb4"
APP_SECRET=70169beadfbc8101c393cbfbba27a313 APP_SECRET=70169beadfbc8101c393cbfbba27a313

View File

@@ -7,8 +7,6 @@ TMDB_API=%%tmdb_api%%
MERCURE_URL=%%mercure_url%% MERCURE_URL=%%mercure_url%%
MERCURE_PUBLIC_URL=%%mercure_public_url%% MERCURE_PUBLIC_URL=%%mercure_public_url%%
MERCURE_JWT_SECRET="%%mercure_jwt_secret%%" MERCURE_JWT_SECRET="%%mercure_jwt_secret%%"
JELLYFIN_URL=%%jellyfin_url%%
JELLYFIN_TOKEN=%%jellyfin_token%%
REDIS_HOST="%%redis_host%%" REDIS_HOST="%%redis_host%%"
@@ -25,7 +23,7 @@ LDAP_UID_KEY="uid"
# LDAP group that identifies an Admin # LDAP group that identifies an Admin
# Users with this LDAP group will automatically # Users with this LDAP group will automatically
# get the admin role in this system. # get the admin role in this system.
LDAP_ADMIN_ROLE_DN="cn=admins,cn=groups,cn=accounts,dc=caldwell,dc=local" LDAP_ADMIN_ROLE_DN=""
LDAP_EMAIL_ATTRIBUTE=mail LDAP_EMAIL_ATTRIBUTE=mail
LDAP_USERNAME_ATTRIBUTE=uid LDAP_USERNAME_ATTRIBUTE=uid
LDAP_NAME_ATTRIBUTE=displayname LDAP_NAME_ATTRIBUTE=displayname

52
.env.example.dist Normal file
View File

@@ -0,0 +1,52 @@
# App must be served over HTTPS (requirement of Mercure)
# Either serve behind an SSL terminating reverse proxy
# or pass your certificates into the 'app' container.
# Please omit any trailing slashes. The APP_URL is passed
# used to generate the Mercure URL behind the scenes.
APP_URL="https://dev.caldwell.digital"
APP_SECRET="70169beadfbc8101c393cbfbba27a313"
# Use the DATABASE_URL below to use the MariaDB container
# provided in the example.compose.yml file, or remove this
# line and fill in the details of your own MySQL/MariaDB server
DATABASE_URL="mysql://root:password@database:3306/app?serverVersion=10.6.19.2-MariaDB&charset=utf8mb4"
# Fill in your MySQL/MariaDB connection details
#DATABASE_URL="mysql://<mysql user>:<mysql pass>@<mysql host>:3306/<mysql db name>?serverVersion=10.6.19.2-MariaDB&charset=utf8mb4"
# Enter you Real Debrid API key
# This key is never saved anywhere
# else and is passed to Torrentio
# to retrieve download options
REAL_DEBRID_KEY="QYYBR7OSQ4VEFKWASDEZ2B4VO67KHUJY6IWOT7HHA7ATXO7QCYDQ"
# Enter you TMDB API key
# This is used to provide rich search results
# when searching for media and rendering the
# Popular Movies and TV Shows section.
TMDB_API="eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOiI0ZTJjYjJhOGUzOGJhNjdiNjVhOGU1NGM0ZWI1MzhmOCIsIm5iZiI6MTczNzkyNjA0NC41NjQsInN1YiI6IjY3OTZhNTljYzdiMDFiNzJjNzIzZWM5YiIsInNjb3BlcyI6WyJhcGlfcmVhZCJdLCJ2ZXJzaW9uIjoxfQ.e8DbNe9qrSBC1y-ANRv-VWBAtls-ZS2r7aNCiI68mpw"
MERCURE_JWT_SECRET="!ChangeThisMercureHubJWTSecretKey!"
# Use your own Redis instance to use the
# below value to use the container included
# in the example.compose.yml file.
REDIS_HOST="redis://redis"
# LDAP Config: To use LDAP, enter the below fields
# and run 'php bin/console config:set auth.method ldap'
LDAP_HOST=
LDAP_PORT=
LDAP_ENCRYPTION=
LDAP_BASE_DN=
LDAP_BIND_USER=
LDAP_BIND_PASS=
LDAP_DN_STRING=
LDAP_UID_KEY="uid"
# LDAP group that identifies an Admin
# Users with this LDAP group will automatically
# get the admin role in this system.
LDAP_ADMIN_ROLE_DN=""
LDAP_EMAIL_ATTRIBUTE=mail
LDAP_USERNAME_ATTRIBUTE=uid
LDAP_NAME_ATTRIBUTE=displayname

View File

@@ -38,6 +38,7 @@ security:
# Easy way to control access for large sections of your site # Easy way to control access for large sections of your site
# Note: Only the *first* access control that matches will be used # Note: Only the *first* access control that matches will be used
access_control: access_control:
- { path: ^/getting-started, roles: PUBLIC_ACCESS }
- { path: ^/register, roles: PUBLIC_ACCESS } - { path: ^/register, roles: PUBLIC_ACCESS }
- { path: ^/login, roles: PUBLIC_ACCESS } - { path: ^/login, roles: PUBLIC_ACCESS }
- { path: ^/, roles: ROLE_USER } # Or ROLE_ADMIN, ROLE_SUPER_ADMIN, - { path: ^/, roles: ROLE_USER } # Or ROLE_ADMIN, ROLE_SUPER_ADMIN,

View File

@@ -36,6 +36,7 @@ security:
# Easy way to control access for large sections of your site # Easy way to control access for large sections of your site
# Note: Only the *first* access control that matches will be used # Note: Only the *first* access control that matches will be used
access_control: access_control:
- { path: ^/getting-started, roles: PUBLIC_ACCESS }
- { path: ^/register, roles: PUBLIC_ACCESS } - { path: ^/register, roles: PUBLIC_ACCESS }
- { path: ^/login, roles: PUBLIC_ACCESS } - { path: ^/login, roles: PUBLIC_ACCESS }
- { path: ^/, roles: ROLE_USER } # Or ROLE_ADMIN, ROLE_SUPER_ADMIN, - { path: ^/, roles: ROLE_USER } # Or ROLE_ADMIN, ROLE_SUPER_ADMIN,

View File

@@ -1,8 +1,8 @@
mercure: mercure:
hubs: hubs:
default: default:
url: '%env(MERCURE_URL)%' url: '%app.mercure.url%'
public_url: '%env(MERCURE_PUBLIC_URL)%' public_url: '%app.mercure.public_url%'
jwt: jwt:
secret: '%env(MERCURE_JWT_SECRET)%' secret: '%env(MERCURE_JWT_SECRET)%'
publish: '*' publish: '*'

View File

@@ -19,13 +19,11 @@ security:
security: false security: false
main: main:
lazy: true lazy: true
provider: app_ldap provider: app_local
form_login_ldap: form_login:
login_path: app_login login_path: app_login
check_path: app_login check_path: app_login
enable_csrf: true enable_csrf: true
service: Symfony\Component\Ldap\Ldap
dn_string: '%env(LDAP_DN_STRING)%'
logout: logout:
path: app_logout path: app_logout
@@ -38,6 +36,7 @@ security:
# Easy way to control access for large sections of your site # Easy way to control access for large sections of your site
# Note: Only the *first* access control that matches will be used # Note: Only the *first* access control that matches will be used
access_control: access_control:
- { path: ^/getting-started, roles: PUBLIC_ACCESS }
- { path: ^/register, roles: PUBLIC_ACCESS } - { path: ^/register, roles: PUBLIC_ACCESS }
- { path: ^/login, roles: PUBLIC_ACCESS } - { path: ^/login, roles: PUBLIC_ACCESS }
- { path: ^/, roles: ROLE_USER } # Or ROLE_ADMIN, ROLE_SUPER_ADMIN, - { path: ^/, roles: ROLE_USER } # Or ROLE_ADMIN, ROLE_SUPER_ADMIN,

View File

@@ -9,6 +9,9 @@ parameters:
media.movies_path: '/var/download/%env(default:media.default_movies_dir:MOVIES_PATH)%' media.movies_path: '/var/download/%env(default:media.default_movies_dir:MOVIES_PATH)%'
media.tvshows_path: '/var/download/%env(default:media.default_tvshows_dir:TVSHOWS_PATH)%' media.tvshows_path: '/var/download/%env(default:media.default_tvshows_dir:TVSHOWS_PATH)%'
app.mercure.url: 'http://mercure/.well-known/mercure'
app.mercure.public_url: '%env(APP_URL)%/hub/.well-known/mercure'
services: services:
# default configuration for services in *this* file # default configuration for services in *this* file
_defaults: _defaults:
@@ -36,6 +39,7 @@ services:
# LDAP # LDAP
App\User\Framework\Security\LdapUserProvider: App\User\Framework\Security\LdapUserProvider:
arguments: arguments:
$registerLdapUserHandler: '@App\User\Action\Handler\RegisterLdapUserHandler'
$userRepository: '@App\User\Framework\Repository\UserRepository' $userRepository: '@App\User\Framework\Repository\UserRepository'
$ldap: '@Symfony\Component\Ldap\LdapInterface' $ldap: '@Symfony\Component\Ldap\LdapInterface'
$baseDn: '%env(LDAP_BASE_DN)%' $baseDn: '%env(LDAP_BASE_DN)%'

102
example.compose.yml Normal file
View File

@@ -0,0 +1,102 @@
services:
caddy:
image: caddy:2.9.1
restart: unless-stopped
cap_add:
- NET_ADMIN
ports:
- "80:80"
- "443:443"
- "443:443/udp"
volumes:
- $PWD/bash/caddy:/etc/caddy
- $PWD/bash/certs:/etc/ssl
app:
image: torsearch_test
ports:
- "8001:80"
environment:
APP_ENV: dev
APP_URL: ${APP_URL}
APP_SECRET: ${APP_SECRET}
DATABASE_URL: ${DATABASE_URL}
READ_DEBRID_KEY: ${REAL_DEBRID_KEY}
TMDB_API: ${TMDB_API}
MERCURE_JWT_SECRET: ${MERCURE_JWT_SECRET}
REDIS_HOST: ${REDIS_HOST}
LDAP_HOST: directory.caldwell.local
LDAP_PORT: 389
LDAP_ENCRYPTION: none
LDAP_BASE_DN: "dc=caldwell,dc=local"
LDAP_BIND_USER: "uid=admin,cn=users,cn=accounts,dc=caldwell,dc=local"
LDAP_BIND_PASS: "Caldwell.24272911"
LDAP_DN_STRING: "uid={user_identifier},cn=users,cn=accounts,dc=caldwell,dc=local"
LDAP_UID_KEY: "uid"
LDAP_ADMIN_ROLE_DN: ""
LDAP_EMAIL_ATTRIBUTE: mail
LDAP_USERNAME_ATTRIBUTE: uid
LDAP_NAME_ATTRIBUTE: displayname
worker:
image: torsearch_test
volumes:
- ./downloads/:/var/download
command: php ./bin/console messenger:consume async -v --time-limit=3600 --limit=10
environment:
APP_URL: ${APP_URL}
APP_SECRET: ${APP_SECRET}
DATABASE_URL: ${DATABASE_URL}
READ_DEBRID_KEY: ${REAL_DEBRID_KEY}
TMDB_API: ${TMDB_API}
MERCURE_JWT_SECRET: ${MERCURE_JWT_SECRET}
REDIS_HOST: ${REDIS_HOST}
scheduler:
image: torsearch_test
volumes:
- ./downloads:/var/download
command: php ./bin/console messenger:consume scheduler_monitor -vv --time-limit=3600
environment:
APP_URL: ${APP_URL}
APP_SECRET: ${APP_SECRET}
DATABASE_URL: ${DATABASE_URL}
READ_DEBRID_KEY: ${REAL_DEBRID_KEY}
TMDB_API: ${TMDB_API}
MERCURE_JWT_SECRET: ${MERCURE_JWT_SECRET}
REDIS_HOST: ${REDIS_HOST}
mercure:
image: dunglas/mercure
restart: unless-stopped
ports:
- "3000:80"
environment:
SERVER_NAME: ':80'
MERCURE_PUBLISHER_JWT_KEY: ${MERCURE_JWT_SECRET}
MERCURE_SUBSCRIBER_JWT_KEY: ${MERCURE_JWT_SECRET}
MERCURE_EXTRA_DIRECTIVES: |
cors_origins *
anonymous
command: /usr/bin/caddy run --config /etc/caddy/dev.Caddyfile
volumes:
- mercure_data:/data
- mercure_config:/config
database:
image: mariadb:10.11.2
ports:
- "3306:3306"
volumes:
- mysql:/var/lib/mysql
environment:
MYSQL_DATABASE: app
MYSQL_USERNAME: app
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: password
volumes:
mysql:
mercure_config:
mercure_data:

View 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,
) {}
}

View 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,
) {}
}

View 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)
);
}
}
}

View 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)
);
}
}
}

View 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,
) {}
}

View 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,
) {}
}

View File

@@ -2,16 +2,31 @@
namespace App\User\Framework\Controller\Web; 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\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils; use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class LoginController extends AbstractController class LoginController extends AbstractController
{ {
#[Route(path: '/login', name: 'app_login')] public function __construct(private readonly RegisterUserHandler $registerUserHandler)
public function login(AuthenticationUtils $authenticationUtils): Response
{ {
}
#[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 // get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError(); $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.'); 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,
]);
}
} }

View File

@@ -2,6 +2,8 @@
namespace App\User\Framework\Controller\Web; 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\User;
use App\User\Framework\Entity\UserPreference; use App\User\Framework\Entity\UserPreference;
use App\User\Framework\Form\RegistrationFormType; use App\User\Framework\Form\RegistrationFormType;
@@ -15,30 +17,24 @@ use Symfony\Component\Routing\Attribute\Route;
class RegistrationController extends AbstractController class RegistrationController extends AbstractController
{ {
public function __construct(private readonly RegisterUserHandler $registerUserHandler)
{
}
#[Route('/register', name: 'app_register')] #[Route('/register', name: 'app_register')]
public function register( public function register(
Request $request, Request $request,
UserPasswordHasherInterface $userPasswordHasher,
EntityManagerInterface $entityManager,
PreferencesRepository $preferencesRepository,
): Response { ): Response {
$user = new User(); $form = $this->createForm(RegistrationFormType::class, new User());
$form = $this->createForm(RegistrationFormType::class, $user);
$form->handleRequest($request); $form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {
/** @var string $plainPassword */ $this->registerUserHandler->handle(new RegisterUserCommand(
$plainPassword = $form->get('plainPassword')->getData(); name: $form->get('name')->getData(),
email: $form->get('email')->getData(),
// encode the plain password username: $form->get('username')->getData(),
$user->setPassword($userPasswordHasher->hashPassword($user, $plainPassword)); password: $form->get('plainPassword')->getData(),
));
$entityManager->persist($user);
$entityManager->flush();
$this->setUserPreferences($user, $preferencesRepository->findEnabled());
$preferencesRepository->getEntityManager()->flush();
return $this->redirectToRoute('app_index'); return $this->redirectToRoute('app_index');
} }
@@ -47,15 +43,4 @@ class RegistrationController extends AbstractController
'registrationForm' => $form, 'registrationForm' => $form,
]); ]);
} }
private function setUserPreferences(User $user, array $preferences): void
{
foreach ($preferences as $preference) {
$user->addUserPreference((new UserPreference())
->setUser($user)
->setPreference($preference)
->setPreferenceValue(null)
);
}
}
} }

View File

@@ -16,6 +16,7 @@ class RegistrationFormType extends AbstractType
{ {
$builder $builder
->add('email') ->add('email')
->add('username')
->add('name') ->add('name')
->add('plainPassword', PasswordType::class, [ ->add('plainPassword', PasswordType::class, [
// instead of being set onto the object directly, // instead of being set onto the object directly,

View File

@@ -11,6 +11,8 @@
namespace App\User\Framework\Security; 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\Entity\User;
use App\User\Framework\Repository\UserRepository; use App\User\Framework\Repository\UserRepository;
use Symfony\Component\Ldap\Entry; use Symfony\Component\Ldap\Entry;
@@ -45,6 +47,7 @@ class LdapUserProvider implements UserProviderInterface, PasswordUpgraderInterfa
private string $displayNameAttribute; private string $displayNameAttribute;
public function __construct( public function __construct(
private RegisterLdapUserHandler $registerLdapUserHandler,
private UserRepository $userRepository, private UserRepository $userRepository,
private LdapInterface $ldap, private LdapInterface $ldap,
private string $baseDn, private string $baseDn,
@@ -159,21 +162,24 @@ class LdapUserProvider implements UserProviderInterface, PasswordUpgraderInterfa
$dbUser = $this->getDbUser($identifier, $entry); $dbUser = $this->getDbUser($identifier, $entry);
if (null === $dbUser) { if (null === $dbUser) {
$dbUser = new User(); return $this->registerLdapUserHandler->handle(new RegisterLdapUserCommand(
$dbUser->setPassword("test"); 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 $dbUser;
} }
/** @return User */
private function getDbUser(string $identifier, Entry $entry): ?UserInterface private function getDbUser(string $identifier, Entry $entry): ?UserInterface
{ {
if (in_array($this->uidKey, ['mail', 'email'])) { if (in_array($this->uidKey, ['mail', 'email'])) {

View File

@@ -0,0 +1,79 @@
{% extends 'bare.html.twig' %}
{% block title %}Getting Started &mdash; Torsearch{% endblock %}
{% block body %}
<div class="flex flex-col bg-orange-500/50 p-4 rounded-lg gap-4 min-w-96 border-orange-500 border-2 text-gray-50">
<h2 class="text-2xl text-bold text-center text-gray-50">Getting Started</h2>
<p class="mb-2">Let's get started by creating your first User.</p>
{{ form_start(registrationForm) }}
<label for="name" class="flex flex-col mb-2">
{{ field_label(registrationForm.name) }}
{% if form_errors(registrationForm.name) %}
<span class="w-full p-1 text-[.775rem] font-bold border-2 border-red-600 text-black bg-red-500/70 rounded-md">
{{ form_errors(registrationForm.name) }}
</span>
{% endif %}
<input type="text"
name="{{ field_name(registrationForm.name) }}"
id="{{ field_name(registrationForm.name) }}"
value="{{ field_value(registrationForm.name) }}"
class="bg-gray-50 text-gray-50 p-1 bg-transparent border-b-2 border-orange-400" />
</label>
<label for="username" class="flex flex-col mb-2">
{{ field_label(registrationForm.username) }}
{% if form_errors(registrationForm.username) %}
<span class="w-full p-1 text-[.775rem] font-bold border-2 border-red-600 text-black bg-red-500/70 rounded-md">
{{ form_errors(registrationForm.username) }}
</span>
{% endif %}
<input type="text"
name="{{ field_name(registrationForm.username) }}"
id="{{ field_name(registrationForm.username) }}"
value="{{ field_value(registrationForm.username) }}"
class="bg-gray-50 text-gray-50 p-1 bg-transparent border-b-2 border-orange-400" />
</label>
<label for="email" class="flex flex-col mb-2">
{{ field_label(registrationForm.email) }}
{% if form_errors(registrationForm.email) %}
<span class="w-full p-1 text-[.775rem] font-bold text-black bg-red-500/70 rounded-md">
{{ form_errors(registrationForm.email) }}
</span>
{% endif %}
<input type="email"
name="{{ field_name(registrationForm.email) }}"
id="{{ field_name(registrationForm.email) }}"
value="{{ field_value(registrationForm.email) }}"
class="bg-gray-50 text-gray-50 p-1 bg-transparent border-b-2 border-orange-400" />
</label>
<label for="password" class="flex flex-col mb-2">
{{ field_label(registrationForm.plainPassword) }}
{% if form_errors(registrationForm.plainPassword) %}
<span class="w-full p-1 text-[.775rem] font-bold border-2 border-red-600 text-black bg-red-500/70 rounded-md">
{{ form_errors(registrationForm.plainPassword) }}
</span>
{% endif %}
<input type="password"
name="{{ field_name(registrationForm.plainPassword) }}"
id="{{ field_name(registrationForm.plainPassword) }}"
value="{{ field_value(registrationForm.plainPassword) }}"
class="bg-gray-50 text-gray-50 p-1 bg-transparent border-b-2 border-orange-400 mb-3" />
</label>
<button type="submit" class="bg-green-600/40 px-1.5 py-1 w-full rounded-md text-gray-50 backdrop-filter backdrop-blur-sm border-2 border-green-500 hover:bg-green-700/40">Register</button>
{{ form_end(registrationForm) }}
</div>
{% endblock %}

View File

@@ -3,11 +3,13 @@
{% block title %}Log in &mdash; Torsearch{% endblock %} {% block title %}Log in &mdash; Torsearch{% endblock %}
{% block body %} {% block body %}
<div class="flex flex-col bg-orange-500 bg-opacity-60 border-orange-500 border-2 p-4 rounded-lg gap-2 min-w-96 text-gray-50"> <div class="flex flex-col bg-orange-500/50 p-4 rounded-lg gap-4 min-w-96 border-orange-500 border-2 text-gray-50">
<h2 class="text-xl font-bold">Login</h2> <h2 class="text-xl font-bold">Login</h2>
<form method="post" class="flex flex-col gap-2"> <form method="post" class="flex flex-col gap-2">
{% if error %} {% if error %}
<div class="bg-red-400 border-red-600 rounded p-2 text-red-600">{{ error.messageKey|trans(error.messageData, 'security') }}</div> <div class="w-full p-1 mb-3 font-bold text-black bg-red-500/70 rounded-md">
{{ error.messageKey|trans(error.messageData, 'security') }}
</div>
{% endif %} {% endif %}
{% if app.user %} {% if app.user %}
@@ -16,15 +18,15 @@
</div> </div>
{% endif %} {% endif %}
<label for="username" class="mb-2 flex flex-col"> <label for="username" class="flex flex-col mb-2">
User User
<input type="" <input type="text"
value="{{ last_username }}"
name="_username" name="_username"
id="username" id="username"
class="bg-gray-50 text-gray-950 p-1 rounded-md" value="{{ last_username }}"
autocomplete="email" class="bg-gray-50 text-gray-50 p-1 bg-transparent border-b-2 border-orange-400"
required autofocus> required autofocus
/>
</label> </label>
<label for="password" class="mb-2 flex flex-col"> <label for="password" class="mb-2 flex flex-col">
@@ -32,7 +34,7 @@
<input type="password" <input type="password"
name="_password" name="_password"
id="password" id="password"
class="bg-gray-50 text-gray-950 p-1 rounded-md" class="bg-gray-50 text-gray-50 p-1 bg-transparent border-b-2 border-orange-400"
autocomplete="current-password" autocomplete="current-password"
required> required>
</label> </label>
@@ -43,8 +45,7 @@
<label for="_remember_me">Remember me</label> <label for="_remember_me">Remember me</label>
</div> </div>
<button type="submit" class="bg-green-600/40 px-1.5 py-1 w-full rounded-md text-gray-50 backdrop-filter backdrop-blur-sm border-2 border-green-500 hover:bg-green-700/40">
<button class="bg-green-600 px-1.5 py-1 rounded-md text-gray-50" type="submit">
Sign in Sign in
</button> </button>
</form> </form>