mirror of
https://github.com/bitwarden/server.git
synced 2025-06-30 15:42:48 -05:00
[PM-2383] Bulk collection assignment (#3919)
* [PM-2383] Add bulk add/remove collection cipher repository methods * [PM-2383] Add additional authorization helpers for CiphersControlle * [PM-2383] Add /bulk-collections endpoint to CiphersController.cs * [PM-2383] Add EF implementation for new CollectionCipherRepository methods * [PM-2383] Ensure V1 logic only applies when the flag is enabled for new bulk functionality
This commit is contained in:
@ -0,0 +1,85 @@
|
||||
CREATE OR ALTER PROCEDURE [dbo].[CollectionCipher_AddCollectionsForManyCiphers]
|
||||
@CipherIds AS [dbo].[GuidIdArray] READONLY,
|
||||
@OrganizationId UNIQUEIDENTIFIER,
|
||||
@CollectionIds AS [dbo].[GuidIdArray] READONLY
|
||||
AS
|
||||
BEGIN
|
||||
SET NOCOUNT ON
|
||||
|
||||
CREATE TABLE #AvailableCollections (
|
||||
[Id] UNIQUEIDENTIFIER
|
||||
)
|
||||
|
||||
INSERT INTO #AvailableCollections
|
||||
SELECT
|
||||
C.[Id]
|
||||
FROM
|
||||
[dbo].[Collection] C
|
||||
INNER JOIN
|
||||
[dbo].[Organization] O ON O.[Id] = C.[OrganizationId]
|
||||
WHERE
|
||||
O.[Id] = @OrganizationId AND O.[Enabled] = 1
|
||||
|
||||
IF (SELECT COUNT(1) FROM #AvailableCollections) < 1
|
||||
BEGIN
|
||||
-- No collections available
|
||||
RETURN
|
||||
END
|
||||
|
||||
;WITH [SourceCollectionCipherCTE] AS(
|
||||
SELECT
|
||||
[Collection].[Id] AS [CollectionId],
|
||||
[Cipher].[Id] AS [CipherId]
|
||||
FROM
|
||||
@CollectionIds AS [Collection]
|
||||
CROSS JOIN
|
||||
@CipherIds AS [Cipher]
|
||||
WHERE
|
||||
[Collection].[Id] IN (SELECT [Id] FROM #AvailableCollections)
|
||||
)
|
||||
MERGE
|
||||
[CollectionCipher] AS [Target]
|
||||
USING
|
||||
[SourceCollectionCipherCTE] AS [Source]
|
||||
ON
|
||||
[Target].[CollectionId] = [Source].[CollectionId]
|
||||
AND [Target].[CipherId] = [Source].[CipherId]
|
||||
WHEN NOT MATCHED BY TARGET THEN
|
||||
INSERT VALUES
|
||||
(
|
||||
[Source].[CollectionId],
|
||||
[Source].[CipherId]
|
||||
)
|
||||
;
|
||||
|
||||
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationId] @OrganizationId
|
||||
END
|
||||
GO
|
||||
|
||||
CREATE OR ALTER PROCEDURE [dbo].[CollectionCipher_RemoveCollectionsForManyCiphers]
|
||||
@CipherIds AS [dbo].[GuidIdArray] READONLY,
|
||||
@OrganizationId UNIQUEIDENTIFIER,
|
||||
@CollectionIds AS [dbo].[GuidIdArray] READONLY
|
||||
AS
|
||||
BEGIN
|
||||
SET NOCOUNT ON
|
||||
|
||||
DECLARE @BatchSize INT = 100
|
||||
|
||||
WHILE @BatchSize > 0
|
||||
BEGIN
|
||||
BEGIN TRANSACTION CollectionCipher_DeleteMany
|
||||
DELETE TOP(@BatchSize)
|
||||
FROM
|
||||
[dbo].[CollectionCipher]
|
||||
WHERE
|
||||
[CipherId] IN (SELECT [Id] FROM @CipherIds) AND
|
||||
[CollectionId] IN (SELECT [Id] FROM @CollectionIds)
|
||||
|
||||
SET @BatchSize = @@ROWCOUNT
|
||||
COMMIT TRANSACTION CollectionCipher_DeleteMany
|
||||
END
|
||||
|
||||
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationId] @OrganizationId
|
||||
END
|
||||
GO
|
Reference in New Issue
Block a user