Files
BusinessMgr-2019/SaBusMgrStandardBase/Base/App/SbmSessionProfile.cs
T
2021-05-05 21:00:33 +01:00

162 lines
5.5 KiB
C#

using Sarand.BusinessMgr.Net.Standard.Base.Bus;
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 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(SbmAffiliate affiliateUser, List<SysIdString> supervisedOwners)
{
SetUserData(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)
{
userContext = new SbmUserContext_Internal(sessionContext);
SysParamCache = new SbmSysParamCache(userContext);
}
//-------------------------------------------------------------------
// Properties.
public SbmUserContext UserContext { get { return userContext; } }
public SbmSessionContext SessionContext { get { return userContext.SessionContext; } }
public SbmSecurityProfile SecurityProfile { get { return UserContext.SecurityProfile; } }
protected SaItemOption.Cache OrganisationConfigOptionsCache { get { return UserContext.Organisation.configOptionsCache; } }
// 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 (SbmAccountMgr accountMgr = CreateAccountMgr())
{
SbmAffiliate affiliateUser = accountMgr.GetAffiliateByUserId(userId);
if (affiliateUser == null)
{
SaLogger.Warn(GetType().Name + ".StartUserSession Unable to retrieve affiliate for UserId: " + SysId.ToString(userId));
return false;
}
List<SysIdString> userSupervisedOwners = accountMgr.GetAffiliatesSupervisedBy(affiliateUser.Id);
userContext.RefreshUser(affiliateUser, userSupervisedOwners);
// Populate security rights.
SecurityProfile.RefreshProfile(SbmGlobals.Application.GetSecurityConsole(UserContext), 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);
}
}
// Data managers.
protected SbmAccountMgr CreateAccountMgr()
{
return SbmGlobals.System.CreateAccountMgr(UserContext);
}
//-------------------------------------------------------------------
}
//=======================================================================
}