mirror of
https://github.com/bitwarden/server.git
synced 2026-01-31 14:13:18 +08:00
update collections admin proc and repo (#6374)
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
using AutoMapper;
|
using AutoMapper;
|
||||||
|
using Bit.Core.Enums;
|
||||||
using Bit.Core.Repositories;
|
using Bit.Core.Repositories;
|
||||||
using Bit.Infrastructure.EntityFramework.Repositories.Queries;
|
using Bit.Infrastructure.EntityFramework.Repositories.Queries;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
@@ -145,9 +146,11 @@ public class CollectionCipherRepository : BaseEntityFrameworkRepository, ICollec
|
|||||||
using (var scope = ServiceScopeFactory.CreateScope())
|
using (var scope = ServiceScopeFactory.CreateScope())
|
||||||
{
|
{
|
||||||
var dbContext = GetDatabaseContext(scope);
|
var dbContext = GetDatabaseContext(scope);
|
||||||
var availableCollections = await (from c in dbContext.Collections
|
|
||||||
|
var availableCollectionIds = await (from c in dbContext.Collections
|
||||||
where c.OrganizationId == organizationId
|
where c.OrganizationId == organizationId
|
||||||
select c).ToListAsync();
|
&& c.Type != CollectionType.DefaultUserCollection
|
||||||
|
select c.Id).ToListAsync();
|
||||||
|
|
||||||
var currentCollectionCiphers = await (from cc in dbContext.CollectionCiphers
|
var currentCollectionCiphers = await (from cc in dbContext.CollectionCiphers
|
||||||
where cc.CipherId == cipherId
|
where cc.CipherId == cipherId
|
||||||
@@ -155,6 +158,8 @@ public class CollectionCipherRepository : BaseEntityFrameworkRepository, ICollec
|
|||||||
|
|
||||||
foreach (var requestedCollectionId in collectionIds)
|
foreach (var requestedCollectionId in collectionIds)
|
||||||
{
|
{
|
||||||
|
if (!availableCollectionIds.Contains(requestedCollectionId)) continue;
|
||||||
|
|
||||||
var requestedCollectionCipher = currentCollectionCiphers
|
var requestedCollectionCipher = currentCollectionCiphers
|
||||||
.FirstOrDefault(cc => cc.CollectionId == requestedCollectionId);
|
.FirstOrDefault(cc => cc.CollectionId == requestedCollectionId);
|
||||||
|
|
||||||
@@ -168,7 +173,7 @@ public class CollectionCipherRepository : BaseEntityFrameworkRepository, ICollec
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
dbContext.RemoveRange(currentCollectionCiphers.Where(cc => !collectionIds.Contains(cc.CollectionId)));
|
dbContext.RemoveRange(currentCollectionCiphers.Where(cc => availableCollectionIds.Contains(cc.CollectionId) && !collectionIds.Contains(cc.CollectionId)));
|
||||||
await dbContext.UserBumpAccountRevisionDateByOrganizationIdAsync(organizationId);
|
await dbContext.UserBumpAccountRevisionDateByOrganizationIdAsync(organizationId);
|
||||||
await dbContext.SaveChangesAsync();
|
await dbContext.SaveChangesAsync();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,46 +4,52 @@
|
|||||||
@CollectionIds AS [dbo].[GuidIdArray] READONLY
|
@CollectionIds AS [dbo].[GuidIdArray] READONLY
|
||||||
AS
|
AS
|
||||||
BEGIN
|
BEGIN
|
||||||
SET NOCOUNT ON
|
SET NOCOUNT ON;
|
||||||
|
|
||||||
;WITH [AvailableCollectionsCTE] AS(
|
-- Available collections for this org, excluding default collections
|
||||||
SELECT
|
SELECT
|
||||||
Id
|
C.[Id]
|
||||||
FROM
|
INTO #TempAvailableCollections
|
||||||
[dbo].[Collection]
|
FROM [dbo].[Collection] AS C
|
||||||
WHERE
|
WHERE
|
||||||
OrganizationId = @OrganizationId
|
C.[OrganizationId] = @OrganizationId
|
||||||
),
|
AND C.[Type] <> 1; -- exclude DefaultUserCollection
|
||||||
[CollectionCiphersCTE] AS(
|
|
||||||
SELECT
|
-- Insert new collection assignments
|
||||||
|
INSERT INTO [dbo].[CollectionCipher] (
|
||||||
[CollectionId],
|
[CollectionId],
|
||||||
[CipherId]
|
[CipherId]
|
||||||
FROM
|
|
||||||
[dbo].[CollectionCipher]
|
|
||||||
WHERE
|
|
||||||
[CipherId] = @CipherId
|
|
||||||
)
|
)
|
||||||
MERGE
|
SELECT
|
||||||
[CollectionCiphersCTE] AS [Target]
|
S.[Id],
|
||||||
USING
|
|
||||||
@CollectionIds AS [Source]
|
|
||||||
ON
|
|
||||||
[Target].[CollectionId] = [Source].[Id]
|
|
||||||
AND [Target].[CipherId] = @CipherId
|
|
||||||
WHEN NOT MATCHED BY TARGET
|
|
||||||
AND [Source].[Id] IN (SELECT [Id] FROM [AvailableCollectionsCTE]) THEN
|
|
||||||
INSERT VALUES
|
|
||||||
(
|
|
||||||
[Source].[Id],
|
|
||||||
@CipherId
|
@CipherId
|
||||||
)
|
FROM @CollectionIds AS S
|
||||||
WHEN NOT MATCHED BY SOURCE
|
INNER JOIN #TempAvailableCollections AS A
|
||||||
AND [Target].[CipherId] = @CipherId THEN
|
ON A.[Id] = S.[Id]
|
||||||
DELETE
|
WHERE NOT EXISTS (
|
||||||
;
|
SELECT 1
|
||||||
|
FROM [dbo].[CollectionCipher] AS CC
|
||||||
|
WHERE CC.[CollectionId] = S.[Id]
|
||||||
|
AND CC.[CipherId] = @CipherId
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Delete removed collection assignments
|
||||||
|
DELETE CC
|
||||||
|
FROM [dbo].[CollectionCipher] AS CC
|
||||||
|
INNER JOIN #TempAvailableCollections AS A
|
||||||
|
ON A.[Id] = CC.[CollectionId]
|
||||||
|
WHERE CC.[CipherId] = @CipherId
|
||||||
|
AND NOT EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM @CollectionIds AS S
|
||||||
|
WHERE S.[Id] = CC.[CollectionId]
|
||||||
|
);
|
||||||
|
|
||||||
IF @OrganizationId IS NOT NULL
|
IF @OrganizationId IS NOT NULL
|
||||||
BEGIN
|
BEGIN
|
||||||
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationId] @OrganizationId
|
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationId] @OrganizationId;
|
||||||
END
|
END
|
||||||
|
|
||||||
|
DROP TABLE #TempAvailableCollections;
|
||||||
END
|
END
|
||||||
|
GO
|
||||||
|
|||||||
@@ -0,0 +1,55 @@
|
|||||||
|
CREATE OR ALTER PROCEDURE [dbo].[CollectionCipher_UpdateCollectionsAdmin]
|
||||||
|
@CipherId UNIQUEIDENTIFIER,
|
||||||
|
@OrganizationId UNIQUEIDENTIFIER,
|
||||||
|
@CollectionIds AS [dbo].[GuidIdArray] READONLY
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
SET NOCOUNT ON;
|
||||||
|
|
||||||
|
-- Available collections for this org, excluding default collections
|
||||||
|
SELECT
|
||||||
|
C.[Id]
|
||||||
|
INTO #TempAvailableCollections
|
||||||
|
FROM [dbo].[Collection] AS C
|
||||||
|
WHERE
|
||||||
|
C.[OrganizationId] = @OrganizationId
|
||||||
|
AND C.[Type] <> 1; -- exclude DefaultUserCollection
|
||||||
|
|
||||||
|
-- Insert new collection assignments
|
||||||
|
INSERT INTO [dbo].[CollectionCipher] (
|
||||||
|
[CollectionId],
|
||||||
|
[CipherId]
|
||||||
|
)
|
||||||
|
SELECT
|
||||||
|
S.[Id],
|
||||||
|
@CipherId
|
||||||
|
FROM @CollectionIds AS S
|
||||||
|
INNER JOIN #TempAvailableCollections AS A
|
||||||
|
ON A.[Id] = S.[Id]
|
||||||
|
WHERE NOT EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM [dbo].[CollectionCipher] AS CC
|
||||||
|
WHERE CC.[CollectionId] = S.[Id]
|
||||||
|
AND CC.[CipherId] = @CipherId
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Delete removed collection assignments
|
||||||
|
DELETE CC
|
||||||
|
FROM [dbo].[CollectionCipher] AS CC
|
||||||
|
INNER JOIN #TempAvailableCollections AS A
|
||||||
|
ON A.[Id] = CC.[CollectionId]
|
||||||
|
WHERE CC.[CipherId] = @CipherId
|
||||||
|
AND NOT EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM @CollectionIds AS S
|
||||||
|
WHERE S.[Id] = CC.[CollectionId]
|
||||||
|
);
|
||||||
|
|
||||||
|
IF @OrganizationId IS NOT NULL
|
||||||
|
BEGIN
|
||||||
|
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationId] @OrganizationId;
|
||||||
|
END
|
||||||
|
|
||||||
|
DROP TABLE #TempAvailableCollections;
|
||||||
|
END
|
||||||
|
GO
|
||||||
Reference in New Issue
Block a user