104 lines
3.7 KiB
C#
104 lines
3.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace UserPermissionTest_CS_WinForms
|
|
{
|
|
public static class SessionManager
|
|
{
|
|
private static readonly IUserRepository UserRepository = new PostgreSqlUserRepository("Host=127.0.0.1;Port=5432;Database=postgres;Username=postgres;Password=postgres");
|
|
|
|
public static User? CurrentUser { get; private set; }
|
|
|
|
public static List<User> Users { get; private set; } = new List<User>();
|
|
|
|
public static List<Permission> AvailablePermissions { get; private set; } = new List<Permission>();
|
|
|
|
public static event Action? SessionStateChanged;
|
|
public static event Action? UsersUpdated;
|
|
|
|
static SessionManager()
|
|
{
|
|
// Load state from repository
|
|
Users = UserRepository.LoadUsers();
|
|
AvailablePermissions = UserRepository.LoadPermissions();
|
|
}
|
|
|
|
public static bool Login(string username, string password)
|
|
{
|
|
var user = Users.FirstOrDefault(u =>
|
|
u.Username.Equals(username, StringComparison.OrdinalIgnoreCase));
|
|
|
|
if (user != null && PasswordHasher.VerifyPassword(password, user.Password))
|
|
{
|
|
CurrentUser = user;
|
|
SessionStateChanged?.Invoke();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static void Logout()
|
|
{
|
|
if (CurrentUser != null)
|
|
{
|
|
CurrentUser = null;
|
|
SessionStateChanged?.Invoke();
|
|
}
|
|
}
|
|
|
|
public static void AddUser(User user)
|
|
{
|
|
Users.Add(user);
|
|
UserRepository.SaveUsers(Users);
|
|
UsersUpdated?.Invoke();
|
|
}
|
|
|
|
public static void UpdateUser(string oldUsername, User updatedUser)
|
|
{
|
|
var index = Users.FindIndex(u => u.Username.Equals(oldUsername, StringComparison.OrdinalIgnoreCase));
|
|
if (index >= 0)
|
|
{
|
|
Users[index] = updatedUser;
|
|
// If updated user is currently logged in, update current user too
|
|
if (CurrentUser != null && CurrentUser.Username.Equals(oldUsername, StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
CurrentUser = updatedUser;
|
|
SessionStateChanged?.Invoke();
|
|
}
|
|
UserRepository.SaveUsers(Users);
|
|
UsersUpdated?.Invoke();
|
|
}
|
|
}
|
|
|
|
public static void DeleteUser(string username)
|
|
{
|
|
var user = Users.FirstOrDefault(u => u.Username.Equals(username, StringComparison.OrdinalIgnoreCase));
|
|
if (user != null)
|
|
{
|
|
Users.Remove(user);
|
|
// If deleted user is logged in, log out
|
|
if (CurrentUser != null && CurrentUser.Username.Equals(username, StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
CurrentUser = null;
|
|
SessionStateChanged?.Invoke();
|
|
}
|
|
UserRepository.SaveUsers(Users);
|
|
UsersUpdated?.Invoke();
|
|
}
|
|
}
|
|
|
|
public static void AddPermission(string permissionName)
|
|
{
|
|
if (!AvailablePermissions.Any(p => p.Name.Equals(permissionName, StringComparison.OrdinalIgnoreCase)))
|
|
{
|
|
int newId = AvailablePermissions.Count > 0 ? AvailablePermissions.Max(p => p.Id) + 1 : 1;
|
|
var newPerm = new Permission { Id = newId, Name = permissionName };
|
|
AvailablePermissions.Add(newPerm);
|
|
UserRepository.SavePermissions(AvailablePermissions);
|
|
UsersUpdated?.Invoke();
|
|
}
|
|
}
|
|
}
|
|
}
|