Problem: Restore nutzt bisher den Snapshot aus dem BackupSet (BackupItem). Wenn der Snapshot “unvollständig”/nicht der gewünschte Stand ist, landen nach Restore nur wenige Admin-Template-Settings in Intune. Lösung: Neue Action “Restore to Intune” direkt an einer konkreten PolicyVersion (inkl. Dry-Run Toggle) → reproduzierbarer Rollback auf exakt diese Version. Restore-UI zeigt jetzt PolicyVersion-Nummer (version: X) in der Item-Auswahl + BackupSet Items Tabelle hat eine Version-Spalte. Implementierung: RestoreService::executeFromPolicyVersion() erzeugt dafür einen kleinen, temporären BackupSet+BackupItem aus der Version und startet einen normalen RestoreRun. Pest-Test: PolicyVersionRestoreToIntuneTest.php Specs/TODO: Offene Follow-ups sind dokumentiert in tasks.md unter “Open TODOs (Follow-up)”. QA (GUI): Inventory → Policies → <Policy> → Versions → Restore to Intune (erst Dry-Run, dann Execute) Backups & Restore → Restore Runs → Create (bei Items steht version: X) Backups & Restore → Backup Sets → <Set> (Version-Spalte) Tests: PolicyVersionRestoreToIntuneTest.php Co-authored-by: Ahmed Darrazi <ahmeddarrazi@adsmac.local> Reviewed-on: #13
147 lines
4.6 KiB
PHP
147 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Intune;
|
|
|
|
class SettingsCatalogPolicyNormalizer implements PolicyTypeNormalizer
|
|
{
|
|
public function __construct(
|
|
private readonly DefaultPolicyNormalizer $defaultNormalizer,
|
|
) {}
|
|
|
|
public function supports(string $policyType): bool
|
|
{
|
|
return $policyType === 'settingsCatalogPolicy';
|
|
}
|
|
|
|
/**
|
|
* @return array{status: string, settings: array<int, array<string, mixed>>, settings_table?: array<string, mixed>, warnings: array<int, string>}
|
|
*/
|
|
public function normalize(?array $snapshot, string $policyType, ?string $platform = null): array
|
|
{
|
|
return $this->defaultNormalizer->normalize($snapshot, $policyType, $platform);
|
|
}
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function flattenForDiff(?array $snapshot, string $policyType, ?string $platform = null): array
|
|
{
|
|
$normalized = $this->normalize($snapshot ?? [], $policyType, $platform);
|
|
|
|
$map = [];
|
|
|
|
if (isset($normalized['settings_table']['rows']) && is_array($normalized['settings_table']['rows'])) {
|
|
$title = $normalized['settings_table']['title'] ?? 'Settings';
|
|
$prefix = is_string($title) && $title !== '' ? $title.' > ' : '';
|
|
$rows = $normalized['settings_table']['rows'];
|
|
|
|
$baseLabels = array_values(array_filter(array_map(function (mixed $row): ?string {
|
|
if (! is_array($row)) {
|
|
return null;
|
|
}
|
|
|
|
return $this->buildSettingsCatalogDiffLabel($row, includePath: false);
|
|
}, $rows)));
|
|
|
|
$labelCounts = array_count_values($baseLabels);
|
|
|
|
foreach ($rows as $row) {
|
|
if (! is_array($row)) {
|
|
continue;
|
|
}
|
|
|
|
$baseLabel = $this->buildSettingsCatalogDiffLabel($row, includePath: false);
|
|
$label = $baseLabel;
|
|
|
|
if (($labelCounts[$baseLabel] ?? 0) > 1) {
|
|
$path = $row['path'] ?? null;
|
|
$pathLabel = is_string($path) && $path !== '' ? $path : null;
|
|
|
|
$label = $this->buildSettingsCatalogDiffLabel($row, includePath: true);
|
|
|
|
if ($pathLabel !== null) {
|
|
$label .= ' @ '.$pathLabel;
|
|
}
|
|
}
|
|
|
|
$key = $prefix.$label;
|
|
$map[$key] = $row['value'] ?? null;
|
|
}
|
|
}
|
|
|
|
foreach ($normalized['settings'] ?? [] as $block) {
|
|
if (! is_array($block)) {
|
|
continue;
|
|
}
|
|
|
|
$title = $block['title'] ?? null;
|
|
$prefix = is_string($title) && $title !== '' ? $title.' > ' : '';
|
|
|
|
if (($block['type'] ?? null) === 'table') {
|
|
foreach ($block['rows'] ?? [] as $row) {
|
|
if (! is_array($row)) {
|
|
continue;
|
|
}
|
|
|
|
$key = $prefix.($row['path'] ?? $row['label'] ?? 'entry');
|
|
$map[$key] = $row['value'] ?? null;
|
|
}
|
|
|
|
continue;
|
|
}
|
|
|
|
foreach ($block['entries'] ?? [] as $entry) {
|
|
if (! is_array($entry)) {
|
|
continue;
|
|
}
|
|
|
|
$key = $prefix.($entry['key'] ?? 'entry');
|
|
$map[$key] = $entry['value'] ?? null;
|
|
}
|
|
}
|
|
|
|
return $map;
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $row
|
|
*/
|
|
private function buildSettingsCatalogDiffLabel(array $row, bool $includePath): string
|
|
{
|
|
$category = $row['category'] ?? null;
|
|
$definition = $row['definition'] ?? null;
|
|
$definitionId = $row['definition_id'] ?? null;
|
|
|
|
$label = is_string($definition) && $definition !== '' ? $definition : 'Setting';
|
|
|
|
if ($includePath) {
|
|
$path = $row['path'] ?? null;
|
|
|
|
if (is_string($path) && $path !== '') {
|
|
$label = $path;
|
|
}
|
|
|
|
if (
|
|
is_string($label)
|
|
&& is_string($definitionId)
|
|
&& $definitionId !== ''
|
|
&& is_string($definition)
|
|
&& $definition !== ''
|
|
) {
|
|
$parts = explode(' > ', $label);
|
|
|
|
if ($parts !== [] && end($parts) === $definitionId) {
|
|
$parts[count($parts) - 1] = $definition;
|
|
$label = implode(' > ', $parts);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (is_string($category) && $category !== '' && $category !== '-') {
|
|
$label = $category.' > '.$label;
|
|
}
|
|
|
|
return $label;
|
|
}
|
|
}
|