Browse Source

Added Keycloak role jobs

gterdem/swagger_update
Galip Tolga Erdem 3 years ago
parent
commit
4085d565ca
  1. 42
      services/identity/src/EShopOnAbp.IdentityService.Application/BackgroundJobs/Roles/KeycloakRoleCreationJob.cs
  2. 42
      services/identity/src/EShopOnAbp.IdentityService.Application/BackgroundJobs/Roles/KeycloakRoleDeletionJob.cs
  3. 45
      services/identity/src/EShopOnAbp.IdentityService.Application/BackgroundJobs/Roles/KeycloakRoleUpdatingJob.cs
  4. 51
      services/identity/src/EShopOnAbp.IdentityService.Application/Identity/EShopIdentityRoleAppService.cs
  5. 4
      services/identity/src/EShopOnAbp.IdentityService.Application/Identity/EShopIdentityUserAppService.cs
  6. 19
      services/identity/src/EShopOnAbp.IdentityService.Application/Keycloak/KeycloakService.cs
  7. 2
      shared/EShopOnAbp.Keycloak.DbMigrator/KeycloakDataSeeder.cs

42
services/identity/src/EShopOnAbp.IdentityService.Application/BackgroundJobs/Roles/KeycloakRoleCreationJob.cs

@ -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);

42
services/identity/src/EShopOnAbp.IdentityService.Application/BackgroundJobs/Roles/KeycloakRoleDeletionJob.cs

@ -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);

45
services/identity/src/EShopOnAbp.IdentityService.Application/BackgroundJobs/Roles/KeycloakRoleUpdatingJob.cs

@ -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);

51
services/identity/src/EShopOnAbp.IdentityService.Application/Identity/EShopIdentityRoleAppService.cs

@ -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));
}
}

4
services/identity/src/EShopOnAbp.IdentityService.Application/EShopIdentityUserAppService.cs → services/identity/src/EShopOnAbp.IdentityService.Application/Identity/EShopIdentityUserAppService.cs

@ -1,13 +1,13 @@
using System;
using System.Threading.Tasks;
using EShopOnAbp.IdentityService.BackgroundJobs.User;
using EShopOnAbp.IdentityService.BackgroundJobs.Users;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Options;
using Volo.Abp.BackgroundJobs;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Identity;
namespace EShopOnAbp.IdentityService;
namespace EShopOnAbp.IdentityService.Identity;
[ExposeServices(typeof(IdentityUserAppService), typeof(IIdentityUserAppService))]
public class EShopIdentityUserAppService : IdentityUserAppService

19
services/identity/src/EShopOnAbp.IdentityService.Application/Keycloak/KeycloakService.cs

@ -63,4 +63,23 @@ public class KeycloakService : ITransientDependency
return _keycloakClient.AddRealmRoleMappingsToUserAsync(_keycloakOptions.RealmName, userId, roles,
cancellationToken);
}
public Task<bool> CreateRoleAsync(string name, CancellationToken cancellationToken = default)
{
Role role = new Role
{
Name = name
};
return _keycloakClient.CreateRoleAsync(_keycloakOptions.RealmName, role, cancellationToken);
}
public Task<bool> DeleteRoleByIdAsync(string id, CancellationToken cancellationToken = default)
{
return _keycloakClient.DeleteRoleByIdAsync(_keycloakOptions.RealmName, id, cancellationToken);
}
public Task<bool> UpdateRoleAsync(string id, Role role, CancellationToken cancellationToken = default)
{
return _keycloakClient.UpdateRoleByIdAsync(_keycloakOptions.RealmName, id, role, cancellationToken);
}
}

2
shared/EShopOnAbp.Keycloak.DbMigrator/KeycloakDataSeeder.cs

@ -39,7 +39,7 @@ public class KeyCloakDataSeeder : IDataSeedContributor, ITransientDependency
{
await UpdateRealmSettingsAsync();
await UpdateAdminUserAsync();
await CreateRoleMapperAsync();
await CreateRoleMapperAsync(); // roles scope
await CreateClientScopesAsync();
await CreateClientsAsync();
}

Loading…
Cancel
Save