diff --git a/Documentation/config/README.md b/Documentation/config/README.md new file mode 100644 index 0000000000..10d4d75fc7 --- /dev/null +++ b/Documentation/config/README.md @@ -0,0 +1,41 @@ +# System-Wide Configuration + +This directory contains an example `config.json` file that can be placed in the application installation directory to override user settings for all users on a machine. + +## Usage + +1. Copy the example configuration file to the NETworkManager installation directory (where `NETworkManager.exe` is located) +2. Rename `config.json.example` to `config.json` +3. Edit the settings as needed +4. Restart NETworkManager + +## Configuration Options + +### Update_DisableUpdateCheck + +When set to `true`, disables the automatic update check at startup for all users. + +**Example:** +```json +{ + "Update_DisableUpdateCheck": true +} +``` + +This is useful for enterprise deployments where you want to: +- Control software updates centrally +- Prevent users from being prompted about updates +- Disable update checks on multiple machines without user intervention + +## File Location + +The `config.json` file should be placed in: +- **Installed version**: `C:\Program Files\NETworkManager\` (or your custom installation path) +- **Portable version**: Same directory as `NETworkManager.exe` + +## Notes + +- System-wide configuration takes precedence over user settings +- If the file doesn't exist or contains invalid JSON, it will be ignored and default user settings will apply +- Changes to `config.json` require restarting the application to take effect +- The file is optional - if not present, user settings will be used as normal diff --git a/Documentation/config/config.json.example b/Documentation/config/config.json.example new file mode 100644 index 0000000000..17f2c6929e --- /dev/null +++ b/Documentation/config/config.json.example @@ -0,0 +1,3 @@ +{ + "Update_DisableUpdateCheck": true +} diff --git a/Source/NETworkManager.Settings/PolicyInfo.cs b/Source/NETworkManager.Settings/PolicyInfo.cs new file mode 100644 index 0000000000..1e16581c8a --- /dev/null +++ b/Source/NETworkManager.Settings/PolicyInfo.cs @@ -0,0 +1,17 @@ +using System.Text.Json.Serialization; + +namespace NETworkManager.Settings; + +/// +/// Class that represents system-wide policies that override user settings. +/// This configuration is loaded from a config.json file in the application directory. +/// +public class PolicyInfo +{ + /// + /// Disable update check for all users. When set to true, the application will not check for updates at startup. + /// This overrides the user's "Update_CheckForUpdatesAtStartup" setting. + /// + [JsonPropertyName("Update_DisableUpdateCheck")] + public bool? Update_DisableUpdateCheck { get; set; } +} diff --git a/Source/NETworkManager.Settings/PolicyManager.cs b/Source/NETworkManager.Settings/PolicyManager.cs new file mode 100644 index 0000000000..966c0a3905 --- /dev/null +++ b/Source/NETworkManager.Settings/PolicyManager.cs @@ -0,0 +1,95 @@ +using log4net; +using System; +using System.IO; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace NETworkManager.Settings; + +/// +/// Manager for system-wide policies that are loaded from a config.json file +/// in the application directory. These policies override user settings. +/// +public static class PolicyManager +{ + #region Variables + + /// + /// Logger for logging. + /// + private static readonly ILog Log = LogManager.GetLogger(typeof(PolicyManager)); + + /// + /// Config file name. + /// + private static string ConfigFileName => "config.json"; + + /// + /// System-wide policies that are currently loaded. + /// + public static PolicyInfo Current { get; private set; } + + /// + /// JSON serializer options for consistent serialization/deserialization. + /// + private static readonly JsonSerializerOptions JsonOptions = new() + { + WriteIndented = true, + PropertyNameCaseInsensitive = true, + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, + Converters = { new JsonStringEnumConverter() } + }; + + #endregion + + #region Methods + + /// + /// Method to get the config file path in the application directory. + /// + /// Config file path. + private static string GetConfigFilePath() + { + return Path.Combine(AssemblyManager.Current.Location, ConfigFileName); + } + + /// + /// Method to load the system-wide policies from config.json file in the application directory. + /// + public static void Load() + { + var filePath = GetConfigFilePath(); + + // Check if config file exists + if (File.Exists(filePath)) + { + try + { + Log.Info($"Loading system-wide policies from: {filePath}"); + + var jsonString = File.ReadAllText(filePath); + Current = JsonSerializer.Deserialize(jsonString, JsonOptions); + + Log.Info("System-wide policies loaded successfully."); + + // Log enabled settings + if (Current.Update_DisableUpdateCheck.HasValue) + { + Log.Info($"System-wide policy - Update_DisableUpdateCheck: {Current.Update_DisableUpdateCheck.Value}"); + } + } + catch (Exception ex) + { + Log.Error($"Failed to load system-wide policies from: {filePath}", ex); + Current = new PolicyInfo(); + } + } + else + { + Log.Debug($"No system-wide policy file found at: {filePath}"); + Current = new PolicyInfo(); + } + } + + #endregion +} diff --git a/Source/NETworkManager.Settings/SettingsManager.cs b/Source/NETworkManager.Settings/SettingsManager.cs index 34ff00a850..58737d0659 100644 --- a/Source/NETworkManager.Settings/SettingsManager.cs +++ b/Source/NETworkManager.Settings/SettingsManager.cs @@ -56,6 +56,25 @@ public static class SettingsManager /// public static bool HotKeysChanged { get; set; } + /// + /// Gets whether update check should be performed at startup. + /// This respects the system-wide policies (config.json) which take precedence over user settings. + /// + public static bool ShouldCheckForUpdatesAtStartup + { + get + { + // System-wide policy takes precedence - if it explicitly disables updates, honor it + if (PolicyManager.Current?.Update_DisableUpdateCheck == true) + { + return false; + } + + // Otherwise, use the user's setting + return Current.Update_CheckForUpdatesAtStartup; + } + } + /// /// JSON serializer options for consistent serialization/deserialization. /// @@ -152,6 +171,9 @@ public static void Initialize() /// public static void Load() { + // Load system-wide policies first (from app directory) + PolicyManager.Load(); + var filePath = GetSettingsFilePath(); var legacyFilePath = GetLegacySettingsFilePath(); diff --git a/Source/NETworkManager/MainWindow.xaml.cs b/Source/NETworkManager/MainWindow.xaml.cs index 6546c727be..ede6f0007c 100644 --- a/Source/NETworkManager/MainWindow.xaml.cs +++ b/Source/NETworkManager/MainWindow.xaml.cs @@ -561,7 +561,7 @@ private void Load() NetworkChange.NetworkAddressChanged += (_, _) => OnNetworkHasChanged(); // Search for updates... - if (SettingsManager.Current.Update_CheckForUpdatesAtStartup) + if (SettingsManager.ShouldCheckForUpdatesAtStartup) CheckForUpdates(); }