Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Role based access on admin panel #930

Merged
merged 3 commits into from
Dec 12, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Check if roles claim exist
Pål Bøckmann committed Dec 12, 2024
commit c4bcc0d4960a81b5965a1303b29c77789ec07019
Original file line number Diff line number Diff line change
@@ -15,17 +15,22 @@ public override async ValueTask<ClaimsPrincipal> CreateUserAsync(
var userAccount = await base.CreateUserAsync(account, options);
var userIdentity = userAccount.Identity as ClaimsIdentity;

if (userIdentity?.IsAuthenticated is true)
if (userIdentity?.IsAuthenticated is not true) return userAccount;

if (account.AdditionalProperties.TryGetValue(userIdentity.RoleClaimType, out var rolesObj) && rolesObj is JsonElement roles && roles.ValueKind == JsonValueKind.Array)
{
var roles = account.AdditionalProperties[userIdentity.RoleClaimType] as JsonElement?;

if (roles?.ValueKind == JsonValueKind.Array)
var roleClaim = userIdentity.Claims.FirstOrDefault(c => c.Type == userIdentity.RoleClaimType);
if (roleClaim != null)
{
userIdentity.TryRemoveClaim(userIdentity.Claims.FirstOrDefault(c => c.Type == userIdentity.RoleClaimType));
userIdentity.TryRemoveClaim(roleClaim);
}

foreach (var element in roles.Value.EnumerateArray())
foreach (var element in roles.EnumerateArray())
{
var role = element.GetString();
if (!string.IsNullOrEmpty(role))
{
userIdentity.AddClaim(new Claim(userIdentity.RoleClaimType, element.GetString()));
userIdentity.AddClaim(new Claim(userIdentity.RoleClaimType, role));
}
}
}