23
23
using Microsoft . AspNetCore . Http ;
24
24
using System . Security . Claims ;
25
25
using Microsoft . AspNetCore . Localization ;
26
+ using static Microsoft . ApplicationInsights . MetricDimensionNames . TelemetryContext ;
27
+ using PostHog ;
26
28
27
29
namespace Resgrid . Web . Controllers
28
30
{
@@ -45,12 +47,14 @@ public class AccountController : Controller
45
47
private readonly IEmailMarketingProvider _emailMarketingProvider ;
46
48
private readonly ISystemAuditsService _systemAuditsService ;
47
49
private readonly ICacheProvider _cacheProvider ;
48
-
50
+ private readonly IPostHogClient _posthog ;
51
+
52
+
49
53
public AccountController (
50
54
UserManager < IdentityUser > userManager , SignInManager < IdentityUser > signInManager ,
51
55
IDepartmentsService departmentsService , IUsersService usersService , IEmailService emailService , IInvitesService invitesService , IUserProfileService userProfileService ,
52
56
ISubscriptionsService subscriptionsService , IAffiliateService affiliateService , IEventAggregator eventAggregator , IEmailMarketingProvider emailMarketingProvider ,
53
- ISystemAuditsService systemAuditsService , ICacheProvider cacheProvider )
57
+ ISystemAuditsService systemAuditsService , ICacheProvider cacheProvider , IPostHogClient posthog )
54
58
{
55
59
_userManager = userManager ;
56
60
_signInManager = signInManager ;
@@ -65,6 +69,7 @@ public AccountController(
65
69
_emailMarketingProvider = emailMarketingProvider ;
66
70
_systemAuditsService = systemAuditsService ;
67
71
_cacheProvider = cacheProvider ;
72
+ _posthog = posthog ;
68
73
}
69
74
#endregion Private Members and Constructors
70
75
@@ -128,6 +133,29 @@ await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,
128
133
{
129
134
var token = await ApiAuthHelper . GetBearerApiTokenAsync ( model . Username , model . Password ) ;
130
135
await _cacheProvider . SetStringAsync ( CacheConfig . ApiBearerTokenKeyName + $ "_${ userId } ", token , new TimeSpan ( 48 , 0 , 0 ) ) ;
136
+
137
+ if ( ! String . IsNullOrWhiteSpace ( TelemetryConfig . PostHogApiKey ) )
138
+ {
139
+ var email = User . Claims . FirstOrDefault ( x => x . Type == ClaimTypes . Email ) ? . Value ;
140
+ var departmentId = User . Claims . FirstOrDefault ( x => x . Type == ClaimTypes . PrimaryGroupSid ) ? . Value ;
141
+ var departmentName = User . Claims . FirstOrDefault ( x => x . Type == ClaimTypes . Actor ) ? . Value ;
142
+ var name = User . Claims . FirstOrDefault ( x => x . Type == ClaimTypes . GivenName ) ? . Value ;
143
+ var createdOn = User . Claims . FirstOrDefault ( x => x . Type == ClaimTypes . OtherPhone ) ? . Value ;
144
+
145
+ await _posthog . IdentifyAsync (
146
+ userId ,
147
+ email ,
148
+ name ,
149
+ personPropertiesToSet : new ( )
150
+ {
151
+ [ "departmentId" ] = departmentId ,
152
+ [ "departmentName" ] = departmentName ,
153
+ } ,
154
+ personPropertiesToSetOnce : new ( )
155
+ {
156
+ [ "createdOn" ] = createdOn
157
+ } ) ;
158
+ }
131
159
}
132
160
}
133
161
catch ( Exception ex )
@@ -139,7 +167,7 @@ await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,
139
167
Response . Cookies . Delete ( ".AspNetCore.Identity.ApplicationC1" ) ;
140
168
Response . Cookies . Delete ( ".AspNetCore.Identity.ApplicationC2" ) ;
141
169
Response . Cookies . Delete ( ".AspNetCore.Identity.ApplicationC3" ) ;
142
-
170
+
143
171
if ( ! String . IsNullOrWhiteSpace ( returnUrl ) )
144
172
return RedirectToLocal ( returnUrl ) ;
145
173
else
0 commit comments