mirror of
https://dev.azure.com/TeilRad/sharee.bike%20App/_git/Code
synced 2024-11-05 10:36:30 +01:00
154 lines
5.1 KiB
C#
154 lines
5.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
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 readonly AccountMutable m_oAccount;
|
|
|
|
/// <summary>
|
|
/// Provides storing functionality.
|
|
/// </summary>
|
|
private IStore m_oStore;
|
|
|
|
/// <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 p_oAccountStore,
|
|
IAccount p_oAccount,
|
|
string p_strDeviceId)
|
|
{
|
|
m_oStore = p_oAccountStore
|
|
?? throw new ArgumentException("Can not instantiate user- object. No store functionality available.");
|
|
DeviceId = p_strDeviceId;
|
|
m_oAccount = new AccountMutable(p_oAccount);
|
|
}
|
|
|
|
/// <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 {
|
|
get
|
|
{
|
|
return m_oAccount.GetIsLoggedIn();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Holds the mail address.
|
|
/// </summary>
|
|
public string Mail
|
|
{
|
|
get { return m_oAccount.Mail; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the sessiong cookie.
|
|
/// </summary>
|
|
public string SessionCookie
|
|
{
|
|
get { return m_oAccount.SessionCookie; }
|
|
}
|
|
/// <summary>
|
|
/// Holds the password.
|
|
/// </summary>
|
|
public string Password
|
|
{
|
|
get { return m_oAccount.Pwd; }
|
|
}
|
|
|
|
/// <summary>Holds the debug level.</summary>
|
|
public Permissions DebugLevel
|
|
{
|
|
get { return m_oAccount.DebugLevel; }
|
|
}
|
|
|
|
/// <summary> Holds the group of the bike (TINK, Konrad, ...).</summary>
|
|
public IEnumerable<string> Group { get { return m_oAccount.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 {m_oAccount} 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 void Login(IAccount account)
|
|
{
|
|
// Update account instance from copri data.
|
|
m_oAccount.Copy(account);
|
|
|
|
// Save data to store.
|
|
m_oStore.Save(m_oAccount);
|
|
|
|
// 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;
|
|
|
|
m_oAccount.Copy(m_oStore.Delete(m_oAccount));
|
|
|
|
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="p_oSource">Groups to filter..</param>
|
|
/// <returns>Filtered bike groups.</returns>
|
|
public IEnumerable<string> DoFilter(IEnumerable<string> p_oSource = null)
|
|
{
|
|
return m_oAccount.DoFilter(p_oSource);
|
|
}
|
|
}
|
|
}
|
|
|