Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
15 changes: 14 additions & 1 deletion resources/BootstrapMate.admx
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
-->
<policyDefinitions xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
revision="1.0"
revision="1.1"
schemaVersion="1.0"
xmlns="http://schemas.microsoft.com/GroupPolicy/2006/07/PolicyDefinitions">

<policyNamespaces>
<target prefix="BootstrapMate" namespace="BootstrapMate.Policies" />
<using prefix="windows" namespace="Microsoft.Policies.Windows" />
</policyNamespaces>

<resources minRequiredRevision="1.0" />
Expand Down Expand Up @@ -127,6 +128,18 @@
<disabledValue><decimal value="0" /></disabledValue>
</policy>

<policy name="NoDialog"
class="Machine"
displayName="$(string.NoDialog)"
explainText="$(string.NoDialog_Help)"
key="SOFTWARE\Policies\BootstrapMate"
valueName="NoDialog">
<parentCategory ref="Dialog" />
<supportedOn ref="windows:SUPPORTED_Windows_10_0" />
<enabledValue><decimal value="1" /></enabledValue>
<disabledValue><decimal value="0" /></disabledValue>
</policy>

<policy name="DialogTitle"
class="Machine"
displayName="$(string.DialogTitle)"
Expand Down
9 changes: 8 additions & 1 deletion resources/en-US/BootstrapMate.adml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
-->
<policyDefinitionResources xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
revision="1.0"
revision="1.1"
schemaVersion="1.0"
xmlns="http://schemas.microsoft.com/GroupPolicy/2006/07/PolicyDefinitions">

Expand Down Expand Up @@ -60,6 +60,13 @@ Default: Disabled</string>

Default: Enabled</string>

<string id="NoDialog">Hide Progress Dialog</string>
<string id="NoDialog_Help">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</string>

<string id="DialogTitle">Dialog Title</string>
<string id="DialogTitle_Help">Custom title text shown in the progress dialog window.

Expand Down
26 changes: 16 additions & 10 deletions src/BootstrapMate.App/ViewModels/PrefsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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) ────────────────────────

Expand Down Expand Up @@ -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 ?? "";
Expand Down
24 changes: 24 additions & 0 deletions src/BootstrapMate.App/Views/PrefsPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -258,18 +258,42 @@
<TextBlock Text="Title" Style="{StaticResource CaptionTextBlockStyle}" Margin="0,4,0,0" />
<TextBox Text="{x:Bind ViewModel.DialogTitle, Mode=TwoWay}" PlaceholderText="Setting Up Your Device"
IsEnabled="{x:Bind ViewModel.CanEditDialogTitle, Mode=OneWay}" />
<StackPanel Orientation="Horizontal" Spacing="4"
Visibility="{x:Bind ViewModel.IsDialogTitleLocked, Mode=OneWay, Converter={StaticResource BoolToVisibility}}">
<FontIcon Glyph="&#xE72E;" FontSize="12" Foreground="{ThemeResource TextFillColorSecondaryBrush}" />
<TextBlock Text="Managed by Policy" Style="{StaticResource CaptionTextBlockStyle}"
Foreground="{ThemeResource TextFillColorSecondaryBrush}" />
</StackPanel>

<TextBlock Text="Message" Style="{StaticResource CaptionTextBlockStyle}" Margin="0,4,0,0" />
<TextBox Text="{x:Bind ViewModel.DialogMessage, Mode=TwoWay}" PlaceholderText="Please wait while we install required software..."
IsEnabled="{x:Bind ViewModel.CanEditDialogMessage, Mode=OneWay}" />
<StackPanel Orientation="Horizontal" Spacing="4"
Visibility="{x:Bind ViewModel.IsDialogMessageLocked, Mode=OneWay, Converter={StaticResource BoolToVisibility}}">
<FontIcon Glyph="&#xE72E;" FontSize="12" Foreground="{ThemeResource TextFillColorSecondaryBrush}" />
<TextBlock Text="Managed by Policy" Style="{StaticResource CaptionTextBlockStyle}"
Foreground="{ThemeResource TextFillColorSecondaryBrush}" />
</StackPanel>

