mirror of
https://dev.azure.com/TeilRad/sharee.bike%20App/_git/Code
synced 2024-11-14 14:26:28 +01:00
148 lines
4.9 KiB
C#
148 lines
4.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using TINK.Model.User.Account;
|
|
|
|
namespace TINK.Model.User
|
|
{
|
|
public delegate void LoginStateChangedDelegate(object p_oSender, EventArgs p_oEventArgs);
|
|
|
|
|
|
/// <summary>
|
|
/// Manages user of the app.
|
|
/// </summary>
|
|
public class User : IUser
|
|
{
|
|
/// <summary> Holds account data. </summary>
|
|
private AccountMutable Account { get; }
|
|
|
|
/// <summary>
|
|
/// Provides storing functionality.
|
|
/// </summary>
|
|
private IStore Store { get; }
|
|
|
|
/// <summary> Holds the id of the device. </summary>
|
|
public string DeviceId { get; }
|
|
|
|
/// Loads user name and passwort from account store.
|
|
/// </summary>
|
|
/// <param name="p_oAccountStore"> Object to use for loading and saving user data.</param>
|
|
public User(
|
|
IStore accountStore,
|
|
IAccount account,
|
|
string deviceId)
|
|
{
|
|
Store = accountStore
|
|
?? throw new ArgumentException("Can not instantiate user- object. No store functionality available.");
|
|
DeviceId = deviceId;
|
|
Account = new AccountMutable(account);
|
|
}
|
|
|
|
/// <summary> Is fired wheneverlogin state changes. </summary>
|
|
public event LoginStateChangedDelegate StateChanged;
|
|
|
|
/// <summary>
|
|
/// Holds a value indicating whether user is logged in or not.
|
|
/// </summary>
|
|
public bool IsLoggedIn => Account.GetIsLoggedIn();
|
|
|
|
/// <summary>
|
|
/// Holds the mail address.
|
|
/// </summary>
|
|
public string Mail
|
|
{
|
|
get { return Account.Mail; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the sessiong cookie.
|
|
/// </summary>
|
|
public string SessionCookie
|
|
{
|
|
get { return Account.SessionCookie; }
|
|
}
|
|
/// <summary>
|
|
/// Holds the password.
|
|
/// </summary>
|
|
public string Password
|
|
{
|
|
get { return Account.Pwd; }
|
|
}
|
|
|
|
/// <summary>Holds the debug level.</summary>
|
|
public Permissions DebugLevel
|
|
{
|
|
get { return Account.DebugLevel; }
|
|
}
|
|
|
|
/// <summary> Holds the group of the bike (TINK, Konrad, ...).</summary>
|
|
public IEnumerable<string> Group { get { return Account.Group; } }
|
|
|
|
/// <summary> Logs in user. </summary>
|
|
/// <param name="p_oAccount">Account to use for login.</param>
|
|
/// <param name="p_str_DeviceId">Holds the Id to identify the device.</param>
|
|
/// <param name="isConnected">True if connector has access to copri server, false if cached values are used.</param>
|
|
public void CheckIsPasswordValid(string mail, string password)
|
|
{
|
|
if (IsLoggedIn)
|
|
{
|
|
throw new Exception($"Can not log in user {mail} because user {Account} is already logged in.");
|
|
}
|
|
|
|
// Check if password might be valid before connecting to copri.
|
|
var l_oResult = Validator.ValidateMailAndPasswordDelegate(mail, password);
|
|
|
|
if (!l_oResult.IsValid)
|
|
{
|
|
// Password is not valid.
|
|
throw new ArgumentException(l_oResult.Description[Elements.Account]);
|
|
}
|
|
}
|
|
|
|
/// <summary> Logs in user. </summary>
|
|
/// <param name="p_oAccount">Account to use for login.</param>
|
|
/// <param name="p_str_DeviceId">Holds the Id to identify the device.</param>
|
|
/// <param name="isConnected">True if connector has access to copri server, false if cached values are used.</param>
|
|
public async Task Login(IAccount account)
|
|
{
|
|
// Update account instance from copri data.
|
|
Account.Copy(account);
|
|
|
|
// Save data to store.
|
|
await Store.Save(Account);
|
|
|
|
// Nothing to do because state did not change.
|
|
StateChanged?.Invoke(this, new EventArgs());
|
|
}
|
|
|
|
/// <summary> Logs in user</summary>
|
|
/// <returns></returns>
|
|
public void Logout()
|
|
{
|
|
var l_oPreviousState = IsLoggedIn;
|
|
|
|
Account.Copy(Store.Delete(Account));
|
|
|
|
if (IsLoggedIn == l_oPreviousState)
|
|
{
|
|
// Nothing to do because state did not change.
|
|
return;
|
|
}
|
|
|
|
StateChanged?.Invoke(this, new EventArgs());
|
|
}
|
|
|
|
/// <summary>
|
|
/// Filters bike groups depending on whether user has access to all groups of bikes.
|
|
/// Some user may be "TINK"- user only, some "Konrad" and some may be "TINK" and "Konrad" users.
|
|
/// </summary>
|
|
/// <param name="p_oAccount">Account to filter with.</param>
|
|
/// <param name="source">Groups to filter..</param>
|
|
/// <returns>Filtered bike groups.</returns>
|
|
public IEnumerable<string> DoFilter(IEnumerable<string> source = null)
|
|
{
|
|
return Account.DoFilter(source);
|
|
}
|
|
}
|
|
}
|
|
|