sharee.bike-App/TINK/TINK/Model/User/Account/Store.cs

119 lines
3.6 KiB
C#
Raw Normal View History

2021-05-13 20:16:41 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
using TINK.Model.Connector;
namespace TINK.Model.User.Account
{
public class Store : IStore
{
/// <summary>
/// Holds the name of the application.
/// </summary>
private const string M_STR_APPNAME = "TINKApp";
/// <summary> Holds the id of the session. </summary>
private const string KEY_SESSIONID = "SessionId";
/// <summary> Holds the id of the session. </summary>
private const string KEY_GROUP = "Group";
/// <summary> Holds the id of the session. </summary>
private const string KEY_DEBUGLEVEL = "DebugLevel";
private Store()
{ }
public Store(string p_strCopriHostHash)
{
m_strCopriHostHash = p_strCopriHostHash
?? throw new ArgumentException("Can not construct account object. Copri hash must not be null.");
}
private string m_strCopriHostHash;
/// <summary>
/// Reads mail address and password from account store.
/// </summary>
/// <returns></returns>
public IAccount Load()
{
#if !WINDOWS_UWP
#if !__IOS__
var account = Xamarin.Auth.AccountStore.Create("System.Char[]").FindAccountsForService($"{M_STR_APPNAME}_{m_strCopriHostHash}").FirstOrDefault();
#else
var account = Xamarin.Auth.AccountStore.Create().FindAccountsForService($"{M_STR_APPNAME}_{m_strCopriHostHash}").FirstOrDefault();
#endif
if (account == null)
{
// Nothing t do if account cannot be accessed.
return new EmptyAccount();
}
return new Account(
account.Username,
string.Empty,
account.Properties.ContainsKey(KEY_SESSIONID) ? account.Properties[KEY_SESSIONID] : null,
account.Properties.ContainsKey(KEY_GROUP) ? TextToTypeHelper.GetGroup(account.Properties[KEY_GROUP]) : new List<string>(),
account.Properties.ContainsKey(KEY_DEBUGLEVEL) && !string.IsNullOrEmpty(account.Properties[KEY_DEBUGLEVEL]) ? Permissions.Parse<Permissions>(account.Properties[KEY_DEBUGLEVEL]) : Permissions.None);
#else
return new Account(
string.Empty,
string.Empty,
string.Empty,
new List<string>(),
null);
#endif
}
/// <summary>
/// Writes mail address and password to account store.
/// </summary>
/// <param name="p_oAccount"></param>
public void Save(IAccount p_oAccount)
{
#if !WINDOWS_UWP
Xamarin.Auth.Account account = new Xamarin.Auth.Account
{
Username = p_oAccount.Mail
};
account.Properties.Add(KEY_SESSIONID, p_oAccount?.SessionCookie);
account.Properties.Add(KEY_GROUP, p_oAccount?.Group?.GetGroup());
account.Properties.Add(KEY_DEBUGLEVEL, p_oAccount.DebugLevel.ToString());
#if !__IOS__
Xamarin.Auth.AccountStore.Create("System.Char[]").Save(account, $"{M_STR_APPNAME}_{m_strCopriHostHash}");
#else
Xamarin.Auth.AccountStore.Create().Save(account, $"{M_STR_APPNAME}_{m_strCopriHostHash}");
#endif
#endif
}
/// <summary>
/// Deletes mail address and password from account store.
/// </summary>
public IAccount Delete(IAccount p_oAccount)
{
#if !WINDOWS_UWP
#if !__IOS__
var account = Xamarin.Auth.AccountStore.Create("System.Char[]").FindAccountsForService($"{M_STR_APPNAME}_{m_strCopriHostHash}").FirstOrDefault();
#else
var account = Xamarin.Auth.AccountStore.Create().FindAccountsForService($"{M_STR_APPNAME}_{m_strCopriHostHash}").FirstOrDefault();
#endif
if (account == null)
{
return new EmptyAccount();
}
#if !__IOS__
Xamarin.Auth.AccountStore.Create("System.Char[]").Delete(account, $"{M_STR_APPNAME}_{m_strCopriHostHash}");
#else
Xamarin.Auth.AccountStore.Create().Delete(account, $"{M_STR_APPNAME}_{m_strCopriHostHash}");
#endif
#endif
return new EmptyAccount();
}
}
}