From e35005e489656896c014c92f9a25e12ca3447c7e Mon Sep 17 00:00:00 2001 From: Marco Wende Date: Mon, 19 Oct 2026 12:43:42 +0200 Subject: [PATCH] Policies: Vergleich zweier Settings-Catalog-Policies (Diff) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Neuer Button "Vergleichen" (aktiv bei genau 2 Settings-Catalog-Policies) + Endpoint POST /api/policies/compare. Diff nach settingDefinitionId über die vorhandene Extraktion (Get-SettingDefinitionId/Get-SettingCanonicalJson): nur in A / nur in B / gleich / unterschiedlich (Werte gegenübergestellt). Modal mit Zählern + Abschnitten, Diff als JSON exportierbar. Read-only. Co-Authored-By: Claude Opus 4.8 --- src/Api.ps1 | 1 + src/PolicyIO.ps1 | 60 +++++++++++++++++++++++++++++ www/app.js | 98 ++++++++++++++++++++++++++++++++++++++++++++++-- www/index.html | 24 ++++++++++++ www/styles.css | 22 +++++++++++ 5 files changed, 202 insertions(+), 3 deletions(-) diff --git a/src/Api.ps1 b/src/Api.ps1 index d9ee017..72a90fe 100644 --- a/src/Api.ps1 +++ b/src/Api.ps1 @@ -60,6 +60,7 @@ function Invoke-ApiHandler { "POST /api/policies/import" { return Import-PoliciesEndpoint -Body $Body } "POST /api/policies/assign" { return Invoke-PolicyAssignEndpoint -Body $Body } "POST /api/policies/consolidate" { return Invoke-PolicyConsolidateEndpoint -Body $Body } + "POST /api/policies/compare" { return Invoke-PolicyCompareEndpoint -Body $Body } "POST /api/policies/git-snapshot" { return Invoke-PolicyGitSnapshotEndpoint } "POST /api/pickfolder" { return Invoke-FolderPickerEndpoint -Body $Body } diff --git a/src/PolicyIO.ps1 b/src/PolicyIO.ps1 index 447fd80..efa2746 100644 --- a/src/PolicyIO.ps1 +++ b/src/PolicyIO.ps1 @@ -1118,6 +1118,66 @@ function Invoke-PolicyConsolidateEndpoint { } } +# ============================================================ +# Zwei Settings-Catalog-Policies vergleichen (Diff nach settingDefinitionId) +# ============================================================ +# Body: { ids:[idA, idB] }. Liefert: nur in A / nur in B / gleich / unterschiedlich. +# Nutzt dieselbe Extraktion wie die Konsolidierung (Get-SettingDefinitionId + +# Get-SettingCanonicalJson -> kanonischer Wert-Vergleich). +function Invoke-PolicyCompareEndpoint { + param($Body) + try { Assert-GraphConnected } catch { return @{ __status = 401; error = 'Nicht mit Microsoft Graph verbunden' } } + + $ids = @(Get-PolicyProp $Body 'ids') | Where-Object { $_ } + if (@($ids).Count -ne 2) { return @{ __status = 400; error = 'Bitte genau zwei Settings-Catalog-Policies auswaehlen.' } } + + $pols = @() + foreach ($id in $ids) { + $detail = $null + try { $detail = Get-GraphPolicyDetail -Type 'settingscatalog' -Id ([string]$id) } catch {} + if (-not $detail) { return @{ __status = 400; error = "Policy $id konnte nicht geladen werden (nur Settings Catalog wird unterstuetzt)." } } + $map = [ordered]@{} + foreach ($el in @(Get-PolicyProp $detail 'settings')) { + if (-not $el) { continue } + $defId = Get-SettingDefinitionId $el + if (-not $defId) { continue } + $map[$defId] = Get-SettingCanonicalJson $el + } + $pols += @{ + id = [string]$id + name = [string](Get-PolicyProp $detail 'name') + platform = [string](Get-PolicyProp $detail 'platforms') + map = $map + count = $map.Count + } + } + + $a = $pols[0]; $b = $pols[1] + $onlyA = @(); $onlyB = @(); $same = @(); $different = @() + foreach ($k in @($a.map.Keys)) { + if ($b.map.Contains($k)) { + if ($a.map[$k] -eq $b.map[$k]) { $same += @{ settingDefinitionId = $k; value = $a.map[$k] } } + else { $different += @{ settingDefinitionId = $k; valueA = $a.map[$k]; valueB = $b.map[$k] } } + } else { + $onlyA += @{ settingDefinitionId = $k; value = $a.map[$k] } + } + } + foreach ($k in @($b.map.Keys)) { + if (-not $a.map.Contains($k)) { $onlyB += @{ settingDefinitionId = $k; value = $b.map[$k] } } + } + + return @{ + ok = $true + a = @{ id = $a.id; name = $a.name; platform = $a.platform; count = $a.count } + b = @{ id = $b.id; name = $b.name; platform = $b.platform; count = $b.count } + onlyA = @($onlyA) + onlyB = @($onlyB) + same = @($same) + different = @($different) + counts = @{ onlyA = @($onlyA).Count; onlyB = @($onlyB).Count; same = @($same).Count; different = @($different).Count } + } +} + # ============================================================ # Policy-Snapshot nach Git (voller Export, stabile Dateinamen) # ============================================================ diff --git a/www/app.js b/www/app.js index e7418d5..fc68c8b 100644 --- a/www/app.js +++ b/www/app.js @@ -6426,14 +6426,24 @@ function polUpdateSelCount() { if (c) c.textContent = n; } // Konsolidieren nur bei >=2 Policies UND alle vom Typ Settings Catalog. + const allSc = keys.every(k => k.startsWith('settingscatalog::')); const btnC = document.getElementById('btnPolConsolidate'); if (btnC) { - const allSc = n >= 2 && keys.every(k => k.startsWith('settingscatalog::')); - btnC.disabled = !allSc; - btnC.title = allSc + const ok = n >= 2 && allSc; + btnC.disabled = !ok; + btnC.title = ok ? 'Ausgewählte Settings-Catalog-Policies zu einer neuen Policy zusammenführen' : 'Mindestens zwei Policies vom Typ „Settings Catalog" auswählen'; } + // Vergleichen: genau 2 Settings-Catalog-Policies. + const btnCmp = document.getElementById('btnPolCompare'); + if (btnCmp) { + const ok = n === 2 && allSc; + btnCmp.disabled = !ok; + btnCmp.title = ok + ? 'Die zwei ausgewählten Settings-Catalog-Policies vergleichen' + : 'Genau zwei Policies vom Typ „Settings Catalog" auswählen'; + } } function polSyncCheckAll() { @@ -6605,6 +6615,88 @@ async function polDoConsolidate() { document.getElementById('btnPolConsolidate')?.addEventListener('click', openPolConsolidate); document.getElementById('polConsConfirm')?.addEventListener('click', polDoConsolidate); +// ============================================================= +// Policies vergleichen (zwei Settings-Catalog-Policies -> Diff) +// ============================================================= +let _polCmpResult = null; + +async function openPolCompare() { + const keys = [...PolState.selected].filter(k => k.startsWith('settingscatalog::')); + if (keys.length !== 2) { toast('Genau zwei Settings-Catalog-Policies auswählen.', 'warn'); return; } + const ids = keys.map(k => k.slice('settingscatalog::'.length)); + _polCmpResult = null; + document.getElementById('polCmpSummary').innerHTML = ' Vergleiche…'; + document.getElementById('polCmpBody').innerHTML = ''; + document.getElementById('polCmpInfo').textContent = ''; + openModal('modalPolCompare'); + try { + const res = await api('/api/policies/compare', { method: 'POST', body: { ids }, timeoutMs: 120000 }); + _polCmpResult = res; + renderPolCompare(res); + } catch (e) { + document.getElementById('polCmpSummary').innerHTML = `Vergleich fehlgeschlagen: ${escapeHtml(e.message)}`; + } +} + +// settingDefinitionId lesbarer machen: letztes Segment hervorheben. +function polCmpShortId(id) { + const s = String(id || ''); + const tail = s.split(/[_~\.]/).filter(Boolean).pop() || s; + return `${escapeHtml(tail)} ${escapeHtml(s)}`; +} + +function polCmpVal(v) { + return `${escapeHtml(String(v ?? ''))}`; +} + +function renderPolCompare(res) { + const c = res.counts || {}; + document.getElementById('polCmpSummary').innerHTML = + `
` + + `
A: ${escapeHtml(res.a?.name || '—')} (${res.a?.count ?? 0} Settings · ${escapeHtml(res.a?.platform || '')})
` + + `
B: ${escapeHtml(res.b?.name || '—')} (${res.b?.count ?? 0} Settings · ${escapeHtml(res.b?.platform || '')})
` + + `
` + + `
` + + `${c.different || 0} unterschiedlich` + + `${c.onlyA || 0} nur in A` + + `${c.onlyB || 0} nur in B` + + `${c.same || 0} gleich` + + `
`; + + const sect = (title, cls, rows) => rows.length + ? `
${title} (${rows.length})
${rows.join('')}
` + : ''; + + const diffRows = (res.different || []).map(r => + `
${polCmpShortId(r.settingDefinitionId)}` + + `
A ${polCmpVal(r.valueA)}` + + `B ${polCmpVal(r.valueB)}
`); + const onlyARows = (res.onlyA || []).map(r => + `
${polCmpShortId(r.settingDefinitionId)} ${polCmpVal(r.value)}
`); + const onlyBRows = (res.onlyB || []).map(r => + `
${polCmpShortId(r.settingDefinitionId)} ${polCmpVal(r.value)}
`); + const sameRows = (res.same || []).map(r => + `
${polCmpShortId(r.settingDefinitionId)} ${polCmpVal(r.value)}
`); + + document.getElementById('polCmpBody').innerHTML = + sect('Unterschiedlich', 'diff', diffRows) + + sect('Nur in A', 'a', onlyARows) + + sect('Nur in B', 'b', onlyBRows) + + sect('Gleich', 'same', sameRows) + || '
Keine Einstellungen zum Vergleichen.
'; + + document.getElementById('polCmpInfo').textContent = + `${(res.a?.count ?? 0)} ↔ ${(res.b?.count ?? 0)} Settings`; +} + +document.getElementById('btnPolCompare')?.addEventListener('click', openPolCompare); +document.getElementById('polCmpExport')?.addEventListener('click', () => { + if (!_polCmpResult) return; + const a = (_polCmpResult.a?.name || 'A').replace(/[^a-zA-Z0-9_-]/g, '_'); + const b = (_polCmpResult.b?.name || 'B').replace(/[^a-zA-Z0-9_-]/g, '_'); + polDownloadJson(_polCmpResult, `policy-compare-${a}-vs-${b}.json`); +}); + // Datei-Inhalt -> Liste von Envelopes { exportType, policy, ... } function polExtractEnvelopes(parsed, fileName) { if (parsed && Array.isArray(parsed.policies)) return parsed.policies; diff --git a/www/index.html b/www/index.html index dfb5d5c..84b847e 100644 --- a/www/index.html +++ b/www/index.html @@ -623,6 +623,10 @@ Gruppen zuweisen (0) + + + + + + +