264 lines
10 KiB
PHP
264 lines
10 KiB
PHP
<?php
|
|
|
|
namespace App\Service;
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\RequestStack;
|
|
|
|
readonly class LoggerService
|
|
{
|
|
public function __construct(
|
|
private LoggerInterface $userManagementLogger,
|
|
private LoggerInterface $organizationManagementLogger,
|
|
private LoggerInterface $accessControlLogger,
|
|
private LoggerInterface $emailNotificationLogger,
|
|
private LoggerInterface $adminActionsLogger,
|
|
private LoggerInterface $securityLogger,
|
|
private LoggerInterface $errorLogger,
|
|
private LoggerInterface $awsLogger,
|
|
private RequestStack $requestStack,
|
|
) {}
|
|
|
|
|
|
|
|
// User Management Logs
|
|
public function logUserCreated(int|string $userId, int|string $actingUserId): void
|
|
{
|
|
$this->userManagementLogger->notice("New user created: $userId", [
|
|
'target_user_id' => $userId,
|
|
'acting_user_id' => $actingUserId,
|
|
'ip' => $this->requestStack->getCurrentRequest()?->getClientIp() ?? 'unknown',
|
|
'timestamp' => $this->now(),
|
|
]);
|
|
}
|
|
|
|
// Organization Management Logs
|
|
public function logUserOrganizationLinkCreated(int|string $userId, int $orgId, int|string $actingUserId, ?int $uoId): void
|
|
{
|
|
$this->organizationManagementLogger->notice('User-Organization link created', [
|
|
'target_user_id' => $userId,
|
|
'organization_id' => $orgId,
|
|
'acting_user_id' => $actingUserId,
|
|
'uo_id' => $uoId,
|
|
'ip' => $this->requestStack->getCurrentRequest()?->getClientIp() ?? 'unknown',
|
|
'timestamp' => $this->now(),
|
|
]);
|
|
}
|
|
|
|
public function logExistingUserAddedToOrg(int|string $userId, int $orgId, int|string $actingUserId, int $uoId): void
|
|
{
|
|
$this->organizationManagementLogger->notice('Existing user added to organization', [
|
|
'target_user_id' => $userId,
|
|
'organization_id' => $orgId,
|
|
'acting_user_id' => $actingUserId,
|
|
'uo_id' => $uoId,
|
|
'ip' => $this->requestStack->getCurrentRequest()?->getClientIp() ?? 'unknown',
|
|
'timestamp' => $this->now(),
|
|
]);
|
|
}
|
|
|
|
// Email Notification Logs
|
|
public function logEmailSent(int|string $userId, ?int $orgId, string $message): void
|
|
{
|
|
$this->emailNotificationLogger->notice($message, [
|
|
'target_user_id' => $userId,
|
|
'organization_id' => $orgId,
|
|
'ip' => $this->requestStack->getCurrentRequest()?->getClientIp() ?? 'unknown',
|
|
'timestamp' => $this->now(),
|
|
]);
|
|
}
|
|
|
|
public function logExistingUserNotificationSent(int|string $userId, int $orgId): void
|
|
{
|
|
$this->emailNotificationLogger->notice("Existing user notification email sent to $userId", [
|
|
'target_user_id' => $userId,
|
|
'organization_id' => $orgId,
|
|
'ip' => $this->requestStack->getCurrentRequest()?->getClientIp() ?? 'unknown',
|
|
'timestamp' => $this->now(),
|
|
]);
|
|
}
|
|
|
|
public function logAdminNotified(array $array): void
|
|
{
|
|
$this->emailNotificationLogger->notice('Organization admin notified', array_merge($array, [
|
|
'ip' => $this->requestStack->getCurrentRequest()?->getClientIp() ?? 'unknown',
|
|
'timestamp' => $this->now(),
|
|
]));
|
|
}
|
|
|
|
public function logSuperAdmin(int|string $userId, int|string $actingUserId, string $message, ?int $orgId = null): void
|
|
{
|
|
$this->adminActionsLogger->notice($message, [
|
|
'target_user_id' => $userId,
|
|
'organization_id' => $orgId,
|
|
'acting_user_id' => $actingUserId,
|
|
'ip' => $this->requestStack->getCurrentRequest()?->getClientIp() ?? 'unknown',
|
|
'timestamp' => $this->now(),
|
|
]);
|
|
}
|
|
|
|
// Error Logs
|
|
public function logError(string $message, array $context = []): void
|
|
{
|
|
$this->errorLogger->error($message, array_merge($context, [
|
|
'timestamp' => $this->now(),
|
|
'ip' => $this->requestStack->getCurrentRequest()?->getClientIp() ?? 'unknown',
|
|
]));
|
|
}
|
|
|
|
public function logCritical(string $message, array $context = []): void
|
|
{
|
|
$this->errorLogger->critical($message, array_merge($context, [
|
|
'timestamp' => $this->now(),
|
|
'ip' => $this->requestStack->getCurrentRequest()?->getClientIp() ?? 'unknown',
|
|
]));
|
|
}
|
|
|
|
// Security Logs
|
|
public function logAccessDenied(int|string $actingUserId): void
|
|
{
|
|
$this->securityLogger->warning('Access denied', [
|
|
'acting_user_id' => $actingUserId,
|
|
'ip' => $this->requestStack->getCurrentRequest()?->getClientIp() ?? 'unknown',
|
|
'timestamp' => $this->now(),
|
|
'page_accessed' => $_SERVER['REQUEST_URI'] ?? 'unknown',
|
|
]);
|
|
}
|
|
|
|
// Helper
|
|
private function now(): string
|
|
{
|
|
return (new \DateTimeImmutable('now'))->format(DATE_ATOM);
|
|
}
|
|
|
|
|
|
public function logUserAction(int $targetId, int|string $actingUserId, string $message): void
|
|
{
|
|
$this->userManagementLogger->notice($message, [
|
|
'target_user_id'=> $targetId,
|
|
'acting_user_id'=> $actingUserId,
|
|
'ip' => $this->requestStack->getCurrentRequest()?->getClientIp() ?? 'unknown',
|
|
'timestamp' => $this->now(),
|
|
]);
|
|
}
|
|
|
|
public function logAdminAction(int $targetId, int|string $actingUserId, int $organizationId, string $message): void
|
|
{
|
|
$this->adminActionsLogger->notice($message, [
|
|
'target_id' => $targetId,
|
|
'acting_user_id'=> $actingUserId,
|
|
'organization_id'=> $organizationId,
|
|
'ip' => $this->requestStack->getCurrentRequest()?->getClientIp() ?? 'unknown',
|
|
'timestamp' => $this->now(),
|
|
]);
|
|
}
|
|
|
|
public function logEntityNotFound(string $entityType, array $criteria, int|string $actingUserId): void
|
|
{
|
|
$this->errorLogger->error('Entity not found', array_merge($criteria, [
|
|
'entity_type' => $entityType,
|
|
'acting_user_id' => $actingUserId,
|
|
'ip' => $this->requestStack->getCurrentRequest()?->getClientIp() ?? 'unknown',
|
|
'timestamp' => $this->now(),
|
|
'page_accessed' => $_SERVER['REQUEST_URI'] ?? 'unknown',
|
|
]));
|
|
}
|
|
|
|
public function logAWSAction(string $action, array $details): void
|
|
{
|
|
$this->awsLogger->info("AWS action performed: $action", array_merge($details, [
|
|
'ip' => $this->requestStack->getCurrentRequest()?->getClientIp() ?? 'unknown',
|
|
'timestamp' => $this->now(),
|
|
]));
|
|
}
|
|
|
|
public function logTokenRevocation(string $message, array $array): void
|
|
{
|
|
$this->securityLogger->warning($message, array_merge($array, [
|
|
'ip' => $this->requestStack->getCurrentRequest()?->getClientIp() ?? 'unknown',
|
|
'timestamp' => $this->now(),
|
|
]));
|
|
}
|
|
|
|
public function logUOALinkDeactivated(int $uoaId, int $appId, int $roleId): void
|
|
{
|
|
$this->organizationManagementLogger->notice('UOA link deactivated', [
|
|
'uoa_id' => $uoaId,
|
|
'app_id' => $appId,
|
|
'role_id' => $roleId,
|
|
'ip' => $this->requestStack->getCurrentRequest()?->getClientIp() ?? 'unknown',
|
|
'timestamp' => $this->now(),
|
|
]);
|
|
}
|
|
|
|
public function logOrganizationInformation(int $organizationId, int|string $actingUserId, string $message): void
|
|
{
|
|
$this->organizationManagementLogger->info($message, [
|
|
'organization_id' => $organizationId,
|
|
'acting_user_id' => $actingUserId,
|
|
'ip' => $this->requestStack->getCurrentRequest()?->getClientIp() ?? 'unknown',
|
|
'timestamp' => $this->now(),
|
|
]);
|
|
}
|
|
|
|
public function logRoleEntityAssignment(int|string $userId, int $organizationId, int $roleId, int|string $actingUserId, string $message): void
|
|
{
|
|
$this->accessControlLogger->info($message, [
|
|
'target_user_id' => $userId,
|
|
'organization_id' => $organizationId,
|
|
'role_id' => $roleId,
|
|
'acting_user_id' => $actingUserId,
|
|
'ip' => $this->requestStack->getCurrentRequest()?->getClientIp() ?? 'unknown',
|
|
'timestamp' => $this->now(),
|
|
]);
|
|
}
|
|
public function logRoleAssignment(string $message, array $context): void
|
|
{
|
|
$this->accessControlLogger->info($message, [
|
|
'context' => $context,
|
|
'ip' => $this->requestStack->getCurrentRequest()?->getClientIp() ?? 'unknown',
|
|
'timestamp' => $this->now(),
|
|
]);
|
|
}
|
|
|
|
public function logUserConnection(string $message, array $array)
|
|
{
|
|
$this->securityLogger->info($message, array_merge($array, [
|
|
'ip' => $this->requestStack->getCurrentRequest()?->getClientIp() ?? 'unknown',
|
|
'timestamp' => $this->now(),
|
|
]));
|
|
}
|
|
|
|
public function logCGUAcceptance(int $it)
|
|
{
|
|
$this->userManagementLogger->info("User accepted CGU", [
|
|
'user_id' => $it,
|
|
'ip' => $this->requestStack->getCurrentRequest()?->getClientIp() ?? 'unknown',
|
|
'timestamp' => $this->now(),
|
|
]);
|
|
$this->securityLogger->info("User accepted CGU", [
|
|
'user_id' => $it,
|
|
'ip' => $this->requestStack->getCurrentRequest()?->getClientIp() ?? 'unknown',
|
|
'timestamp' => $this->now(),
|
|
]);
|
|
}
|
|
|
|
public function logTokenError(string $message, array $context = []): void
|
|
{
|
|
$this->securityLogger->error($message, array_merge($context, [
|
|
'timestamp' => $this->now(),
|
|
'ip' => $this->requestStack->getCurrentRequest()?->getClientIp() ?? 'unknown',
|
|
]));
|
|
}
|
|
|
|
public function logApplicationInformation(string $string, array $array, int|string $actingUser)
|
|
{
|
|
$this->accessControlLogger->info($string, array_merge($array, [
|
|
'acting_user_id' => $actingUser,
|
|
'ip' => $this->requestStack->getCurrentRequest()?->getClientIp() ?? 'unknown',
|
|
'timestamp' => $this->now(),
|
|
]));
|
|
}
|
|
}
|