登录用户名改为现有账号下拉选择

This commit is contained in:
tfarcraw 2026-11-23 11:25:16 +08:00
parent 5ae9228c07
commit 8afc2edf1d
3 changed files with 84 additions and 7 deletions

View File

@ -95,6 +95,10 @@ public sealed class UserAccess : BindableBase
/// <summary>退出当前会话,通知命令与设置界面立即收回权限。</summary>
public void Logout() => SetSession(null);
/// <summary>为登录下拉框提供现有用户名;未登录时也可读取,不暴露角色或密码校验材料。</summary>
public IReadOnlyList<string> LoginUserNames() =>
_accounts.Select(a => a.Name).OrderBy(name => name, StringComparer.OrdinalIgnoreCase).ToArray();
/// <summary>人员列表只对管理员开放。</summary>
public IReadOnlyList<UserSummary> Users()
{

View File

@ -17,12 +17,12 @@ public sealed class AccountWindow : Window
var panel = new StackPanel { Margin = new Thickness(24), Spacing = 12 };
Content = panel;
panel.Children.Add(new TextBlock { Text = Title, FontSize = 20 });
var users = new ComboBox { HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Stretch };
var name = new TextBox { PlaceholderText = "用户名" };
var password = new TextBox { PlaceholderText = "密码(至少 8 个字符)", PasswordChar = '●' };
var users = new ComboBox { Name = "AccountUserSelector", HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Stretch };
var name = new TextBox { Name = "AccountNameInput", PlaceholderText = "用户名" };
var password = new TextBox { Name = "AccountPasswordInput", PlaceholderText = "密码(至少 8 个字符)", PasswordChar = '●' };
var roles = Enum.GetValues<UserRole>();
var role = new ComboBox { ItemsSource = roles.Select(UserAccess.RoleName).ToArray(), SelectedIndex = 2 };
var status = new TextBlock { TextWrapping = Avalonia.Media.TextWrapping.Wrap };
var status = new TextBlock { Name = "AccountStatus", TextWrapping = Avalonia.Media.TextWrapping.Wrap };
if (manage)
{
users.ItemsSource = access.Users();
@ -33,13 +33,24 @@ public sealed class AccountWindow : Window
name.Text = selected.Name; role.SelectedIndex = Array.IndexOf(roles, selected.Role); password.Text = "";
};
}
panel.Children.Add(name); panel.Children.Add(password);
// 普通登录只能选择已有账号;初始化和管理员新增人员时仍允许输入新用户名。
if (!manage && !access.NeedsSetup)
{
var names = access.LoginUserNames();
users.ItemsSource = names;
users.SelectedItem = access.User?.Name ?? names.FirstOrDefault();
// 切换账号后清空口令,避免误用上一账号已输入的密码。
users.SelectionChanged += (_, _) => { password.Text = ""; status.Text = ""; };
panel.Children.Add(users);
}
else panel.Children.Add(name);
panel.Children.Add(password);
if (manage)
{
panel.Children.Add(role);
panel.Children.Add(new TextBlock { Text = "新增账号请填写密码;修改已有账号时密码留空表示保留。", TextWrapping = Avalonia.Media.TextWrapping.Wrap });
}
var submit = new Button { Content = manage ? "新增 / 保存" : access.NeedsSetup ? "创建并登录" : "登录" };
var submit = new Button { Name = "SubmitAccountButton", Content = manage ? "新增 / 保存" : access.NeedsSetup ? "创建并登录" : "登录" };
var actions = new StackPanel { Orientation = Avalonia.Layout.Orientation.Horizontal, Spacing = 12 };
actions.Children.Add(submit);
if (!manage)
@ -58,7 +69,16 @@ public sealed class AccountWindow : Window
try
{
if (manage) { access.SaveUser(name.Text ?? "", password.Text ?? "", roles[role.SelectedIndex]); users.ItemsSource = access.Users(); password.Text = ""; status.Text = "已保存"; }
else { if (access.NeedsSetup) access.Setup(name.Text ?? "", password.Text ?? ""); else if (!access.Login(name.Text ?? "", password.Text ?? "")) { status.Text = "用户名或密码错误"; return; } Close(); }
else
{
if (access.NeedsSetup) access.Setup(name.Text ?? "", password.Text ?? "");
else
{
if (users.SelectedItem is not string selectedName) { status.Text = "请选择用户名"; return; }
if (!access.Login(selectedName, password.Text ?? "")) { status.Text = "用户名或密码错误"; return; }
}
Close();
}
}
catch (Exception error) { status.Text = error.Message; }
};

View File

