1
0
mirror of https://github.com/bitwarden/server.git synced 2025-04-05 13:08:17 -05:00

[PS-1806] Fix EF LEFT JOIN's on multiple values (#2389)

* Fix OrganizationUserOrganizationDetails

* Rewrote query to be equal with SQL Server implementation

* Fix OrganizationUserUserView

* Rewrote query to be equal to SQL Server implementation

* Formatting
This commit is contained in:
Justin Baur 2022-11-08 10:01:32 -05:00 committed by GitHub
parent 3e092be55c
commit c222562b6f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 49 deletions

View File

@ -7,58 +7,58 @@ public class OrganizationUserOrganizationDetailsViewQuery : IQuery<OrganizationU
public IQueryable<OrganizationUserOrganizationDetails> Run(DatabaseContext dbContext) public IQueryable<OrganizationUserOrganizationDetails> Run(DatabaseContext dbContext)
{ {
var query = from ou in dbContext.OrganizationUsers var query = from ou in dbContext.OrganizationUsers
join o in dbContext.Organizations on ou.OrganizationId equals o.Id join o in dbContext.Organizations on ou.OrganizationId equals o.Id into outerOrganization
join su in dbContext.SsoUsers on ou.UserId equals su.UserId into su_g from o in outerOrganization.DefaultIfEmpty()
join su in dbContext.SsoUsers on new { ou.UserId, OrganizationId = (Guid?)ou.OrganizationId } equals new { UserId = (Guid?)su.UserId, su.OrganizationId } into su_g
from su in su_g.DefaultIfEmpty() from su in su_g.DefaultIfEmpty()
join po in dbContext.ProviderOrganizations on o.Id equals po.OrganizationId into po_g join po in dbContext.ProviderOrganizations on o.Id equals po.OrganizationId into po_g
from po in po_g.DefaultIfEmpty() from po in po_g.DefaultIfEmpty()
join p in dbContext.Providers on po.ProviderId equals p.Id into p_g join p in dbContext.Providers on po.ProviderId equals p.Id into p_g
from p in p_g.DefaultIfEmpty() from p in p_g.DefaultIfEmpty()
join os in dbContext.OrganizationSponsorships on ou.Id equals os.SponsoringOrganizationUserId into os_g
from os in os_g.DefaultIfEmpty()
join ss in dbContext.SsoConfigs on ou.OrganizationId equals ss.OrganizationId into ss_g join ss in dbContext.SsoConfigs on ou.OrganizationId equals ss.OrganizationId into ss_g
from ss in ss_g.DefaultIfEmpty() from ss in ss_g.DefaultIfEmpty()
where ((su == null || !su.OrganizationId.HasValue) || su.OrganizationId == ou.OrganizationId) join os in dbContext.OrganizationSponsorships on ou.Id equals os.SponsoringOrganizationUserId into os_g
select new { ou, o, su, p, ss, os }; from os in os_g.DefaultIfEmpty()
select new OrganizationUserOrganizationDetails
return query.Select(x => new OrganizationUserOrganizationDetails
{ {
OrganizationId = x.ou.OrganizationId, UserId = ou.UserId,
UserId = x.ou.UserId, OrganizationId = ou.OrganizationId,
Name = x.o.Name, Name = o.Name,
Enabled = x.o.Enabled, Enabled = o.Enabled,
PlanType = x.o.PlanType, PlanType = o.PlanType,
UsePolicies = x.o.UsePolicies, UsePolicies = o.UsePolicies,
UseSso = x.o.UseSso, UseSso = o.UseSso,
UseKeyConnector = x.o.UseKeyConnector, UseKeyConnector = o.UseKeyConnector,
UseScim = x.o.UseScim, UseScim = o.UseScim,
UseGroups = x.o.UseGroups, UseGroups = o.UseGroups,
UseDirectory = x.o.UseDirectory, UseDirectory = o.UseDirectory,
UseEvents = x.o.UseEvents, UseEvents = o.UseEvents,
UseTotp = x.o.UseTotp, UseTotp = o.UseTotp,
Use2fa = x.o.Use2fa, Use2fa = o.Use2fa,
UseApi = x.o.UseApi, UseApi = o.UseApi,
SelfHost = x.o.SelfHost, UseResetPassword = o.UseResetPassword,
UsersGetPremium = x.o.UsersGetPremium, SelfHost = o.SelfHost,
Seats = x.o.Seats, UsersGetPremium = o.UsersGetPremium,
MaxCollections = x.o.MaxCollections, Seats = o.Seats,
MaxStorageGb = x.o.MaxStorageGb, MaxCollections = o.MaxCollections,
Identifier = x.o.Identifier, MaxStorageGb = o.MaxStorageGb,
Key = x.ou.Key, Identifier = o.Identifier,
ResetPasswordKey = x.ou.ResetPasswordKey, Key = ou.Key,
Status = x.ou.Status, ResetPasswordKey = ou.ResetPasswordKey,
Type = x.ou.Type, PublicKey = o.PublicKey,
SsoExternalId = x.su.ExternalId, PrivateKey = o.PrivateKey,
Permissions = x.ou.Permissions, Status = ou.Status,
PublicKey = x.o.PublicKey, Type = ou.Type,
PrivateKey = x.o.PrivateKey, SsoExternalId = su.ExternalId,
ProviderId = x.p.Id, Permissions = ou.Permissions,
ProviderName = x.p.Name, ProviderId = p.Id,
SsoConfig = x.ss.Data, ProviderName = p.Name,
FamilySponsorshipFriendlyName = x.os.FriendlyName, SsoConfig = ss.Data,
FamilySponsorshipLastSyncDate = x.os.LastSyncDate, FamilySponsorshipFriendlyName = os.FriendlyName,
FamilySponsorshipToDelete = x.os.ToDelete, FamilySponsorshipLastSyncDate = os.LastSyncDate,
FamilySponsorshipValidUntil = x.os.ValidUntil FamilySponsorshipToDelete = os.ToDelete,
}); FamilySponsorshipValidUntil = os.ValidUntil
};
return query;
} }
} }

