Skip to content

Commit

Permalink
refactor!: Removing reset password service, adding login link, refact…
Browse files Browse the repository at this point in the history
…oring routes
  • Loading branch information
philipsorst committed Mar 19, 2024
1 parent 9e0b97c commit 84c30ec
Show file tree
Hide file tree
Showing 30 changed files with 275 additions and 404 deletions.
13 changes: 0 additions & 13 deletions config/routes/health.php

This file was deleted.

13 changes: 0 additions & 13 deletions config/routes/login.php

This file was deleted.

14 changes: 0 additions & 14 deletions config/routes/reset_password.php

This file was deleted.

26 changes: 26 additions & 0 deletions config/services/login_link.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Dontdrinkandroot\BridgeBundle\Config;

use Dontdrinkandroot\BridgeBundle\Command\User\EditCommand;
use Dontdrinkandroot\BridgeBundle\Controller\Security\LoginLink\CheckAction;
use Dontdrinkandroot\BridgeBundle\Controller\Security\LoginAction;
use Dontdrinkandroot\BridgeBundle\Controller\Security\LoginLink\RequestAction;
use Dontdrinkandroot\BridgeBundle\Repository\User\UserRepository;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

use function Symfony\Component\DependencyInjection\Loader\Configurator\param;

return function (ContainerConfigurator $configurator): void {
$services = $configurator->services();

$services->set(RequestAction::class)
->autoconfigure()
->autowire()
->tag('controller.service_arguments');

$services->set(CheckAction::class)
->autoconfigure()
->autowire()
->tag('controller.service_arguments');
};
19 changes: 0 additions & 19 deletions config/services/reset_password.php

This file was deleted.

8 changes: 8 additions & 0 deletions config/services/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
use Dontdrinkandroot\BridgeBundle\Controller\ValueResolver\IdEntityArgumentValueResolver;
use Dontdrinkandroot\BridgeBundle\Controller\ValueResolver\UuidEntityArgumentValueResolver;
use Dontdrinkandroot\BridgeBundle\Form\Type\FlexDateType;
use Dontdrinkandroot\BridgeBundle\Model\Container\ParamName;
use Dontdrinkandroot\BridgeBundle\Model\Container\TagName;
use Dontdrinkandroot\BridgeBundle\Routing\DdrBridgeLoader;
use Dontdrinkandroot\BridgeBundle\Routing\NestedLoader;
use Dontdrinkandroot\BridgeBundle\Service\EncryptionService;
use Dontdrinkandroot\BridgeBundle\Service\Health\HttpHealthProvider;
Expand All @@ -17,6 +19,7 @@
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\HttpFoundation\RequestStack;

use function Symfony\Component\DependencyInjection\Loader\Configurator\param;
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;
use function Symfony\Component\DependencyInjection\Loader\Configurator\tagged_iterator;

Expand All @@ -32,6 +35,11 @@
])
->tag('routing.loader');

$services->set(DdrBridgeLoader::class)#
->arg('$userEnabled', param(ParamName::USER_ENABLED))
->arg('$loginLinkEnabled', param(ParamName::USER_LOGIN_LINK_ENABLED))
->tag('routing.loader');

$services->set(EncryptionService::class);

$services->set(GenerateKeyCommand::class)
Expand Down
6 changes: 4 additions & 2 deletions config/services/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Dontdrinkandroot\BridgeBundle\Command\User\EditCommand;
use Dontdrinkandroot\BridgeBundle\Controller\Security\LoginAction;
use Dontdrinkandroot\BridgeBundle\Model\Container\ParamName;
use Dontdrinkandroot\BridgeBundle\Repository\User\UserRepository;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

Expand All @@ -15,16 +16,17 @@
$services->set(UserRepository::class, UserRepository::class)
->autoconfigure()
->autowire()
->arg('$entityClass', param('ddr.bridge_bundle.user.class'));
->arg('$entityClass', param(ParamName::USER_CLASS));

$services->set(EditCommand::class, EditCommand::class)
->autoconfigure()
->autowire()
->arg('$userClass', param('ddr.bridge_bundle.user.class'))
->arg('$userClass', param(ParamName::USER_CLASS))
->tag('console.command');

$services->set(LoginAction::class)
->autoconfigure()
->autowire()
->arg('$loginLinkEnabled', param(ParamName::USER_LOGIN_LINK_ENABLED))
->tag('controller.service_arguments');
};
5 changes: 2 additions & 3 deletions recipe/config/routes/ddr_bridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

