Files
BusinessMgr-2019/SaBusMgrStandardBase/Base/App/SbmSessionProfile.cs
T

173 lines
6.2 KiB
C#

using Sarand.BusinessMgr.Net.Standard.Base.Bus;
using Sarand.BusinessMgr.Net.Standard.Base.Consoles;
using Sarand.BusinessMgr.Net.Standard.Base.Core;
using Sarand.BusinessMgr.Net.Standard.Base.Mgr;
using Sarand.Net.Standard.Base.App;
using Sarand.Net.Standard.Base.Bus;
using Sarand.Net.Standard.Base.Core;
using Sarand.Net.Standard.Base.Dbas;
using Sarand.Net.Standard.Base.ItemOptions;
using Sarand.Net.Standard.Base.Security;
using Sarand.Net.Standard.Base.Utils;
using System.Collections.Generic;
namespace Sarand.BusinessMgr.Net.Standard.Base.App
{
//=======================================================================
public abstract class SbmSessionProfile
{
//-------------------------------------------------------------------
// Definitions.
internal class SbmUserContext_Internal : SbmUserContext
{
// Construction.
public SbmUserContext_Internal(SbmSessionContext sessionContext) :
base(sessionContext)
{
}
// Refresh context.
public void RefreshUser(SaSecurityItem securityItemUser, SbmAffiliate affiliateUser, List<SysIdString> supervisedOwners)
{
SetUserData(securityItemUser, affiliateUser, supervisedOwners);
}
public void ClearUser()
{
ClearUserData();
}
public void RefreshSecurityProfile(SbmSecurityProfile securityProfile)
{
SetSecurityProfile(securityProfile);
}
public void RefreshOrganisation(SysId affiliateIdOrganisation)
{
SetOrganisationData(affiliateIdOrganisation);
}
public void ClearOrganisation()
{
ClearOrganisationData();
}
}
//-------------------------------------------------------------------
// Data members.
private readonly SbmUserContext_Internal userContext;
//-------------------------------------------------------------------
// Protected Construction - use derived objects.
protected SbmSessionProfile(SbmSessionContext sessionContext, SysId affiliateIdUser)
{
userContext = new SbmUserContext_Internal(sessionContext);
SysParamCache = new SbmSysParamCache(userContext, affiliateIdUser);
}
//-------------------------------------------------------------------
// Properties.
public SbmUserContext UserContext { get { return userContext; } }
public SbmSessionContext SessionContext { get { return userContext.SessionContext; } }
public ISbmSecurityProfile_Core SecurityProfile { get { return UserContext.SecurityProfile_Core; } }
protected SaItemOption.Cache OrganisationConfigOptionsCache { get { return UserContext.OrganisationConfigOptionsCache; } }
// User parameters.
protected SbmSysParamCache SysParamCache { get; }
//-------------------------------------------------------------------
// O w n e r O r g a n i s a t i o n
//-------------------------------------------------------------------
public abstract bool SetSelectedOwner(SysId affiliateId);
protected void RefreshOwnerOrganisation(SysId affiliateIdOrganisation)
{
if (SysId.IsValid(affiliateIdOrganisation))
userContext.RefreshOrganisation(affiliateIdOrganisation);
else userContext.ClearOrganisation();
}
//-------------------------------------------------------------------
// U s e r S e s s i o n
//-------------------------------------------------------------------
public bool StartUserSession(SysId userId, List<TypeCodeDesc> securityRights)
{
// Clear existing User details.
ClearUserSession();
using (SbmConfigConsole configConsole = SbmGlobals.Application.CreateConfigConsole(UserContext.SessionContext))
using (SaSecurityConsole securityConsole = SbmGlobals.Application.CreateSecurityConsole(UserContext))
using (SbmAccountMgr accountMgr = CreateAccountMgr())
{
// Security item.
SaSecurityItem securityItemUser = securityConsole.GetSecurityItemById(SaSecurity.ItemType.User, userId);
SbmAffiliate affiliateUser = accountMgr.GetAffiliateByUserId(userId);
if (affiliateUser == null)
{
SaLogger.Warn($"{SysUtil.Fn} Unable to retrieve affiliate for UserId: {SysId.ToString(userId)}");
return false;
}
List<SysIdString> userSupervisedOwners = accountMgr.GetAffiliatesSupervisedBy(affiliateUser.Id);
userContext.RefreshUser(securityItemUser, affiliateUser, userSupervisedOwners);
// Populate security rights.
SecurityProfile.RefreshProfile(configConsole, securityConsole, userId, securityRights);
// Reinstate previous owner if defined and verify that it is in the supervised list.
SysId affiliateIdOwner = UserContext.Organisation.AffiliateId;
if (!userSupervisedOwners.Exists(item => item.Id.Equals(affiliateIdOwner)))
affiliateIdOwner = new SysId();
// If not otherwise defined, select first owner in supervised list.
if (!SysId.IsValid(affiliateIdOwner) && userSupervisedOwners.Count > 0)
affiliateIdOwner = userSupervisedOwners[0].Id;
SetSelectedOwner(affiliateIdOwner);
}
// ToDo StartUserSession Reinstate previous default view.
return true;
}
public void ClearUserSession()
{
// Save user parameters.
SysParamCache.Store();
// Reset user details.
userContext.ClearUser();
SysParamCache.Clear();
}
//-------------------------------------------------------------------
// Save the user's session parameters.
public bool SaveSessionProfile()
{
bool success = false;
// Update the list of parameters that we know about to the database.
success = SysParamCache.Store();
// Clear down the list of parameters for this user.
SysParamCache.Clear();
return success;
}
//-------------------------------------------------------------------
// D a t a b a s e
//-------------------------------------------------------------------
// Database Update
public DbUpdateInfo DbUpdateInfo
{
get
{
return new DbUpdateInfo(SecurityProfile.UserId, SecurityProfile.UserDisplayName, SacsSystem.ComputerName);
}
}
public SaSecurityConsole SaSecurityConsole { get; private set; }
// Data managers.
protected SbmAccountMgr CreateAccountMgr()
{
return SbmGlobals.System.CreateAccountMgr(UserContext);
}
//-------------------------------------------------------------------
}
//=======================================================================
}