forked from tsai/eShopOnAbp
7 changed files with 202 additions and 3 deletions
@ -0,0 +1,42 @@ |
|||||
|
using System; |
||||
|
using System.Linq; |
||||
|
using System.Threading.Tasks; |
||||
|
using EShopOnAbp.IdentityService.Keycloak; |
||||
|
using Microsoft.Extensions.Logging; |
||||
|
using Volo.Abp.BackgroundJobs; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
|
namespace EShopOnAbp.IdentityService.BackgroundJobs.Roles; |
||||
|
|
||||
|
public class KeycloakRoleCreationJob : AsyncBackgroundJob<IdentityRoleCreationArgs>, ITransientDependency |
||||
|
{ |
||||
|
private readonly KeycloakService _keycloakService; |
||||
|
private readonly ILogger<KeycloakRoleCreationJob> _logger; |
||||
|
|
||||
|
public KeycloakRoleCreationJob(KeycloakService keycloakService, ILogger<KeycloakRoleCreationJob> logger) |
||||
|
{ |
||||
|
_keycloakService = keycloakService; |
||||
|
_logger = logger; |
||||
|
} |
||||
|
|
||||
|
public override async Task ExecuteAsync(IdentityRoleCreationArgs args) |
||||
|
{ |
||||
|
try |
||||
|
{ |
||||
|
var existingRole = (await _keycloakService.GetRolesAsync()).FirstOrDefault(q => q.Name == args.Name); |
||||
|
if (existingRole != null) |
||||
|
{ |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
await _keycloakService.CreateRoleAsync(args.Name); |
||||
|
} |
||||
|
catch (Exception e) |
||||
|
{ |
||||
|
_logger.LogWarning($"Keycloak role creation with the name:{args.Name} failed!"); |
||||
|
throw; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public record IdentityRoleCreationArgs(string Name); |
||||
@ -0,0 +1,42 @@ |
|||||
|
using System; |
||||
|
using System.Linq; |
||||
|
using System.Threading.Tasks; |
||||
|
using EShopOnAbp.IdentityService.Keycloak; |
||||
|
using Microsoft.Extensions.Logging; |
||||
|
using Volo.Abp.BackgroundJobs; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
|
namespace EShopOnAbp.IdentityService.BackgroundJobs.Roles; |
||||
|
|
||||
|
public class KeycloakRoleDeletionJob : AsyncBackgroundJob<IdentityRoleDeletionArgs>, ITransientDependency |
||||
|
{ |
||||
|
private readonly KeycloakService _keycloakService; |
||||
|
private readonly ILogger<KeycloakRoleDeletionJob> _logger; |
||||
|
|
||||
|
public KeycloakRoleDeletionJob(KeycloakService keycloakService, ILogger<KeycloakRoleDeletionJob> logger) |
||||
|
{ |
||||
|
_keycloakService = keycloakService; |
||||
|
_logger = logger; |
||||
|
} |
||||
|
|
||||
|
public override async Task ExecuteAsync(IdentityRoleDeletionArgs args) |
||||
|
{ |
||||
|
try |
||||
|
{ |
||||
|
var existingRole = (await _keycloakService.GetRolesAsync()).FirstOrDefault(q => q.Name == args.Name); |
||||
|
if (existingRole == null) |
||||
|
{ |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
await _keycloakService.DeleteRoleByIdAsync(existingRole.Id); |
||||
|
} |
||||
|
catch (Exception e) |
||||
|
{ |
||||
|
_logger.LogWarning($"Could not delete the role with the name:{args.Name} from Keycloak server!"); |
||||
|
throw; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public record IdentityRoleDeletionArgs(string Name); |
||||
@ -0,0 +1,45 @@ |
|||||
|
using System; |
||||
|
using System.Linq; |
||||
|
using System.Threading.Tasks; |
||||
|
using EShopOnAbp.IdentityService.Keycloak; |
||||
|
using Microsoft.Extensions.Logging; |
||||
|
using Volo.Abp.BackgroundJobs; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
|
namespace EShopOnAbp.IdentityService.BackgroundJobs.Roles; |
||||
|
|
||||
|
public class KeycloakRoleUpdatingJob : AsyncBackgroundJob<IdentityRoleUpdatingArgs>, ITransientDependency |
||||
|
{ |
||||
|
private readonly KeycloakService _keycloakService; |
||||
|
private readonly ILogger<KeycloakRoleUpdatingJob> _logger; |
||||
|
|
||||
|
public KeycloakRoleUpdatingJob(KeycloakService keycloakService, ILogger<KeycloakRoleUpdatingJob> logger) |
||||
|
{ |
||||
|
_keycloakService = keycloakService; |
||||
|
_logger = logger; |
||||
|
} |
||||
|
|
||||
|
public override async Task ExecuteAsync(IdentityRoleUpdatingArgs args) |
||||
|
{ |
||||
|
try |
||||
|
{ |
||||
|
var existingRole = (await _keycloakService.GetRolesAsync()).FirstOrDefault(q => q.Name == args.Name); |
||||
|
if (existingRole == null) |
||||
|
{ |
||||
|
_logger.LogWarning($"Role with the name:{args.Name} couldn't be found to update!"); |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
existingRole.Name = args.Name; |
||||
|
|
||||
|
await _keycloakService.UpdateRoleAsync(existingRole.Id, existingRole); |
||||
|
} |
||||
|
catch (Exception e) |
||||
|
{ |
||||
|
_logger.LogWarning($"Could not delete the role with the name:{args.Name} from Keycloak server!"); |
||||
|
throw; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public record IdentityRoleUpdatingArgs(string Name); |
||||
@ -0,0 +1,51 @@ |
|||||
|
using System; |
||||
|
using System.Threading.Tasks; |
||||
|
using EShopOnAbp.IdentityService.BackgroundJobs.Roles; |
||||
|
using Volo.Abp.BackgroundJobs; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
using Volo.Abp.Identity; |
||||
|
|
||||
|
namespace EShopOnAbp.IdentityService.Identity; |
||||
|
|
||||
|
[ExposeServices(typeof(IdentityRoleAppService), typeof(IIdentityRoleAppService))] |
||||
|
public class EShopIdentityRoleAppService : IdentityRoleAppService |
||||
|
{ |
||||
|
private readonly IBackgroundJobManager _backgroundJobManager; |
||||
|
private readonly IdentityRoleManager _identityRoleManager; |
||||
|
|
||||
|
public EShopIdentityRoleAppService(IdentityRoleManager roleManager, IIdentityRoleRepository roleRepository, |
||||
|
IBackgroundJobManager backgroundJobManager, IdentityRoleManager identityRoleManager) : base( |
||||
|
roleManager, roleRepository) |
||||
|
{ |
||||
|
_backgroundJobManager = backgroundJobManager; |
||||
|
_identityRoleManager = identityRoleManager; |
||||
|
} |
||||
|
|
||||
|
public override async Task<IdentityRoleDto> CreateAsync(IdentityRoleCreateDto input) |
||||
|
{ |
||||
|
var result = await base.CreateAsync(input); |
||||
|
await _backgroundJobManager.EnqueueAsync(new IdentityRoleCreationArgs(result.Name)); |
||||
|
|
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
public override async Task<IdentityRoleDto> UpdateAsync(Guid id, IdentityRoleUpdateDto input) |
||||
|
{ |
||||
|
var result = await base.UpdateAsync(id, input); |
||||
|
await _backgroundJobManager.EnqueueAsync(new IdentityRoleUpdatingArgs(result.Name)); |
||||
|
|
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
public override async Task DeleteAsync(Guid id) |
||||
|
{ |
||||
|
var existingRole = await _identityRoleManager.FindByIdAsync(id.ToString()); |
||||
|
await base.DeleteAsync(id); |
||||
|
if (existingRole == null) |
||||
|
{ |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
await _backgroundJobManager.EnqueueAsync(new IdentityRoleDeletionArgs(existingRole.Name)); |
||||
|
} |
||||
|
} |
||||
@ -1,13 +1,13 @@ |
|||||
using System; |
using System; |
||||
using System.Threading.Tasks; |
using System.Threading.Tasks; |
||||
using EShopOnAbp.IdentityService.BackgroundJobs.User; |
using EShopOnAbp.IdentityService.BackgroundJobs.Users; |
||||
using Microsoft.AspNetCore.Identity; |
using Microsoft.AspNetCore.Identity; |
||||
using Microsoft.Extensions.Options; |
using Microsoft.Extensions.Options; |
||||
using Volo.Abp.BackgroundJobs; |
using Volo.Abp.BackgroundJobs; |
||||
using Volo.Abp.DependencyInjection; |
using Volo.Abp.DependencyInjection; |
||||
using Volo.Abp.Identity; |
using Volo.Abp.Identity; |
||||
|
|
||||
namespace EShopOnAbp.IdentityService; |
namespace EShopOnAbp.IdentityService.Identity; |
||||
|
|
||||
[ExposeServices(typeof(IdentityUserAppService), typeof(IIdentityUserAppService))] |
[ExposeServices(typeof(IdentityUserAppService), typeof(IIdentityUserAppService))] |
||||
public class EShopIdentityUserAppService : IdentityUserAppService |
public class EShopIdentityUserAppService : IdentityUserAppService |
||||
Loading…
Reference in new issue