Files
squad-rain-ops-mini/Helpers/UserManagement.cs

229 lines
6.3 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
namespace RainOpsMini.Helpers
{
public class UserAccount
{
public string Id { get; set; } = Guid.NewGuid().ToString();
public string Username { get; set; } = string.Empty;
public string Password { get; set; } = string.Empty;
public bool IsEnabled { get; set; } = true;
public List<string> Permissions { get; set; } = new List<string>();
public string RoleId { get; set; } = string.Empty; // New field for Role
public DateTime CreatedAt { get; set; } = DateTime.Now;
}
public class Role
{
public string Id { get; set; } = Guid.NewGuid().ToString();
public string Name { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
public List<string> Permissions { get; set; } = new List<string>();
public DateTime CreatedAt { get; set; } = DateTime.Now;
}
public class RoleStorage
{
private static readonly string FilePath = "roles.json";
private static List<Role> _roles = new List<Role>();
private static readonly object _lock = new object();
static RoleStorage()
{
Load();
}
public static void Load()
{
lock (_lock)
{
if (File.Exists(FilePath))
{
try
{
var json = File.ReadAllText(FilePath);
_roles = JsonSerializer.Deserialize<List<Role>>(json) ?? new List<Role>();
}
catch
{
_roles = new List<Role>();
}
}
}
}
public static void Save()
{
lock (_lock)
{
var json = JsonSerializer.Serialize(_roles, new JsonSerializerOptions { WriteIndented = true });
File.WriteAllText(FilePath, json);
}
}
public static List<Role> GetAll()
{
lock (_lock)
{
return _roles.ToList();
}
}
public static Role? GetById(string id)
{
lock (_lock)
{
return _roles.FirstOrDefault(r => r.Id == id);
}
}
public static void Add(Role role)
{
lock (_lock)
{
_roles.Add(role);
Save();
}
}
public static void Update(Role role)
{
lock (_lock)
{
var existing = _roles.FirstOrDefault(r => r.Id == role.Id);
if (existing != null)
{
existing.Name = role.Name;
existing.Description = role.Description;
existing.Permissions = role.Permissions;
Save();
}
}
}
public static void Delete(string id)
{
lock (_lock)
{
var role = _roles.FirstOrDefault(r => r.Id == id);
if (role != null)
{
_roles.Remove(role);
Save();
}
}
}
}
public class UserStorage
{
private static readonly string FilePath = "users.json";
private static List<UserAccount> _users = new List<UserAccount>();
private static readonly object _lock = new object();
static UserStorage()
{
Load();
}
public static void Load()
{
lock (_lock)
{
if (File.Exists(FilePath))
{
try
{
var json = File.ReadAllText(FilePath);
_users = JsonSerializer.Deserialize<List<UserAccount>>(json) ?? new List<UserAccount>();
}
catch
{
_users = new List<UserAccount>();
}
}
}
}
public static void Save()
{
lock (_lock)
{
var json = JsonSerializer.Serialize(_users, new JsonSerializerOptions { WriteIndented = true });
File.WriteAllText(FilePath, json);
}
}
public static List<UserAccount> GetAll()
{
lock (_lock)
{
return _users.ToList();
}
}
public static UserAccount? GetByUsername(string username)
{
lock (_lock)
{
return _users.FirstOrDefault(u => u.Username.Equals(username, StringComparison.OrdinalIgnoreCase));
}
}
public static UserAccount? GetById(string id)
{
lock (_lock)
{
return _users.FirstOrDefault(u => u.Id == id);
}
}
public static void Add(UserAccount user)
{
lock (_lock)
{
_users.Add(user);
Save();
}
}
public static void Update(UserAccount user)
{
lock (_lock)
{
var existing = _users.FirstOrDefault(u => u.Id == user.Id);
if (existing != null)
{
existing.Username = user.Username;
existing.IsEnabled = user.IsEnabled;
existing.Permissions = user.Permissions;
existing.RoleId = user.RoleId;
// Password is updated separately usually, but here we can allow it if passed
if (!string.IsNullOrEmpty(user.Password))
{
existing.Password = user.Password;
}
Save();
}
}
}
public static void Delete(string id)
{
lock (_lock)
{
var user = _users.FirstOrDefault(u => u.Id == id);
if (user != null)
{
_users.Remove(user);
Save();
}
}
}
}
}