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

Implement proposal Improve authorization performance #1046 #1169

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TRe
{
foreach (var role in roles)
{
var isInRole = await _identityService.IsInRoleAsync(_user.Id, role.Trim());
var isInRole = _user.Roles?.Any(x => role == x)??false;
if (isInRole)
{
authorized = true;
Expand Down
2 changes: 2 additions & 0 deletions src/Application/Common/Interfaces/IUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
public interface IUser
{
string? Id { get; }
List<string>? Roles { get; }

}
2 changes: 2 additions & 0 deletions src/Web/Services/CurrentUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ public CurrentUser(IHttpContextAccessor httpContextAccessor)
}

public string? Id => _httpContextAccessor.HttpContext?.User?.FindFirstValue(ClaimTypes.NameIdentifier);
public List<string>? Roles => _httpContextAccessor.HttpContext?.User?.FindAll(ClaimTypes.Role).Select(x => x.Value).ToList();

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)
{
services
.RemoveAll<IUser>()
.AddTransient(provider => Mock.Of<IUser>(s => s.Id == GetUserId()));

.AddTransient(provider =>
{
var mock = new Mock<IUser>();
mock.SetupGet(x => x.Roles).Returns(GetRoles());
mock.SetupGet(x => x.Id).Returns(GetUserId());
return mock.Object;
});
services
.RemoveAll<DbContextOptions<ApplicationDbContext>>()
.AddDbContext<ApplicationDbContext>((sp, options) =>
Expand Down
9 changes: 7 additions & 2 deletions tests/Application.FunctionalTests/Testing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public partial class Testing
private static CustomWebApplicationFactory _factory = null!;
private static IServiceScopeFactory _scopeFactory = null!;
private static string? _userId;

private static List<string>? _roles;
[OneTimeSetUp]
public async Task RunBeforeAnyTests()
{
Expand Down Expand Up @@ -48,6 +48,11 @@ public static async Task SendAsync(IBaseRequest request)
{
return _userId;
}

public static List<string>? GetRoles()
{
return _roles;
}

public static async Task<string> RunAsDefaultUserAsync()
{
Expand Down Expand Up @@ -84,7 +89,7 @@ public static async Task<string> RunAsUserAsync(string userName, string password
if (result.Succeeded)
{
_userId = user.Id;

_roles = roles.ToList();
return _userId;
}

Expand Down
Loading