<?php
function loadUserPermissions(PDO $pdo, int $roleId): array {
    $stmt = $pdo->prepare("
        SELECT p.code
        FROM permissions p
        INNER JOIN role_permissions rp ON rp.permission_id = p.id
        WHERE rp.role_id = ?
    ");
    $stmt->execute([$roleId]);
    return array_column($stmt->fetchAll(), 'code');
}

function can(string $perm): bool {
    return in_array($perm, $_SESSION['permissions'] ?? []);
}
function refreshPermissions(PDO $pdo, int $roleId): void {
    $_SESSION['permissions'] = loadUserPermissions($pdo, $roleId);
}
function isAdmin(): bool {
    return ($_SESSION['role_name'] ?? '') === 'Admin';
}