Skip to content

Commit

Permalink
Merge pull request #167 from crf-devs/improve-login-log
Browse files Browse the repository at this point in the history
Improve login log
  • Loading branch information
mRoca authored Mar 28, 2020
2 parents b538c36 + 6020a44 commit 3abc461
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
10 changes: 9 additions & 1 deletion src/Entity/Organization.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* )
* @ORM\Entity(repositoryClass="App\Repository\OrganizationRepository")
*/
class Organization implements UserInterface
class Organization implements UserInterface, \JsonSerializable
{
/**
* @ORM\Id
Expand Down Expand Up @@ -59,6 +59,14 @@ public function __toString(): string
return $this->name;
}

public function jsonSerialize(): array
{
return [
'id' => $this->id,
'name' => $this->__toString(),
];
}

public function getRoles(): array
{
return ['ROLE_ORGANIZATION'];
Expand Down
11 changes: 10 additions & 1 deletion src/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use JsonSerializable;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\UserInterface;
use function Symfony\Component\String\u;
Expand All @@ -25,7 +26,7 @@
* @UniqueEntity("emailAddress")
* @UniqueEntity("identificationNumber")
*/
class User implements UserInterface, AvailabilitableInterface
class User implements UserInterface, AvailabilitableInterface, JsonSerializable
{
const NIVOL_FORMAT = '#^\d+[A-Z]$#';

Expand Down Expand Up @@ -162,6 +163,14 @@ public function __toString(): string
return $this->organization->name.' / '.$this->getFullName();
}

public function jsonSerialize(): array
{
return [
'id' => $this->id,
'identificationNumber' => $this->identificationNumber,
];
}

public function getId(): ?int
{
return $this->id;
Expand Down
31 changes: 30 additions & 1 deletion src/EventListener/RequestLoggerListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\Security\Http\SecurityEvents;

final class RequestLoggerListener implements EventSubscriberInterface, LoggerAwareInterface
{
Expand All @@ -30,6 +33,9 @@ public static function getSubscribedEvents(): array
['onRequest', 255],
['logUser'],
],
SecurityEvents::INTERACTIVE_LOGIN => [
['onLogin'],
],
];
}

Expand Down Expand Up @@ -60,7 +66,30 @@ public function logUser(RequestEvent $event): void
return;
}

$this->logger->info('User logged in', ['username' => (string) $this->tokenStorage->getToken()->getUser()]);
$user = $this->tokenStorage->getToken()->getUser();
if (!$user instanceof UserInterface) {
return;
}

$this->logLogIn($user);
}

public function onLogin(InteractiveLoginEvent $event): void
{
$user = $event->getAuthenticationToken()->getUser();
if (!$user instanceof UserInterface) {
return;
}

$this->logLogIn($user);
}

private function logLogIn(UserInterface $user): void
{
if (!$user instanceof \JsonSerializable) {
return;
}
$this->logger->info('User logged in', $user->jsonSerialize());
}

private static function isHealthCheck(Request $request): bool
Expand Down
1 change: 1 addition & 0 deletions src/Monolog/Processor/ChangeLevelProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class ChangeLevelProcessor implements ProcessorInterface
private static array $debugMessages = [
'Matched route',
'Populated the TokenStorage with an anonymous Token',
'Guard authentication successful',
];

public function __invoke(array $record): array
Expand Down

0 comments on commit 3abc461

Please sign in to comment.