From 27155a057de2427fac8010d54ef1d4b27c89bd46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damian=20B=C3=BCchel?= Date: Thu, 11 Jan 2024 19:01:56 +0100 Subject: [PATCH] SEBWIN-821: Implemented configuration value for lock screen on user session change. --- .../ClientControllerTests.cs | 6 +++--- SafeExamBrowser.Client/ClientController.cs | 13 ++++++------ .../DataMapping/SecurityDataMapper.cs | 11 ++++++++++ .../ConfigurationData/DataValues.cs | 1 + .../ConfigurationData/Keys.cs | 1 + ...afeExamBrowser.Monitoring.Contracts.csproj | 2 +- ...ndler.cs => SessionChangedEventHandler.cs} | 2 +- .../System/ISystemMonitor.cs | 2 +- .../System/SystemMonitor.cs | 20 +++++++++---------- .../Security/SecuritySettings.cs | 6 ++++++ SebWindowsConfig/SEBSettings.cs | 2 ++ 11 files changed, 44 insertions(+), 22 deletions(-) rename SafeExamBrowser.Monitoring.Contracts/System/Events/{SessionSwitchedEventHandler.cs => SessionChangedEventHandler.cs} (89%) diff --git a/SafeExamBrowser.Client.UnitTests/ClientControllerTests.cs b/SafeExamBrowser.Client.UnitTests/ClientControllerTests.cs index ca4a46cc..abfde74c 100644 --- a/SafeExamBrowser.Client.UnitTests/ClientControllerTests.cs +++ b/SafeExamBrowser.Client.UnitTests/ClientControllerTests.cs @@ -1228,7 +1228,7 @@ namespace SafeExamBrowser.Client.UnitTests uiFactory.Setup(f => f.CreateLockScreen(It.IsAny(), It.IsAny(), It.IsAny>())).Returns(lockScreen.Object); sut.TryStart(); - systemMonitor.Raise(m => m.SessionSwitched += null); + systemMonitor.Raise(m => m.SessionChanged += null); lockScreen.Verify(l => l.Show(), Times.Once); } @@ -1248,7 +1248,7 @@ namespace SafeExamBrowser.Client.UnitTests .Returns(lockScreen.Object); sut.TryStart(); - systemMonitor.Raise(m => m.SessionSwitched += null); + systemMonitor.Raise(m => m.SessionChanged += null); lockScreen.Verify(l => l.Show(), Times.Once); runtimeProxy.Verify(p => p.RequestShutdown(), Times.Once); @@ -1266,7 +1266,7 @@ namespace SafeExamBrowser.Client.UnitTests uiFactory.Setup(f => f.CreateLockScreen(It.IsAny(), It.IsAny(), It.IsAny>())).Returns(lockScreen.Object); sut.TryStart(); - systemMonitor.Raise(m => m.SessionSwitched += null); + systemMonitor.Raise(m => m.SessionChanged += null); lockScreen.Verify(l => l.Show(), Times.Never); } diff --git a/SafeExamBrowser.Client/ClientController.cs b/SafeExamBrowser.Client/ClientController.cs index fda26380..209cd66b 100644 --- a/SafeExamBrowser.Client/ClientController.cs +++ b/SafeExamBrowser.Client/ClientController.cs @@ -213,7 +213,7 @@ namespace SafeExamBrowser.Client displayMonitor.DisplayChanged += DisplayMonitor_DisplaySettingsChanged; registry.ValueChanged += Registry_ValueChanged; runtime.ConnectionLost += Runtime_ConnectionLost; - systemMonitor.SessionSwitched += SystemMonitor_SessionSwitched; + systemMonitor.SessionChanged += SystemMonitor_SessionChanged; taskbar.LoseFocusRequested += Taskbar_LoseFocusRequested; taskbar.QuitButtonClicked += Shell_QuitButtonClicked; @@ -248,7 +248,7 @@ namespace SafeExamBrowser.Client displayMonitor.DisplayChanged -= DisplayMonitor_DisplaySettingsChanged; registry.ValueChanged -= Registry_ValueChanged; runtime.ConnectionLost -= Runtime_ConnectionLost; - systemMonitor.SessionSwitched -= SystemMonitor_SessionSwitched; + systemMonitor.SessionChanged -= SystemMonitor_SessionChanged; taskbar.QuitButtonClicked -= Shell_QuitButtonClicked; if (Browser != null) @@ -838,21 +838,22 @@ namespace SafeExamBrowser.Client ResumeActivators(); } - private void SystemMonitor_SessionSwitched() + private void SystemMonitor_SessionChanged() { var allow = !Settings.Service.IgnoreService && (!Settings.Service.DisableUserLock || !Settings.Service.DisableUserSwitch); + var disable = Settings.Security.DisableSessionChangeLockScreen; var message = text.Get(TextKey.LockScreen_UserSessionMessage); var title = text.Get(TextKey.LockScreen_Title); var continueOption = new LockScreenOption { Text = text.Get(TextKey.LockScreen_UserSessionContinueOption) }; var terminateOption = new LockScreenOption { Text = text.Get(TextKey.LockScreen_UserSessionTerminateOption) }; - if (allow) + if (allow || disable) { - logger.Info("Detected user session switch, but user lock and/or user switch are allowed."); + logger.Info($"Detected user session change, but {(allow ? "session locking and/or switching is allowed" : "lock screen is deactivated")}."); } else { - logger.Warn("Detected user session switch!"); + logger.Warn("Detected user session change!"); if (!sessionLocked) { diff --git a/SafeExamBrowser.Configuration/ConfigurationData/DataMapping/SecurityDataMapper.cs b/SafeExamBrowser.Configuration/ConfigurationData/DataMapping/SecurityDataMapper.cs index a2633419..1fe99eab 100644 --- a/SafeExamBrowser.Configuration/ConfigurationData/DataMapping/SecurityDataMapper.cs +++ b/SafeExamBrowser.Configuration/ConfigurationData/DataMapping/SecurityDataMapper.cs @@ -35,6 +35,9 @@ namespace SafeExamBrowser.Configuration.ConfigurationData.DataMapping case Keys.Security.ClipboardPolicy: MapClipboardPolicy(settings, value); break; + case Keys.Security.DisableSessionChangeLockScreen: + MapDisableSessionChangeLockScreen(settings, value); + break; case Keys.Security.QuitPasswordHash: MapQuitPasswordHash(settings, value); break; @@ -137,6 +140,14 @@ namespace SafeExamBrowser.Configuration.ConfigurationData.DataMapping } } + private void MapDisableSessionChangeLockScreen(AppSettings settings, object value) + { + if (value is bool disable) + { + settings.Security.DisableSessionChangeLockScreen = disable; + } + } + private void MapVirtualMachinePolicy(AppSettings settings, object value) { if (value is bool allow) diff --git a/SafeExamBrowser.Configuration/ConfigurationData/DataValues.cs b/SafeExamBrowser.Configuration/ConfigurationData/DataValues.cs index db003f02..40236966 100644 --- a/SafeExamBrowser.Configuration/ConfigurationData/DataValues.cs +++ b/SafeExamBrowser.Configuration/ConfigurationData/DataValues.cs @@ -272,6 +272,7 @@ namespace SafeExamBrowser.Configuration.ConfigurationData settings.Security.AllowTermination = true; settings.Security.AllowReconfiguration = false; settings.Security.ClipboardPolicy = ClipboardPolicy.Isolated; + settings.Security.DisableSessionChangeLockScreen = false; settings.Security.KioskMode = KioskMode.CreateNewDesktop; settings.Security.VirtualMachinePolicy = VirtualMachinePolicy.Deny; diff --git a/SafeExamBrowser.Configuration/ConfigurationData/Keys.cs b/SafeExamBrowser.Configuration/ConfigurationData/Keys.cs index 81e965f3..d06dc813 100644 --- a/SafeExamBrowser.Configuration/ConfigurationData/Keys.cs +++ b/SafeExamBrowser.Configuration/ConfigurationData/Keys.cs @@ -283,6 +283,7 @@ namespace SafeExamBrowser.Configuration.ConfigurationData internal const string AllowTermination = "allowQuit"; internal const string AllowVirtualMachine = "allowVirtualMachine"; internal const string ClipboardPolicy = "clipboardPolicy"; + internal const string DisableSessionChangeLockScreen = "disableSessionChangeLockScreen"; internal const string KioskModeCreateNewDesktop = "createNewDesktop"; internal const string KioskModeDisableExplorerShell = "killExplorerShell"; internal const string QuitPasswordHash = "hashedQuitPassword"; diff --git a/SafeExamBrowser.Monitoring.Contracts/SafeExamBrowser.Monitoring.Contracts.csproj b/SafeExamBrowser.Monitoring.Contracts/SafeExamBrowser.Monitoring.Contracts.csproj index cf850617..5582f699 100644 --- a/SafeExamBrowser.Monitoring.Contracts/SafeExamBrowser.Monitoring.Contracts.csproj +++ b/SafeExamBrowser.Monitoring.Contracts/SafeExamBrowser.Monitoring.Contracts.csproj @@ -68,7 +68,7 @@ - + diff --git a/SafeExamBrowser.Monitoring.Contracts/System/Events/SessionSwitchedEventHandler.cs b/SafeExamBrowser.Monitoring.Contracts/System/Events/SessionChangedEventHandler.cs similarity index 89% rename from SafeExamBrowser.Monitoring.Contracts/System/Events/SessionSwitchedEventHandler.cs rename to SafeExamBrowser.Monitoring.Contracts/System/Events/SessionChangedEventHandler.cs index 459cc032..72ad08ea 100644 --- a/SafeExamBrowser.Monitoring.Contracts/System/Events/SessionSwitchedEventHandler.cs +++ b/SafeExamBrowser.Monitoring.Contracts/System/Events/SessionChangedEventHandler.cs @@ -11,5 +11,5 @@ namespace SafeExamBrowser.Monitoring.Contracts.System.Events /// /// Indicates that the active user session of the operating system has changed. /// - public delegate void SessionSwitchedEventHandler(); + public delegate void SessionChangedEventHandler(); } diff --git a/SafeExamBrowser.Monitoring.Contracts/System/ISystemMonitor.cs b/SafeExamBrowser.Monitoring.Contracts/System/ISystemMonitor.cs index 87dc16a0..962f796a 100644 --- a/SafeExamBrowser.Monitoring.Contracts/System/ISystemMonitor.cs +++ b/SafeExamBrowser.Monitoring.Contracts/System/ISystemMonitor.cs @@ -18,7 +18,7 @@ namespace SafeExamBrowser.Monitoring.Contracts.System /// /// Event fired when the active user session has changed. /// - event SessionSwitchedEventHandler SessionSwitched; + event SessionChangedEventHandler SessionChanged; /// /// Starts the monitoring. diff --git a/SafeExamBrowser.Monitoring/System/SystemMonitor.cs b/SafeExamBrowser.Monitoring/System/SystemMonitor.cs index 67cfd834..b4f2aa62 100644 --- a/SafeExamBrowser.Monitoring/System/SystemMonitor.cs +++ b/SafeExamBrowser.Monitoring/System/SystemMonitor.cs @@ -19,7 +19,7 @@ namespace SafeExamBrowser.Monitoring.System { private readonly ILogger logger; - public event SessionSwitchedEventHandler SessionSwitched; + public event SessionChangedEventHandler SessionChanged; public SystemMonitor(ILogger logger) { @@ -33,7 +33,7 @@ namespace SafeExamBrowser.Monitoring.System SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged; SystemEvents.SessionEnded += SystemEvents_SessionEnded; SystemEvents.SessionEnding += SystemEvents_SessionEnding; - SystemEvents.SessionSwitch += SystemEvents_SessionSwitch; + SystemEvents.SessionSwitch += SystemEvents_SessionChanged; SystemEvents.TimeChanged += SystemEvents_TimeChanged; SystemEvents.UserPreferenceChanged += SystemEvents_UserPreferenceChanged; logger.Info("Started monitoring the operating system."); @@ -46,7 +46,7 @@ namespace SafeExamBrowser.Monitoring.System SystemEvents.PowerModeChanged -= SystemEvents_PowerModeChanged; SystemEvents.SessionEnded -= SystemEvents_SessionEnded; SystemEvents.SessionEnding -= SystemEvents_SessionEnding; - SystemEvents.SessionSwitch -= SystemEvents_SessionSwitch; + SystemEvents.SessionSwitch -= SystemEvents_SessionChanged; SystemEvents.TimeChanged -= SystemEvents_TimeChanged; SystemEvents.UserPreferenceChanged -= SystemEvents_UserPreferenceChanged; logger.Info("Stopped monitoring the operating system."); @@ -64,23 +64,23 @@ namespace SafeExamBrowser.Monitoring.System private void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e) { - logger.Info($"Power mode changed: {e.Mode}"); + logger.Info($"Power mode changed: {e.Mode}."); } private void SystemEvents_SessionEnded(object sender, SessionEndedEventArgs e) { - logger.Warn($"User session ended! Reason: {e.Reason}"); + logger.Warn($"User session ended! Reason: {e.Reason}."); } private void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e) { - logger.Warn($"User session is ending! Reason: {e.Reason}"); + logger.Warn($"User session is ending! Reason: {e.Reason}."); } - private void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e) + private void SystemEvents_SessionChanged(object sender, SessionSwitchEventArgs e) { - logger.Info($"User session switch detected! Reason: {e.Reason}"); - Task.Run(() => SessionSwitched?.Invoke()); + logger.Info($"User session change detected: {e.Reason}."); + Task.Run(() => SessionChanged?.Invoke()); } private void SystemEvents_TimeChanged(object sender, EventArgs e) @@ -90,7 +90,7 @@ namespace SafeExamBrowser.Monitoring.System private void SystemEvents_UserPreferenceChanged(object sender, UserPreferenceChangedEventArgs e) { - logger.Info($"User preference changed. Category: {e.Category}"); + logger.Info($"User preference changed. Category: {e.Category}."); } } } diff --git a/SafeExamBrowser.Settings/Security/SecuritySettings.cs b/SafeExamBrowser.Settings/Security/SecuritySettings.cs index 7ce2edbe..d6808800 100644 --- a/SafeExamBrowser.Settings/Security/SecuritySettings.cs +++ b/SafeExamBrowser.Settings/Security/SecuritySettings.cs @@ -42,6 +42,12 @@ namespace SafeExamBrowser.Settings.Security /// public ClipboardPolicy ClipboardPolicy { get; set; } + /// + /// Determines whether the lock screen is disabled in case of a user session change. This setting overrides the activation based on + /// and or . + /// + public bool DisableSessionChangeLockScreen { get; set; } + /// /// The kiosk mode which determines how the computer is locked down. /// diff --git a/SebWindowsConfig/SEBSettings.cs b/SebWindowsConfig/SEBSettings.cs index 3f6957f9..0f7381c9 100644 --- a/SebWindowsConfig/SEBSettings.cs +++ b/SebWindowsConfig/SEBSettings.cs @@ -379,6 +379,7 @@ namespace SebWindowsConfig public const String KeyAllowWindowsUpdate = "enableWindowsUpdate"; public const String KeyVersionRestrictions = "sebAllowedVersions"; public const String KeyClipboardPolicy = "clipboardPolicy"; + public const String KeyDisableSessionChangeLockScreen = "disableSessionChangeLockScreen"; // Group "macOS specific settings" public const String KeyMinMacOSVersion = "minMacOSVersion"; @@ -1013,6 +1014,7 @@ namespace SebWindowsConfig SEBSettings.settingsDefault.Add(SEBSettings.KeyAllowChromeNotifications, false); SEBSettings.settingsDefault.Add(SEBSettings.KeyAllowWindowsUpdate, false); SEBSettings.settingsDefault.Add(SEBSettings.KeyClipboardPolicy, 2); + SEBSettings.settingsDefault.Add(SEBSettings.KeyDisableSessionChangeLockScreen, false); // Default selected index and string in combo box for minMacOSVersion SEBSettings.intArrayDefault[SEBSettings.ValMinMacOSVersion] = 4;