mirror of
https://github.com/Leonmmcoset/CMLeonOS.git
synced 2026-03-03 11:37:01 +00:00
多用户系统
This commit is contained in:
75
Kernel.cs
75
Kernel.cs
@@ -1,4 +1,4 @@
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
@@ -19,7 +19,8 @@ namespace CMLeonOS
|
||||
|
||||
protected override void BeforeRun()
|
||||
{
|
||||
Console.Clear();
|
||||
// Console.Clear();
|
||||
Console.WriteLine(@"-------------------------------------------------");
|
||||
Console.WriteLine(@" ____ __ __ _ ___ ____ ");
|
||||
Console.WriteLine(@" / ___| \/ | | ___ ___ _ __ / _ \/ ___| ");
|
||||
Console.WriteLine(@" | | | |\/| | | / _ \/ _ \| '_ \| | | \___ \ ");
|
||||
@@ -44,54 +45,46 @@ namespace CMLeonOS
|
||||
var fs_type = fs.GetFileSystemType(@"0:\");
|
||||
Console.WriteLine("File System Type: " + fs_type);
|
||||
|
||||
// 删除默认示例文件和文件夹
|
||||
try
|
||||
// 检查并创建system文件夹
|
||||
string systemFolderPath = @"0:\system";
|
||||
if (!System.IO.Directory.Exists(systemFolderPath))
|
||||
{
|
||||
// 删除示例文件
|
||||
if (System.IO.File.Exists(@"0:\example.txt"))
|
||||
{
|
||||
System.IO.File.Delete(@"0:\example.txt");
|
||||
Console.WriteLine("Deleted example.txt");
|
||||
}
|
||||
|
||||
// 删除示例文件夹
|
||||
if (System.IO.Directory.Exists(@"0:\example"))
|
||||
{
|
||||
System.IO.Directory.Delete(@"0:\example", true);
|
||||
Console.WriteLine("Deleted example directory");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Error deleting example files: {ex.Message}");
|
||||
System.IO.Directory.CreateDirectory(systemFolderPath);
|
||||
Console.WriteLine("Created system folder.");
|
||||
}
|
||||
|
||||
// 初始化用户系统
|
||||
userSystem = new UserSystem();
|
||||
|
||||
// 第一次启动,设置管理员密码
|
||||
if (!userSystem.IsPasswordSet)
|
||||
// 循环直到登录成功或退出
|
||||
while (true)
|
||||
{
|
||||
userSystem.SetAdminPassword();
|
||||
}
|
||||
// 后续启动,需要登录
|
||||
else
|
||||
{
|
||||
// 循环直到登录成功
|
||||
while (!userSystem.Login())
|
||||
// 第一次启动,设置管理员账户
|
||||
if (!userSystem.HasUsers)
|
||||
{
|
||||
// 登录失败,继续尝试
|
||||
userSystem.FirstTimeSetup();
|
||||
}
|
||||
// 后续启动,需要登录
|
||||
else
|
||||
{
|
||||
// 循环直到登录成功
|
||||
while (!userSystem.Login())
|
||||
{
|
||||
// 登录失败,继续尝试
|
||||
}
|
||||
|
||||
// 登录成功后,初始化Shell
|
||||
shell = new Shell();
|
||||
|
||||
// 检查并执行启动脚本
|
||||
ExecuteStartupScript();
|
||||
|
||||
// 运行Shell(用户可以输入exit退出)
|
||||
shell.Run();
|
||||
|
||||
// 如果用户输入了exit,Shell.Run()会返回,继续循环
|
||||
}
|
||||
}
|
||||
|
||||
// 登录成功后,初始化Shell
|
||||
shell = new Shell();
|
||||
|
||||
// 检查并执行启动脚本
|
||||
ExecuteStartupScript();
|
||||
|
||||
// 系统启动完成,蜂鸣器响一声
|
||||
Console.Beep();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -101,7 +94,7 @@ namespace CMLeonOS
|
||||
|
||||
private void ExecuteStartupScript()
|
||||
{
|
||||
string startupFilePath = @"0:\sys\startup.cm";
|
||||
string startupFilePath = @"0:\system\startup.cm";
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
147
Shell.cs
147
Shell.cs
@@ -21,6 +21,7 @@ namespace CMLeonOS
|
||||
|
||||
public void Run()
|
||||
{
|
||||
bool shouldExit = false;
|
||||
while (true)
|
||||
{
|
||||
// 显示当前文件夹路径作为提示符(彩色)
|
||||
@@ -30,6 +31,15 @@ namespace CMLeonOS
|
||||
Console.Write($"{currentPath} | /");
|
||||
Console.ForegroundColor = originalColor;
|
||||
var input = Console.ReadLine();
|
||||
|
||||
// 检查是否为退出命令
|
||||
if (input != null && input.ToLower().Trim() == "exit")
|
||||
{
|
||||
Console.WriteLine("Exiting system...");
|
||||
shouldExit = true;
|
||||
break;
|
||||
}
|
||||
|
||||
commandHistory.Add(input);
|
||||
var parts = input.Split(' ', StringSplitOptions.RemoveEmptyEntries);
|
||||
if (parts.Length > 0)
|
||||
@@ -38,6 +48,12 @@ namespace CMLeonOS
|
||||
var args = parts.Length > 1 ? string.Join(" ", parts, 1, parts.Length - 1) : "";
|
||||
ProcessCommand(command, args);
|
||||
}
|
||||
|
||||
// 如果需要退出,返回到登录页面
|
||||
if (shouldExit)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,7 +110,6 @@ namespace CMLeonOS
|
||||
Console.WriteLine(" rmdir <dir> - Remove directory");
|
||||
Console.WriteLine(" cat <file> - Display file content");
|
||||
Console.WriteLine(" echo <text> > <file> - Write text to file");
|
||||
Console.WriteLine(" cpass - Change password");
|
||||
Console.WriteLine(" head <file> - Display first lines of file");
|
||||
Console.WriteLine(" Usage: head <file> <lines>");
|
||||
Console.WriteLine(" tail <file> - Display last lines of file");
|
||||
@@ -104,6 +119,14 @@ namespace CMLeonOS
|
||||
Console.WriteLine(" mv <src> <dst> - Move/rename file");
|
||||
Console.WriteLine(" touch <file> - Create empty file");
|
||||
Console.WriteLine(" find <name> - Find file");
|
||||
Console.WriteLine(" getdisk - Show disk information");
|
||||
Console.WriteLine(" user <cmd> - User management");
|
||||
Console.WriteLine(" user add admin <username> <password> - Add admin user");
|
||||
Console.WriteLine(" user add user <username> <password> - Add regular user");
|
||||
Console.WriteLine(" user delete <username> - Delete user");
|
||||
Console.WriteLine(" user list - List all users");
|
||||
Console.WriteLine(" logout - Logout current user");
|
||||
Console.WriteLine(" cpass - Change password");
|
||||
Console.WriteLine(" version - Show OS version");
|
||||
Console.WriteLine(" about - Show about information");
|
||||
Console.WriteLine(" help - Show this help message");
|
||||
@@ -164,7 +187,7 @@ namespace CMLeonOS
|
||||
else
|
||||
{
|
||||
// 检查是否在sys文件夹中(修复模式下绕过检测)
|
||||
bool isInSysFolder = (args.Contains(@"\sys\") || args.Contains(@"/sys/")) && !fixMode;
|
||||
bool isInSysFolder = (args.Contains(@"\system\") || args.Contains(@"/sys/")) && !fixMode;
|
||||
|
||||
if (isInSysFolder)
|
||||
{
|
||||
@@ -203,7 +226,7 @@ namespace CMLeonOS
|
||||
else
|
||||
{
|
||||
// 检查是否在sys文件夹中(修复模式下绕过检测)
|
||||
bool isInSysFolder = (args.Contains(@"\sys\") || args.Contains(@"/sys/")) && !fixMode;
|
||||
bool isInSysFolder = (args.Contains(@"\system\") || args.Contains(@"/sys/")) && !fixMode;
|
||||
|
||||
if (isInSysFolder)
|
||||
{
|
||||
@@ -233,9 +256,6 @@ namespace CMLeonOS
|
||||
Console.WriteLine("CMLeonOS Test Project");
|
||||
Console.WriteLine("By LeonOS 2 Developement Team");
|
||||
break;
|
||||
case "cpass":
|
||||
userSystem.ChangePassword();
|
||||
break;
|
||||
case "head":
|
||||
HeadFile(args);
|
||||
break;
|
||||
@@ -257,6 +277,18 @@ namespace CMLeonOS
|
||||
case "find":
|
||||
FindFile(args);
|
||||
break;
|
||||
case "getdisk":
|
||||
GetDiskInfo();
|
||||
break;
|
||||
case "user":
|
||||
ProcessUserCommand(args);
|
||||
break;
|
||||
case "logout":
|
||||
userSystem.Logout();
|
||||
break;
|
||||
case "cpass":
|
||||
userSystem.ChangePassword();
|
||||
break;
|
||||
default:
|
||||
Console.WriteLine($"Unknown command: {command}");
|
||||
break;
|
||||
@@ -805,5 +837,108 @@ namespace CMLeonOS
|
||||
Console.WriteLine($"Error finding file: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private void GetDiskInfo()
|
||||
{
|
||||
Console.WriteLine("====================================");
|
||||
Console.WriteLine(" Disk Information");
|
||||
Console.WriteLine("====================================");
|
||||
|
||||
try
|
||||
{
|
||||
// 使用VFSManager获取所有磁盘
|
||||
var disks = Sys.FileSystem.VFS.VFSManager.GetDisks();
|
||||
|
||||
if (disks == null || disks.Count == 0)
|
||||
{
|
||||
Console.WriteLine("No disks found.");
|
||||
return;
|
||||
}
|
||||
|
||||
Console.WriteLine($"Total Disks: {disks.Count}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Error getting disk info: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private string FormatBytes(long bytes)
|
||||
{
|
||||
string[] units = { "B", "KB", "MB", "GB", "TB" };
|
||||
int unitIndex = 0;
|
||||
double size = bytes;
|
||||
|
||||
while (size >= 1024 && unitIndex < units.Length - 1)
|
||||
{
|
||||
size /= 1024;
|
||||
unitIndex++;
|
||||
}
|
||||
|
||||
return $"{size:F2} {units[unitIndex]}";
|
||||
}
|
||||
|
||||
private void ProcessUserCommand(string args)
|
||||
{
|
||||
if (string.IsNullOrEmpty(args))
|
||||
{
|
||||
Console.WriteLine("Error: Please specify a user command");
|
||||
Console.WriteLine("Usage: user <add|delete> [args]");
|
||||
Console.WriteLine(" user add admin <username> <password> - Add admin user");
|
||||
Console.WriteLine(" user add user <username> <password> - Add regular user");
|
||||
Console.WriteLine(" user delete <username> - Delete user");
|
||||
Console.WriteLine(" user list - List all users");
|
||||
return;
|
||||
}
|
||||
|
||||
string[] parts = args.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
if (parts.Length < 1)
|
||||
{
|
||||
Console.WriteLine("Error: Please specify a user command");
|
||||
Console.WriteLine("Usage: user <add|delete> [args]");
|
||||
return;
|
||||
}
|
||||
|
||||
string subCommand = parts[0].ToLower();
|
||||
|
||||
if (subCommand == "add")
|
||||
{
|
||||
if (parts.Length < 4)
|
||||
{
|
||||
Console.WriteLine("Error: Please specify user type and username and password");
|
||||
Console.WriteLine("Usage: user add admin <username> <password>");
|
||||
Console.WriteLine("Usage: user add user <username> <password>");
|
||||
return;
|
||||
}
|
||||
|
||||
string userType = parts[1].ToLower();
|
||||
string username = parts[2];
|
||||
string password = parts[3];
|
||||
bool isAdmin = userType == "admin";
|
||||
|
||||
userSystem.AddUser($"{username} {password}", isAdmin);
|
||||
}
|
||||
else if (subCommand == "delete")
|
||||
{
|
||||
if (parts.Length < 2)
|
||||
{
|
||||
Console.WriteLine("Error: Please specify username");
|
||||
Console.WriteLine("Usage: user delete <username>");
|
||||
return;
|
||||
}
|
||||
|
||||
string username = parts[1];
|
||||
userSystem.DeleteUser(username);
|
||||
}
|
||||
else if (subCommand == "list")
|
||||
{
|
||||
userSystem.ListUsers();
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"Error: Unknown user command '{subCommand}'");
|
||||
Console.WriteLine("Available commands: add, delete, list");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
503
UserSystem.cs
503
UserSystem.cs
@@ -1,23 +1,31 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
namespace CMLeonOS
|
||||
{
|
||||
public class User
|
||||
{
|
||||
public string Username { get; set; }
|
||||
public string Password { get; set; }
|
||||
public bool IsAdmin { get; set; }
|
||||
}
|
||||
|
||||
public class UserSystem
|
||||
{
|
||||
private string sysDirectory = @"0:\sys";
|
||||
private string adminPasswordFilePath;
|
||||
private bool isPasswordSet = false;
|
||||
private string sysDirectory = @"0:\system";
|
||||
private string userFilePath;
|
||||
private List<User> users;
|
||||
|
||||
public UserSystem()
|
||||
{
|
||||
// 确保sys目录存在
|
||||
EnsureSysDirectoryExists();
|
||||
|
||||
// 设置密码文件路径
|
||||
adminPasswordFilePath = Path.Combine(sysDirectory, "admin_password.txt");
|
||||
// 设置用户文件路径
|
||||
userFilePath = Path.Combine(sysDirectory, "user.dat");
|
||||
|
||||
CheckPasswordStatus();
|
||||
// 加载用户数据
|
||||
LoadUsers();
|
||||
}
|
||||
|
||||
private void EnsureSysDirectoryExists()
|
||||
@@ -35,53 +43,172 @@ namespace CMLeonOS
|
||||
}
|
||||
}
|
||||
|
||||
private void CheckPasswordStatus()
|
||||
private void LoadUsers()
|
||||
{
|
||||
try
|
||||
{
|
||||
isPasswordSet = File.Exists(adminPasswordFilePath);
|
||||
if (File.Exists(userFilePath))
|
||||
{
|
||||
string[] lines = File.ReadAllLines(userFilePath);
|
||||
users = new List<User>();
|
||||
|
||||
foreach (string line in lines)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(line) || line.StartsWith("#"))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
string[] parts = line.Split('|');
|
||||
if (parts.Length >= 2)
|
||||
{
|
||||
User user = new User
|
||||
{
|
||||
Username = parts[0].Trim(),
|
||||
Password = parts[1].Trim(),
|
||||
IsAdmin = parts.Length >= 3 && parts[2].Trim().ToLower() == "admin"
|
||||
};
|
||||
users.Add(user);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
users = new List<User>();
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
isPasswordSet = false;
|
||||
users = new List<User>();
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsPasswordSet
|
||||
private void SaveUsers()
|
||||
{
|
||||
get { return isPasswordSet; }
|
||||
try
|
||||
{
|
||||
List<string> lines = new List<string>();
|
||||
foreach (User user in users)
|
||||
{
|
||||
string line = $"{user.Username}|{user.Password}|{(user.IsAdmin ? "admin" : "user")}";
|
||||
lines.Add(line);
|
||||
}
|
||||
File.WriteAllLines(userFilePath, lines.ToArray());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Error saving users: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public void SetAdminPassword()
|
||||
public bool HasUsers
|
||||
{
|
||||
get { return users.Count > 0; }
|
||||
}
|
||||
|
||||
public bool IsAdminSet
|
||||
{
|
||||
get
|
||||
{
|
||||
foreach (User user in users)
|
||||
{
|
||||
if (user.IsAdmin)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void FirstTimeSetup()
|
||||
{
|
||||
Console.WriteLine("====================================");
|
||||
Console.WriteLine(" First Time Setup");
|
||||
Console.WriteLine("====================================");
|
||||
Console.WriteLine("Please set a password for admin user:");
|
||||
Console.WriteLine("Please set admin username and password:");
|
||||
|
||||
Console.Write("Username: ");
|
||||
string username = Console.ReadLine();
|
||||
|
||||
while (string.IsNullOrWhiteSpace(username))
|
||||
{
|
||||
Console.WriteLine("Username cannot be empty.");
|
||||
Console.Write("Username: ");
|
||||
username = Console.ReadLine();
|
||||
}
|
||||
|
||||
Console.WriteLine("Password: ");
|
||||
string password = ReadPassword();
|
||||
|
||||
Console.WriteLine("Please confirm your password:");
|
||||
string confirmPassword = ReadPassword();
|
||||
|
||||
if (password == confirmPassword)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 简单存储密码(实际应用中应使用加密)
|
||||
File.WriteAllText(adminPasswordFilePath, password);
|
||||
Console.WriteLine("Password set successfully!");
|
||||
isPasswordSet = true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Error setting password: {ex.Message}");
|
||||
}
|
||||
}
|
||||
else
|
||||
while (password != confirmPassword)
|
||||
{
|
||||
Console.WriteLine("Passwords do not match. Please try again.");
|
||||
SetAdminPassword();
|
||||
|
||||
Console.Write("Username: ");
|
||||
username = Console.ReadLine();
|
||||
|
||||
while (string.IsNullOrWhiteSpace(username))
|
||||
{
|
||||
Console.WriteLine("Username cannot be empty.");
|
||||
Console.Write("Username: ");
|
||||
username = Console.ReadLine();
|
||||
}
|
||||
|
||||
Console.WriteLine("Password: ");
|
||||
password = ReadPassword();
|
||||
|
||||
Console.WriteLine("Please confirm your password:");
|
||||
confirmPassword = ReadPassword();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
User adminUser = new User
|
||||
{
|
||||
Username = username,
|
||||
Password = password,
|
||||
IsAdmin = true
|
||||
};
|
||||
users.Add(adminUser);
|
||||
SaveUsers();
|
||||
Console.WriteLine("Admin user created successfully!");
|
||||
|
||||
// 创建用户文件夹
|
||||
CreateUserFolder(username);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Error creating admin user: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private void CreateUserFolder(string username)
|
||||
{
|
||||
try
|
||||
{
|
||||
Console.WriteLine($"Creating user folder for {username}...");
|
||||
|
||||
// 在user文件夹下创建用户文件夹
|
||||
string userFolderPath = Path.Combine(@"0:\user", username);
|
||||
|
||||
// 检查用户文件夹是否存在
|
||||
if (!Directory.Exists(userFolderPath))
|
||||
{
|
||||
Directory.CreateDirectory(userFolderPath);
|
||||
Console.WriteLine($"Created user folder for {username}.");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"User folder for {username} already exists.");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Error creating user folder: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,8 +217,8 @@ namespace CMLeonOS
|
||||
Console.WriteLine("====================================");
|
||||
Console.WriteLine(" System Login");
|
||||
Console.WriteLine("====================================");
|
||||
Console.WriteLine("Username: admin");
|
||||
Console.WriteLine("Password:");
|
||||
Console.WriteLine("Press any key to continue...");
|
||||
Console.ReadKey(true);
|
||||
|
||||
// 检测ALT+Space按键
|
||||
bool useFixMode = false;
|
||||
@@ -142,50 +269,48 @@ namespace CMLeonOS
|
||||
}
|
||||
else
|
||||
{
|
||||
// 正常密码输入
|
||||
string password = "";
|
||||
password += keyInfo.KeyChar;
|
||||
Console.Write("*");
|
||||
// 正常登录流程
|
||||
Console.Write("Username: ");
|
||||
string username = Console.ReadLine();
|
||||
|
||||
while (true)
|
||||
if (string.IsNullOrWhiteSpace(username))
|
||||
{
|
||||
var passKey = Console.ReadKey(true);
|
||||
if (passKey.Key == ConsoleKey.Enter)
|
||||
Console.WriteLine("Username cannot be empty.");
|
||||
return false;
|
||||
}
|
||||
|
||||
Console.Write("Password: ");
|
||||
string password = ReadPassword();
|
||||
|
||||
// 查找用户
|
||||
User foundUser = null;
|
||||
foreach (User user in users)
|
||||
{
|
||||
if (user.Username.ToLower() == username.ToLower())
|
||||
{
|
||||
Console.WriteLine();
|
||||
foundUser = user;
|
||||
break;
|
||||
}
|
||||
else if (passKey.Key == ConsoleKey.Backspace)
|
||||
{
|
||||
if (password.Length > 0)
|
||||
{
|
||||
password = password.Substring(0, password.Length - 1);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
password += passKey.KeyChar;
|
||||
Console.Write("*");
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
if (foundUser == null)
|
||||
{
|
||||
string storedPassword = File.ReadAllText(adminPasswordFilePath);
|
||||
if (password == storedPassword)
|
||||
{
|
||||
Console.WriteLine("Login successful!");
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("Invalid password. Please try again.");
|
||||
return false;
|
||||
}
|
||||
Console.WriteLine("User not found.");
|
||||
return false;
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
if (foundUser.Password == password)
|
||||
{
|
||||
Console.WriteLine($"Error during login: {ex.Message}");
|
||||
Console.WriteLine("Login successful!");
|
||||
|
||||
// 创建用户文件夹
|
||||
CreateUserFolder(foundUser.Username);
|
||||
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("Invalid password. Please try again.");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -206,6 +331,202 @@ namespace CMLeonOS
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool AddUser(string args, bool isAdmin)
|
||||
{
|
||||
Console.WriteLine("====================================");
|
||||
Console.WriteLine($" Add {(isAdmin ? "Admin" : "User")}");
|
||||
Console.WriteLine("====================================");
|
||||
|
||||
string[] parts = args.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
if (parts.Length < 2)
|
||||
{
|
||||
Console.WriteLine("Error: Please specify username and password");
|
||||
Console.WriteLine($"Usage: user add {(isAdmin ? "admin" : "user")} <username> <password>");
|
||||
return false;
|
||||
}
|
||||
|
||||
string username = parts[0];
|
||||
string password = parts[1];
|
||||
|
||||
// 检查用户名是否已存在
|
||||
foreach (User user in users)
|
||||
{
|
||||
if (user.Username.ToLower() == username.ToLower())
|
||||
{
|
||||
Console.WriteLine($"Error: User '{username}' already exists.");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
User newUser = new User
|
||||
{
|
||||
Username = username,
|
||||
Password = password,
|
||||
IsAdmin = isAdmin
|
||||
};
|
||||
users.Add(newUser);
|
||||
SaveUsers();
|
||||
|
||||
// 创建用户文件夹
|
||||
CreateUserFolder(username);
|
||||
|
||||
Console.WriteLine($"{(isAdmin ? "Admin" : "User")} '{username}' created successfully!");
|
||||
Console.WriteLine("You shall restart the system to apply the changes.");
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Error adding user: {ex.Message}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public bool DeleteUser(string username)
|
||||
{
|
||||
Console.WriteLine("====================================");
|
||||
Console.WriteLine(" Delete User");
|
||||
Console.WriteLine("====================================");
|
||||
|
||||
if (string.IsNullOrWhiteSpace(username))
|
||||
{
|
||||
Console.WriteLine("Error: Please specify username");
|
||||
Console.WriteLine("Usage: user delete <username>");
|
||||
return false;
|
||||
}
|
||||
|
||||
// 查找用户
|
||||
User foundUser = null;
|
||||
foreach (User user in users)
|
||||
{
|
||||
if (user.Username.ToLower() == username.ToLower())
|
||||
{
|
||||
foundUser = user;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (foundUser == null)
|
||||
{
|
||||
Console.WriteLine($"Error: User '{username}' not found.");
|
||||
return false;
|
||||
}
|
||||
|
||||
// 检查是否是最后一个管理员
|
||||
int adminCount = 0;
|
||||
foreach (User user in users)
|
||||
{
|
||||
if (user.IsAdmin)
|
||||
{
|
||||
adminCount++;
|
||||
}
|
||||
}
|
||||
|
||||
if (foundUser.IsAdmin && adminCount <= 1)
|
||||
{
|
||||
Console.WriteLine("Error: Cannot delete the last admin user.");
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
users.Remove(foundUser);
|
||||
SaveUsers();
|
||||
Console.WriteLine($"User '{username}' deleted successfully!");
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Error deleting user: {ex.Message}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void ListUsers()
|
||||
{
|
||||
Console.WriteLine("====================================");
|
||||
Console.WriteLine(" User List");
|
||||
Console.WriteLine("====================================");
|
||||
|
||||
if (users.Count == 0)
|
||||
{
|
||||
Console.WriteLine("No users found.");
|
||||
return;
|
||||
}
|
||||
|
||||
Console.WriteLine();
|
||||
foreach (User user in users)
|
||||
{
|
||||
string userType = user.IsAdmin ? "[ADMIN]" : "[USER]";
|
||||
Console.WriteLine($"{userType} {user.Username}");
|
||||
}
|
||||
}
|
||||
|
||||
public bool ChangePassword()
|
||||
{
|
||||
Console.WriteLine("====================================");
|
||||
Console.WriteLine(" Change Password");
|
||||
Console.WriteLine("====================================");
|
||||
|
||||
Console.Write("Please enter your current password: ");
|
||||
string currentPassword = ReadPassword();
|
||||
|
||||
// 查找当前登录用户
|
||||
User currentUser = null;
|
||||
foreach (User user in users)
|
||||
{
|
||||
if (user.Username.ToLower() == "current")
|
||||
{
|
||||
currentUser = user;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (currentUser == null)
|
||||
{
|
||||
Console.WriteLine("Error: No user logged in.");
|
||||
return false;
|
||||
}
|
||||
|
||||
Console.Write("Please enter your new password: ");
|
||||
string newPassword = ReadPassword();
|
||||
|
||||
Console.WriteLine("Please confirm your new password: ");
|
||||
string confirmPassword = ReadPassword();
|
||||
|
||||
if (newPassword == confirmPassword)
|
||||
{
|
||||
try
|
||||
{
|
||||
currentUser.Password = newPassword;
|
||||
SaveUsers();
|
||||
Console.WriteLine("Password changed successfully!");
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Error changing password: {ex.Message}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("New passwords do not match.");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void Logout()
|
||||
{
|
||||
Console.WriteLine("====================================");
|
||||
Console.WriteLine(" User Logout");
|
||||
Console.WriteLine("====================================");
|
||||
Console.WriteLine("Logging out...");
|
||||
Console.WriteLine("Logout successful!");
|
||||
}
|
||||
|
||||
private string ReadPassword()
|
||||
{
|
||||
string password = "";
|
||||
@@ -222,8 +543,6 @@ namespace CMLeonOS
|
||||
if (password.Length > 0)
|
||||
{
|
||||
password = password.Substring(0, password.Length - 1);
|
||||
// 简化退格处理,只修改password字符串
|
||||
// 在Cosmos中,Console.Write("\b \b")可能不被支持
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -234,51 +553,5 @@ namespace CMLeonOS
|
||||
}
|
||||
return password;
|
||||
}
|
||||
|
||||
public bool ChangePassword()
|
||||
{
|
||||
Console.WriteLine("====================================");
|
||||
Console.WriteLine(" Change Password");
|
||||
Console.WriteLine("====================================");
|
||||
|
||||
// 验证当前密码
|
||||
Console.WriteLine("Please enter your current password:");
|
||||
string currentPassword = ReadPassword();
|
||||
|
||||
try
|
||||
{
|
||||
string storedPassword = File.ReadAllText(adminPasswordFilePath);
|
||||
if (currentPassword != storedPassword)
|
||||
{
|
||||
Console.WriteLine("Current password is incorrect.");
|
||||
return false;
|
||||
}
|
||||
|
||||
// 设置新密码
|
||||
Console.WriteLine("Please enter your new password:");
|
||||
string newPassword = ReadPassword();
|
||||
|
||||
Console.WriteLine("Please confirm your new password:");
|
||||
string confirmPassword = ReadPassword();
|
||||
|
||||
if (newPassword == confirmPassword)
|
||||
{
|
||||
// 存储新密码
|
||||
File.WriteAllText(adminPasswordFilePath, newPassword);
|
||||
Console.WriteLine("Password changed successfully!");
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("New passwords do not match.");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Error changing password: {ex.Message}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user