Browse Source

Added backgroundjobs for keycloak sync

pull/180/head
Galip Tolga Erdem 3 years ago
parent
commit
2bd2e6d5dd
  1. 46
      services/identity/src/EShopOnAbp.IdentityService.Application/BackgroundJobs/User/KeycloakUserCreationJob.cs
  2. 26
      services/identity/src/EShopOnAbp.IdentityService.Application/BackgroundJobs/User/KeycloakUserDeletionJob.cs
  3. 25
      services/identity/src/EShopOnAbp.IdentityService.Application/BackgroundJobs/User/KeycloakUserUpdatingJob.cs

46
services/identity/src/EShopOnAbp.IdentityService.Application/BackgroundJobs/KeycloakUserCreationJob.cs → services/identity/src/EShopOnAbp.IdentityService.Application/BackgroundJobs/User/KeycloakUserCreationJob.cs

@ -1,43 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Keycloak.Net;
using Keycloak.Net.Models.Users;
using EShopOnAbp.IdentityService.Keycloak;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Volo.Abp.BackgroundJobs;
using Volo.Abp.DependencyInjection;
using Keycloak.Net.Models.Users;
namespace EShopOnAbp.IdentityService.BackgroundJobs;
namespace EShopOnAbp.IdentityService.BackgroundJobs.User;
public class KeycloakUserCreationJob : AsyncBackgroundJob<IdentityUserCreationArgs>, ITransientDependency
{
private readonly KeycloakClient _keycloakClient;
private readonly KeycloakClientOptions _keycloakOptions;
private readonly ILogger<KeycloakUserCreationJob> _logger;
private readonly KeycloakService _keycloakService;
private readonly ILogger _logger;
public KeycloakUserCreationJob(IOptions<KeycloakClientOptions> keycloakOptions,
public KeycloakUserCreationJob(KeycloakService keycloakService,
ILogger<KeycloakUserCreationJob> logger)
{
_logger = logger;
_keycloakOptions = keycloakOptions.Value;
_keycloakClient = new KeycloakClient(
_keycloakOptions.Url,
_keycloakOptions.AdminUserName,
_keycloakOptions.AdminPassword
);
_keycloakService = keycloakService;
}
public override async Task ExecuteAsync(IdentityUserCreationArgs args)
{
var keycloakUser = new User
var keycloakUser = new global::Keycloak.Net.Models.Users.User()
{
Email = args.Email,
UserName = args.UserName,
FirstName = args.Name,
LastName = args.Surname,
Enabled = true,
Enabled = args.IsActive,
Credentials = new List<Credentials>()
{
new() { Type = "password", Value = args.Password }
@ -46,9 +39,14 @@ public class KeycloakUserCreationJob : AsyncBackgroundJob<IdentityUserCreationAr
try
{
var result = await _keycloakClient.CreateUserAsync(_keycloakOptions.RealmName, keycloakUser);
var result = await _keycloakService.CreateUserAsync(keycloakUser);
if (result)
{
if (args.RoleNames.Length != 0)
{
await AddRolesToKeycloakUserAsync(keycloakUser.UserName, args.RoleNames);
}
_logger.LogInformation($"Keycloak user with the username:{args.UserName} has been created.");
}
}
@ -58,6 +56,16 @@ public class KeycloakUserCreationJob : AsyncBackgroundJob<IdentityUserCreationAr
throw;
}
}
private async Task AddRolesToKeycloakUserAsync(string userName, string[] roleNames)
{
var user = (await _keycloakService.GetUsersAsync(username: userName)).First();
var allTheRoles = await _keycloakService.GetRolesAsync();
var roles = allTheRoles.Where(q => roleNames.Contains(q.Name));
await _keycloakService.AddRolesToUserAsync(user.Id, roles);
_logger.LogInformation($"Keycloak roles:{roleNames} has been added to user with the username:{userName}.");
}
}
public class IdentityUserCreationArgs
@ -67,4 +75,6 @@ public class IdentityUserCreationArgs
public string Name { get; set; }
public string Surname { get; set; }
public string Password { get; set; }
public bool IsActive { get; set; }
public string[] RoleNames { get; set; }
}

26
services/identity/src/EShopOnAbp.IdentityService.Application/BackgroundJobs/IdentityUserDeletionJob.cs → services/identity/src/EShopOnAbp.IdentityService.Application/BackgroundJobs/User/KeycloakUserDeletionJob.cs

@ -1,39 +1,31 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Keycloak.Net;
using EShopOnAbp.IdentityService.Keycloak;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Volo.Abp;
using Volo.Abp.BackgroundJobs;
using Volo.Abp.DependencyInjection;
namespace EShopOnAbp.IdentityService.BackgroundJobs;
namespace EShopOnAbp.IdentityService.BackgroundJobs.User;
public class IdentityUserDeletionJob : AsyncBackgroundJob<IdentityUserDeletionArgs>, ITransientDependency
public class KeycloakUserDeletionJob : AsyncBackgroundJob<IdentityUserDeletionArgs>, ITransientDependency
{
private readonly KeycloakClient _keycloakClient;
private readonly KeycloakClientOptions _keycloakOptions;
private readonly ILogger<KeycloakUserCreationJob> _logger;
private readonly KeycloakService _keycloakService;
private readonly ILogger _logger;
public IdentityUserDeletionJob(IOptions<KeycloakClientOptions> keycloakOptions,
public KeycloakUserDeletionJob(KeycloakService keycloakService,
ILogger<KeycloakUserCreationJob> logger)
{
_keycloakService = keycloakService;
_logger = logger;
_keycloakOptions = keycloakOptions.Value;
_keycloakClient = new KeycloakClient(
_keycloakOptions.Url,
_keycloakOptions.AdminUserName,
_keycloakOptions.AdminPassword
);
}
public override async Task ExecuteAsync(IdentityUserDeletionArgs args)
{
try
{
var keycloakUser = (await _keycloakClient.GetUsersAsync(_keycloakOptions.RealmName, username: args.UserName))
var keycloakUser = (await _keycloakService.GetUsersAsync(username: args.UserName))
.First();
if (keycloakUser == null)
{
@ -41,7 +33,7 @@ public class IdentityUserDeletionJob : AsyncBackgroundJob<IdentityUserDeletionAr
throw new UserFriendlyException($"Keycloak user with the username:{args.UserName} could not be found!");
}
var result = await _keycloakClient.DeleteUserAsync(_keycloakOptions.RealmName, keycloakUser.Id);
var result = await _keycloakService.DeleteUserAsync(keycloakUser.Id);
if (result)
{
_logger.LogInformation($"Keycloak user with the username:{args.UserName} has been deleted.");

25
services/identity/src/EShopOnAbp.IdentityService.Application/BackgroundJobs/KeycloakUserUpdatingJob.cs → services/identity/src/EShopOnAbp.IdentityService.Application/BackgroundJobs/User/KeycloakUserUpdatingJob.cs

@ -1,39 +1,32 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using EShopOnAbp.IdentityService.Keycloak;
using Keycloak.Net;
using Keycloak.Net.Models.Users;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Volo.Abp;
using Volo.Abp.BackgroundJobs;
using Volo.Abp.DependencyInjection;
namespace EShopOnAbp.IdentityService.BackgroundJobs;
namespace EShopOnAbp.IdentityService.BackgroundJobs.User;
public class KeycloakUserUpdatingJob : AsyncBackgroundJob<IdentityUserUpdatingArgs>, ITransientDependency
{
private readonly KeycloakClient _keycloakClient;
private readonly KeycloakClientOptions _keycloakOptions;
private readonly KeycloakService _keycloakService;
private readonly ILogger<KeycloakUserCreationJob> _logger;
public KeycloakUserUpdatingJob(IOptions<KeycloakClientOptions> keycloakOptions, ILogger<KeycloakUserCreationJob> logger)
public KeycloakUserUpdatingJob(KeycloakService keycloakService, ILogger<KeycloakUserCreationJob> logger)
{
_keycloakService = keycloakService;
_logger = logger;
_keycloakOptions = keycloakOptions.Value;
_keycloakClient = new KeycloakClient(
_keycloakOptions.Url,
_keycloakOptions.AdminUserName,
_keycloakOptions.AdminPassword
);
}
public override async Task ExecuteAsync(IdentityUserUpdatingArgs args)
{
try
{
var keycloakUser = (await _keycloakClient.GetUsersAsync(_keycloakOptions.RealmName, username: args.UserName))
var keycloakUser = (await _keycloakService.GetUsersAsync( username: args.UserName))
.First();
if (keycloakUser == null)
{
@ -45,9 +38,11 @@ public class KeycloakUserUpdatingJob : AsyncBackgroundJob<IdentityUserUpdatingAr
keycloakUser.Email = args.Email;
keycloakUser.FirstName = args.Name;
keycloakUser.LastName = args.Surname;
keycloakUser.Enabled = args.IsActive;
keycloakUser.EmailVerified = args.EmailConfirmed;
var result = await _keycloakClient.UpdateUserAsync(_keycloakOptions.RealmName, keycloakUser.Id, keycloakUser);
var result =
await _keycloakService.UpdateUserAsync( keycloakUser.Id, keycloakUser);
if (result)
{
_logger.LogInformation($"Keycloak user with the username:{args.UserName} has been updated.");
@ -67,4 +62,6 @@ public class IdentityUserUpdatingArgs
public string Name { get; set; }
public string Surname { get; set; }
public bool EmailConfirmed { get; set; }
public bool IsActive { get; set; }
public string[] RoleNames { get; set; }
}
Loading…
Cancel
Save