63 lines
1.8 KiB
C#
63 lines
1.8 KiB
C#
using OwnChar.Api;
|
|
using OwnChar.Api.Exceptions;
|
|
using OwnChar.Data.Model.Client;
|
|
using OwnChar.Modules;
|
|
using Pilz.Cryptography;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
|
|
namespace OwnChar;
|
|
|
|
internal class OwnCharManager : IOwnCharManager
|
|
{
|
|
// User
|
|
public bool IsLoggedIn => Api is not null && Api.IsLoggedIn;
|
|
|
|
// Data Provider
|
|
public OwnCharApiClient? Api { get; private 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(Api))]
|
|
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 Task<UserProfile?> Login(string? username, SecureString? password)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(Api, nameof(Api));
|
|
ArgumentException.ThrowIfNullOrWhiteSpace(username, nameof(username));
|
|
ArgumentException.ThrowIfNullOrWhiteSpace(password, nameof(password));
|
|
|
|
username = username.Trim().ToLower();
|
|
|
|
return Api.Auth.Login(username, OwnCharUtils.HashPassword(username, password));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Closes the session on the current data provider.
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<bool> Logout()
|
|
{
|
|
if (Api is null)
|
|
return true;
|
|
return await Api.Auth.Logout();
|
|
}
|
|
}
|