mirror of
https://github.com/bitwarden/server
synced 2026-02-22 04:13:43 +00:00
Move transaction up to code layer
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
-- Creates default user collections for organization users
|
||||
-- Uses semaphore table to prevent duplicate default collections at database level
|
||||
-- NOTE: this MUST be executed in a single transaction to obtain semaphore protection
|
||||
CREATE PROCEDURE [dbo].[Collection_CreateDefaultCollections]
|
||||
@OrganizationId UNIQUEIDENTIFIER,
|
||||
@DefaultCollectionName VARCHAR(MAX),
|
||||
@@ -10,70 +11,58 @@ BEGIN
|
||||
|
||||
DECLARE @Now DATETIME2(7) = GETUTCDATE()
|
||||
|
||||
BEGIN TRANSACTION;
|
||||
-- Insert semaphore entries first to obtain the "lock"
|
||||
-- If this fails due to duplicate key, the entire transaction will be rolled back
|
||||
INSERT INTO [dbo].[DefaultCollectionSemaphore]
|
||||
(
|
||||
[OrganizationUserId],
|
||||
[CreationDate]
|
||||
)
|
||||
SELECT
|
||||
ids.[Id1], -- OrganizationUserId
|
||||
@Now
|
||||
FROM
|
||||
@OrganizationUserCollectionIds ids;
|
||||
|
||||
BEGIN TRY
|
||||
-- Insert semaphore entries first to obtain the "lock"
|
||||
-- If this fails due to duplicate key, the entire transaction will be rolled back
|
||||
INSERT INTO [dbo].[DefaultCollectionSemaphore]
|
||||
(
|
||||
[OrganizationUserId],
|
||||
[CreationDate]
|
||||
)
|
||||
SELECT
|
||||
ids.[Id1], -- OrganizationUserId
|
||||
@Now
|
||||
FROM
|
||||
@OrganizationUserCollectionIds ids;
|
||||
-- Insert collections for users who obtained semaphore entries
|
||||
INSERT INTO [dbo].[Collection]
|
||||
(
|
||||
[Id],
|
||||
[OrganizationId],
|
||||
[Name],
|
||||
[CreationDate],
|
||||
[RevisionDate],
|
||||
[Type],
|
||||
[ExternalId],
|
||||
[DefaultUserCollectionEmail]
|
||||
)
|
||||
SELECT
|
||||
ids.[Id2], -- CollectionId
|
||||
@OrganizationId,
|
||||
@DefaultCollectionName,
|
||||
@Now,
|
||||
@Now,
|
||||
1, -- CollectionType.DefaultUserCollection
|
||||
NULL,
|
||||
NULL
|
||||
FROM
|
||||
@OrganizationUserCollectionIds ids;
|
||||
|
||||
-- Insert collections for users who obtained semaphore entries
|
||||
INSERT INTO [dbo].[Collection]
|
||||
(
|
||||
[Id],
|
||||
[OrganizationId],
|
||||
[Name],
|
||||
[CreationDate],
|
||||
[RevisionDate],
|
||||
[Type],
|
||||
[ExternalId],
|
||||
[DefaultUserCollectionEmail]
|
||||
)
|
||||
SELECT
|
||||
ids.[Id2], -- CollectionId
|
||||
@OrganizationId,
|
||||
@DefaultCollectionName,
|
||||
@Now,
|
||||
@Now,
|
||||
1, -- CollectionType.DefaultUserCollection
|
||||
NULL,
|
||||
NULL
|
||||
FROM
|
||||
@OrganizationUserCollectionIds ids;
|
||||
|
||||
-- Insert collection user mappings
|
||||
INSERT INTO [dbo].[CollectionUser]
|
||||
(
|
||||
[CollectionId],
|
||||
[OrganizationUserId],
|
||||
[ReadOnly],
|
||||
[HidePasswords],
|
||||
[Manage]
|
||||
)
|
||||
SELECT
|
||||
ids.[Id2], -- CollectionId
|
||||
ids.[Id1], -- OrganizationUserId
|
||||
0, -- ReadOnly = false
|
||||
0, -- HidePasswords = false
|
||||
1 -- Manage = true
|
||||
FROM
|
||||
@OrganizationUserCollectionIds ids;
|
||||
|
||||
COMMIT TRANSACTION;
|
||||
END TRY
|
||||
BEGIN CATCH
|
||||
IF @@TRANCOUNT > 0
|
||||
ROLLBACK TRANSACTION;
|
||||
|
||||
THROW;
|
||||
END CATCH
|
||||
-- Insert collection user mappings
|
||||
INSERT INTO [dbo].[CollectionUser]
|
||||
(
|
||||
[CollectionId],
|
||||
[OrganizationUserId],
|
||||
[ReadOnly],
|
||||
[HidePasswords],
|
||||
[Manage]
|
||||
)
|
||||
SELECT
|
||||
ids.[Id2], -- CollectionId
|
||||
ids.[Id1], -- OrganizationUserId
|
||||
0, -- ReadOnly = false
|
||||
0, -- HidePasswords = false
|
||||
1 -- Manage = true
|
||||
FROM
|
||||
@OrganizationUserCollectionIds ids;
|
||||
END
|
||||
|
||||
Reference in New Issue
Block a user