clqms-be/app/Services/AuditService.php
root 2bcdf09b55 chore: repo-wide normalization + rules test coverage
Normalize formatting/line endings across configs, controllers, models, tests, and OpenAPI specs.

Update rule expression/rule engine implementation and remove obsolete RuleAction controller/model.

Add unit tests for rule expression syntax and multi-action behavior, and include docs updates.
2026-03-16 07:24:50 +07:00

197 lines
5.9 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' => $previousValue,
'new_value' => $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' => $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' => $resourceDetails,
'previous_value' => $previousValue,
'new_value' => $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' => $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' => $previousValue,
'new_value' => $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' => $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' => $errorDetails,
'previous_value' => $previousValue,
'new_value' => $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' => $context,
'created_at' => date('Y-m-d H:i:s')
]);
}
private static function log(string $table, array $data): void {
$db = \Config\Database::connect();
$db->table($table)->insert($data);
}
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');
}
}