Skip to content

Commit

Permalink
event subscriber to check online users
Browse files Browse the repository at this point in the history
  • Loading branch information
Steelwix committed Mar 6, 2023
1 parent ff09024 commit 4495f8a
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 51 deletions.
7 changes: 6 additions & 1 deletion config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ services:
- '../src/DependencyInjection/'
- '../src/Entity/'
- '../src/Kernel.php'

App\EventSubscriber\UserLastConnectionSubscriber:
arguments:
$security: '@security.helper'
$entityManager: '@doctrine.orm.entity_manager'
tags:
- { name: kernel.event_subscriber }
# add more service definitions when explicit configuration is needed
# please note that last definitions always *replace* previous ones
41 changes: 41 additions & 0 deletions src/EventSubscriber/UserLastConnectionSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace App\EventSubscriber;

use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;

class UserLastConnectionSubscriber implements EventSubscriberInterface
{
private $security;
private $entityManager;

public function __construct(Security $security, EntityManagerInterface $entityManager)
{
$this->security = $security;
$this->entityManager = $entityManager;
}

public static function getSubscribedEvents()
{
return [
KernelEvents::REQUEST => 'onKernelRequest'
];
}

public function onKernelRequest(RequestEvent $event)
{
$user = $this->security->getUser();

if ($user instanceof User) {
$user->setLastConnection(new \DateTime());
$this->entityManager->persist($user);
$this->entityManager->flush();
}
}
}
4 changes: 2 additions & 2 deletions src/Form/RegistrationFormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
],
'label' => 'Email'
])
->add('RGPDConsent', CheckboxType::class, [
->add('agreeTerms', CheckboxType::class, [
'mapped' => false,
'constraints' => [
new IsTrue([
Expand All @@ -40,7 +40,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
],
'label' => 'J\'accepte les conditions d\'utilisation '
])
->add('plainpassword', PasswordType::class, [
->add('plainPassword', PasswordType::class, [
// instead of being set onto the object directly,
// this is read and encoded in the controller
'mapped' => false,
Expand Down
5 changes: 4 additions & 1 deletion src/Security/PHPHUBAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace App\Security;

use App\EventListener\UserOnlineListener;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\SecurityBundle\Security as SecurityBundleSecurity;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -14,6 +17,7 @@
use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials;
use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
use Symfony\Component\Security\Http\Util\TargetPathTrait;
use Symfony\Component\HttpFoundation\Session\SessionInterface;

class PHPHUBAuthenticator extends AbstractLoginFormAuthenticator
{
Expand Down Expand Up @@ -46,7 +50,6 @@ public function onAuthenticationSuccess(Request $request, TokenInterface $token,
if ($targetPath = $this->getTargetPath($request->getSession(), $firewallName)) {
return new RedirectResponse($targetPath);
}

return new RedirectResponse($this->urlGenerator->generate('app_home'));
}

Expand Down
36 changes: 0 additions & 36 deletions src/Service/OnlineUser.php

This file was deleted.

2 changes: 2 additions & 0 deletions templates/default/main.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@
{% endblock %}

{% block body %}

<div {{ vue_component('Home', { 'name': name, 'services': services } ) }}></div>

{% endblock %}
24 changes: 13 additions & 11 deletions templates/registration/register.html.twig
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
{% extends 'base.html.twig' %}

{% block title %}Register{% endblock %}
{% block title %}Register
{% endblock %}

{% block body %}
{% for flash_error in app.flashes('verify_email_error') %}
<div class="alert alert-danger" role="alert">{{ flash_error }}</div>
{% endfor %}
{% for flash_error in app.flashes('verify_email_error') %}
<div class="alert alert-danger" role="alert">{{ flash_error }}</div>
{% endfor %}

<h1>Register</h1>
<h1>Register</h1>

{{ form_start(registrationForm) }}
{{ form_row(registrationForm.email) }}
{{ form_row(registrationForm.plainPassword, {
{{ form_start(registrationForm) }}
{{ form_row(registrationForm.email) }}
{{ form_row(registrationForm.username) }}
{{ form_row(registrationForm.plainPassword, {
label: 'Password'
}) }}
{{ form_row(registrationForm.agreeTerms) }}
{{ form_row(registrationForm.agreeTerms) }}

<button type="submit" class="btn">Register</button>
{{ form_end(registrationForm) }}
<button type="submit" class="btn">Register</button>
{{ form_end(registrationForm) }}
{% endblock %}

0 comments on commit 4495f8a

Please sign in to comment.