using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using Sarand.BusinessMgr.Net.Core.Web.App; using Sarand.BusinessMgr.Net.Core.Web.Helpers; using Sarand.BusinessMgr.Net.Core.Web.Portal.Models; using Sarand.BusinessMgr.Net.Standard.Base.App; 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.Security; using Sarand.Net.Standard.Base.Utils; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Threading.Tasks; namespace Sarand.BusinessMgr.Net.Core.Web.Portal.Controllers { //======================================================================= public class HomeController : SbmController_Portal { //------------------------------------------------------------------- // Data members. //------------------------------------------------------------------- // Construction. public HomeController(SbmLocaliser localiser, ILogger logger) : base(localiser, logger) { } //------------------------------------------------------------------- [HttpGet] public IActionResult Index() { String fn = GetType().Name + ".Index"; // Check for 'Keep Logged In' cookie. // NB Only allowed if there isn't a current user session. if (!ValidUserSession && AppConfig.Security.SignIn.KeepLoggedIn.Allowed) { // Extract parameters and validate that they are populated. String encryptedParams = GetRequestCookie(CookieName_KeepLoggedIn); KeepLoggedInParams keepLoggedInParams = ExtractKeepLoggedInParams(encryptedParams); String logPrefix = fn + " 'Keep Logged In' cookie found but "; String logSuffix = keepLoggedInParams.GetValidParams().Concat("", " ", "."); if (keepLoggedInParams.IsValid()) { // User may be defined on any one of the BusinessMgr databases - need to find out which. // Identify session contexts with a BusMgr database that hosts the specified email address. List sessionContextIdxs = SbmSessionContextHelper_Web.GetSessionContextIdxsForUsersBusMgrDatabase(keepLoggedInParams.EmailAddress); foreach (ushort sessionContextIdx in sessionContextIdxs) { // Consoles and Managers must be associated with the specific context. SbmSessionContext sessionContext = SbmSessionContextHelper_Web.GetSessionContext(sessionContextIdx); SbmUserContext_Web userContext = new SbmUserContext_Web(sessionContext); using (SaSecurityConsole securityConsoleForUser = App.CreateSecurityConsole(userContext)) using (SbmAccountMgr accountMgrForUser = App.System.CreateAccountMgr(userContext)) { using (SaSecurityItem securityItemUser = securityConsoleForUser.GetUserByEmail(keepLoggedInParams.EmailAddress)) using (SbmAffiliate affiliateUser = accountMgrForUser.GetAffiliateById(keepLoggedInParams.AffiliateIdUser)) using (SbmAssociation associationOrganisation = accountMgrForUser.GetAssociationById(keepLoggedInParams.AssociationIdOrganisation)) using (SbmAffiliate affiliateOrganisation = accountMgrForUser.GetAffiliateById(associationOrganisation?.AffiliateIdOwner ?? null)) { if (securityItemUser != null && affiliateUser != null && associationOrganisation != null && affiliateOrganisation != null) { if (securityItemUser.Id.Equals(keepLoggedInParams.SecurityItemId) && affiliateUser.UserId.Equals(securityItemUser.Id)) { if (affiliateUser.IsStatusCurrent) { List supervisedOrganisations = accountMgrForUser.GetAffiliatesSupervisedBy(affiliateUser.Id); String errorMessage = ""; if (SignUserIn(userContext, securityItemUser, affiliateUser, supervisedOrganisations, associationOrganisation, affiliateOrganisation, true, ref errorMessage)) { SaLogger.Info(logPrefix + " 'Keep Logged In' cookie found." + logSuffix); break; } else SaLogger.Warn(logPrefix + "failed to sign user in: " + errorMessage + "." + logSuffix); } else SaLogger.Warn(logPrefix + "Affiliate is not active." + logSuffix); } else SaLogger.Warn(logPrefix + "Security Item and Affiliate are inconsistent. " + logSuffix); } else SaLogger.Warn(logPrefix + "Security Item or Affiliate not found. " + logSuffix); } } if (ValidUserSession) break; } } else if (!StringUtil.IsEmpty(encryptedParams)) SaLogger.Warn(fn + " Keep Logged In cookie found but with incomplete details." + logSuffix); } // Redirect to sign-in if not signed-in. if (!ValidUserSession) { ClearKeepLoggedInCookie(); return RedirectToAction("", "Security"); } // Home page. Model_Home model = new Model_Home(Localiser); model.ProhibitCache = false; return View(model); } //------------------------------------------------------------------- [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() { return View(new ErrorViewModel(Localiser) { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); } //------------------------------------------------------------------- } //======================================================================= }