Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion EssentialCSharp.Web/Controllers/BaseController.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using EssentialCSharp.Web.Extensions;
using EssentialCSharp.Web.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
Expand All @@ -7,16 +8,22 @@ namespace EssentialCSharp.Web.Controllers;
public abstract class BaseController : Controller
{
private readonly IRouteConfigurationService _RouteConfigurationService;
private readonly IHttpContextAccessor _HttpContextAccessor;

protected BaseController(IRouteConfigurationService routeConfigurationService)
protected BaseController(IRouteConfigurationService routeConfigurationService, IHttpContextAccessor httpContextAccessor)
{
_RouteConfigurationService = routeConfigurationService;
_HttpContextAccessor = httpContextAccessor;
}

public override void OnActionExecuting(ActionExecutingContext context)
{
// Automatically add static routes to all views
ViewBag.StaticRoutes = System.Text.Json.JsonSerializer.Serialize(_RouteConfigurationService.GetStaticRoutes());

// Set the referral Id for use in the front end if available
ViewBag.ReferralId = _HttpContextAccessor.HttpContext.GetReferrerId();

base.OnActionExecuting(context);
}
}
4 changes: 1 addition & 3 deletions EssentialCSharp.Web/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace EssentialCSharp.Web.Controllers;

public class HomeController(ILogger<HomeController> logger, IWebHostEnvironment hostingEnvironment, ISiteMappingService siteMappingService, IHttpContextAccessor httpContextAccessor) : Controller
public class HomeController(ILogger<HomeController> logger, IWebHostEnvironment hostingEnvironment, ISiteMappingService siteMappingService, IHttpContextAccessor httpContextAccessor, IRouteConfigurationService routeConfigurationService) : BaseController(routeConfigurationService, httpContextAccessor)
{
public IActionResult Index()
{
Expand All @@ -34,8 +34,6 @@ public IActionResult Index()
ViewBag.PreviousPage = FlipPage(siteMapping.ChapterNumber, siteMapping.PageNumber, false);
ViewBag.HeadContents = headHtml;
ViewBag.Contents = html;
// Set the referral Id for use in the front end if available
ViewBag.ReferralId = httpContextAccessor.HttpContext?.User?.Claims?.FirstOrDefault(f => f.Type == ClaimsExtensions.ReferrerIdClaimType)?.Value;
return View();
}
else
Expand Down
11 changes: 11 additions & 0 deletions EssentialCSharp.Web/Extensions/ClaimsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,16 @@ public static class ClaimsExtensions
{
return claims.FirstOrDefault(claim => claim.Type == ReferrerIdClaimType)?.Value;
}

/// <summary>
/// Gets the referral ID from the current user's claims in the HttpContext
/// </summary>
/// <param name="httpContext">The HttpContext to get the referral ID from</param>
/// <returns>The referral ID if found, otherwise null</returns>
public static string? GetReferrerId(this HttpContext? httpContext)
{
return httpContext?.User?.GetReferrerId();
}

public const string ReferrerIdClaimType = "ReferrerId";
}
2 changes: 1 addition & 1 deletion EssentialCSharp.Web/Services/Referrals/ReferralService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public class ReferralService(EssentialCSharpWebContext dbContext, UserManager<Es
public void TrackReferralAsync(string referralId, ClaimsPrincipal? user)
{
// Check if the referrer ID exists in the claims principal
string? claimsReferrerId = user?.Claims.FirstOrDefault(c => c.Type == ClaimsExtensions.ReferrerIdClaimType)?.Value;
string? claimsReferrerId = user?.GetReferrerId();

if (claimsReferrerId == referralId)
{
Expand Down