210 lines
6.6 KiB
PHP
210 lines
6.6 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use CodeIgniter\Database\BaseConnection;
|
|
|
|
class AuditService {
|
|
protected BaseConnection $db;
|
|
|
|
public function __construct() {
|
|
$this->db = \Config\Database::connect();
|
|
}
|
|
|
|
public static function logData(
|
|
string $operation,
|
|
string $entityType,
|
|
string $entityId,
|
|
?string $tableName = null,
|
|
?string $fieldName = null,
|
|
?array $previousValue = null,
|
|
?array $newValue = null,
|
|
?string $reason = null,
|
|
?array $context = null
|
|
): void {
|
|
self::log('data_audit_log', [
|
|
'operation' => $operation,
|
|
'entity_type' => $entityType,
|
|
'entity_id' => $entityId,
|
|
'table_name' => $tableName,
|
|
'field_name' => $fieldName,
|
|
'previous_value' => self::normalizeAuditValue($previousValue),
|
|
'new_value' => self::normalizeAuditValue($newValue),
|
|
'mechanism' => 'MANUAL',
|
|
'application_id' => 'CLQMS-WEB',
|
|
'web_page' => self::getUri(),
|
|
'session_id' => self::getSessionId(),
|
|
'event_type' => strtoupper($entityType) . '_' . strtoupper($operation),
|
|
'site_id' => self::getSiteId(),
|
|
'workstation_id' => self::getWorkstationId(),
|
|
'pc_name' => self::getPcName(),
|
|
'ip_address' => self::getIpAddress(),
|
|
'user_id' => self::getUserId(),
|
|
'reason' => $reason,
|
|
'context' => self::normalizeAuditValue($context),
|
|
'created_at' => date('Y-m-d H:i:s')
|
|
]);
|
|
}
|
|
|
|
public static function logService(
|
|
string $operation,
|
|
string $entityType,
|
|
string $entityId,
|
|
string $serviceClass,
|
|
?string $resourceType = null,
|
|
?array $resourceDetails = null,
|
|
?array $previousValue = null,
|
|
?array $newValue = null,
|
|
?string $serviceName = null,
|
|
?array $context = null
|
|
): void {
|
|
self::log('service_audit_log', [
|
|
'operation' => $operation,
|
|
'entity_type' => $entityType,
|
|
'entity_id' => $entityId,
|
|
'service_class' => $serviceClass,
|
|
'resource_type' => $resourceType,
|
|
'resource_details' => self::normalizeAuditValue($resourceDetails),
|
|
'previous_value' => self::normalizeAuditValue($previousValue),
|
|
'new_value' => self::normalizeAuditValue($newValue),
|
|
'mechanism' => 'AUTOMATIC',
|
|
'application_id' => $serviceName ?? 'SYSTEM-SERVICE',
|
|
'service_name' => $serviceName,
|
|
'session_id' => self::getSessionId() ?: 'service_session',
|
|
'event_type' => strtoupper($serviceClass) . '_' . strtoupper($operation),
|
|
'site_id' => self::getSiteId(),
|
|
'workstation_id' => self::getWorkstationId(),
|
|
'pc_name' => self::getPcName(),
|
|
'ip_address' => self::getIpAddress(),
|
|
'port' => $resourceDetails['port'] ?? null,
|
|
'user_id' => 'SYSTEM',
|
|
'reason' => null,
|
|
'context' => self::normalizeAuditValue($context),
|
|
'created_at' => date('Y-m-d H:i:s')
|
|
]);
|
|
}
|
|
|
|
public static function logSecurity(
|
|
string $operation,
|
|
string $entityType,
|
|
string $entityId,
|
|
string $securityClass,
|
|
?string $eventType = 'SUCCESS',
|
|
?string $resourcePath = null,
|
|
?array $previousValue = null,
|
|
?array $newValue = null,
|
|
?string $reason = null,
|
|
?array $context = null
|
|
): void {
|
|
self::log('security_audit_log', [
|
|
'operation' => $operation,
|
|
'entity_type' => $entityType,
|
|
'entity_id' => $entityId,
|
|
'security_class' => $securityClass,
|
|
'resource_path' => $resourcePath,
|
|
'previous_value' => self::normalizeAuditValue($previousValue),
|
|
'new_value' => self::normalizeAuditValue($newValue),
|
|
'mechanism' => 'MANUAL',
|
|
'application_id' => 'CLQMS-WEB',
|
|
'web_page' => self::getUri(),
|
|
'session_id' => self::getSessionId(),
|
|
'event_type' => $eventType,
|
|
'site_id' => self::getSiteId(),
|
|
'workstation_id' => self::getWorkstationId(),
|
|
'pc_name' => self::getPcName(),
|
|
'ip_address' => self::getIpAddress(),
|
|
'user_id' => self::getUserId() ?? 'UNKNOWN',
|
|
'reason' => $reason,
|
|
'context' => self::normalizeAuditValue($context),
|
|
'created_at' => date('Y-m-d H:i:s')
|
|
]);
|
|
}
|
|
|
|
public static function logError(
|
|
string $entityType,
|
|
string $entityId,
|
|
string $errorCode,
|
|
string $errorMessage,
|
|
string $eventType,
|
|
?array $errorDetails = null,
|
|
?array $previousValue = null,
|
|
?array $newValue = null,
|
|
?string $reason = null,
|
|
?array $context = null
|
|
): void {
|
|
self::log('error_audit_log', [
|
|
'operation' => 'ERROR',
|
|
'entity_type' => $entityType,
|
|
'entity_id' => $entityId,
|
|
'error_code' => $errorCode,
|
|
'error_message' => $errorMessage,
|
|
'error_details' => self::normalizeAuditValue($errorDetails),
|
|
'previous_value' => self::normalizeAuditValue($previousValue),
|
|
'new_value' => self::normalizeAuditValue($newValue),
|
|
'mechanism' => 'AUTOMATIC',
|
|
'application_id' => 'CLQMS-WEB',
|
|
'web_page' => self::getUri(),
|
|
'session_id' => self::getSessionId() ?: 'system',
|
|
'event_type' => $eventType,
|
|
'site_id' => self::getSiteId(),
|
|
'workstation_id' => self::getWorkstationId(),
|
|
'pc_name' => self::getPcName(),
|
|
'ip_address' => self::getIpAddress(),
|
|
'user_id' => self::getUserId() ?? 'SYSTEM',
|
|
'reason' => $reason,
|
|
'context' => self::normalizeAuditValue($context),
|
|
'created_at' => date('Y-m-d H:i:s')
|
|
]);
|
|
}
|
|
|
|
private static function log(string $table, array $data): void {
|
|
$db = \Config\Database::connect();
|
|
if (!$db->tableExists($table)) {
|
|
return;
|
|
}
|
|
$db->table($table)->insert($data);
|
|
}
|
|
|
|
private static function normalizeAuditValue($value)
|
|
{
|
|
if ($value === null || is_scalar($value)) {
|
|
return $value;
|
|
}
|
|
|
|
$json = json_encode($value, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
|
|
return $json !== false ? $json : null;
|
|
}
|
|
|
|
private static function getUri(): ?string {
|
|
return $_SERVER['REQUEST_URI'] ?? null;
|
|
}
|
|
|
|
private static function getSessionId(): ?string {
|
|
$session = session();
|
|
return $session->get('session_id');
|
|
}
|
|
|
|
private static function getSiteId(): ?string {
|
|
$session = session();
|
|
return $session->get('site_id');
|
|
}
|
|
|
|
private static function getWorkstationId(): ?string {
|
|
$session = session();
|
|
return $session->get('workstation_id');
|
|
}
|
|
|
|
private static function getPcName(): ?string {
|
|
return gethostname();
|
|
}
|
|
|
|
private static function getIpAddress(): ?string {
|
|
return $_SERVER['REMOTE_ADDR'] ?? null;
|
|
}
|
|
|
|
private static function getUserId(): ?string {
|
|
$session = session();
|
|
return $session->get('user_id');
|
|
}
|
|
}
|