added "UserPermissionTesting_CS_WinForms" project
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace UserPermissionTest_CS_WinForms
|
||||
{
|
||||
public static class SessionManager
|
||||
{
|
||||
private static readonly IUserRepository UserRepository = new JsonUserRepository();
|
||||
|
||||
public static User? CurrentUser { get; private set; }
|
||||
|
||||
public static List<User> Users { get; private set; } = new List<User>();
|
||||
|
||||
public static List<string> AvailablePermissions { get; private set; } = new List<string>();
|
||||
|
||||
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 permission)
|
||||
{
|
||||
if (!AvailablePermissions.Contains(permission))
|
||||
{
|
||||
AvailablePermissions.Add(permission);
|
||||
UserRepository.SavePermissions(AvailablePermissions);
|
||||
UsersUpdated?.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user