sharee.bike-App/TINKLib/Model/User/User.cs

156 lines
5.1 KiB
C#
Raw Normal View History

2021-05-13 20:03:07 +02:00
using System;
using System.Collections.Generic;
2021-06-26 20:57:55 +02:00
using System.Threading.Tasks;
2021-05-13 20:03:07 +02:00
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(
2021-06-26 20:57:55 +02:00
IStore accountStore,
IAccount account,
string deviceId)
2021-05-13 20:03:07 +02:00
{
2021-06-26 20:57:55 +02:00
m_oStore = accountStore
2021-05-13 20:03:07 +02:00
?? throw new ArgumentException("Can not instantiate user- object. No store functionality available.");
2021-06-26 20:57:55 +02:00
DeviceId = deviceId;
m_oAccount = new AccountMutable(account);
2021-05-13 20:03:07 +02:00
}
/// <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>
2021-06-26 20:57:55 +02:00
public async Task Login(IAccount account)
2021-05-13 20:03:07 +02:00
{
// Update account instance from copri data.
m_oAccount.Copy(account);
// Save data to store.
2021-06-26 20:57:55 +02:00
await m_oStore.Save(m_oAccount);
2021-05-13 20:03:07 +02:00
// 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);
}
}
}