major update
This commit is contained in:
@@ -3,8 +3,8 @@
|
||||
namespace MintyPHP\Service\Audit;
|
||||
|
||||
use MintyPHP\Http\ApiAuth;
|
||||
use MintyPHP\Http\RequestContext;
|
||||
use MintyPHP\Repository\Audit\ApiAuditLogRepository;
|
||||
use MintyPHP\Repository\Support\RepoQuery;
|
||||
|
||||
class ApiAuditService
|
||||
{
|
||||
@@ -25,6 +25,10 @@ class ApiAuditService
|
||||
return;
|
||||
}
|
||||
|
||||
RequestContext::ensureStarted();
|
||||
RequestContext::setChannel('api');
|
||||
$requestContext = RequestContext::context();
|
||||
|
||||
$method = strtoupper(trim((string) ($_SERVER['REQUEST_METHOD'] ?? 'GET')));
|
||||
if ($method === 'OPTIONS') {
|
||||
$this->context = ['active' => false, 'finished' => true];
|
||||
@@ -42,13 +46,23 @@ class ApiAuditService
|
||||
'active' => true,
|
||||
'finished' => false,
|
||||
'started_at' => microtime(true),
|
||||
'request_id' => RepoQuery::uuidV4(),
|
||||
'method' => substr($method !== '' ? $method : 'GET', 0, 8),
|
||||
'path' => substr($path, 0, 255),
|
||||
'request_id' => (string) ($requestContext['request_id'] ?? RequestContext::id()),
|
||||
'method' => (string) ($requestContext['method'] ?? substr($method !== '' ? $method : 'GET', 0, 8)),
|
||||
'path' => (string) ($requestContext['path'] ?? substr($path, 0, 255)),
|
||||
'query_json' => $this->buildRedactedQueryJson($_GET),
|
||||
];
|
||||
}
|
||||
|
||||
public function currentRequestId(): ?string
|
||||
{
|
||||
if (!is_array($this->context)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$requestId = trim((string) ($this->context['request_id'] ?? ''));
|
||||
return $requestId !== '' ? $requestId : null;
|
||||
}
|
||||
|
||||
public function finish(int $statusCode, ?string $errorCode = null): void
|
||||
{
|
||||
if (!is_array($this->context)) {
|
||||
@@ -79,14 +93,16 @@ class ApiAuditService
|
||||
$normalizedErrorCode = substr($normalizedErrorCode, 0, 100);
|
||||
}
|
||||
|
||||
$ip = trim((string) ($_SERVER['REMOTE_ADDR'] ?? ''));
|
||||
$requestContext = RequestContext::context();
|
||||
|
||||
$ip = trim((string) ($requestContext['ip'] ?? ($_SERVER['REMOTE_ADDR'] ?? '')));
|
||||
if ($ip === '') {
|
||||
$ip = null;
|
||||
} elseif (strlen($ip) > 45) {
|
||||
$ip = substr($ip, 0, 45);
|
||||
}
|
||||
|
||||
$userAgent = trim((string) ($_SERVER['HTTP_USER_AGENT'] ?? ''));
|
||||
$userAgent = trim((string) ($requestContext['user_agent'] ?? ($_SERVER['HTTP_USER_AGENT'] ?? '')));
|
||||
if ($userAgent === '') {
|
||||
$userAgent = null;
|
||||
} elseif (strlen($userAgent) > self::MAX_USER_AGENT_LENGTH) {
|
||||
@@ -99,7 +115,7 @@ class ApiAuditService
|
||||
$tokenTenantId = ApiAuth::isAuthenticated() ? (int) (ApiAuth::scopedTenantId() ?? 0) : 0;
|
||||
|
||||
$payload = [
|
||||
'request_id' => (string) ($this->context['request_id'] ?? RepoQuery::uuidV4()),
|
||||
'request_id' => (string) ($this->context['request_id'] ?? RequestContext::id()),
|
||||
'method' => (string) ($this->context['method'] ?? ''),
|
||||
'path' => (string) ($this->context['path'] ?? ''),
|
||||
'query_json' => $this->context['query_json'] ?? null,
|
||||
@@ -131,6 +147,11 @@ class ApiAuditService
|
||||
return $this->apiAuditLogRepository->find($id);
|
||||
}
|
||||
|
||||
public function filterOptions(int $limit = 200): array
|
||||
{
|
||||
return $this->apiAuditLogRepository->listFilterOptions($limit);
|
||||
}
|
||||
|
||||
public function purgeExpired(): int
|
||||
{
|
||||
return $this->apiAuditLogRepository->purgeOlderThanDays(self::RETENTION_DAYS);
|
||||
|
||||
Reference in New Issue
Block a user