1
0
mirror of https://github.com/bitwarden/server.git synced 2025-04-05 05:00:19 -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 {
{ UserId = ou.UserId,
OrganizationId = x.ou.OrganizationId, OrganizationId = ou.OrganizationId,
UserId = x.ou.UserId, Name = o.Name,
Name = x.o.Name, Enabled = o.Enabled,
Enabled = x.o.Enabled, PlanType = o.PlanType,
PlanType = x.o.PlanType, UsePolicies = o.UsePolicies,
UsePolicies = x.o.UsePolicies, UseSso = o.UseSso,
UseSso = x.o.UseSso, UseKeyConnector = o.UseKeyConnector,
UseKeyConnector = x.o.UseKeyConnector, UseScim = o.UseScim,
UseScim = x.o.UseScim, UseGroups = o.UseGroups,
UseGroups = x.o.UseGroups, UseDirectory = o.UseDirectory,
UseDirectory = x.o.UseDirectory, UseEvents = o.UseEvents,
UseEvents = x.o.UseEvents, UseTotp = o.UseTotp,
UseTotp = x.o.UseTotp, Use2fa = o.Use2fa,
Use2fa = x.o.Use2fa, UseApi = o.UseApi,
UseApi = x.o.UseApi, UseResetPassword = o.UseResetPassword,
SelfHost = x.o.SelfHost, SelfHost = o.SelfHost,
UsersGetPremium = x.o.UsersGetPremium, UsersGetPremium = o.UsersGetPremium,
Seats = x.o.Seats, Seats = o.Seats,
MaxCollections = x.o.MaxCollections, MaxCollections = o.MaxCollections,
MaxStorageGb = x.o.MaxStorageGb, MaxStorageGb = o.MaxStorageGb,
Identifier = x.o.Identifier, Identifier = o.Identifier,
Key = x.ou.Key, Key = ou.Key,
ResetPasswordKey = x.ou.ResetPasswordKey, ResetPasswordKey = ou.ResetPasswordKey,
Status = x.ou.Status, PublicKey = o.PublicKey,
Type = x.ou.Type, PrivateKey = o.PrivateKey,
SsoExternalId = x.su.ExternalId, Status = ou.Status,
Permissions = x.ou.Permissions, Type = ou.Type,
PublicKey = x.o.PublicKey, SsoExternalId = su.ExternalId,
PrivateKey = x.o.PrivateKey, Permissions = ou.Permissions,
ProviderId = x.p.Id, ProviderId = p.Id,
ProviderName = x.p.Name, ProviderName = p.Name,
SsoConfig = x.ss.Data, SsoConfig = ss.Data,
FamilySponsorshipFriendlyName = x.os.FriendlyName, FamilySponsorshipFriendlyName = os.FriendlyName,
FamilySponsorshipLastSyncDate = x.os.LastSyncDate, FamilySponsorshipLastSyncDate = os.LastSyncDate,
FamilySponsorshipToDelete = x.os.ToDelete, FamilySponsorshipToDelete = os.ToDelete,
FamilySponsorshipValidUntil = x.os.ValidUntil 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,
}); });
} }
} }