Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Login form update #206

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/packages/security.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
$mainFirewall->anonymous();
$mainFirewall
->formLogin()
->usernameParameter('number')
->passwordParameter('password')
->loginPath('login')
->checkPath('login');
$mainFirewall
Expand Down
10 changes: 10 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use BikeShare\App\Configuration;
use BikeShare\App\EventListener\ErrorListener;
use BikeShare\Credit\CodeGenerator\CodeGenerator;
use BikeShare\Credit\CodeGenerator\CodeGeneratorInterface;
use BikeShare\Credit\CreditSystem;
Expand Down Expand Up @@ -41,6 +42,14 @@
param('kernel.environment'),
]);

$services->set('exception_listener', ErrorListener::class)
->args([
param('kernel.error_controller'),
service('logger'),
param('kernel.debug'),
])
->tag('kernel.event_subscriber');

$services->set(Configuration::class)
->args([__DIR__ . '/../config.php']);

Expand All @@ -49,6 +58,7 @@
'../src/Db/MysqliDbResult.php',
'../src/SmsConnector/SmsGateway/SmsGateway.php',
'../src/App/Configuration.php',
'../src/App/EventListener/ErrorListener.php',
'../src/App/Kernel.php',
'../src/App/Entity',
]);
Expand Down
26 changes: 26 additions & 0 deletions src/App/EventListener/ErrorListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace BikeShare\App\EventListener;

use Symfony\Component\HttpKernel\EventListener\ErrorListener as SymfonyErrorListener;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class ErrorListener extends SymfonyErrorListener
{
protected function logException(\Throwable $exception, string $message, string $logLevel = null): void
{
if (null !== $this->logger) {
if (!$exception instanceof HttpExceptionInterface || $exception->getStatusCode() >= 500) {
$this->logger->critical($message, ['exception' => $exception]);
} elseif ($exception instanceof NotFoundHttpException) {
//do not log 404 errors
return;
} else {
$this->logger->error($message, ['exception' => $exception]);
}
}
}
}
5 changes: 2 additions & 3 deletions src/App/Security/TokenProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ public function updateToken(string $series, string $tokenValue, \DateTime $lastU

$token = new PersistentToken(
$currentToken->getClass(),
method_exists($currentToken, 'getUserIdentifier') ?
$currentToken->getUserIdentifier() : $currentToken->getUsername(),
$currentToken->getUserIdentifier(),
$series,
$tokenValue,
$lastUsed
Expand Down Expand Up @@ -83,7 +82,7 @@ public function createNewToken(PersistentTokenInterface $token)
$this->db->query(
"INSERT INTO remember_me_tokens (class, username, series, value, lastUsed)
VALUES ('{$token->getClass()}',
'{$token->getUsername()}',
'{$token->getUserIdentifier()}',
'{$token->getSeries()}',
'{$token->getTokenValue()}',
'{$token->getLastUsed()->format('Y-m-d H:i:s')}')"
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/SecurityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function resetPassword(
TranslatorInterface $translator
) {
if ($request->isMethod('POST')) {
$number = $request->request->get('_username');
$number = $request->request->get('number');

try {
$user = $userProvider->loadUserByIdentifier($number);
Expand Down
14 changes: 13 additions & 1 deletion src/Mail/DebugMailSender.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
<?php

declare(strict_types=1);

namespace BikeShare\Mail;

use Psr\Log\LoggerInterface;

class DebugMailSender implements MailSenderInterface
{
private LoggerInterface $logger;

public function __construct(
LoggerInterface $logger
) {
$this->logger = $logger;
}

public function sendMail($recipient, $subject, $message)
{
echo $recipient, ' | ', $subject, ' | ', $message . PHP_EOL;
$this->logger->debug('Sending email', compact('recipient', 'subject', 'message'));
}
}
4 changes: 2 additions & 2 deletions templates/security/login.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<label for="username">
{% if isSmsSystemEnabled %}{{ 'Phone number:'|trans }}{% else %}{{ 'User number:'|trans }}{% endif %}
</label>
<input type="text" id="username" name="_username" value="{{ last_username }}" class="form-control" required autofocus>
<input type="text" id="username" name="number" value="{{ last_username }}" class="form-control" required autofocus>
</div>

<div class="form-group">
Expand All @@ -21,7 +21,7 @@
(<a href="{{ path('reset_password') }}">{{ 'Forgotten? Reset password'|trans }}</a>)
</small>
</label>
<input type="password" id="password" name="_password" class="form-control" required>
<input type="password" id="password" name="password" class="form-control" required>
</div>

<div class="form-group form-check">
Expand Down
2 changes: 1 addition & 1 deletion templates/security/reset_password.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<label for="username">
{% if isSmsSystemEnabled %}{{ 'Phone number:'|trans }}{% else %}{{ 'User number:'|trans }}{% endif %}
</label>
<input type="text" id="username" name="_username" value="" class="form-control" required autofocus>
<input type="text" id="username" name="number" value="" class="form-control" required autofocus>
</div>

<button type="submit" class="btn btn-primary btn-block">
Expand Down
12 changes: 10 additions & 2 deletions tests/Mail/DebugMailSenderTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<?php

declare(strict_types=1);

namespace Test\BikeShare\Mail;

use BikeShare\Mail\DebugMailSender;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;

class DebugMailSenderTest extends TestCase
{
Expand All @@ -12,8 +15,13 @@ public function testSendMail()
$recipient = 'recipient';
$subject = 'subject';
$message = 'message';
$mailer = new DebugMailSender();
$loggerMock = $this->createMock(LoggerInterface::class);
$mailer = new DebugMailSender($loggerMock);

$loggerMock->expects($this->once())
->method('debug')
->with('Sending email', compact('recipient', 'subject', 'message'));

$mailer->sendMail($recipient, $subject, $message);
$this->expectOutputString($recipient . ' | ' . $subject . ' | ' . $message . PHP_EOL);
}
}