Simplify user validation messages and add user logged-in check function

This commit is contained in:
Raphire
2026-03-18 22:39:49 +01:00
parent 1d4cf4a801
commit 999e442658
3 changed files with 61 additions and 6 deletions

View File

@@ -1283,7 +1283,7 @@ function Show-MainWindow {
$appRemovalScopeCombo.SelectedIndex = 0 $appRemovalScopeCombo.SelectedIndex = 0
} }
1 { 1 {
$userSelectionDescription.Text = "Changes will be applied to a different user profile on this system. Note: changes may not apply correctly if the target user is currently logged in." $userSelectionDescription.Text = "Changes will be applied to a different user profile on this system."
$otherUserPanel.Visibility = 'Visible' $otherUserPanel.Visibility = 'Visible'
$usernameValidationMessage.Text = "" $usernameValidationMessage.Text = ""
# Hide "Current user only" option, show "Target user only" option # Hide "Current user only" option, show "Target user only" option
@@ -1353,13 +1353,13 @@ function Show-MainWindow {
$successBrush = $window.Resources['ValidationSuccessColor'] $successBrush = $window.Resources['ValidationSuccessColor']
if ($username.Length -eq 0) { if ($username.Length -eq 0) {
$usernameValidationMessage.Text = "[X] Please enter a username" $usernameValidationMessage.Text = "Please enter a username"
$usernameValidationMessage.Foreground = $errorBrush $usernameValidationMessage.Foreground = $errorBrush
return $false return $false
} }
if ($username -eq $env:USERNAME) { if ($username -eq $env:USERNAME) {
$usernameValidationMessage.Text = "[X] Cannot enter your own username, use 'Current User' option instead" $usernameValidationMessage.Text = "Cannot enter your own username, use 'Current User' option instead"
$usernameValidationMessage.Foreground = $errorBrush $usernameValidationMessage.Foreground = $errorBrush
return $false return $false
} }
@@ -1367,12 +1367,18 @@ function Show-MainWindow {
$userExists = CheckIfUserExists -Username $username $userExists = CheckIfUserExists -Username $username
if ($userExists) { if ($userExists) {
$usernameValidationMessage.Text = "[OK] User found: $username" if (TestIfUserIsLoggedIn -Username $username) {
$usernameValidationMessage.Text = "User '$username' is currently logged in. Please sign out that user first."
$usernameValidationMessage.Foreground = $errorBrush
return $false
}
$usernameValidationMessage.Text = "User found: $username"
$usernameValidationMessage.Foreground = $successBrush $usernameValidationMessage.Foreground = $successBrush
return $true return $true
} }
$usernameValidationMessage.Text = "[X] User not found, please enter a valid username" $usernameValidationMessage.Text = "User not found, please enter a valid username"
$usernameValidationMessage.Foreground = $errorBrush $usernameValidationMessage.Foreground = $errorBrush
return $false return $false
} }
@@ -1515,7 +1521,13 @@ function Show-MainWindow {
$deploymentApplyBtn = $window.FindName('DeploymentApplyBtn') $deploymentApplyBtn = $window.FindName('DeploymentApplyBtn')
$deploymentApplyBtn.Add_Click({ $deploymentApplyBtn.Add_Click({
if (-not (ValidateOtherUsername)) { if (-not (ValidateOtherUsername)) {
Show-MessageBox -Message "Please enter a valid username." -Title "Invalid Username" -Button 'OK' -Icon 'Warning' | Out-Null $validationMessage = if (-not [string]::IsNullOrWhiteSpace($usernameValidationMessage.Text)) {
$usernameValidationMessage.Text
}
else {
"Please enter a valid username."
}
Show-MessageBox -Message $validationMessage -Title "Invalid Username" -Button 'OK' -Icon 'Warning' | Out-Null
return return
} }

View File

@@ -0,0 +1,42 @@
function TestIfUserIsLoggedIn {
param(
[Parameter(Mandatory)]
[string]$Username
)
try {
$quserOutput = @(& quser 2>$null)
if ($LASTEXITCODE -ne 0 -or -not $quserOutput) {
return $false
}
foreach ($line in ($quserOutput | Select-Object -Skip 1)) {
if ([string]::IsNullOrWhiteSpace($line)) { continue }
# Remove current-session marker and split columns.
$normalizedLine = $line.TrimStart('>', ' ')
$parts = $normalizedLine -split '\s+'
if ($parts.Count -eq 0) { continue }
$sessionUser = $parts[0]
if ([string]::IsNullOrWhiteSpace($sessionUser)) { continue }
# Normalize possible DOMAIN\user or user@domain formats.
if ($sessionUser.Contains('\')) {
$sessionUser = ($sessionUser -split '\\')[-1]
}
if ($sessionUser.Contains('@')) {
$sessionUser = ($sessionUser -split '@')[0]
}
if ($sessionUser.Equals($Username, [System.StringComparison]::OrdinalIgnoreCase)) {
return $true
}
}
}
catch {
return $false
}
return $false
}

View File

@@ -278,6 +278,7 @@ if (-not $script:WingetInstalled -and -not $Silent) {
. "$PSScriptRoot/Scripts/Helpers/GetTargetUserForAppRemoval.ps1" . "$PSScriptRoot/Scripts/Helpers/GetTargetUserForAppRemoval.ps1"
. "$PSScriptRoot/Scripts/Helpers/GetUserDirectory.ps1" . "$PSScriptRoot/Scripts/Helpers/GetUserDirectory.ps1"
. "$PSScriptRoot/Scripts/Helpers/GetUserName.ps1" . "$PSScriptRoot/Scripts/Helpers/GetUserName.ps1"
. "$PSScriptRoot/Scripts/Helpers/TestIfUserIsLoggedIn.ps1"
# Threading functions # Threading functions
. "$PSScriptRoot/Scripts/Threading/DoEvents.ps1" . "$PSScriptRoot/Scripts/Threading/DoEvents.ps1"