修改登陆页面

This commit is contained in:
2026-03-22 14:01:47 +08:00
parent 126017b26c
commit 1907cdb7a6
4 changed files with 83 additions and 81 deletions

View File

@@ -1 +1 @@
2026-03-21 23:18:24 2026-03-22 13:55:03

View File

@@ -1 +1 @@
ca30113 126017b

View File

@@ -30,8 +30,8 @@ namespace CMLeonOS.Gui.ShellComponents
AppWindow window; AppWindow window;
TextBox usernameBox; Table userTable;
TextBox passwordBox; TextBox passwordBox;
WindowManager wm = ProcessManager.GetProcess<WindowManager>(); WindowManager wm = ProcessManager.GetProcess<WindowManager>();
@@ -46,7 +46,7 @@ namespace CMLeonOS.Gui.ShellComponents
} }
private const int width = 352; private const int width = 352;
private const int height = 128; private const int height = 200;
private const int padding = 12; private const int padding = 12;
private double shakiness = 0; private double shakiness = 0;
@@ -57,7 +57,7 @@ namespace CMLeonOS.Gui.ShellComponents
window.DrawImageAlpha(Images.Icon_Key, padding, padding); window.DrawImageAlpha(Images.Icon_Key, padding, padding);
window.DrawString("Enter your username and password,\nthen press Enter to log on", Color.Black, (int)(padding + Images.Icon_Key.Width + padding), padding); window.DrawString("Select a user and enter password,\nthen press Enter to log on", Color.Black, (int)(padding + Images.Icon_Key.Width + padding), padding);
} }
private void ShowError(string text) private void ShowError(string text)
@@ -73,17 +73,21 @@ namespace CMLeonOS.Gui.ShellComponents
private void LogOn() private void LogOn()
{ {
if (usernameBox.Text.Trim() == string.Empty || passwordBox.Text.Trim() == string.Empty) if (userTable.SelectedCellIndex < 0 || passwordBox.Text.Trim() == string.Empty)
{ {
return; return;
} }
string username = usernameBox.Text.Trim(); if (userTable.SelectedCellIndex >= UserSystem.GetUsers().Count)
string password = passwordBox.Text.Trim(); {
return;
Logger.Logger.Instance.Info("Lock", $"Attempting login for user: {username}"); }
var users = UserSystem.GetUsers(); var users = UserSystem.GetUsers();
User selectedUser = users[userTable.SelectedCellIndex];
string password = passwordBox.Text.Trim();
Logger.Logger.Instance.Info("Lock", $"Attempting login for user: {selectedUser.Username}");
Logger.Logger.Instance.Info("Lock", $"UserSystem.GetUsers() returned: {(users == null ? "null" : users.Count.ToString())}"); Logger.Logger.Instance.Info("Lock", $"UserSystem.GetUsers() returned: {(users == null ? "null" : users.Count.ToString())}");
if (users == null || users.Count == 0) if (users == null || users.Count == 0)
@@ -94,37 +98,17 @@ namespace CMLeonOS.Gui.ShellComponents
return; return;
} }
User foundUser = null; Logger.Logger.Instance.Info("Lock", $"User found: {selectedUser.Username}, Admin: {selectedUser.Admin}");
foreach (User user in users)
{
Logger.Logger.Instance.Info("Lock", $"Checking user: {user.Username} (lower: {user.Username.ToLower()})");
if (user.Username.ToLower() == username.ToLower())
{
foundUser = user;
Logger.Logger.Instance.Info("Lock", $"User matched: {foundUser.Username}");
break;
}
}
if (foundUser == null)
{
Logger.Logger.Instance.Warning("Lock", $"User not found: {username}");
Shake();
return;
}
Logger.Logger.Instance.Info("Lock", $"User found: {foundUser.Username}, Admin: {foundUser.Admin}");
string hashedInputPassword = UserSystem.HashPasswordSha256(password); string hashedInputPassword = UserSystem.HashPasswordSha256(password);
if (foundUser.Password != hashedInputPassword) if (selectedUser.Password != hashedInputPassword)
{ {
Logger.Logger.Instance.Warning("Lock", $"Authentication failed for user: {foundUser.Username}"); Logger.Logger.Instance.Warning("Lock", $"Authentication failed for user: {selectedUser.Username}");
passwordBox.Text = string.Empty; passwordBox.Text = string.Empty;
if (foundUser.LockedOut) if (selectedUser.LockedOut)
{ {
TimeSpan remaining = foundUser.LockoutEnd - DateTime.Now; TimeSpan remaining = selectedUser.LockoutEnd - DateTime.Now;
if (remaining.Minutes > 0) if (remaining.Minutes > 0)
{ {
ShowError($"Try again in {remaining.Minutes}m, {remaining.Seconds}s."); ShowError($"Try again in {remaining.Minutes}m, {remaining.Seconds}s.");
@@ -141,15 +125,14 @@ namespace CMLeonOS.Gui.ShellComponents
return; return;
} }
Logger.Logger.Instance.Info("Lock", $"Authentication successful for user: {foundUser.Username}"); Logger.Logger.Instance.Info("Lock", $"Authentication successful for user: {selectedUser.Username}");
TryStop(); TryStop();
UserSystem.SetCurrentLoggedInUser(foundUser); UserSystem.SetCurrentLoggedInUser(selectedUser);
ProcessManager.AddProcess(wm, new ShellComponents.Taskbar()).Start(); ProcessManager.AddProcess(wm, new ShellComponents.Taskbar()).Start();
ProcessManager.AddProcess(wm, new ShellComponents.Dock.Dock()).Start(); ProcessManager.AddProcess(wm, new ShellComponents.Dock.Dock()).Start();
soundService.PlaySystemSound(Sound.SystemSound.Login); soundService.PlaySystemSound(Sound.SystemSound.Login);
Logger.Logger.Instance.Info("Lock", $"{foundUser.Username} logged on to the GUI."); Logger.Logger.Instance.Info("Lock", $"{selectedUser.Username} logged on to GUI.");
} }
private void LogOnClick(int x, int y) private void LogOnClick(int x, int y)
@@ -182,13 +165,32 @@ namespace CMLeonOS.Gui.ShellComponents
RenderBackground(); RenderBackground();
int boxesStartY = (int)(padding + Images.Icon_Key.Height + padding); int boxesStartY = (int)(padding + Images.Icon_Key.Height + padding);
userTable = new Table(window, padding, boxesStartY, 160, 80);
userTable.CellHeight = 25;
userTable.Background = Color.White;
userTable.Foreground = Color.Black;
userTable.Border = Color.Gray;
userTable.SelectedBackground = Color.FromArgb(221, 246, 255);
userTable.SelectedForeground = Color.Black;
userTable.SelectedBorder = Color.FromArgb(126, 205, 234);
userTable.AllowSelection = true;
userTable.AllowDeselection = false;
foreach (var user in users)
{
var cell = new TableCell(user.Username);
userTable.Cells.Add(cell);
}
if (userTable.Cells.Count > 0)
{
userTable.SelectedCellIndex = 0;
}
wm.AddWindow(userTable);
usernameBox = new TextBox(window, padding, boxesStartY, 160, 20); passwordBox = new TextBox(window, padding, boxesStartY + 90, 160, 20);
usernameBox.PlaceholderText = "Username";
usernameBox.Submitted = LogOn;
wm.AddWindow(usernameBox);
passwordBox = new TextBox(window, padding, boxesStartY + padding + 20, 160, 20);
passwordBox.Shield = true; passwordBox.Shield = true;
passwordBox.PlaceholderText = "Password"; passwordBox.PlaceholderText = "Password";
passwordBox.Submitted = LogOn; passwordBox.Submitted = LogOn;

View File

@@ -17,6 +17,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
@@ -648,52 +649,52 @@ namespace CMLeonOS
CMLeonOS.UI.TUIHelper.SetColors(global::System.ConsoleColor.White, global::System.ConsoleColor.Black); CMLeonOS.UI.TUIHelper.SetColors(global::System.ConsoleColor.White, global::System.ConsoleColor.Black);
global::System.Console.Clear(); global::System.Console.Clear();
var loginBox = new CMLeonOS.UI.Window(new CMLeonOS.UI.Rect(15, 8, 50, 10), "Login", () => { }, true); var loginBox = new CMLeonOS.UI.Window(new CMLeonOS.UI.Rect(15, 8, 50, 15), "Login", () => { }, true);
loginBox.Render(); loginBox.Render();
CMLeonOS.UI.TUIHelper.SetColors(global::System.ConsoleColor.White, global::System.ConsoleColor.Black); CMLeonOS.UI.TUIHelper.SetColors(global::System.ConsoleColor.White, global::System.ConsoleColor.Black);
global::System.Console.SetCursorPosition(17, 10); global::System.Console.SetCursorPosition(17, 10);
global::System.Console.Write("Username: "); global::System.Console.Write("Select User:");
string username = global::System.Console.ReadLine();
if (string.IsNullOrWhiteSpace(username)) var userMenu = new CMLeonOS.UI.Menu(new CMLeonOS.UI.Rect(17, 11, 46, 8));
{
CMLeonOS.UI.TUIHelper.SetColors(global::System.ConsoleColor.Red, global::System.ConsoleColor.Black);
global::System.Console.SetCursorPosition(17, 24);
global::System.Console.Write("Username cannot be empty. ");
global::System.Threading.Thread.Sleep(1000);
return false;
}
CMLeonOS.UI.TUIHelper.SetColors(global::System.ConsoleColor.White, global::System.ConsoleColor.Black);
global::System.Console.SetCursorPosition(17, 11);
global::System.Console.Write("Password: ");
string password = ReadPassword();
User foundUser = null;
foreach (User user in users) foreach (User user in users)
{ {
if (user.Username.ToLower() == username.ToLower()) userMenu.Items.Add(new CMLeonOS.UI.MenuItem(user.Username));
}
userMenu.Render();
User selectedUser = null;
while (selectedUser == null)
{
var key = global::System.Console.ReadKey(true);
if (key.Key == global::System.ConsoleKey.Enter)
{ {
foundUser = user; if (userMenu.SelectedIndex >= 0 && userMenu.SelectedIndex < users.Count)
break; {
selectedUser = users[userMenu.SelectedIndex];
}
}
else if (key.Key == global::System.ConsoleKey.Escape)
{
return false;
}
else if (userMenu.HandleKey(key))
{
userMenu.Render();
} }
} }
if (foundUser == null) CMLeonOS.UI.TUIHelper.SetColors(global::System.ConsoleColor.White, global::System.ConsoleColor.Black);
{ global::System.Console.SetCursorPosition(17, 20);
CMLeonOS.UI.TUIHelper.SetColors(global::System.ConsoleColor.Red, global::System.ConsoleColor.Black); global::System.Console.Write("Password: ");
global::System.Console.SetCursorPosition(17, 24); string password = ReadPassword();
global::System.Console.Write("User not found. ");
global::System.Threading.Thread.Sleep(1000);
return false;
}
string hashedInputPassword = HashPasswordSha256(password); string hashedInputPassword = HashPasswordSha256(password);
if (foundUser.Password != hashedInputPassword) if (selectedUser.Password != hashedInputPassword)
{ {
string usernameLower = username.ToLower(); string usernameLower = selectedUser.Username.ToLower();
if (!loginAttempts.ContainsKey(usernameLower)) if (!loginAttempts.ContainsKey(usernameLower))
{ {
@@ -722,20 +723,19 @@ namespace CMLeonOS
return false; return false;
} }
loginAttempts.Remove(username.ToLower()); loginAttempts.Remove(selectedUser.Username.ToLower());
CMLeonOS.UI.TUIHelper.SetColors(global::System.ConsoleColor.Green, global::System.ConsoleColor.Black); CMLeonOS.UI.TUIHelper.SetColors(global::System.ConsoleColor.Green, global::System.ConsoleColor.Black);
global::System.Console.SetCursorPosition(17, 24); global::System.Console.SetCursorPosition(17, 24);
global::System.Console.Write("Login successful! "); global::System.Console.Write("Login successful! ");
// global::System.Console.WriteLine("Please wait... ");
global::System.Threading.Thread.Sleep(1500); global::System.Threading.Thread.Sleep(1500);
global::System.Console.ResetColor(); global::System.Console.ResetColor();
global::System.Console.Clear(); global::System.Console.Clear();
global::System.Console.Beep(); global::System.Console.Beep();
currentLoggedInUser = foundUser; currentLoggedInUser = selectedUser;
CreateUserFolder(foundUser.Username); CreateUserFolder(selectedUser.Username);
return true; return true;
} }