refactor(audit): extract audit domain into self-contained module

Move the entire audit subsystem (system audit, API audit, import audit,
user lifecycle audit, frontend telemetry) from core into modules/audit/.

Core decoupling via interface-based injection:
- AuditRecorderInterface replaces SystemAuditService in 10+ core services
- UserLifecycleAuditInterface / ImportAuditInterface for specialized flows
- NullAuditRecorder fallback when audit module is disabled
- ApiBootstrap/ApiResponse use null-safe callable resolvers

Module structure (modules/audit/):
- Manifest with routes, permissions, scheduler jobs, authorization policy
- 9 services, 8 repositories, 6 domain enums, 4 job handlers
- 33 page files, 4 JS files, 8 test files, migration scripts, i18n

Core cleanup:
- OperationsAuthorizationPolicy, UiCapabilityMap, PermissionService
  surgically cleaned of audit-specific constants
- Sidebar template cleared of hardcoded audit navigation
- AuditModuleIsolationContractTest ensures no future core→module coupling

All quality gates pass: 1346 tests (19,276 assertions), PHPStan level 5
clean, architecture contracts verified.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-25 21:12:49 +01:00
parent 12a837bda9
commit 0c351f6aff
176 changed files with 2157 additions and 834 deletions

View File

@@ -2,7 +2,6 @@
namespace MintyPHP\Http;
use MintyPHP\Service\Audit\ApiAuditService;
use MintyPHP\Service\Security\RateLimiterService;
use MintyPHP\Service\Settings\SettingsApiPolicyGateway;
@@ -20,20 +19,20 @@ class ApiBootstrap
private static bool $initialized = false;
private static bool $shutdownRegistered = false;
/** @var (callable(): ApiAuditService)|null */
/** @var (callable(): object)|null Resolver for API audit service (provided by audit module) */
private static $apiAuditServiceResolver = null;
/** @var (callable(): SettingsApiPolicyGateway)|null */
private static $settingsApiPolicyGatewayResolver = null;
/** @var (callable(): RateLimiterService)|null */
private static $rateLimiterServiceResolver = null;
/** @var (callable(): ApiSystemAuditReporter)|null */
/** @var (callable(): object)|null Resolver for API system audit reporter (provided by audit module) */
private static $apiSystemAuditReporterResolver = null;
public static function configure(
callable $apiAuditServiceResolver,
?callable $apiAuditServiceResolver,
callable $settingsApiPolicyGatewayResolver,
callable $rateLimiterServiceResolver,
callable $apiSystemAuditReporterResolver
?callable $apiSystemAuditReporterResolver
): void {
self::$apiAuditServiceResolver = $apiAuditServiceResolver;
self::$settingsApiPolicyGatewayResolver = $settingsApiPolicyGatewayResolver;
@@ -66,7 +65,7 @@ class ApiBootstrap
header('X-Request-Id: ' . RequestContext::requestHeaderValue());
self::registerShutdownHandler();
self::apiAuditService()->startRequestContext();
self::tryStartAudit();
self::startSystemAuditReporter();
self::setCorsHeaders();
@@ -123,7 +122,7 @@ class ApiBootstrap
if ($statusCode <= 0) {
$statusCode = 200;
}
self::apiAuditService()->finish($statusCode);
self::tryFinishAudit($statusCode);
self::finishSystemAuditReporter($statusCode);
return;
}
@@ -135,7 +134,7 @@ class ApiBootstrap
if ($statusCode <= 0) {
$statusCode = 200;
}
self::apiAuditService()->finish($statusCode);
self::tryFinishAudit($statusCode);
self::finishSystemAuditReporter($statusCode);
return;
}
@@ -144,7 +143,7 @@ class ApiBootstrap
if ($statusCode < 400) {
$statusCode = 500;
}
self::apiAuditService()->finish($statusCode, 'fatal_error');
self::tryFinishAudit($statusCode, 'fatal_error');
self::finishSystemAuditReporter($statusCode, 'fatal_error');
});
}
@@ -252,20 +251,43 @@ class ApiBootstrap
return self::resolveDependency(self::$rateLimiterServiceResolver, RateLimiterService::class, 'ApiBootstrap');
}
private static function apiAuditService(): ApiAuditService
private static function tryStartAudit(): void
{
return self::resolveDependency(self::$apiAuditServiceResolver, ApiAuditService::class, 'ApiBootstrap');
try {
if (is_callable(self::$apiAuditServiceResolver)) {
$service = (self::$apiAuditServiceResolver)();
if (is_object($service) && method_exists($service, 'startRequestContext')) {
$service->startRequestContext();
}
}
} catch (\Throwable) {
// fail-open
}
}
private static function systemAuditReporter(): ApiSystemAuditReporter
private static function tryFinishAudit(int $statusCode, ?string $errorCode = null): void
{
return self::resolveDependency(self::$apiSystemAuditReporterResolver, ApiSystemAuditReporter::class, 'ApiBootstrap');
try {
if (is_callable(self::$apiAuditServiceResolver)) {
$service = (self::$apiAuditServiceResolver)();
if (is_object($service) && method_exists($service, 'finish')) {
$service->finish($statusCode, $errorCode);
}
}
} catch (\Throwable) {
// fail-open
}
}
private static function startSystemAuditReporter(): void
{
try {
self::systemAuditReporter()->start();
if (is_callable(self::$apiSystemAuditReporterResolver)) {
$service = (self::$apiSystemAuditReporterResolver)();
if (is_object($service) && method_exists($service, 'start')) {
$service->start();
}
}
} catch (\Throwable) {
// fail-open
}
@@ -274,7 +296,12 @@ class ApiBootstrap
private static function finishSystemAuditReporter(int $statusCode, ?string $errorCode = null): void
{
try {
self::systemAuditReporter()->finish($statusCode, $errorCode);
if (is_callable(self::$apiSystemAuditReporterResolver)) {
$service = (self::$apiSystemAuditReporterResolver)();
if (is_object($service) && method_exists($service, 'finish')) {
$service->finish($statusCode, $errorCode);
}
}
} catch (\Throwable) {
// fail-open
}