Skip to content

Commit

Permalink
Auto create roles when synchronize;
Browse files Browse the repository at this point in the history
  • Loading branch information
CuteLeon committed Jul 27, 2023
1 parent 2bd29eb commit 1c6bd88
Showing 1 changed file with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -622,9 +622,22 @@ private async Task<CommonResult> UpdateUserRoles(OceanusUser user, IEnumerable<s
var rolesToAdd = requestRoleSet
.Except(existingRoleSet)
.Where(role =>
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);
Expand Down

0 comments on commit 1c6bd88

Please sign in to comment.