<TextBlock Text="Icon" Style="{StaticResource CaptionTextBlockStyle}" Margin="0,4,0,0" />
<TextBox Text="{x:Bind ViewModel.DialogIcon, Mode=TwoWay}" PlaceholderText="Path to icon file (.ico, .png)"
IsEnabled="{x:Bind ViewModel.CanEditDialogIcon, Mode=OneWay}" />
<StackPanel Orientation="Horizontal" Spacing="4"
Visibility="{x:Bind ViewModel.IsDialogIconLocked, Mode=OneWay, Converter={StaticResource BoolToVisibility}}">
<FontIcon Glyph="&#xE72E;" FontSize="12" Foreground="{ThemeResource TextFillColorSecondaryBrush}" />
<TextBlock Text="Managed by Policy" Style="{StaticResource CaptionTextBlockStyle}"
Foreground="{ThemeResource TextFillColorSecondaryBrush}" />
</StackPanel>

<ToggleSwitch Margin="0,4,0,0" OnContent="Blur screen behind dialog" OffContent="Blur screen behind dialog"
IsOn="{x:Bind ViewModel.BlurScreen, Mode=TwoWay}"
IsEnabled="{x:Bind ViewModel.CanEditBlurScreen, Mode=OneWay}" />
<StackPanel Orientation="Horizontal" Spacing="4"
Visibility="{x:Bind ViewModel.IsBlurScreenLocked, Mode=OneWay, Converter={StaticResource BoolToVisibility}}">
<FontIcon Glyph="&#xE72E;" FontSize="12" Foreground="{ThemeResource TextFillColorSecondaryBrush}" />
<TextBlock Text="Managed by Policy" Style="{StaticResource CaptionTextBlockStyle}"
Foreground="{ThemeResource TextFillColorSecondaryBrush}" />
</StackPanel>
</StackPanel>
</Border>

Expand Down
40 changes: 40 additions & 0 deletions src/BootstrapMate.Core/PrefsPolicy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
namespace BootstrapMate.Core;

/// <summary>
/// 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
/// <c>EnableDialog</c> or <c>NoDialog</c>, because both turn the dialog off.
/// </summary>
public static class PrefsPolicy
{
/// <summary>Prefs tab field → the policy value names (canonical keys) that manage it.</summary>
public static IReadOnlyDictionary<string, string[]> FieldKeys { get; } =
new Dictionary<string, string[]>(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"],
};

/// <summary>True when policy manages the Prefs tab field <paramref name="field"/>.</summary>
public static bool IsLocked(string field, IReadOnlySet<string> managedKeys)
{
var keys = FieldKeys.TryGetValue(field, out var k) ? k : [field];
return keys.Any(managedKeys.Contains);
}

/// <summary>
/// Whether the progress dialog is shown: <c>EnableDialog</c> false or <c>NoDialog</c> true
/// turns it off, wherever each comes from.
/// </summary>
public static bool IsDialogShown(BootstrapMateConfig config) => config.EnableDialog && !config.NoDialog;
}
144 changes: 144 additions & 0 deletions tests/BootstrapMate.Core.Tests/PrefsPolicyTests.cs
Original file line number Diff line number Diff line change
@@ -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<string> 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<string> Fields()
{
var data = new TheoryData<string>();
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"));

/// <summary>Policy value name → how the ADMX writes it: "boolean", "text" or "decimal".</summary>
private static Dictionary<string, string> AdmxValues()
{
var result = new Dictionary<string, string>(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<string> PolicyKeys()
{
var data = new TheoryData<string>();
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<string>();
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<string>().ToHashSet();
foreach (var element in elements)
Assert.Contains((string)element.Attribute("id")!, refIds);
}
}
}
Loading