refactor user service
This commit is contained in:
parent
87ecf70d95
commit
271c2e31d1
|
|
@ -0,0 +1,25 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Event;
|
||||||
|
|
||||||
|
use App\Entity\User;
|
||||||
|
use Symfony\Contracts\EventDispatcher\Event;
|
||||||
|
|
||||||
|
class UserCreatedEvent extends Event
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private readonly User $newUser,
|
||||||
|
private readonly User $actingUser
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getNewUser(): User
|
||||||
|
{
|
||||||
|
return $this->newUser;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getActingUser(): User
|
||||||
|
{
|
||||||
|
return $this->actingUser;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,55 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\EventSubscriber;
|
||||||
|
|
||||||
|
use App\Event\UserCreatedEvent;
|
||||||
|
use App\Service\ActionService;
|
||||||
|
use App\Service\EmailService;
|
||||||
|
use App\Service\LoggerService;
|
||||||
|
use App\Service\UserService; // Only if you need helper methods, otherwise avoid to prevent circular ref
|
||||||
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||||
|
|
||||||
|
class UserSubscriber implements EventSubscriberInterface
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private readonly EmailService $emailService,
|
||||||
|
private readonly LoggerService $loggerService,
|
||||||
|
private readonly ActionService $actionService,
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getSubscribedEvents(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
UserCreatedEvent::class => 'onUserCreated',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function onUserCreated(UserCreatedEvent $event): void
|
||||||
|
{
|
||||||
|
$user = $event->getNewUser();
|
||||||
|
$actingUser = $event->getActingUser();
|
||||||
|
|
||||||
|
// 1. Generate Token (If logic was moved here, otherwise assume UserService set it)
|
||||||
|
// If the token generation logic is still in UserService, just send the email here.
|
||||||
|
// If you moved generating the token here, do it now.
|
||||||
|
|
||||||
|
// 2. Send Email
|
||||||
|
// Note: You might need to pass the token in the Event if it's not stored in the DB entity
|
||||||
|
// or generate a new one here if appropriate.
|
||||||
|
if ($user->getPasswordToken()) {
|
||||||
|
$this->emailService->sendPasswordSetupEmail($user, $user->getPasswordToken());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Log the creation
|
||||||
|
$this->loggerService->logUserCreated($user->getId(), $actingUser->getId());
|
||||||
|
|
||||||
|
// 4. Create the Audit Action
|
||||||
|
$this->actionService->createAction(
|
||||||
|
"Create new user",
|
||||||
|
$actingUser,
|
||||||
|
null,
|
||||||
|
$user->getUserIdentifier()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -8,7 +8,6 @@ use App\Entity\Roles;
|
||||||
use App\Entity\User;
|
use App\Entity\User;
|
||||||
use App\Entity\UserOrganizatonApp;
|
use App\Entity\UserOrganizatonApp;
|
||||||
use App\Entity\UsersOrganizations;
|
use App\Entity\UsersOrganizations;
|
||||||
use App\Service\AwsService;
|
|
||||||
use DateTimeImmutable;
|
use DateTimeImmutable;
|
||||||
use DateTimeZone;
|
use DateTimeZone;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
|
@ -17,9 +16,10 @@ use Exception;
|
||||||
use League\Bundle\OAuth2ServerBundle\Model\AccessToken;
|
use League\Bundle\OAuth2ServerBundle\Model\AccessToken;
|
||||||
use Random\RandomException;
|
use Random\RandomException;
|
||||||
use RuntimeException;
|
use RuntimeException;
|
||||||
use SebastianBergmann\CodeCoverage\Util\DirectoryCouldNotBeCreatedException;
|
|
||||||
use Symfony\Bundle\SecurityBundle\Security;
|
use Symfony\Bundle\SecurityBundle\Security;
|
||||||
use Symfony\Component\HttpFoundation\File\Exception\FileException;
|
use Symfony\Component\HttpFoundation\File\Exception\FileException;
|
||||||
|
use App\Event\UserCreatedEvent;
|
||||||
|
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
|
||||||
|
|
||||||
class UserService
|
class UserService
|
||||||
{
|
{
|
||||||
|
|
@ -33,9 +33,7 @@ class UserService
|
||||||
private readonly ActionService $actionService,
|
private readonly ActionService $actionService,
|
||||||
private readonly EmailService $emailService,
|
private readonly EmailService $emailService,
|
||||||
private readonly OrganizationsService $organizationsService,
|
private readonly OrganizationsService $organizationsService,
|
||||||
|
private readonly EventDispatcherInterface $eventDispatcher
|
||||||
|
|
||||||
|
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
@ -492,7 +490,8 @@ class UserService
|
||||||
User $existingUser,
|
User $existingUser,
|
||||||
Organizations $org,
|
Organizations $org,
|
||||||
User $actingUser,
|
User $actingUser,
|
||||||
): int {
|
): int
|
||||||
|
{
|
||||||
try {
|
try {
|
||||||
$uoId = $this->handleExistingUser($existingUser, $org);
|
$uoId = $this->handleExistingUser($existingUser, $org);
|
||||||
|
|
||||||
|
|
@ -528,14 +527,17 @@ class UserService
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$this->formatUserData($user, $picture, true);
|
$this->formatUserData($user, $picture, true);
|
||||||
|
|
||||||
|
// Generate token here if it's part of the user persistence flow
|
||||||
|
$token = $this->generatePasswordToken($user);
|
||||||
|
|
||||||
$this->entityManager->persist($user);
|
$this->entityManager->persist($user);
|
||||||
$this->entityManager->flush();
|
$this->entityManager->flush();
|
||||||
|
|
||||||
$this->loggerService->logUserCreated($user->getId(), $actingUser->getId());
|
$this->eventDispatcher->dispatch(new UserCreatedEvent($user, $actingUser));
|
||||||
$token = $this->generatePasswordToken($user);
|
|
||||||
$this->emailService->sendPasswordSetupEmail($user, $token);
|
|
||||||
$this->actionService->createAction("Create new user", $actingUser, null, $user->getUserIdentifier());
|
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
|
// Error logging remains here because the event won't fire if exception occurs
|
||||||
$this->loggerService->logError('Error creating new user: ' . $e->getMessage(), [
|
$this->loggerService->logError('Error creating new user: ' . $e->getMessage(), [
|
||||||
'target_user_email' => $user->getEmail(),
|
'target_user_email' => $user->getEmail(),
|
||||||
'acting_user_id' => $actingUser->getId(),
|
'acting_user_id' => $actingUser->getId(),
|
||||||
|
|
@ -551,7 +553,8 @@ class UserService
|
||||||
User $user,
|
User $user,
|
||||||
Organizations $org,
|
Organizations $org,
|
||||||
User $actingUser,
|
User $actingUser,
|
||||||
): UsersOrganizations {
|
): UsersOrganizations
|
||||||
|
{
|
||||||
try {
|
try {
|
||||||
$uo = new UsersOrganizations();
|
$uo = new UsersOrganizations();
|
||||||
$uo->setUsers($user);
|
$uo->setUsers($user);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue