using Newtonsoft.Json; using Sarand.Net.Standard.Base.Bus; using Sarand.Net.Standard.Base.Core; using Sarand.Net.Standard.Base.Dbas; using Saxonite.Net.Standard.Base.Bus; using Saxonite.Net.Standard.Base.App; using System; using System.Collections.Generic; using System.Text; using System.Diagnostics; using Sarand.Net.Standard.Base.Utils; using Newtonsoft.Json.Converters; namespace Saxonite.Net.Standard.Base.App { //======================================================================= public class SaxSessionData { //------------------------------------------------------------------- // Protected construction. protected SaxSessionData() { } //------------------------------------------------------------------- // Construction. /// Hidden constructor for deserialisation only. private SaxSessionData(SaxSessionContext sessionContext) { SessionContext = sessionContext; User = new UserData(SessionContext, null, null); } /// /// Primary constructor - on user login. /// SessionContext and UserAccount must be supplied. /// Roles list is optional. /// protected SaxSessionData(SaxSessionContext sessionContext, SaxUserAccount userAccount, List userRoles) { SessionContext = sessionContext; User = new UserData(SessionContext, userAccount, userRoles); } //------------------------------------------------------------------- // Properties. public SaxSessionContext SessionContext { get; } [JsonIgnore] public bool ValidUserSession { get { return SysId.IsValid(User.UserAccountId) && SysId.IsValid(User.UserAccountId); } } [JsonIgnore] public DbUpdateInfo DbUpdateInfo { get { return new DbUpdateInfo(User.UserAccountId, StringUtil.OnEmpty(User.DisplayName, User.EmailAddress), SaxSystem.ComputerName); } } //------------------------------------------------------------------- // User. public class UserData { protected UserData() { } public UserData(SaxSessionContext sessionContext, SaxUserAccount userAccount, List userRoles) { if (userAccount != null) { UserAccountId = userAccount.Id; FirstName = userAccount.FirstName; LastName = userAccount.LastName; DisplayName = userAccount.GetDisplayName(); EmailAddress = userAccount.EmailAddress; if (sessionContext.Application == Sax.Application.Admin) KeepLoggedIn = userAccount.KeepLoggedIn_Admin; else if (sessionContext.Application == Sax.Application.Online) KeepLoggedIn = userAccount.KeepLoggedIn_Online; else Debugger.Break(); if (userRoles != null) { foreach (SaxUserRole userRole in userRoles.FindAll(item => item.IsStatusActive)) roles.Add(new RoleData(userRole)); } } } public SysId UserAccountId { get; protected set; } public String FirstName { get; protected set; } public String LastName { get; protected set; } public String DisplayName { get; protected set; } public String EmailAddress { get; protected set; } public bool KeepLoggedIn { get; protected set; } public bool UserAccountSupplied { get; protected set; } // Roles. public class RoleData { private RoleData() { } public RoleData(SaxUserRole userRole) { if (userRole != null) { Role = userRole.UserRole; Status = userRole.Status; } } public Sax.UserRole Role { get; protected set; } public SaStatus Status { get; protected set; } } [JsonIgnore] private readonly List roles = new List(); public List Roles { get { return new List(roles); } protected set { roles.Clear(); roles.AddRange(value); } } /// User - JSON deserialisation route. protected void SetRoles(List roles) { if (this.roles.Count > 0) throw new NotSupportedException("Session Data Roles cannot be replaced."); this.roles.AddRange(roles); } [JsonIgnore] public bool IsJournalist { get { return roles?.Exists(item => item.Role == Sax.UserRole.Journalist) ?? false; } } } public UserData User { get; private set; } /// User - JSON deserialisation route. protected void SetUser(UserData user) { if (User != null && User.UserAccountSupplied) throw new NotSupportedException("Session Data User cannot be replaced."); User = user; } //------------------------------------------------------------------- } //======================================================================= }