I Have tried to get the connected user by injecting the security dependancy but it return null all time.I have used Docker for mercure Hub server, it’s working well (get above this question to get the configuration of mercure Hub).
Can you give me a solution ?
Here is my message
<?php
namespace AppSchedulerMessage;
class ResetPasswordNotification
{
}
Here is my messageHandler
<?php
namespace AppSchedulerHandler;
use SymfonyComponentMercureUpdate;
use SymfonyComponentMercureHubInterface;
use AppSchedulerMessageResetPasswordNotification;
use AppServiceUserService;
use SymfonyComponentMessengerAttributeAsMessageHandler;
use SymfonyBundleSecurityBundleSecurity;
#[AsMessageHandler]
class ResetPasswordNotificationHandler
{
public function __construct(private HubInterface $hub, private Security $security)
{
}
public function __invoke(ResetPasswordNotification $message)
{
dump($this->security->getUser()); // getting null all time
dump('hello');
//dd($this->security->getUser()); // getting null all time
$update = new Update(
'notifPasswordReset',
'Veuillez modifier votre mot de passe'
);
$this->hub->publish($update);
// dd('ok');
// $user = $this->security->getUser(); // L"utilisateur connecté n'est pas reconnu et je ne sais pas pourquoi
// dd($user);
// if ($user) {
// $intervals = ['P0D', 'P83D', 'P85D', 'P87D', 'P90D'];
// $lastUpdatedPassword = $user->getLastUpdatedPassword();
// $today = new DateTime();
// foreach ($intervals as $interval) {
// $dateToCheck = clone $lastUpdatedPassword;
// $dateToCheck->add(new DateInterval($interval));
// if ($dateToCheck->format('Y-m-d') === $today->format('Y-m-d')) {
// $update = new Update(
// 'notifPasswordReset',
// 'Veuillez modifier votre mot de passe'
// );
// $this->hub->publish($update);
// // return new Response('Notif envoyé!');
// }
// }
// } else {
// throw new ErrorException("Aucun utilisateur connecté");
// }
}
}
Here is my scheduler program of automatisation of task
<?php
namespace AppScheduler;
use AppSchedulerMessageResetPasswordNotification;
use SymfonyComponentSchedulerAttributeAsSchedule;
use SymfonyComponentSchedulerRecurringMessage;
use SymfonyComponentSchedulerSchedule;
use SymfonyComponentSchedulerScheduleProviderInterface;
use SymfonyContractsCacheCacheInterface;
#[AsSchedule]
final class MainSchedule implements ScheduleProviderInterface
{
public function __construct(
private CacheInterface $cache,
) {
}
public function getSchedule(): Schedule
{
return (new Schedule())
->add(
// @TODO - Create a Message to schedule
// RecurringMessage::every('1 hour', new AppMessageMessage()),
RecurringMessage::every('1 second', new ResetPasswordNotification())
)
->stateful($this->cache);
}
}
//////////////////////// CONFIGURATION OF MERCURE HUB ////////////////////////////////////////////
“/compose.yaml” file
version: '3.8'
services:
mercure:
image: dunglas/mercure
restart: unless-stopped
environment:
SERVER_NAME: ':80'
MERCURE_PUBLISHER_JWT_KEY: '!ChangeThisMercureHubJWTSecretKey!'
MERCURE_SUBSCRIBER_JWT_KEY: '!ChangeThisMercureHubJWTSecretKey!'
MERCURE_EXTRA_DIRECTIVES: |
cors_origins *
command: /usr/bin/caddy run --config /etc/caddy/Caddyfile.dev --adapter caddyfile
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost/healthz"]
timeout: 5s
retries: 5
start_period: 60s
volumes:
- mercure_data:/data
- mercure_config:/config
- ./config/docker/dev.Caddyfile:/etc/caddy/Caddyfile.dev:ro
ports:
- "80:80"
- "443:443"
volumes:
database_data:
mercure_data:
mercure_config:
“/compose.override.yaml” file
version: '3.8'
services:
###> symfony/mercure-bundle ###
mercure:
ports:
- "3000:80"
###< symfony/mercure-bundle ###
“/env.local” file
DATABASE_URL="mysql://root:connexion_2024&@127.0.0.1:3306/opera?serverVersion=8.3.0&charset=utf8mb4"
MESSENGER_TRANSPORT_DSN=doctrine://default?auto_setup=0
MERCURE_URL=http://127.0.0.1:3000/.well-known/mercure
# The public URL of the Mercure hub, used by the browser to connect
MERCURE_PUBLIC_URL=http://127.0.0.1:3000/.well-known/mercure
# The secret used to sign the JWTs
MERCURE_JWT_SECRET="!ChangeThisMercureHubJWTSecretKey!"
I have tried to inject Security dependancy, i have also tried to create my own service to get the connected user like this :
The service
<?php
namespace AppService;
use AppEntityUser;
use SymfonyBundleSecurityBundleSecurity;
class UserService
{
private ?Security $security = null;
public function __construct(Security $security)
{
$this->security = $security;
}
public function getUser(): ?User
{
return $this->security->getUser();
}
}
Declaration of the service in “config/services.yaml”
`AppServiceUserService:`
Using the service in the handler of scheduler
<?php
namespace AppSchedulerHandler;
use SymfonyComponentMercureUpdate;
use SymfonyComponentMercureHubInterface;
use AppSchedulerMessageResetPasswordNotification;
use AppServiceUserService;
use SymfonyComponentMessengerAttributeAsMessageHandler;
use SymfonyBundleSecurityBundleSecurity;
#[AsMessageHandler]
class ResetPasswordNotificationHandler
{
public function __construct(private HubInterface $hub, private UserService $userService, private Security $security)
{
}
public function __invoke(ResetPasswordNotification $message)
{
dump($this->userService->getUser()); // getting null all time
dump($this->security->getUser()); // getting null all time
dump('hello');
//dd($this->security->getUser()); // getting null all time
$update = new Update(
'notifPasswordReset',
'Veuillez modifier votre mot de passe'
);
$this->hub->publish($update);
// dd('ok');
// $user = $this->security->getUser(); // L"utilisateur connecté n'est pas reconnu et je ne sais pas pourquoi
// dd($user);
// if ($user) {
// $intervals = ['P0D', 'P83D', 'P85D', 'P87D', 'P90D'];
// $lastUpdatedPassword = $user->getLastUpdatedPassword();
// $today = new DateTime();
// foreach ($intervals as $interval) {
// $dateToCheck = clone $lastUpdatedPassword;
// $dateToCheck->add(new DateInterval($interval));
// if ($dateToCheck->format('Y-m-d') === $today->format('Y-m-d')) {
// $update = new Update(
// 'notifPasswordReset',
// 'Veuillez modifier votre mot de passe'
// );
// $this->hub->publish($update);
// // return new Response('Notif envoyé!');
// }
// }
// } else {
// throw new ErrorException("Aucun utilisateur connecté");
// }
}
}