1
0
mirror of https://github.com/bitwarden/server.git synced 2025-07-04 01:22:50 -05:00

[AC-2730] Remove AccessAll - CollectionCipher (#4468)

Final removal of AccessAll logic in CollectionCipher sprocs. We had v2 sprocs already
containing this updated logic that were never used; copy the v2 logic back to the 
original sprocs so that we start using it. v2 sprocs will be dropped later.
This commit is contained in:
Thomas Rittson
2024-07-09 09:39:24 +10:00
committed by GitHub
parent 3f443ac49b
commit 202dce3459
7 changed files with 241 additions and 132 deletions

View File

@ -1,5 +1,4 @@
using AutoMapper;
using Bit.Core.Enums;
using Bit.Core.Repositories;
using Bit.Infrastructure.EntityFramework.Repositories.Queries;
using Microsoft.EntityFrameworkCore;
@ -81,36 +80,10 @@ public class CollectionCipherRepository : BaseEntityFrameworkRepository, ICollec
.Select(c => c.OrganizationId)
.FirstAsync();
List<Guid> availableCollections;
// TODO AC-1375: use the query below to remove AccessAll from this method
// var availableCollectionsQuery = new CollectionsReadByOrganizationIdUserIdQuery(organizationId, userId);
// availableCollections = await availableCollectionsQuery
// .Run(dbContext)
// .Select(c => c.Id).ToListAsync();
availableCollections = await (from c in dbContext.Collections
join o in dbContext.Organizations on c.OrganizationId equals o.Id
join ou in dbContext.OrganizationUsers
on new { OrganizationId = o.Id, UserId = (Guid?)userId } equals
new { ou.OrganizationId, ou.UserId }
join cu in dbContext.CollectionUsers
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 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.DefaultIfEmpty()
join cg in dbContext.CollectionGroups
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 o.Id == organizationId && o.Enabled && ou.Status == OrganizationUserStatusType.Confirmed
&& (ou.AccessAll || !cu.ReadOnly || g.AccessAll || !cg.ReadOnly)
select c.Id).ToListAsync();
var availableCollectionsQuery = new CollectionsReadByOrganizationIdUserIdQuery(organizationId, userId);
var availableCollections = await availableCollectionsQuery
.Run(dbContext)
.Select(c => c.Id).ToListAsync();
var collectionCiphers = await (from cc in dbContext.CollectionCiphers
where cc.CipherId == cipherId
@ -190,37 +163,9 @@ public class CollectionCipherRepository : BaseEntityFrameworkRepository, ICollec
{
var dbContext = GetDatabaseContext(scope);
IQueryable<Models.Collection> availableCollections;
// TODO AC-1375: use the query below to remove AccessAll from this method
// var availableCollectionsQuery = new CollectionsReadByOrganizationIdUserIdQuery(organizationId, userId);
// availableCollections = availableCollectionsQuery
// .Run(dbContext);
availableCollections = 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 == userId
join cu in dbContext.CollectionUsers
on ou.Id equals cu.OrganizationUserId into cu_g
from cu in cu_g.DefaultIfEmpty()
where !ou.AccessAll && cu.CollectionId == c.Id
join gu in dbContext.GroupUsers
on ou.Id equals 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 gu.GroupId equals cg.GroupId into cg_g
from cg in cg_g.DefaultIfEmpty()
where !g.AccessAll && cg.CollectionId == c.Id &&
(o.Id == organizationId && o.Enabled && ou.Status == OrganizationUserStatusType.Confirmed &&
(ou.AccessAll || !cu.ReadOnly || g.AccessAll || !cg.ReadOnly))
select c;
var availableCollectionsQuery = new CollectionsReadByOrganizationIdUserIdQuery(organizationId, userId);
var availableCollections = availableCollectionsQuery
.Run(dbContext);
if (await availableCollections.CountAsync() < 1)
{