1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-26 12:55:17 +01:00

PS-1806 - fix joins on some ef queries (#2421)

* fix joins on some ef queries

* fix formatting
This commit is contained in:
Kyle Spearrin 2022-11-18 11:17:46 -05:00 committed by GitHub
parent 02e4b10ae8
commit 47c8f043e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 18 deletions

View File

@ -23,31 +23,39 @@ public class CipherUpdateCollectionsQuery : IQuery<CollectionCipher>
}
var availibleCollections = !_cipher.UserId.HasValue ?
from c in dbContext.Collections
where c.OrganizationId == _cipher.OrganizationId
select c.Id :
from c in dbContext.Collections
join o in dbContext.Organizations
on c.OrganizationId equals o.Id
join ou in dbContext.OrganizationUsers
on o.Id equals ou.OrganizationId
where ou.UserId == _cipher.UserId
on new { OrganizationId = o.Id, _cipher.UserId } equals new { ou.OrganizationId, ou.UserId }
join cu in dbContext.CollectionUsers
on c.Id equals cu.CollectionId into cu_g
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()
where !ou.AccessAll && cu.OrganizationUserId == ou.Id
join gu in dbContext.GroupUsers
on ou.Id equals gu.OrganizationUserId into gu_g
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()
where cu.CollectionId == null && !ou.AccessAll
join g in dbContext.Groups
on gu.GroupId equals g.Id into g_g
from g in g_g.DefaultIfEmpty()
join cg in dbContext.CollectionGroups
on c.Id equals cg.CollectionId into cg_g
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 !g.AccessAll && gu.GroupId == cg.GroupId &&
o.Id == _cipher.OrganizationId &&
where o.Id == _cipher.OrganizationId &&
o.Enabled &&
ou.Status == OrganizationUserStatusType.Confirmed &&
(ou.AccessAll || !cu.ReadOnly || g.AccessAll || !cg.ReadOnly)

View File

@ -16,29 +16,33 @@ public class UserBumpAccountRevisionDateByCipherIdQuery : IQuery<User>
public IQueryable<User> Run(DatabaseContext dbContext)
{
var query = from u in dbContext.Users
join ou in dbContext.OrganizationUsers
on u.Id equals ou.UserId
join collectionCipher in dbContext.CollectionCiphers
on _cipher.Id equals collectionCipher.CipherId into cc_g
from cc in cc_g.DefaultIfEmpty()
join collectionUser in dbContext.CollectionUsers
on cc.CollectionId equals collectionUser.CollectionId into cu_g
on new { ou.AccessAll, OrganizationUserId = ou.Id, cc.CollectionId } equals
new { AccessAll = false, collectionUser.OrganizationUserId, collectionUser.CollectionId } into cu_g
from cu in cu_g.DefaultIfEmpty()
where ou.AccessAll &&
cu.OrganizationUserId == ou.Id
join groupUser in dbContext.GroupUsers
on ou.Id equals groupUser.OrganizationUserId into gu_g
on new { CollectionId = (Guid?)cu.CollectionId, ou.AccessAll, OrganizationUserId = ou.Id } equals
new { CollectionId = (Guid?)null, AccessAll = false, groupUser.OrganizationUserId } into gu_g
from gu in gu_g.DefaultIfEmpty()
where cu.CollectionId == null &&
!ou.AccessAll
join grp in dbContext.Groups
on gu.GroupId equals grp.Id into g_g
from g in g_g.DefaultIfEmpty()
join collectionGroup in dbContext.CollectionGroups
on cc.CollectionId equals collectionGroup.CollectionId into cg_g
on new { g.AccessAll, gu.GroupId, cc.CollectionId } equals
new { AccessAll = false, collectionGroup.GroupId, collectionGroup.CollectionId } into cg_g
from cg in cg_g.DefaultIfEmpty()
where !g.AccessAll &&
cg.GroupId == gu.GroupId
where ou.OrganizationId == _cipher.OrganizationId &&
ou.Status == OrganizationUserStatusType.Confirmed &&
(cu.CollectionId != null ||