64 lines
1.9 KiB
C#
64 lines
1.9 KiB
C#
using OwnChar.Api;
|
|
using OwnChar.Api.Exceptions;
|
|
using OwnChar.Manager.Modules;
|
|
using OwnChar.Model;
|
|
using Pilz.Cryptography;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
|
|
namespace OwnChar.Manager;
|
|
|
|
public class OwnCharManager : IOwnCharManager
|
|
{
|
|
// User
|
|
public bool IsLoggedIn => CurrentUser != null;
|
|
public UserAccount? CurrentUser { get; private set; }
|
|
|
|
// Data Provider
|
|
public IDataManager? DataManager { get; set; }
|
|
|
|
// Manager
|
|
public IUserManager Users { get; }
|
|
public IGroupsManager Groups { get; }
|
|
public ICharacterManager Characters { get; }
|
|
|
|
public OwnCharManager()
|
|
{
|
|
Users = new UserManager(this);
|
|
Groups = new GroupsManager(this);
|
|
Characters = new CharacterManager(this);
|
|
}
|
|
|
|
[MemberNotNull(nameof(CurrentUser), nameof(DataManager))]
|
|
internal protected void CheckLogin()
|
|
{
|
|
if (!IsLoggedIn || DataManager == null)
|
|
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;
|
|
}
|
|
}
|