Skip to content

Commit

Permalink
update logic set role
Browse files Browse the repository at this point in the history
  • Loading branch information
meofiscoding committed Nov 17, 2023
1 parent d4a1d62 commit 6ac2539
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Identity.API/Controllers/AuthController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public async Task<IActionResult> Register([FromBody] RegisterViewModel vm)
};

var result = await _userManager.CreateAsync(user, vm.Password);
//await _userManager.AddToRoleAsync(user, UserRoles.User);
await _userManager.AddToRoleAsync(user, UserRoles.User);

if (!result.Succeeded)
{
Expand Down
1 change: 1 addition & 0 deletions Identity.API/Entity/UserRoles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace Identity.API.Entity
public static class UserRoles
{
public const string Member = "Member";
public const string User = "User";
}
}

14 changes: 11 additions & 3 deletions Identity.API/SeedData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,18 @@ await retryPolicy.ExecuteAsync(async () =>
context.Database.Migrate();

var roleMgr = scope.ServiceProvider.GetRequiredService<RoleManager<IdentityRole>>();
// create roles member if not exists
if (!await roleMgr.RoleExistsAsync(UserRoles.Member))
// for each const string in UserRoles, create a role if it doesn't exist
foreach (var role in (List<string>)typeof(UserRoles).GetFields().Select(x => x.GetValue(null).ToString()).ToList())
{
await roleMgr.CreateAsync(new IdentityRole(UserRoles.Member));
var roleExist = await roleMgr.RoleExistsAsync(role);
if (!roleExist)
{
var result = await roleMgr.CreateAsync(new IdentityRole(role));
if (!result.Succeeded)
{
throw new Exception(result.Errors.First().Description);
}
}
}

var userMgr = scope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>();
Expand Down
23 changes: 21 additions & 2 deletions Identity.API/Service/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,27 @@ public override async Task<PaymentResponse> UpdateUserMembership(PaymentRequest
};
}

var result = request.Success ? await _userManager.AddToRoleAsync(user, "PayingUser") : await _userManager.RemoveFromRoleAsync(user, "PayingUser");

var result = new IdentityResult();
if (request.Success)
{
// if user have role user, then add role user
var oldRole = await _userManager.GetRolesAsync(user);
if (oldRole.Contains(UserRoles.User))
{
await _userManager.RemoveFromRoleAsync(user, UserRoles.User);
}
result = await _userManager.AddToRoleAsync(user, UserRoles.Member);
}
else
{
// if have role member, then remove role member
var oldRole = await _userManager.GetRolesAsync(user);
if (oldRole.Contains(UserRoles.Member))
{
await _userManager.RemoveFromRoleAsync(user, UserRoles.Member);
}
result = await _userManager.AddToRoleAsync(user, UserRoles.User);
}
return new PaymentResponse
{
Success = result.Succeeded,
Expand Down

0 comments on commit 6ac2539

Please sign in to comment.