namespace App\Config\Routes;

use Dontdrinkandroot\BridgeBundle\Routing\DdrBridgeLoader;
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;

return function (RoutingConfigurator $routes): void {
$routes->import('@DdrBridgeBundle/config/routes/health.php');
// $routes->import('@DdrBridgeBundle/config/routes/login.php');
// $routes->import('@DdrBridgeBundle/config/routes/reset_password.php');
$routes->import('.', DdrBridgeLoader::TYPE);
};
12 changes: 3 additions & 9 deletions src/Controller/Security/LoginAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
class LoginAction extends AbstractController
{
public function __construct(
private readonly UrlGeneratorInterface $urlGenerator,
private readonly AuthenticationUtils $authenticationUtils
private readonly AuthenticationUtils $authenticationUtils,
private readonly bool $loginLinkEnabled
) {
}

Expand All @@ -24,16 +24,10 @@ public function __invoke(): Response
/* last username entered by the user */
$lastUsername = $this->authenticationUtils->getLastUsername();

try {
$pathPasswordReset = $this->urlGenerator->generate('ddr.bridge.security.reset_password');
} catch (RouteNotFoundException) {
$pathPasswordReset = null;
}

return $this->render('@DdrBridge/Security/login.html.twig', [
'last_username' => $lastUsername,
'error' => $error,
'pathPasswordReset' => $pathPasswordReset
'loginLinkEnabled' => $this->loginLinkEnabled,
]);
}
}
16 changes: 16 additions & 0 deletions src/Controller/Security/LoginLink/CheckAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Dontdrinkandroot\BridgeBundle\Controller\Security\LoginLink;

use Dontdrinkandroot\BridgeBundle\Model\Container\RouteName;
use LogicException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Attribute\Route;

class CheckAction extends AbstractController
{
public function __invoke(): never
{
throw new LogicException('This code should never be reached');
}
}
68 changes: 68 additions & 0 deletions src/Controller/Security/LoginLink/RequestAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Dontdrinkandroot\BridgeBundle\Controller\Security\LoginLink;

use Dontdrinkandroot\BridgeBundle\Model\Container\RouteName;
use Dontdrinkandroot\BridgeBundle\Repository\User\UserRepository;
use Dontdrinkandroot\BridgeBundle\Service\Mail\MailServiceInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mime\Address;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Http\LoginLink\LoginLinkHandlerInterface;
use Symfony\Component\Translation\TranslatableMessage;

class RequestAction extends AbstractController
{
public function __construct(
private readonly LoginLinkHandlerInterface $loginLinkHandler,
private readonly TokenStorageInterface $tokenStorage,
private readonly UserRepository $userRepository,
private readonly MailServiceInterface $mailService,
) {
}

public function __invoke(Request $request): Response
{
if (null !== ($user = $this->tokenStorage->getToken()?->getUser())) {
return $this->redirectToRoute('app.index');
}

$form = $this->createFormBuilder()
->add('email', EmailType::class)
->getForm();

$success = false;
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$email = $form->getData()['email'];
$user = $this->userRepository->findOneByEmail($email);
if (null !== $user) {
$loginLinkDetails = $this->loginLinkHandler->createLoginLink($user);
$loginLink = $loginLinkDetails->getUrl();
$this->mailService->sendMailMarkdownTemplate(
to: new Address($user->email),
subject: 'Login Link',
template: '@DdrBridge/Security/login_link.mail.md.twig',
templateParameters: [
'user' => $user,
'link' => $loginLink,
]
);
}
$this->addFlash(
'success',
new TranslatableMessage('login_link.flash.success', [], 'ddr_security')
);
$success = true;
}

return $this->render('@DdrBridge/Security/request_login_link.html.twig', [
'form' => $form->createView(),
'success' => $success,
]);
}
}
106 changes: 0 additions & 106 deletions src/Controller/Security/ResetPasswordAction.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->addDefaultsIfNotSet()
->children()
->scalarNode('class')->defaultValue('App\Entity\User')->end()
->booleanNode('reset_password')->defaultFalse()->end()
->booleanNode('login_link')->defaultFalse()->end()
->end()
->end()
->arrayNode('mail')
Expand Down
Loading

0 comments on commit 84c30ec

Please sign in to comment.