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
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<PackageReference Include="FluentMigrator.Extensions.Postgres" Version="7.0.0" />
<PackageReference Include="FluentMigrator.Runner.Postgres" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.3" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Quidjibo" Version="0.6.0" />
</ItemGroup>

Expand Down
34 changes: 31 additions & 3 deletions Web/Resgrid.Web/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
using Microsoft.AspNetCore.Http;
using System.Security.Claims;
using Microsoft.AspNetCore.Localization;
using static Microsoft.ApplicationInsights.MetricDimensionNames.TelemetryContext;
using PostHog;

namespace Resgrid.Web.Controllers
{
Expand All @@ -45,12 +47,14 @@ public class AccountController : Controller
private readonly IEmailMarketingProvider _emailMarketingProvider;
private readonly ISystemAuditsService _systemAuditsService;
private readonly ICacheProvider _cacheProvider;

private readonly IPostHogClient _posthog;


public AccountController(
UserManager<IdentityUser> userManager, SignInManager<IdentityUser> signInManager,
IDepartmentsService departmentsService, IUsersService usersService, IEmailService emailService, IInvitesService invitesService, IUserProfileService userProfileService,
ISubscriptionsService subscriptionsService, IAffiliateService affiliateService, IEventAggregator eventAggregator, IEmailMarketingProvider emailMarketingProvider,
ISystemAuditsService systemAuditsService, ICacheProvider cacheProvider)
ISystemAuditsService systemAuditsService, ICacheProvider cacheProvider, IPostHogClient posthog)
{
_userManager = userManager;
_signInManager = signInManager;
Expand All @@ -65,6 +69,7 @@ public AccountController(
_emailMarketingProvider = emailMarketingProvider;
_systemAuditsService = systemAuditsService;
_cacheProvider = cacheProvider;
_posthog = posthog;
}
#endregion Private Members and Constructors

Expand Down Expand Up @@ -128,6 +133,29 @@ await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,
{
var token = await ApiAuthHelper.GetBearerApiTokenAsync(model.Username, model.Password);
await _cacheProvider.SetStringAsync(CacheConfig.ApiBearerTokenKeyName + $"_${userId}", token, new TimeSpan(48, 0, 0));

if (!String.IsNullOrWhiteSpace(TelemetryConfig.PostHogApiKey))
{
var email = User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Email)?.Value;
var departmentId = User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.PrimaryGroupSid)?.Value;
var departmentName = User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Actor)?.Value;
var name = User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.GivenName)?.Value;
var createdOn = User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.OtherPhone)?.Value;

await _posthog.IdentifyAsync(
userId,
email,
name,
personPropertiesToSet: new()
{
["departmentId"] = departmentId,
["departmentName"] = departmentName,
},
personPropertiesToSetOnce: new()
{
["createdOn"] = createdOn
});
}
}
}
catch (Exception ex)
Expand All @@ -139,7 +167,7 @@ await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,
Response.Cookies.Delete(".AspNetCore.Identity.ApplicationC1");
Response.Cookies.Delete(".AspNetCore.Identity.ApplicationC2");
Response.Cookies.Delete(".AspNetCore.Identity.ApplicationC3");

if (!String.IsNullOrWhiteSpace(returnUrl))
return RedirectToLocal(returnUrl);
else
Expand Down
36 changes: 36 additions & 0 deletions Web/Resgrid.Web/Middleware/FeatureFlagContextProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Microsoft.AspNetCore.Http;
using PostHog.FeatureManagement;
using PostHog;
using System.Collections.Generic;
using System.Threading.Tasks;
using Resgrid.Web.Helpers;

namespace Resgrid.Web.Middleware
{
/// <summary>
/// Provides context and options used to evaluate feature flags for the Resgrid application.
/// </summary>
/// <param name="httpContextAccessor">The <see cref="IHttpContextAccessor"/> used to get the current user's Id.</param>
public class FeatureFlagContextProvider(IHttpContextAccessor httpContextAccessor)
: PostHogFeatureFlagContextProvider
{
protected override string? GetDistinctId() =>
httpContextAccessor.HttpContext?.User.Identity?.Name;

protected override ValueTask<FeatureFlagOptions> GetFeatureFlagOptionsAsync()
{
return ValueTask.FromResult(
new FeatureFlagOptions
{
PersonProperties = new Dictionary<string, object?>
{
["email"] = ClaimsAuthorizationHelper.GetEmailAddress(),
},
Groups = [
new Group("departmentId", ClaimsAuthorizationHelper.GetDepartmentId().ToString())
],
OnlyEvaluateLocally = true
});
}
}
}
1 change: 1 addition & 0 deletions Web/Resgrid.Web/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.Extensions.Logging;
using Resgrid.Config;
using Sentry.Profiling;
using PostHog;

namespace Resgrid.Web
{
Expand Down
1 change: 1 addition & 0 deletions Web/Resgrid.Web/Resgrid.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.11.2" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.11.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.11.1" />
<PackageReference Include="PostHog.AspNetCore" Version="1.0.4" />
<PackageReference Include="protobuf-net" Version="3.2.46" />
<PackageReference Include="Sentry" Version="5.4.0" />
<PackageReference Include="Sentry.AspNetCore" Version="5.4.0" />
Expand Down
22 changes: 22 additions & 0 deletions Web/Resgrid.Web/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Reflection.PortableExecutable;
using System.Threading.Tasks;
using Audit.Core;
using Autofac;
Expand All @@ -26,6 +27,8 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using PostHog.Config;
using PostHog;
using Resgrid.Config;
using Resgrid.Framework;
using Resgrid.Localization;
Expand Down Expand Up @@ -53,6 +56,8 @@
using StackExchange.Redis;
using Stripe;
using IPNetwork = Microsoft.AspNetCore.HttpOverrides.IPNetwork;
using Microsoft.Extensions.Http.Logging;
using Resgrid.Web.Middleware;

namespace Resgrid.Web
{
Expand Down Expand Up @@ -429,6 +434,23 @@ public void ConfigureServices(IServiceCollection services)
services.AddSentryTunneling();
}

if (!string.IsNullOrWhiteSpace(Config.TelemetryConfig.PostHogApiKey))
{
services.AddPostHog(options =>
{
options.PostConfigure(o =>
{
o.HostUrl = new Uri(TelemetryConfig.PostHogUrl);
o.ProjectApiKey = TelemetryConfig.PostHogApiKey;
o.SuperProperties.Add("app_name", "ResgridWeb");
o.SuperProperties.Add("environment", SystemBehaviorConfig.Environment);
});

// Enables PostHog as a provider for ASP.NET Core's feature management system.
options.UseFeatureManagement<FeatureFlagContextProvider>();
});
}

this.Services = services;

//if (Config.ExternalErrorConfig.ApplicationInsightsEnabled)
Expand Down
1 change: 1 addition & 0 deletions Workers/Support/Quidjibo.Postgres/Quidjibo.Postgres.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.3" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Npgsql" Version="8.0.5" />
<PackageReference Include="Quidjibo" Version="0.6.0" />
</ItemGroup>
Expand Down
Loading