feat: introduce user management events

Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
2026-08-07 22:32:45 -04:00
parent 985e4a6450
commit f147ffc5c7
11 changed files with 457 additions and 19 deletions
+48 -11
View File
@@ -6,14 +6,19 @@ use KTXC\Models\Identity\User;
use KTXC\Context\IdentityContextInterface;
use KTXC\Context\TenantContextInterface;
use KTXC\Stores\UserAccountsStore;
use KTXC\User\Event\UserCreatedEvent;
use KTXC\User\Event\UserDeletingEvent;
use KTXC\User\Event\UserUpdatedEvent;
use KTXF\Event\EventDispatcherInterface;
class UserAccountsService
{
public function __construct(
private readonly TenantContextInterface $tenantContext,
private readonly IdentityContextInterface $identityContext,
private readonly UserAccountsStore $userStore
private readonly IdentityContextInterface $identityContext,
private readonly UserAccountsStore $userStore,
private readonly EventDispatcherInterface $events,
) {
}
@@ -65,17 +70,53 @@ class UserAccountsService
public function createUser(array $userData): array
{
return $this->userStore->createUser($this->tenantContext->identifier(), $userData);
$tenantId = $this->tenantContext->requireIdentifier();
$user = $this->userStore->createUser($tenantId, $userData);
$this->events->dispatch(UserCreatedEvent::fromUser(
$user,
$tenantId,
$this->identityContext->identifier(),
));
return $user;
}
public function updateUser(string $uid, array $updates): bool
public function updateUser(string $userId, array $updates): bool
{
return $this->userStore->updateUser($this->tenantContext->identifier(), $uid, $updates);
$tenantId = $this->tenantContext->requireIdentifier();
if (!$this->userStore->updateUser($tenantId, $userId, $updates)) {
return false;
}
$user = $this->userStore->fetchByIdentifier($tenantId, $userId);
if ($user === null) {
throw new \RuntimeException("Updated user '{$userId}' could not be retrieved.");
}
$this->events->dispatch(UserUpdatedEvent::fromUser(
$user,
$tenantId,
$this->identityContext->identifier(),
));
return true;
}
public function deleteUser(string $uid): bool
public function deleteUser(string $userId): bool
{
return $this->userStore->deleteUser($this->tenantContext->identifier(), $uid);
$tenantId = $this->tenantContext->requireIdentifier();
$user = $this->userStore->fetchByIdentifier($tenantId, $userId);
if ($user === null) {
return false;
}
$this->events->dispatch(UserDeletingEvent::fromUser(
$user,
$tenantId,
$this->identityContext->identifier(),
));
return $this->userStore->deleteUser($tenantId, $userId);
}
// =========================================================================
@@ -126,10 +167,6 @@ class UserAccountsService
return $this->userStore->storeSettings($this->tenantContext->identifier(), $this->identityContext->identifier(), $settings);
}
// =========================================================================
// Helper Methods
// =========================================================================
/**
* Check if a profile field is editable by the user
*