public async Task OnResourceExecutionAsync(ResourceExecutingContext context, ResourceExecutionDelegate next) { var httpContext = context.HttpContext; try { var userId = new UserClaimLookup(httpContext.User).GetId(ClaimType.UserId); var activeId = httpContext.Session.GetInt32(SessionKey.ActiveUserId); if (userId != activeId) { httpContext.Session.SetInt32(SessionKey.ActiveUserId, userId); } } catch (Exception ex) { _logger.LogDebug($"Attempted Mission Control access while not logged in: {ex.Message}"); } if (httpContext.User.HasClaim(ClaimType.Permission, Domain.Model.Permission.ReadAllMail.ToString())) { httpContext.Items[ItemKey.UnreadCount] = await _mailService.GetAdminUnreadCountAsync(); } await next(); }
public async Task OnResourceExecutionAsync(ResourceExecutingContext context, ResourceExecutionDelegate next) { Site site = null; var httpContext = context.HttpContext; // if we've already fetched it on this request it's present in Items int?siteId = null; if (httpContext.User.Identity.IsAuthenticated) { // if the user is authenticated, that is their site siteId = new UserClaimLookup(httpContext.User).GetId(ClaimType.SiteId); } else { string sitePath = context.RouteData.Values["sitePath"]?.ToString(); // first check, did they use a sitePath giving them a specific site if (!string.IsNullOrEmpty(sitePath)) { site = await _siteLookupService.GetSiteByPathAsync(sitePath); if (site != null) { siteId = site.Id; } } // if not check if they already have one in their session if (siteId == null) { siteId = httpContext.Session.GetInt32(SessionKey.SiteId); } // if not then resort to the default if (siteId == null) { siteId = await _siteLookupService.GetDefaultSiteIdAsync(); } } if (site == null) { site = await _siteLookupService.GetByIdAsync((int)siteId); } httpContext.Items[ItemKey.GoogleAnalytics] = site.GoogleAnalyticsTrackingId; httpContext.Items[ItemKey.SiteStage] = _siteLookupService.GetSiteStageAsync(site); httpContext.Session.SetInt32(SessionKey.SiteId, (int)siteId); httpContext.Items[ItemKey.SiteId] = (int)siteId; await next(); }