diff --git a/README.md b/README.md index 55ecda3..02e82b7 100644 --- a/README.md +++ b/README.md @@ -313,7 +313,8 @@ A value still set for either is logged as a warning and otherwise ignored. `DryRun` is honoured by refusing to run, exactly like `--dry-run`: BootstrapMate has no simulated install, so a policy-set `DryRun` exits `1` without installing anything. -`EnableDialog` set to false turns the dialog off, as `NoDialog` does. `DialogIcon` +`EnableDialog` set to false turns the dialog off, as `NoDialog` does; the ADMX has a +policy for each, and either one locks the Prefs tab's "Show progress dialog" switch. `DialogIcon` sets the dialog's icon. `SilentMode` and `VerboseMode` from policy or settings turn those modes on, and a CLI switch cannot turn them off. diff --git a/resources/BootstrapMate.admx b/resources/BootstrapMate.admx index 18c703c..6b12782 100644 --- a/resources/BootstrapMate.admx +++ b/resources/BootstrapMate.admx @@ -12,12 +12,13 @@ --> + @@ -127,6 +128,18 @@ + + + + + + + @@ -60,6 +60,13 @@ Default: Disabled Default: Enabled + Hide Progress Dialog + When enabled, no progress dialog is shown, as with the --no-dialog command-line switch. This overrides "Enable Progress Dialog": the dialog is shown only when that policy is not disabled and this one is not enabled. + +Either policy, when configured, locks the "Show progress dialog" switch in the BootstrapMate Prefs tab. + +Default: Disabled + Dialog Title Custom title text shown in the progress dialog window. diff --git a/src/BootstrapMate.App/ViewModels/PrefsViewModel.cs b/src/BootstrapMate.App/ViewModels/PrefsViewModel.cs index 28e5ba5..84771fc 100644 --- a/src/BootstrapMate.App/ViewModels/PrefsViewModel.cs +++ b/src/BootstrapMate.App/ViewModels/PrefsViewModel.cs @@ -137,7 +137,9 @@ public bool TryRelaunchElevated() // ── Editability (elevated and not managed by policy) ──────── - private bool CanEdit(string key) => PrefsElevation.CanEdit(IsElevated, _managedKeys.Contains(key)); + private bool CanEdit(string field) => PrefsElevation.CanEdit(IsElevated, IsLocked(field)); + + private bool IsLocked(string field) => PrefsPolicy.IsLocked(field, _managedKeys); public bool CanEditManifestUrl => CanEdit("ManifestUrl"); public bool CanEditAuthHeader => CanEdit("AuthorizationHeader"); @@ -152,14 +154,18 @@ public bool TryRelaunchElevated() public bool CanEditCustomInstallPath => CanEdit("CustomInstallPath"); public bool CanEditNetworkTimeout => CanEdit("NetworkTimeout"); - public bool IsManifestUrlLocked => _managedKeys.Contains("ManifestUrl"); - public bool IsAuthHeaderLocked => _managedKeys.Contains("AuthorizationHeader"); - public bool IsSilentModeLocked => _managedKeys.Contains("SilentMode"); - public bool IsVerboseModeLocked => _managedKeys.Contains("VerboseMode"); - public bool IsDryRunLocked => _managedKeys.Contains("DryRun"); - public bool IsEnableDialogLocked => _managedKeys.Contains("EnableDialog"); - public bool IsCustomInstallPathLocked => _managedKeys.Contains("CustomInstallPath"); - public bool IsNetworkTimeoutLocked => _managedKeys.Contains("NetworkTimeout"); + public bool IsManifestUrlLocked => IsLocked("ManifestUrl"); + public bool IsAuthHeaderLocked => IsLocked("AuthorizationHeader"); + public bool IsSilentModeLocked => IsLocked("SilentMode"); + public bool IsVerboseModeLocked => IsLocked("VerboseMode"); + public bool IsDryRunLocked => IsLocked("DryRun"); + public bool IsEnableDialogLocked => IsLocked("EnableDialog"); + public bool IsDialogTitleLocked => IsLocked("DialogTitle"); + public bool IsDialogMessageLocked => IsLocked("DialogMessage"); + public bool IsDialogIconLocked => IsLocked("DialogIcon"); + public bool IsBlurScreenLocked => IsLocked("BlurScreen"); + public bool IsCustomInstallPathLocked => IsLocked("CustomInstallPath"); + public bool IsNetworkTimeoutLocked => IsLocked("NetworkTimeout"); // ── Save Status Display (for x:Bind) ──────────────────────── @@ -204,7 +210,7 @@ public void Load() SilentMode = config.SilentMode; VerboseMode = config.VerboseMode; DryRun = config.DryRun; - EnableDialog = config.EnableDialog; + EnableDialog = PrefsPolicy.IsDialogShown(config); DialogTitle = config.DialogTitle; DialogMessage = config.DialogMessage; DialogIcon = config.DialogIcon ?? ""; diff --git a/src/BootstrapMate.App/Views/PrefsPage.xaml b/src/BootstrapMate.App/Views/PrefsPage.xaml index 074e3ab..91d213e 100644 --- a/src/BootstrapMate.App/Views/PrefsPage.xaml +++ b/src/BootstrapMate.App/Views/PrefsPage.xaml @@ -258,18 +258,42 @@ + + + + + + + + + + + + + + + + diff --git a/src/BootstrapMate.Core/PrefsPolicy.cs b/src/BootstrapMate.Core/PrefsPolicy.cs new file mode 100644 index 0000000..a500196 --- /dev/null +++ b/src/BootstrapMate.Core/PrefsPolicy.cs @@ -0,0 +1,40 @@ +namespace BootstrapMate.Core; + +/// +/// Which policy values lock each setting the GUI Prefs tab shows. Most fields are locked by a +/// policy value of the same name; the "Show progress dialog" switch is locked by either +/// EnableDialog or NoDialog, because both turn the dialog off. +/// +public static class PrefsPolicy +{ + /// Prefs tab field → the policy value names (canonical keys) that manage it. + public static IReadOnlyDictionary FieldKeys { get; } = + new Dictionary(StringComparer.OrdinalIgnoreCase) + { + ["ManifestUrl"] = ["ManifestUrl"], + ["AuthorizationHeader"] = ["AuthorizationHeader"], + ["SilentMode"] = ["SilentMode"], + ["VerboseMode"] = ["VerboseMode"], + ["DryRun"] = ["DryRun"], + ["EnableDialog"] = ["EnableDialog", "NoDialog"], + ["DialogTitle"] = ["DialogTitle"], + ["DialogMessage"] = ["DialogMessage"], + ["DialogIcon"] = ["DialogIcon"], + ["BlurScreen"] = ["BlurScreen"], + ["CustomInstallPath"] = ["CustomInstallPath"], + ["NetworkTimeout"] = ["NetworkTimeout"], + }; + + /// True when policy manages the Prefs tab field . + public static bool IsLocked(string field, IReadOnlySet managedKeys) + { + var keys = FieldKeys.TryGetValue(field, out var k) ? k : [field]; + return keys.Any(managedKeys.Contains); + } + + /// + /// Whether the progress dialog is shown: EnableDialog false or NoDialog true + /// turns it off, wherever each comes from. + /// + public static bool IsDialogShown(BootstrapMateConfig config) => config.EnableDialog && !config.NoDialog; +} diff --git a/tests/BootstrapMate.Core.Tests/PrefsPolicyTests.cs b/tests/BootstrapMate.Core.Tests/PrefsPolicyTests.cs new file mode 100644 index 0000000..b3b57e0 --- /dev/null +++ b/tests/BootstrapMate.Core.Tests/PrefsPolicyTests.cs @@ -0,0 +1,144 @@ +using System.Xml.Linq; +using BootstrapMate.Core; +using Xunit; + +namespace BootstrapMate.Core.Tests; + +public class PrefsPolicyTests +{ + private static readonly XNamespace Ns = "http://schemas.microsoft.com/GroupPolicy/2006/07/PolicyDefinitions"; + + private static HashSet Managed(params string[] keys) => + new(keys, StringComparer.OrdinalIgnoreCase); + + [Theory] + [MemberData(nameof(Fields))] + public void EveryFieldLocksWhenItsOwnPolicyValueIsSet(string field) + { + Assert.False(PrefsPolicy.IsLocked(field, Managed())); + Assert.True(PrefsPolicy.IsLocked(field, Managed(field))); + } + + [Fact] + public void NoDialogLocksTheShowDialogSwitch() => + Assert.True(PrefsPolicy.IsLocked("EnableDialog", Managed("NoDialog"))); + + [Fact] + public void ManagedKeysMatchCaseInsensitively() => + Assert.True(PrefsPolicy.IsLocked("EnableDialog", Managed("nodialog"))); + + [Fact] + public void AnotherFieldsPolicyDoesNotLockThisOne() => + Assert.False(PrefsPolicy.IsLocked("DialogTitle", Managed("DialogMessage", "NoDialog"))); + + [Theory] + [InlineData(true, false, true)] + [InlineData(false, false, false)] + [InlineData(true, true, false)] + [InlineData(false, true, false)] + public void TheDialogShowsOnlyWhenEnabledAndNotSuppressed(bool enable, bool noDialog, bool shown) => + Assert.Equal(shown, PrefsPolicy.IsDialogShown(new BootstrapMateConfig { EnableDialog = enable, NoDialog = noDialog })); + + public static TheoryData Fields() + { + var data = new TheoryData(); + foreach (var field in PrefsPolicy.FieldKeys.Keys) data.Add(field); + return data; + } + + // ── Policy template coverage ─────────────────────────────────── + + private static string ResourcesDir() + { + for (var dir = new DirectoryInfo(AppContext.BaseDirectory); dir is not null; dir = dir.Parent) + { + var candidate = Path.Combine(dir.FullName, "resources", "BootstrapMate.admx"); + if (File.Exists(candidate)) return Path.GetDirectoryName(candidate)!; + } + throw new FileNotFoundException("resources/BootstrapMate.admx not found above the test output directory"); + } + + private static XDocument Admx() => XDocument.Load(Path.Combine(ResourcesDir(), "BootstrapMate.admx")); + private static XDocument Adml() => XDocument.Load(Path.Combine(ResourcesDir(), "en-US", "BootstrapMate.adml")); + + /// Policy value name → how the ADMX writes it: "boolean", "text" or "decimal". + private static Dictionary AdmxValues() + { + var result = new Dictionary(StringComparer.OrdinalIgnoreCase); + foreach (var policy in Admx().Descendants(Ns + "policy")) + { + Assert.Equal(BootstrapMateConstants.PolicyRegistryPath, (string?)policy.Attribute("key")); + if (policy.Attribute("valueName") is { } v && policy.Element(Ns + "enabledValue") is not null) + result[v.Value] = "boolean"; + foreach (var element in policy.Element(Ns + "elements")?.Elements() ?? []) + result[(string)element.Attribute("valueName")!] = element.Name.LocalName; + } + return result; + } + + [Theory] + [MemberData(nameof(PolicyKeys))] + public void TheAdmxCoversEveryPolicyValueThatLocksAPrefsField(string key) + { + var values = AdmxValues(); + Assert.True(values.ContainsKey(key), $"ADMX has no policy for {key}"); + + // The type must be one ManagementDetector reads for that setting. + var expected = key switch + { + "SilentMode" or "VerboseMode" or "DryRun" or "EnableDialog" or "NoDialog" or "BlurScreen" => "boolean", + "NetworkTimeout" => "decimal", + _ => "text", + }; + Assert.Equal(expected, values[key]); + } + + public static TheoryData PolicyKeys() + { + var data = new TheoryData(); + foreach (var key in PrefsPolicy.FieldKeys.Values.SelectMany(k => k).Distinct()) data.Add(key); + return data; + } + + [Fact] + public void EveryAdmxReferenceResolvesInTheAdml() + { + var admx = Admx(); + var adml = Adml(); + var strings = adml.Descendants(Ns + "string").Select(s => (string)s.Attribute("id")!).ToHashSet(); + var presentations = adml.Descendants(Ns + "presentation") + .ToDictionary(p => (string)p.Attribute("id")!); + var categories = admx.Descendants(Ns + "category").Select(c => (string)c.Attribute("name")!).ToHashSet(); + var prefixes = admx.Descendants(Ns + "target").Concat(admx.Descendants(Ns + "using")) + .Select(e => (string)e.Attribute("prefix")!).ToHashSet(); + + static string Ref(string value, string kind) + { + Assert.StartsWith($"$({kind}.", value); + return value[(kind.Length + 3)..^1]; + } + + foreach (var c in admx.Descendants(Ns + "category")) + Assert.Contains(Ref((string)c.Attribute("displayName")!, "string"), strings); + + var names = new HashSet(); + foreach (var policy in admx.Descendants(Ns + "policy")) + { + var name = (string)policy.Attribute("name")!; + Assert.True(names.Add(name), $"duplicate policy {name}"); + Assert.Contains(Ref((string)policy.Attribute("displayName")!, "string"), strings); + Assert.Contains(Ref((string)policy.Attribute("explainText")!, "string"), strings); + Assert.Contains((string)policy.Element(Ns + "parentCategory")!.Attribute("ref")!, categories); + var supported = (string)policy.Element(Ns + "supportedOn")!.Attribute("ref")!; + Assert.Contains(supported.Split(':')[0], prefixes); + + var elements = policy.Element(Ns + "elements")?.Elements().ToList() ?? []; + if (elements.Count == 0) continue; + + var presentation = presentations[Ref((string)policy.Attribute("presentation")!, "presentation")]; + var refIds = presentation.Descendants().Select(e => (string?)e.Attribute("refId")).OfType().ToHashSet(); + foreach (var element in elements) + Assert.Contains((string)element.Attribute("id")!, refIds); + } + } +}