From 1c6bd88c44893477f882e40cc9cf313138c752c1 Mon Sep 17 00:00:00 2001 From: Leon Date: Thu, 27 Jul 2023 15:59:09 +0800 Subject: [PATCH] Auto create roles when synchronize; --- .../Controllers/UserController.cs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/MicroserviceComponents/IdentityService/CleanMicroserviceSystem.Themis.WebAPI/Controllers/UserController.cs b/src/MicroserviceComponents/IdentityService/CleanMicroserviceSystem.Themis.WebAPI/Controllers/UserController.cs index 3b0bc1af..0bc30553 100644 --- a/src/MicroserviceComponents/IdentityService/CleanMicroserviceSystem.Themis.WebAPI/Controllers/UserController.cs +++ b/src/MicroserviceComponents/IdentityService/CleanMicroserviceSystem.Themis.WebAPI/Controllers/UserController.cs @@ -622,9 +622,22 @@ private async Task UpdateUserRoles(OceanusUser user, IEnumerable - this.roleManager.RoleExistsAsync(role).Result && !this.userManager.IsInRoleAsync(user, role).Result) .ToArray(); + foreach (var roleToAdd in rolesToAdd) + { + if (!(await this.roleManager.RoleExistsAsync(roleToAdd))) + { + var result = await this.roleManager.CreateAsync(new OceanusRole() { Name = roleToAdd }); + if (!result.Succeeded) + { + foreach (var error in result.Errors) + { + commonResult.Errors.Add(new CommonResultError(error.Code, error.Description)); + } + } + } + } if (rolesToAdd.Any()) { var result = await this.userManager.AddToRolesAsync(user, rolesToAdd);