1
0
mirror of https://github.com/bitwarden/server synced 2025-12-24 12:13:17 +00:00

fix ef query joins (#2386)

This commit is contained in:
Kyle Spearrin
2022-11-07 12:08:30 -05:00
committed by GitHub
parent 8a6f780d55
commit 3e092be55c
4 changed files with 85 additions and 50 deletions

View File

@@ -15,27 +15,34 @@ public class CollectionCipherReadByUserIdQuery : IQuery<CollectionCipher>
public virtual IQueryable<CollectionCipher> Run(DatabaseContext dbContext)
{
var query = from cc in dbContext.CollectionCiphers
join c in dbContext.Collections
on cc.CollectionId equals c.Id
join ou in dbContext.OrganizationUsers
on c.OrganizationId equals ou.OrganizationId
where ou.UserId == _userId
on new { c.OrganizationId, UserId = (Guid?)_userId } equals
new { ou.OrganizationId, ou.UserId }
join cu in dbContext.CollectionUsers
on c.Id equals cu.CollectionId into cu_g
from cu in cu_g
where ou.AccessAll && cu.OrganizationUserId == ou.Id
on new { ou.AccessAll, CollectionId = c.Id, OrganizationUserId = ou.Id } equals
new { AccessAll = false, cu.CollectionId, cu.OrganizationUserId } into cu_g
from cu in cu_g.DefaultIfEmpty()
join gu in dbContext.GroupUsers
on ou.Id equals gu.OrganizationUserId into gu_g
from gu in gu_g
where cu.CollectionId == null && !ou.AccessAll
on new { CollectionId = (Guid?)cu.CollectionId, ou.AccessAll, OrganizationUserId = ou.Id } equals
new { CollectionId = (Guid?)null, AccessAll = false, gu.OrganizationUserId } into gu_g
from gu in gu_g.DefaultIfEmpty()
join g in dbContext.Groups
on gu.GroupId equals g.Id into g_g
from g in g_g
from g in g_g.DefaultIfEmpty()
join cg in dbContext.CollectionGroups
on cc.CollectionId equals cg.CollectionId into cg_g
from cg in cg_g
where g.AccessAll && cg.GroupId == gu.GroupId &&
ou.Status == OrganizationUserStatusType.Confirmed &&
on new { g.AccessAll, CollectionId = c.Id, gu.GroupId } equals
new { AccessAll = false, cg.CollectionId, cg.GroupId } into cg_g
from cg in cg_g.DefaultIfEmpty()
where ou.Status == OrganizationUserStatusType.Confirmed &&
(ou.AccessAll || cu.CollectionId != null || g.AccessAll || cg.CollectionId != null)
select cc;
return query;