View File

@ -9,14 +9,14 @@ public class OrganizationUserUserDetailsViewQuery : IQuery<OrganizationUserUserD
var query = from ou in dbContext.OrganizationUsers var query = from ou in dbContext.OrganizationUsers
join u in dbContext.Users on ou.UserId equals u.Id into u_g join u in dbContext.Users on ou.UserId equals u.Id into u_g
from u in u_g.DefaultIfEmpty() from u in u_g.DefaultIfEmpty()
join su in dbContext.SsoUsers on u.Id equals su.UserId into su_g join su in dbContext.SsoUsers on new { ou.UserId, OrganizationId = (Guid?)ou.OrganizationId } equals new { UserId = (Guid?)su.UserId, su.OrganizationId } into su_g
from su in su_g.DefaultIfEmpty() from su in su_g.DefaultIfEmpty()
select new { ou, u, su }; select new { ou, u, su };
return query.Select(x => new OrganizationUserUserDetails return query.Select(x => new OrganizationUserUserDetails
{ {
Id = x.ou.Id, Id = x.ou.Id,
OrganizationId = x.ou.OrganizationId,
UserId = x.ou.UserId, UserId = x.ou.UserId,
OrganizationId = x.ou.OrganizationId,
Name = x.u.Name, Name = x.u.Name,
Email = x.u.Email ?? x.ou.Email, Email = x.u.Email ?? x.ou.Email,
TwoFactorProviders = x.u.TwoFactorProviders, TwoFactorProviders = x.u.TwoFactorProviders,
@ -28,7 +28,7 @@ public class OrganizationUserUserDetailsViewQuery : IQuery<OrganizationUserUserD
SsoExternalId = x.su.ExternalId, SsoExternalId = x.su.ExternalId,
Permissions = x.ou.Permissions, Permissions = x.ou.Permissions,
ResetPasswordKey = x.ou.ResetPasswordKey, ResetPasswordKey = x.ou.ResetPasswordKey,
UsesKeyConnector = x.u != null && x.u.UsesKeyConnector, UsesKeyConnector = x.u.UsesKeyConnector,
}); });
} }
} }