nis2-agile/application/controllers/AuditController.php
DevEnv nis2-agile 874eabb6fc [FEAT] Simulazioni Demo + Audit Trail Certificato SHA-256
- 5 scenari reali: Onboarding, Ransomware Art.23, Data Breach Supply Chain,
  Whistleblowing SCADA, Audit Hash Chain Verification
- simulate-nis2.php: 3 aziende (DataCore/MedClinic/EnerNet), 10 fasi, CLI+SSE
- AuditService.php: hash chain SHA-256 stile lg231 (prev_hash+entry_hash)
- Migration 010: prev_hash, entry_hash, severity, performed_by su audit_logs
- AuditController: GET chain-verify + GET export-certified
- reset-demo.sql: reset dati demo idempotente
- public/simulate.html: web runner SSE con console dark-theme
- Sidebar: link Simulazione Demo + Integrazioni

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-07 13:56:53 +01:00

317 lines
12 KiB
PHP

<?php
/**
* NIS2 Agile - Audit Controller
*
* Controlli compliance, evidenze, audit logs, mapping ISO 27001.
*/
require_once __DIR__ . '/BaseController.php';
require_once APP_PATH . '/services/ReportService.php';
class AuditController extends BaseController
{
public function listControls(): void
{
$this->requireOrgAccess();
$controls = Database::fetchAll(
'SELECT cc.*, u.full_name as responsible_name
FROM compliance_controls cc
LEFT JOIN users u ON u.id = cc.responsible_user_id
WHERE cc.organization_id = ?
ORDER BY cc.control_code',
[$this->getCurrentOrgId()]
);
$this->jsonSuccess($controls);
}
public function updateControl(int $id): void
{
$this->requireOrgRole(['org_admin', 'compliance_manager', 'auditor']);
$updates = [];
foreach (['status', 'implementation_percentage', 'evidence_description', 'responsible_user_id', 'next_review_date'] as $field) {
if ($this->hasParam($field)) {
$updates[$field] = $this->getParam($field);
}
}
if (isset($updates['status']) && $updates['status'] === 'verified') {
$updates['last_verified_at'] = date('Y-m-d H:i:s');
}
if (!empty($updates)) {
Database::update('compliance_controls', $updates, 'id = ? AND organization_id = ?', [$id, $this->getCurrentOrgId()]);
$this->logAudit('control_updated', 'compliance_control', $id, $updates);
}
$this->jsonSuccess($updates, 'Controllo aggiornato');
}
public function uploadEvidence(): void
{
$this->requireOrgRole(['org_admin', 'compliance_manager', 'auditor']);
if (!isset($_FILES['file'])) {
$this->jsonError('File non fornito', 400, 'NO_FILE');
}
$file = $_FILES['file'];
$maxSize = 10 * 1024 * 1024; // 10MB
if ($file['size'] > $maxSize) {
$this->jsonError('File troppo grande (max 10MB)', 400, 'FILE_TOO_LARGE');
}
$orgId = $this->getCurrentOrgId();
$uploadDir = UPLOAD_PATH . "/evidence/{$orgId}";
if (!is_dir($uploadDir)) {
mkdir($uploadDir, 0755, true);
}
$ext = pathinfo($file['name'], PATHINFO_EXTENSION);
$filename = uniqid('ev_') . '.' . $ext;
$filePath = $uploadDir . '/' . $filename;
if (!move_uploaded_file($file['tmp_name'], $filePath)) {
$this->jsonError('Errore caricamento file', 500, 'UPLOAD_ERROR');
}
$evidenceId = Database::insert('evidence_files', [
'organization_id' => $orgId,
'control_id' => $this->getParam('control_id'),
'entity_type' => $this->getParam('entity_type'),
'entity_id' => $this->getParam('entity_id'),
'file_name' => $file['name'],
'file_path' => "evidence/{$orgId}/{$filename}",
'file_size' => $file['size'],
'mime_type' => $file['type'],
'uploaded_by' => $this->getCurrentUserId(),
]);
$this->logAudit('evidence_uploaded', 'evidence', $evidenceId);
$this->jsonSuccess(['id' => $evidenceId], 'Evidenza caricata', 201);
}
public function listEvidence(): void
{
$this->requireOrgAccess();
$where = 'organization_id = ?';
$params = [$this->getCurrentOrgId()];
if ($this->hasParam('control_id')) {
$where .= ' AND control_id = ?';
$params[] = $this->getParam('control_id');
}
if ($this->hasParam('entity_type') && $this->hasParam('entity_id')) {
$where .= ' AND entity_type = ? AND entity_id = ?';
$params[] = $this->getParam('entity_type');
$params[] = $this->getParam('entity_id');
}
$evidence = Database::fetchAll(
"SELECT ef.*, u.full_name as uploaded_by_name
FROM evidence_files ef
LEFT JOIN users u ON u.id = ef.uploaded_by
WHERE ef.{$where}
ORDER BY ef.created_at DESC",
$params
);
$this->jsonSuccess($evidence);
}
public function generateReport(): void
{
$this->requireOrgAccess();
$orgId = $this->getCurrentOrgId();
$org = Database::fetchOne('SELECT * FROM organizations WHERE id = ?', [$orgId]);
$controls = Database::fetchAll('SELECT * FROM compliance_controls WHERE organization_id = ? ORDER BY control_code', [$orgId]);
$lastAssessment = Database::fetchOne('SELECT * FROM assessments WHERE organization_id = ? AND status = "completed" ORDER BY completed_at DESC LIMIT 1', [$orgId]);
$riskCount = Database::count('risks', 'organization_id = ? AND status != "closed"', [$orgId]);
$incidentCount = Database::count('incidents', 'organization_id = ?', [$orgId]);
$policyCount = Database::count('policies', 'organization_id = ? AND status IN ("approved","published")', [$orgId]);
$totalControls = count($controls);
$implemented = count(array_filter($controls, fn($c) => in_array($c['status'], ['implemented', 'verified'])));
$this->jsonSuccess([
'organization' => $org,
'report_date' => date('Y-m-d H:i:s'),
'compliance_summary' => [
'total_controls' => $totalControls,
'implemented_controls' => $implemented,
'compliance_percentage' => $totalControls > 0 ? round($implemented / $totalControls * 100) : 0,
],
'controls' => $controls,
'last_assessment' => $lastAssessment,
'risk_count' => $riskCount,
'incident_count' => $incidentCount,
'policy_count' => $policyCount,
]);
}
public function getAuditLogs(): void
{
$this->requireOrgRole(['org_admin', 'auditor']);
$pagination = $this->getPagination(50);
$total = Database::count('audit_logs', 'organization_id = ?', [$this->getCurrentOrgId()]);
$logs = Database::fetchAll(
"SELECT al.*, u.full_name
FROM audit_logs al
LEFT JOIN users u ON u.id = al.user_id
WHERE al.organization_id = ?
ORDER BY al.created_at DESC
LIMIT {$pagination['per_page']} OFFSET {$pagination['offset']}",
[$this->getCurrentOrgId()]
);
$this->jsonPaginated($logs, $total, $pagination['page'], $pagination['per_page']);
}
public function getIsoMapping(): void
{
$this->requireOrgAccess();
$mapping = [
['nis2' => '21.2.a', 'iso27001' => 'A.5.1, A.5.2, A.8.1, A.8.2', 'title' => 'Risk analysis and security policies'],
['nis2' => '21.2.b', 'iso27001' => 'A.5.24, A.5.25, A.5.26, A.6.8', 'title' => 'Incident handling'],
['nis2' => '21.2.c', 'iso27001' => 'A.5.29, A.5.30', 'title' => 'Business continuity'],
['nis2' => '21.2.d', 'iso27001' => 'A.5.19, A.5.20, A.5.21, A.5.22', 'title' => 'Supply chain security'],
['nis2' => '21.2.e', 'iso27001' => 'A.8.25, A.8.26, A.8.27, A.8.28', 'title' => 'System acquisition and development'],
['nis2' => '21.2.f', 'iso27001' => 'A.5.35, A.5.36', 'title' => 'Effectiveness assessment'],
['nis2' => '21.2.g', 'iso27001' => 'A.6.3, A.6.6', 'title' => 'Cyber hygiene and training'],
['nis2' => '21.2.h', 'iso27001' => 'A.8.24', 'title' => 'Cryptography and encryption'],
['nis2' => '21.2.i', 'iso27001' => 'A.5.15, A.5.16, A.5.17, A.5.18, A.6.1, A.6.2', 'title' => 'HR security, access control, asset management'],
['nis2' => '21.2.j', 'iso27001' => 'A.8.5', 'title' => 'Multi-factor authentication'],
];
$this->jsonSuccess($mapping);
}
/**
* GET /api/audit/executive-report
* Genera report esecutivo HTML (stampabile come PDF)
*/
public function executiveReport(): void
{
$this->requireOrgRole(['org_admin', 'compliance_manager', 'board_member']);
$reportService = new ReportService();
$html = $reportService->generateExecutiveReport($this->getCurrentOrgId());
header('Content-Type: text/html; charset=utf-8');
echo $html;
exit;
}
/**
* GET /api/audit/export/{type}
* Esporta dati in CSV
*/
public function export(int $type = 0): void
{
$this->requireOrgRole(['org_admin', 'compliance_manager', 'auditor']);
$exportType = $_GET['type'] ?? 'controls';
$orgId = $this->getCurrentOrgId();
$reportService = new ReportService();
$csv = match ($exportType) {
'risks' => $reportService->exportRisksCSV($orgId),
'incidents' => $reportService->exportIncidentsCSV($orgId),
'controls' => $reportService->exportControlsCSV($orgId),
'assets' => $reportService->exportAssetsCSV($orgId),
default => $reportService->exportControlsCSV($orgId),
};
$filename = "nis2_{$exportType}_" . date('Y-m-d') . '.csv';
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename="' . $filename . '"');
echo $csv;
exit;
}
/**
* GET /api/audit/chain-verify
* Verifica l'integrità dell'hash chain per l'organizzazione corrente.
* Risponde con: valid, total, hashed, coverage_pct, broken_at, last_hash
*/
public function chainVerify(): void
{
$this->requireOrgRole(['org_admin', 'auditor']);
$orgId = $this->getCurrentOrgId();
$db = Database::getInstance();
$result = AuditService::verifyChain($db, $orgId);
// Se la catena è rotta, registra la violazione
if (!$result['valid'] && $result['broken_at'] !== null) {
$performer = $this->currentUser['email'] ?? 'system';
try {
$ins = $db->prepare(
"INSERT INTO audit_violations
(organization_id, detected_by, broken_at_id, chain_length, notes)
VALUES (:org, :by, :bid, :len, :notes)"
);
$ins->execute([
'org' => $orgId,
'by' => $performer,
'bid' => $result['broken_at'],
'len' => $result['total'],
'notes' => 'Violazione rilevata tramite API chain-verify',
]);
} catch (Throwable $e) {
error_log('[AuditController] chain violation log error: ' . $e->getMessage());
}
$this->logAudit('audit.chain_broken', 'audit_logs', $result['broken_at'], [
'total' => $result['total'],
'coverage_pct' => $result['coverage_pct'],
]);
}
$this->jsonSuccess($result);
}
/**
* GET /api/audit/export-certified
* Genera un export JSON certificato con hash SHA-256 dell'intero contenuto.
* Adatto per ispezioni ACN, audit NIS2 Art.32, certificazione ISO 27001.
*/
public function exportCertified(): void
{
$this->requireOrgRole(['org_admin', 'auditor']);
$orgId = $this->getCurrentOrgId();
$userId = $this->getCurrentUserId();
$email = $this->currentUser['email'] ?? 'system';
$db = Database::getInstance();
$result = AuditService::exportCertified(
$db,
$orgId,
$userId,
$email,
$_GET['purpose'] ?? 'export_certificato',
$_GET['from'] ?? null,
$_GET['to'] ?? null
);
$this->logAudit('audit.export_certified', 'audit_logs', null, [
'records_count' => $result['records_count'],
'chain_valid' => $result['chain_valid'],
'export_hash' => $result['export_hash'],
]);
header('Content-Disposition: attachment; filename="nis2_audit_certified_' . date('Y-m-d') . '.json"');
$this->jsonSuccess($result);
}
}