Skip to content
This repository was archived by the owner on Jan 10, 2025. It is now read-only.

Commit

Permalink
Merge pull request #244 from abpframework/gterdem/adminuser-sync-fix
Browse files Browse the repository at this point in the history
Admin user sync problem
  • Loading branch information
enisn authored May 21, 2024
2 parents 94cbef9 + 4dcb437 commit 936ab2b
Showing 1 changed file with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using System.Threading.Tasks;
using System.Linq;
using System.Threading.Tasks;
using EShopOnAbp.IdentityService.ETOs;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Logging;
using Volo.Abp;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.EventBus.Distributed;
using Volo.Abp.Identity;
using Volo.Abp.Uow;
Expand All @@ -13,16 +16,22 @@ public class UserLoggedInEventHandler : IDistributedEventHandler<UserLoggedInEto
{
private readonly IdentityUserManager _userManager;
private readonly ILogger<UserLoggedInEventHandler> _logger;
private readonly IIdentityUserRepository _identityUserRepository;
private readonly ILookupNormalizer _lookupNormalizer;

public UserLoggedInEventHandler(IdentityUserManager userManager, ILogger<UserLoggedInEventHandler> logger)
public UserLoggedInEventHandler(IdentityUserManager userManager,
ILogger<UserLoggedInEventHandler> logger,
IIdentityUserRepository identityUserRepository,
ILookupNormalizer lookupNormalizer)
{
_userManager = userManager;
_logger = logger;
_identityUserRepository = identityUserRepository;
_lookupNormalizer = lookupNormalizer;
}



[UnitOfWork]
public async virtual Task HandleEventAsync(UserLoggedInEto eventData)
public virtual async Task HandleEventAsync(UserLoggedInEto eventData)
{
if (eventData == null)
{
Expand Down Expand Up @@ -59,8 +68,15 @@ protected virtual async Task CreateCurrentUserAsync(UserLoggedInEto userInfo)
// This should run once to sync the admin userIds that seeded by IdentityModule and the Keycloak admin
if (userInfo.UserName == "admin")
{
var adminUser = await _userManager.FindByNameAsync("admin");
await _userManager.DeleteAsync(adminUser);
var normalizedName = _lookupNormalizer.NormalizeName(userInfo.UserName);
var adminUser = await _identityUserRepository.FindByNormalizedUserNameAsync(normalizedName);
foreach (var role in adminUser.Roles.ToList())
{
user.AddRole(role.RoleId);
}

// Hard delete the admin-user
await _identityUserRepository.HardDeleteAsync(adminUser, true);
}

var result = await _userManager.CreateAsync(user);
Expand Down

0 comments on commit 936ab2b

Please sign in to comment.