@ -111,6 +111,8 @@ internal static class Smoke
var app=(App)Application.Current!;var window=lifetime.MainWindow!;
if (Environment.GetCommandLineArgs().Contains("--license-ui-only"))
{ await Licensing.Run(app, lifetime); _exit = 0; return; }
if (Environment.GetCommandLineArgs().Contains("--login-users-only"))
{ await CheckLoginUserSelection(window); _exit = 0; return; }
if (Environment.GetCommandLineArgs().Contains("--permissions-only"))
{ await CheckPermissions(app, window); await CheckOwnPasswords(window); _exit = 0; return; }
// 既有业务回归使用显式测试管理员;不改变生产环境默认未登录状态。
@ -1758,6 +1760,57 @@ internal static class Smoke
access.Login("admin", "AdminPassword123");
}
/// <summary>通过真实登录控件验证首次建号、账号选择、密码验证及切换时的口令清理。</summary>
private static async Task CheckLoginUserSelection(Window window)
{
T Control<T>(Window dialog, string name) where T : Avalonia.Controls.Control =>
dialog.GetVisualDescendants().OfType<T>().Single(c => c.Name == name);
void Submit(Window dialog) => Control<Button>(dialog, "SubmitAccountButton")
.RaiseEvent(new Avalonia.Interactivity.RoutedEventArgs(Button.ClickEvent));
var access = UserAccess.Current;
var setup = new SopHelper.VVMs.MainWindow.AccountWindow();
setup.Show(window); await Task.Delay(40);
Check(setup.Title == "创建首个管理员" && !setup.GetVisualDescendants().OfType<ComboBox>().Any(), "首次创建管理员仍使用用户名输入框");
Control<TextBox>(setup, "AccountNameInput").Text = "admin";
Control<TextBox>(setup, "AccountPasswordInput").Text = "AdminPassword123";
Submit(setup); await Task.Delay(20);
Check(!setup.IsVisible && access.IsAdministrator, "首次建号能够输入用户名并成功登录");
access.SaveUser("engineer", "EngineerPassword123", UserRole.Engineer);
access.SaveUser("operator", "OperatorPassword123", UserRole.Operator);
access.SaveUser("deleted", "DeletedPassword123", UserRole.Operator);
access.DeleteUser("deleted");
access.Logout();
Check(access.LoginUserNames().SequenceEqual(new[] { "admin", "engineer", "operator" }), "未登录可获取现有用户名,删除的账号不出现在列表");
var dialog = new SopHelper.VVMs.MainWindow.AccountWindow();
dialog.Show(window); await Task.Delay(40);
var users = Control<ComboBox>(dialog, "AccountUserSelector");
var password = Control<TextBox>(dialog, "AccountPasswordInput");
Check(users.ItemsSource!.Cast<string>().SequenceEqual(access.LoginUserNames())
&& !dialog.GetVisualDescendants().OfType<TextBox>().Any(t => t.Name == "AccountNameInput"), "普通登录使用已有用户名下拉框,移除手动用户名输入");
users.SelectedItem = "engineer"; password.Text = "wrong";
Submit(dialog);
Check(dialog.IsVisible && !access.IsAuthenticated && Control<TextBlock>(dialog, "AccountStatus").Text == "用户名或密码错误", "选择账号后仍必须验证密码");
users.SelectedItem = "operator";
Check(password.Text == "", "选择其他用户名时清空已输入的密码");
users.SelectedIndex = -1; Submit(dialog);
Check(Control<TextBlock>(dialog, "AccountStatus").Text == "请选择用户名" && !access.IsAuthenticated, "没有选择用户名时提示选择");
users.SelectedItem = "operator";
users.IsDropDownOpen = true; await Task.Delay(60);
using (var rendered = new RenderTargetBitmap(new PixelSize(440,340)))
{
rendered.Render(dialog);
var output = Path.GetFullPath("../scratch/screenshots"); Directory.CreateDirectory(output);
rendered.Save(Path.Combine(output, "login-user-selection.png"));
}
users.IsDropDownOpen = false;
password.Text = "OperatorPassword123"; Submit(dialog); await Task.Delay(20);
Check(!dialog.IsVisible && access.User == new UserSummary("operator", UserRole.Operator), "下拉选择账号并验证正确密码后登录相应角色");
var switchDialog = new SopHelper.VVMs.MainWindow.AccountWindow();
switchDialog.Show(window); await Task.Delay(40);
Check((string?)Control<ComboBox>(switchDialog, "AccountUserSelector").SelectedItem == "operator", "切换用户窗口默认选中当前账号");
switchDialog.Close();
}
private static void Check(bool condition,string message){if(!condition)throw new Exception(message);Console.WriteLine("PASS: "+message);}
}
internal sealed class FakeCamera : ICameraService