61 lines
1.7 KiB
C#
61 lines
1.7 KiB
C#
using OwnChar.Data;
|
|
using OwnChar.Manager.Exceptions;
|
|
using OwnChar.Model;
|
|
using Pilz.Cryptography;
|
|
|
|
namespace OwnChar.Manager;
|
|
|
|
public class OwnCharManager
|
|
{
|
|
// User
|
|
public bool IsLoggedIn => CurrentUser != null;
|
|
public UserAccount? CurrentUser { get; private set; }
|
|
|
|
// Data Provider
|
|
public IDataManager? DataManager { get; set; }
|
|
|
|
// Manager
|
|
public UserManager Users { get; }
|
|
public GroupsManager Groups { get; }
|
|
public CharacterManager Characters { get; }
|
|
|
|
public OwnCharManager()
|
|
{
|
|
Users = new(this);
|
|
Groups = new(this);
|
|
Characters = new(this);
|
|
}
|
|
|
|
internal protected void CheckLogin()
|
|
{
|
|
if (!IsLoggedIn)
|
|
throw new LoginException("You are already logged in!");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Tries to login on the given data provider.
|
|
/// </summary>
|
|
/// <returns>Returns <see cref="true"/> if the login was successfull and <see cref="false"/> if not.</returns>
|
|
public bool Login(IDataManager? proxy, string? username, SecureString? password)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(proxy, nameof(proxy));
|
|
ArgumentException.ThrowIfNullOrWhiteSpace(username, nameof(username));
|
|
ArgumentException.ThrowIfNullOrWhiteSpace(password, nameof(password));
|
|
|
|
username = username.Trim().ToLower();
|
|
CurrentUser = proxy.Login(username, Utils.HashPassword(username, password));
|
|
DataManager = proxy;
|
|
|
|
return IsLoggedIn;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Closes the session on the current data provider.
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public bool Logout()
|
|
{
|
|
return DataManager?.Logout(CurrentUser) ?? true;
|
|
}
|
|
}
|