mirror of
https://github.com/bitwarden/server.git
synced 2024-12-04 14:13:28 +01:00
213 lines
6.3 KiB
MySQL
213 lines
6.3 KiB
MySQL
|
-- Flexible Collections: create new CollectionCipher sprocs that don't use AccessAll logic
|
||
|
|
||
|
-- CollectionCipher_ReadByUserId_V2
|
||
|
CREATE OR ALTER PROCEDURE [dbo].[CollectionCipher_ReadByUserId_V2]
|
||
|
@UserId UNIQUEIDENTIFIER
|
||
|
AS
|
||
|
BEGIN
|
||
|
SET NOCOUNT ON
|
||
|
|
||
|
SELECT
|
||
|
CC.*
|
||
|
FROM
|
||
|
[dbo].[CollectionCipher] CC
|
||
|
INNER JOIN
|
||
|
[dbo].[Collection] S ON S.[Id] = CC.[CollectionId]
|
||
|
INNER JOIN
|
||
|
[dbo].[OrganizationUser] OU ON OU.[OrganizationId] = S.[OrganizationId] AND OU.[UserId] = @UserId
|
||
|
LEFT JOIN
|
||
|
[dbo].[CollectionUser] CU ON CU.[CollectionId] = S.[Id] AND CU.[OrganizationUserId] = OU.[Id]
|
||
|
LEFT JOIN
|
||
|
[dbo].[GroupUser] GU ON CU.[CollectionId] IS NULL AND GU.[OrganizationUserId] = OU.[Id]
|
||
|
LEFT JOIN
|
||
|
[dbo].[Group] G ON G.[Id] = GU.[GroupId]
|
||
|
LEFT JOIN
|
||
|
[dbo].[CollectionGroup] CG ON CG.[CollectionId] = CC.[CollectionId] AND CG.[GroupId] = GU.[GroupId]
|
||
|
WHERE
|
||
|
OU.[Status] = 2 -- Confirmed
|
||
|
AND (
|
||
|
CU.[CollectionId] IS NOT NULL
|
||
|
OR CG.[CollectionId] IS NOT NULL
|
||
|
)
|
||
|
END
|
||
|
GO
|
||
|
|
||
|
-- CollectionCipher_ReadByUserIdCipherId_V2
|
||
|
CREATE OR ALTER PROCEDURE [dbo].[CollectionCipher_ReadByUserIdCipherId_V2]
|
||
|
@UserId UNIQUEIDENTIFIER,
|
||
|
@CipherId UNIQUEIDENTIFIER
|
||
|
AS
|
||
|
BEGIN
|
||
|
SET NOCOUNT ON
|
||
|
|
||
|
SELECT
|
||
|
CC.*
|
||
|
FROM
|
||
|
[dbo].[CollectionCipher] CC
|
||
|
INNER JOIN
|
||
|
[dbo].[Collection] S ON S.[Id] = CC.[CollectionId]
|
||
|
INNER JOIN
|
||
|
[dbo].[OrganizationUser] OU ON OU.[OrganizationId] = S.[OrganizationId] AND OU.[UserId] = @UserId
|
||
|
LEFT JOIN
|
||
|
[dbo].[CollectionUser] CU ON CU.[CollectionId] = S.[Id] AND CU.[OrganizationUserId] = OU.[Id]
|
||
|
LEFT JOIN
|
||
|
[dbo].[GroupUser] GU ON CU.[CollectionId] IS NULL AND GU.[OrganizationUserId] = OU.[Id]
|
||
|
LEFT JOIN
|
||
|
[dbo].[Group] G ON G.[Id] = GU.[GroupId]
|
||
|
LEFT JOIN
|
||
|
[dbo].[CollectionGroup] CG ON CG.[CollectionId] = CC.[CollectionId] AND CG.[GroupId] = GU.[GroupId]
|
||
|
WHERE
|
||
|
CC.[CipherId] = @CipherId
|
||
|
AND OU.[Status] = 2 -- Confirmed
|
||
|
AND (
|
||
|
CU.[CollectionId] IS NOT NULL
|
||
|
OR CG.[CollectionId] IS NOT NULL
|
||
|
)
|
||
|
END
|
||
|
GO
|
||
|
|
||
|
-- CollectionCipher_UpdateCollections_V2
|
||
|
CREATE OR ALTER PROCEDURE [dbo].[CollectionCipher_UpdateCollections_V2]
|
||
|
@CipherId UNIQUEIDENTIFIER,
|
||
|
@UserId UNIQUEIDENTIFIER,
|
||
|
@CollectionIds AS [dbo].[GuidIdArray] READONLY
|
||
|
AS
|
||
|
BEGIN
|
||
|
SET NOCOUNT ON
|
||
|
|
||
|
DECLARE @OrgId UNIQUEIDENTIFIER = (
|
||
|
SELECT TOP 1
|
||
|
[OrganizationId]
|
||
|
FROM
|
||
|
[dbo].[Cipher]
|
||
|
WHERE
|
||
|
[Id] = @CipherId
|
||
|
)
|
||
|
|
||
|
;WITH [AvailableCollectionsCTE] AS(
|
||
|
SELECT
|
||
|
C.[Id]
|
||
|
FROM
|
||
|
[dbo].[Collection] C
|
||
|
INNER JOIN
|
||
|
[Organization] O ON O.[Id] = C.[OrganizationId]
|
||
|
INNER JOIN
|
||
|
[dbo].[OrganizationUser] OU ON OU.[OrganizationId] = O.[Id] AND OU.[UserId] = @UserId
|
||
|
LEFT JOIN
|
||
|
[dbo].[CollectionUser] CU ON CU.[CollectionId] = C.[Id] AND CU.[OrganizationUserId] = OU.[Id]
|
||
|
LEFT JOIN
|
||
|
[dbo].[GroupUser] GU ON CU.[CollectionId] IS NULL AND GU.[OrganizationUserId] = OU.[Id]
|
||
|
LEFT JOIN
|
||
|
[dbo].[Group] G ON G.[Id] = GU.[GroupId]
|
||
|
LEFT JOIN
|
||
|
[dbo].[CollectionGroup] CG ON CG.[CollectionId] = C.[Id] AND CG.[GroupId] = GU.[GroupId]
|
||
|
WHERE
|
||
|
O.[Id] = @OrgId
|
||
|
AND O.[Enabled] = 1
|
||
|
AND OU.[Status] = 2 -- Confirmed
|
||
|
AND (
|
||
|
CU.[ReadOnly] = 0
|
||
|
OR CG.[ReadOnly] = 0
|
||
|
)
|
||
|
),
|
||
|
[CollectionCiphersCTE] AS(
|
||
|
SELECT
|
||
|
[CollectionId],
|
||
|
[CipherId]
|
||
|
FROM
|
||
|
[dbo].[CollectionCipher]
|
||
|
WHERE
|
||
|
[CipherId] = @CipherId
|
||
|
)
|
||
|
MERGE
|
||
|
[CollectionCiphersCTE] AS [Target]
|
||
|
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
|
||
|
)
|
||
|
WHEN NOT MATCHED BY SOURCE
|
||
|
AND [Target].[CipherId] = @CipherId
|
||
|
AND [Target].[CollectionId] IN (SELECT [Id] FROM [AvailableCollectionsCTE]) THEN
|
||
|
DELETE
|
||
|
;
|
||
|
|
||
|
IF @OrgId IS NOT NULL
|
||
|
BEGIN
|
||
|
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationId] @OrgId
|
||
|
END
|
||
|
END
|
||
|
GO
|
||
|
|
||
|
-- CollectionCipher_UpdateCollectionsForCiphers_V2
|
||
|
CREATE OR ALTER PROCEDURE [dbo].[CollectionCipher_UpdateCollectionsForCiphers_V2]
|
||
|
@CipherIds AS [dbo].[GuidIdArray] READONLY,
|
||
|
@OrganizationId UNIQUEIDENTIFIER,
|
||
|
@UserId 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
|
||
|
[Organization] O ON O.[Id] = C.[OrganizationId]
|
||
|
INNER JOIN
|
||
|
[dbo].[OrganizationUser] OU ON OU.[OrganizationId] = O.[Id] AND OU.[UserId] = @UserId
|
||
|
LEFT JOIN
|
||
|
[dbo].[CollectionUser] CU ON CU.[CollectionId] = C.[Id] AND CU.[OrganizationUserId] = OU.[Id]
|
||
|
LEFT JOIN
|
||
|
[dbo].[GroupUser] GU ON CU.[CollectionId] IS NULL AND GU.[OrganizationUserId] = OU.[Id]
|
||
|
LEFT JOIN
|
||
|
[dbo].[Group] G ON G.[Id] = GU.[GroupId]
|
||
|
LEFT JOIN
|
||
|
[dbo].[CollectionGroup] CG ON CG.[CollectionId] = C.[Id] AND CG.[GroupId] = GU.[GroupId]
|
||
|
WHERE
|
||
|
O.[Id] = @OrganizationId
|
||
|
AND O.[Enabled] = 1
|
||
|
AND OU.[Status] = 2 -- Confirmed
|
||
|
AND (
|
||
|
CU.[ReadOnly] = 0
|
||
|
OR CG.[ReadOnly] = 0
|
||
|
)
|
||
|
|
||
|
IF (SELECT COUNT(1) FROM #AvailableCollections) < 1
|
||
|
BEGIN
|
||
|
-- No writable collections available to share with in this organization.
|
||
|
RETURN
|
||
|
END
|
||
|
|
||
|
INSERT INTO [dbo].[CollectionCipher]
|
||
|
(
|
||
|
[CollectionId],
|
||
|
[CipherId]
|
||
|
)
|
||
|
SELECT
|
||
|
[Collection].[Id],
|
||
|
[Cipher].[Id]
|
||
|
FROM
|
||
|
@CollectionIds [Collection]
|
||
|
INNER JOIN
|
||
|
@CipherIds [Cipher] ON 1 = 1
|
||
|
WHERE
|
||
|
[Collection].[Id] IN (SELECT [Id] FROM #AvailableCollections)
|
||
|
|
||
|
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationId] @OrganizationId
|
||
|
END
|
||
|
GO
|