This commit is contained in:
2026-02-03 23:26:43 +08:00
parent 2270200b33
commit 1933eee16b
2 changed files with 120 additions and 5 deletions

View File

@@ -443,6 +443,9 @@ namespace CMLeonOS
case "user": case "user":
ProcessUserCommand(args); ProcessUserCommand(args);
break; break;
case "hostname":
ProcessHostnameCommand(args);
break;
case "cpass": case "cpass":
userSystem.ChangePassword(); userSystem.ChangePassword();
break; break;
@@ -1441,6 +1444,17 @@ namespace CMLeonOS
return $"{size:F2} {units[unitIndex]}"; return $"{size:F2} {units[unitIndex]}";
} }
private void ProcessHostnameCommand(string args)
{
if (string.IsNullOrEmpty(args))
{
ShowError("Usage: hostname <new_hostname>");
return;
}
userSystem.ProcessHostnameCommand(args);
}
private void ProcessUserCommand(string args) private void ProcessUserCommand(string args)
{ {
if (string.IsNullOrEmpty(args)) if (string.IsNullOrEmpty(args))

View File

@@ -13,6 +13,7 @@ namespace CMLeonOS
public string Username { get; set; } public string Username { get; set; }
public string Password { get; set; } public string Password { get; set; }
public bool IsAdmin { get; set; } public bool IsAdmin { get; set; }
public string Hostname { get; set; }
} }
public class UserSystem public class UserSystem
@@ -51,14 +52,13 @@ namespace CMLeonOS
{ {
EnsureSysDirectoryExists(); EnsureSysDirectoryExists();
// 设置用户文件路径
userFilePath = Path.Combine(sysDirectory, "user.dat"); userFilePath = Path.Combine(sysDirectory, "user.dat");
// 初始化当前登录用户
currentLoggedInUser = null; currentLoggedInUser = null;
// 加载用户数据
LoadUsers(); LoadUsers();
LoadHostname();
} }
private void EnsureSysDirectoryExists() private void EnsureSysDirectoryExists()
@@ -76,6 +76,87 @@ namespace CMLeonOS
} }
} }
private void LoadHostname()
{
string hostnameFilePath = Path.Combine(sysDirectory, "hostname.dat");
try
{
if (File.Exists(hostnameFilePath))
{
string hostname = File.ReadAllText(hostnameFilePath);
if (!string.IsNullOrWhiteSpace(hostname))
{
if (users.Count > 0)
{
users[0].Hostname = hostname;
}
}
}
}
catch
{
}
}
private void SaveHostname()
{
string hostnameFilePath = Path.Combine(sysDirectory, "hostname.dat");
try
{
if (users.Count > 0)
{
string hostname = users[0].Hostname;
File.WriteAllText(hostnameFilePath, hostname);
}
}
catch (Exception ex)
{
ShowError($"Error saving hostname: {ex.Message}");
}
}
public string GetHostname()
{
if (users.Count > 0)
{
return users[0].Hostname;
}
return "Not set";
}
public void SetHostname(string hostname)
{
if (string.IsNullOrWhiteSpace(hostname))
{
ShowError("Hostname cannot be empty");
return;
}
if (users.Count > 0)
{
users[0].Hostname = hostname;
SaveHostname();
ShowSuccess($"Hostname set to: {hostname}");
}
else
{
ShowError("No users found. Please create a user first.");
}
}
public void ProcessHostnameCommand(string args)
{
if (string.IsNullOrEmpty(args))
{
ShowError("Usage: hostname <new_hostname>");
return;
}
SetHostname(args);
}
private void LoadUsers() private void LoadUsers()
{ {
try try
@@ -99,7 +180,8 @@ namespace CMLeonOS
{ {
Username = parts[0].Trim(), Username = parts[0].Trim(),
Password = parts[1].Trim(), Password = parts[1].Trim(),
IsAdmin = parts.Length >= 3 && parts[2].Trim().ToLower() == "admin" IsAdmin = parts.Length >= 3 && parts[2].Trim().ToLower() == "admin",
Hostname = parts.Length >= 4 ? parts[3].Trim() : ""
}; };
users.Add(user); users.Add(user);
} }
@@ -128,7 +210,7 @@ namespace CMLeonOS
{ {
// 使用SHA256加密密码 // 使用SHA256加密密码
string hashedPassword = HashPasswordSha256(user.Password); string hashedPassword = HashPasswordSha256(user.Password);
string line = $"{user.Username}|{hashedPassword}|{(user.IsAdmin ? "admin" : "user")}"; string line = $"{user.Username}|{hashedPassword}|{(user.IsAdmin ? "admin" : "user")}|{user.Hostname}";
lines.Add(line); lines.Add(line);
} }
File.WriteAllLines(userFilePath, lines.ToArray()); File.WriteAllLines(userFilePath, lines.ToArray());
@@ -227,6 +309,25 @@ namespace CMLeonOS
SaveUsers(); SaveUsers();
ShowSuccess("Admin user created successfully!"); ShowSuccess("Admin user created successfully!");
Console.WriteLine();
Console.WriteLine("Please set system hostname:");
Console.Write("Hostname: ");
string hostname = Console.ReadLine();
while (string.IsNullOrWhiteSpace(hostname))
{
ShowError("Hostname cannot be empty.");
Console.Write("Hostname: ");
hostname = Console.ReadLine();
}
if (users.Count > 0)
{
users[0].Hostname = hostname;
SaveHostname();
ShowSuccess($"Hostname set to: {hostname}");
}
Console.WriteLine(); Console.WriteLine();
Console.WriteLine("System will restart in 3 seconds..."); Console.WriteLine("System will restart in 3 seconds...");
Console.WriteLine("Please wait..."); Console.WriteLine("Please wait...");