Browse Source

Fix update passowrd in UpdateAsync IdentityUserAppService (sync with keycloak)

fix issue with updating the password (it will be updated only in the identity user)
pull/262/head
Ali Hassan 2 years ago
parent
commit
dc2e7b9880
  1. 6
      services/identity/src/EShopOnAbp.IdentityService.Application/BackgroundJobs/Users/KeycloakUserUpdatingJob.cs
  2. 5
      services/identity/src/EShopOnAbp.IdentityService.Application/Identity/EShopIdentityUserAppService.cs
  3. 2
      services/identity/src/EShopOnAbp.IdentityService.Application/Keycloak/Service/IKeycloakService.cs
  4. 25
      services/identity/src/EShopOnAbp.IdentityService.Application/Keycloak/Service/KeycloakService.cs

6
services/identity/src/EShopOnAbp.IdentityService.Application/BackgroundJobs/Users/KeycloakUserUpdatingJob.cs

@ -92,6 +92,11 @@ public class KeycloakUserUpdatingJob : AsyncBackgroundJob<IdentityUserUpdatingAr
_logger.LogInformation($"Keycloak user with the username:{args.UserName} has been updated.");
}
}
if (!args.Password.IsNullOrEmpty() && keycloakUser != null)
{
await _keycloakService.SetNewPassword(keycloakUser.UserName, args.Password);
}
}
catch (Exception e)
{
@ -116,6 +121,7 @@ public class IdentityUserUpdatingArgs
public bool OldIsActive { get; init; }
public string[] RoleNames { get; init; }
public string[] OldRoleNames { get; init; }
public string Password { get; init; }
public IEnumerable<FieldChange> GetDifferentFields()
{

5
services/identity/src/EShopOnAbp.IdentityService.Application/Identity/EShopIdentityUserAppService.cs

@ -73,7 +73,7 @@ public class EShopIdentityUserAppService : IdentityUserAppService
{
var userRoles = existingUser.Roles.Select(q => q.RoleId).ToList();
var roles = await _roleRepository.GetListAsync();
var args = new IdentityUserUpdatingArgs
{
Email = input.Email,
@ -87,7 +87,8 @@ public class EShopIdentityUserAppService : IdentityUserAppService
IsActive = input.IsActive,
OldIsActive = existingUser.IsActive,
RoleNames = input.RoleNames,
OldRoleNames = roles.Where(q => userRoles.Contains(q.Id)).Select(q => q.Name).ToArray()
OldRoleNames = roles.Where(q => userRoles.Contains(q.Id)).Select(q => q.Name).ToArray(),
Password = input.Password
};
return args;

2
services/identity/src/EShopOnAbp.IdentityService.Application/Keycloak/Service/IKeycloakService.cs

@ -33,4 +33,6 @@ public interface IKeycloakService : ITransientDependency
Task<bool> DeleteRoleByIdAsync(string id, CancellationToken cancellationToken = default);
Task<bool> UpdateRoleAsync(string id, Role role, CancellationToken cancellationToken = default);
Task<bool> SetNewPassword(string username, string newPassword, CancellationToken cancellationToken = default);
}

25
services/identity/src/EShopOnAbp.IdentityService.Application/Keycloak/Service/KeycloakService.cs

@ -158,4 +158,29 @@ public class KeycloakService : IKeycloakService
return result;
}
public async Task<bool> SetNewPassword(string username, string newPassword, CancellationToken cancellationToken = default)
{
var users = await _keycloakClient.GetUsersAsync(_keycloakOptions.RealmName, username: username, cancellationToken: cancellationToken);
if (!users.Any()) return false;
var user = users.First();
var newCredentials = new Credentials
{
Type = "password",
Value = newPassword,
Temporary = false
};
var isSuccessReset = await _keycloakClient.ResetUserPasswordAsync(
realm: _keycloakOptions.RealmName,
userId: user.Id,
credentials: newCredentials,
cancellationToken: cancellationToken
);
return isSuccessReset;
}
}
Loading…
Cancel
Save