Skip to content

Commit

Permalink
feat: add login event data
Browse files Browse the repository at this point in the history
  • Loading branch information
jxnkwlp committed Nov 3, 2024
1 parent 2a0ea49 commit 96c6934
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using JetBrains.Annotations;
Expand Down Expand Up @@ -148,7 +149,12 @@ protected virtual async Task<SignInResult> ExternalLoginSignInAsync(ExternalLogi
{
var user = await UserManager.FindByLoginAsync(loginInfo.LoginProvider, loginInfo.ProviderKey);

await LocalEventBus.PublishAsync(new UserLoginEvent(user!.Id, UserLoginEvent.ExternalLogin), onUnitOfWorkComplete: true);
var loginEventData = new Dictionary<string, object>
{
{ "LoginProvider", loginInfo.LoginProvider },
{ "ProviderKey", loginInfo.ProviderKey },
};
await LocalEventBus.PublishAsync(new UserLoginEvent(user!.Id, UserLoginEvent.ExternalLogin, loginEventData), onUnitOfWorkComplete: true);

await IdentitySecurityLogManager.SaveAsync(new IdentitySecurityLogContext()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Passingwind.Abp.Account.Events;
using Passingwind.Abp.Identity;
Expand Down Expand Up @@ -84,11 +83,20 @@ public virtual async Task LoginAsync(Guid userId)
throw new AbpAuthorizationException();
}

var currentUserId = CurrentUser.Id;
var currentUserName = CurrentUser.Name;

IdentityUser user = await UserManager.FindByIdAsync(userId.ToString()) ?? throw new UserNotFoundException();

await ImpersonateLoginAsync(user);

await LocalEventBus.PublishAsync(new UserLoginEvent(user.Id, UserLoginEvent.ImpersonationLogout), onUnitOfWorkComplete: true);
var loginEventData = new Dictionary<string, object>
{
{ "CurrentUserName", currentUserName! },
{ "CurrentUserId", currentUserId! },
};

await LocalEventBus.PublishAsync(new UserLoginEvent(user.Id, UserLoginEvent.ImpersonationLogout, loginEventData), onUnitOfWorkComplete: true);
}

public virtual async Task LinkLoginAsync(Guid userId)
Expand All @@ -100,6 +108,9 @@ public virtual async Task LinkLoginAsync(Guid userId)
var source = new IdentityLinkUserInfo(CurrentUser.GetId(), CurrentUser.TenantId);
var target = new IdentityLinkUserInfo(userId, user.TenantId);

var currentUserId = CurrentUser.Id;
var currentUserName = CurrentUser.Name;

if (user.TenantId.HasValue && user.TenantId != CurrentUser.TenantId)
{
CurrentTenant.Change(user.TenantId.Value);
Expand All @@ -117,7 +128,13 @@ await SecurityLogManager.SaveAsync(new IdentitySecurityLogContext()
ExtraProperties = { { "SourceUserId", source.UserId } }
});

await LocalEventBus.PublishAsync(new UserLoginEvent(user.Id, UserLoginEvent.LinkLogin), onUnitOfWorkComplete: true);
var loginEventData = new Dictionary<string, object>
{
{ "CurrentUserName", currentUserName! },
{ "CurrentUserId", currentUserId! },
};

await LocalEventBus.PublishAsync(new UserLoginEvent(user.Id, UserLoginEvent.LinkLogin, loginEventData), onUnitOfWorkComplete: true);
}
else
{
Expand All @@ -135,6 +152,9 @@ public virtual async Task DelegationLoginAsync(Guid id)
throw new BusinessException(AccountErrorCodes.UserNotDelegated);
}

var currentUserId = CurrentUser.Id;
var currentUserName = CurrentUser.Name;

// Get the delegation source user
var user = await UserManager.GetByIdAsync(delegation.SourceUserId);

Expand All @@ -148,7 +168,13 @@ await SecurityLogManager.SaveAsync(new IdentitySecurityLogContext()
ExtraProperties = { { "SourceUserId", CurrentUser.GetId() } }
});

await LocalEventBus.PublishAsync(new UserLoginEvent(user.Id, UserLoginEvent.DelegationLogin), onUnitOfWorkComplete: true);
var loginEventData = new Dictionary<string, object>
{
{ "CurrentUserName", currentUserName! },
{ "CurrentUserId", currentUserId! },
};

await LocalEventBus.PublishAsync(new UserLoginEvent(user.Id, UserLoginEvent.DelegationLogin, loginEventData), onUnitOfWorkComplete: true);
}

protected virtual async Task ImpersonateLoginAsync(IdentityUser user)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
Expand Down Expand Up @@ -145,7 +146,12 @@ await SecurityLogManager.SaveAsync(new IdentitySecurityLogContext()
UserName = user.UserName,
});

await LocalEventBus.PublishAsync(new UserLoginEvent(user.Id, UserLoginEvent.TwoFactorLogin), onUnitOfWorkComplete: true);
var loginEventData = new Dictionary<string, object>
{
{ "ProviderName", provider! },
};

await LocalEventBus.PublishAsync(new UserLoginEvent(user.Id, UserLoginEvent.TwoFactorLogin, loginEventData), onUnitOfWorkComplete: true);

return GetAccountLoginResult(signInResult);
}
Expand Down

0 comments on commit 96c6934

Please sign in to comment.