Xabaril/Balea

Using implicit joins in FindAuthorizationAsync

mentaldriller opened this issue · 0 comments

In method FindAuthorizationSync in Balea.EntityFramework.Store, I found that using Joins via "Include" method causes performance issues in the resulting query, especially when there is intensive data reads. My suggestion is to modify the method to use implicit joins as follows:

public async Task<AuthorizationContext> FindAuthorizationAsync(ClaimsPrincipal user, CancellationToken cancellationToken = default) { var sourceRoleClaims = user.GetClaimValues(_options.DefaultClaimTypeMap.RoleClaimType); var delegation = await _context.Delegations.GetCurrentDelegation( user.GetUserId(_options), _options.ApplicationName, cancellationToken); var subject = GetSubject(user, delegation); var roles = await _context.Roles .AsNoTracking() .Where(role => role.Application.Name == _options.ApplicationName && role.Enabled && (role.Users.Any(rs => rs.User.Sub == subject) || role.Mappings.Any(rm => sourceRoleClaims.Contains(rm.Mapping.Name))) ).Select(r => new Role(r.Name, r.Description, r.Users.Select(rs => rs.User.Sub), r.Mappings.Select(rm => rm.Mapping.Name), r.Permissions.Select(rp => rp.Permission.Value), r.Enabled)) .ToListAsync(cancellationToken); return new AuthorizationContext(roles, delegation.To()); }