-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't assign role base claims when a role has full access
- Loading branch information
1 parent
067ad37
commit cfc94a7
Showing
12 changed files
with
121 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/OrchardCore/OrchardCore.Users.Core/Services/RoleClaimsProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using System.Security.Claims; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.Extensions.Options; | ||
using OrchardCore.Security; | ||
using OrchardCore.Users; | ||
using OrchardCore.Users.Services; | ||
|
||
namespace OrchardCore.Roles; | ||
|
||
public class RoleClaimsProvider : IUserClaimsProvider | ||
{ | ||
private readonly UserManager<IUser> _userManager; | ||
private readonly RoleManager<IRole> _roleManager; | ||
private readonly IdentityOptions _identityOptions; | ||
|
||
public RoleClaimsProvider( | ||
UserManager<IUser> userManager, | ||
RoleManager<IRole> roleManager, | ||
IOptions<IdentityOptions> identityOptions) | ||
{ | ||
_userManager = userManager; | ||
_roleManager = roleManager; | ||
_identityOptions = identityOptions.Value; | ||
} | ||
|
||
public async Task GenerateAsync(IUser user, ClaimsIdentity claims) | ||
{ | ||
if (!_userManager.SupportsUserRole) | ||
{ | ||
return; | ||
} | ||
|
||
var roleNames = await _userManager.GetRolesAsync(user); | ||
var roles = new List<IRole>(); | ||
|
||
foreach (var roleName in roleNames) | ||
{ | ||
claims.AddClaim(new Claim(_identityOptions.ClaimsIdentity.RoleClaimType, roleName)); | ||
|
||
if (!_roleManager.SupportsRoleClaims) | ||
{ | ||
continue; | ||
} | ||
|
||
var role = await _roleManager.FindByNameAsync(roleName); | ||
|
||
if (role == null) | ||
{ | ||
continue; | ||
} | ||
|
||
roles.Add(role); | ||
} | ||
|
||
if (roles.Count == 0 || roles.Any(x => x.HasFullAccess)) | ||
{ | ||
return; | ||
} | ||
|
||
foreach (var role in roles) | ||
{ | ||
claims.AddClaims(await _roleManager.GetClaimsAsync(role)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters