1
0
mirror of https://github.com/bitwarden/server.git synced 2025-07-01 08:02:49 -05:00

cleanup sql aliases from collection rename

This commit is contained in:
Kyle Spearrin
2017-04-27 09:46:03 -04:00
parent 77aec3977c
commit 44883091f6
15 changed files with 44 additions and 44 deletions

View File

@ -5,7 +5,7 @@ BEGIN
;WITH [CTE] AS( ;WITH [CTE] AS(
SELECT SELECT
CASE WHEN OU.[AccessAllCollections] = 1 OR SU.[ReadOnly] = 0 THEN 1 ELSE 0 END [CanEdit] CASE WHEN OU.[AccessAllCollections] = 1 OR CU.[ReadOnly] = 0 THEN 1 ELSE 0 END [CanEdit]
FROM FROM
[dbo].[Cipher] C [dbo].[Cipher] C
INNER JOIN INNER JOIN
@ -13,14 +13,14 @@ BEGIN
INNER JOIN INNER JOIN
[dbo].[OrganizationUser] OU ON OU.[OrganizationId] = O.[Id] AND OU.[UserId] = @UserId [dbo].[OrganizationUser] OU ON OU.[OrganizationId] = O.[Id] AND OU.[UserId] = @UserId
LEFT JOIN LEFT JOIN
[dbo].[CollectionCipher] SC ON C.[UserId] IS NULL AND OU.[AccessAllCollections] = 0 AND SC.[CipherId] = C.[Id] [dbo].[CollectionCipher] CC ON C.[UserId] IS NULL AND OU.[AccessAllCollections] = 0 AND CC.[CipherId] = C.[Id]
LEFT JOIN LEFT JOIN
[dbo].[CollectionUser] SU ON SU.[CollectionId] = SC.[CollectionId] AND SU.[OrganizationUserId] = OU.[Id] [dbo].[CollectionUser] CU ON CU.[CollectionId] = CC.[CollectionId] AND CU.[OrganizationUserId] = OU.[Id]
WHERE WHERE
C.[Id] = @CipherId C.[Id] = @CipherId
AND OU.[Status] = 2 -- 2 = Confirmed AND OU.[Status] = 2 -- 2 = Confirmed
AND O.[Enabled] = 1 AND O.[Enabled] = 1
AND (OU.[AccessAllCollections] = 1 OR SU.[CollectionId] IS NOT NULL) AND (OU.[AccessAllCollections] = 1 OR CU.[CollectionId] IS NOT NULL)
) )
SELECT SELECT
@CanEdit = CASE WHEN COUNT(1) > 0 THEN 1 ELSE 0 END @CanEdit = CASE WHEN COUNT(1) > 0 THEN 1 ELSE 0 END

View File

@ -14,9 +14,9 @@ BEGIN
LEFT JOIN LEFT JOIN
[dbo].[OrganizationUser] OU ON OU.[OrganizationId] = O.[Id] AND OU.[UserId] = @UserId [dbo].[OrganizationUser] OU ON OU.[OrganizationId] = O.[Id] AND OU.[UserId] = @UserId
LEFT JOIN LEFT JOIN
[dbo].[CollectionCipher] SC ON C.[UserId] IS NULL AND OU.[AccessAllCollections] = 0 AND SC.[CipherId] = C.[Id] [dbo].[CollectionCipher] CC ON C.[UserId] IS NULL AND OU.[AccessAllCollections] = 0 AND CC.[CipherId] = C.[Id]
LEFT JOIN LEFT JOIN
[dbo].[CollectionUser] SU ON SU.[CollectionId] = SC.[CollectionId] AND SU.[OrganizationUserId] = OU.[Id] [dbo].[CollectionUser] CU ON CU.[CollectionId] = CC.[CollectionId] AND CU.[OrganizationUserId] = OU.[Id]
WHERE WHERE
C.Id = @Id C.Id = @Id
AND ( AND (
@ -25,7 +25,7 @@ BEGIN
C.[UserId] IS NULL C.[UserId] IS NULL
AND OU.[Status] = 2 -- 2 = Confirmed AND OU.[Status] = 2 -- 2 = Confirmed
AND O.[Enabled] = 1 AND O.[Enabled] = 1
AND (OU.[AccessAllCollections] = 1 OR SU.[CollectionId] IS NOT NULL) AND (OU.[AccessAllCollections] = 1 OR CU.[CollectionId] IS NOT NULL)
) )
) )
END END

View File

@ -14,9 +14,9 @@ BEGIN
LEFT JOIN LEFT JOIN
[dbo].[OrganizationUser] OU ON OU.[OrganizationId] = O.[Id] AND OU.[UserId] = @UserId [dbo].[OrganizationUser] OU ON OU.[OrganizationId] = O.[Id] AND OU.[UserId] = @UserId
LEFT JOIN LEFT JOIN
[dbo].[CollectionCipher] SC ON C.[UserId] IS NULL AND OU.[AccessAllCollections] = 0 AND SC.[CipherId] = C.[Id] [dbo].[CollectionCipher] CC ON C.[UserId] IS NULL AND OU.[AccessAllCollections] = 0 AND CC.[CipherId] = C.[Id]
LEFT JOIN LEFT JOIN
[dbo].[CollectionUser] SU ON SU.[CollectionId] = SC.[CollectionId] AND SU.[OrganizationUserId] = OU.[Id] [dbo].[CollectionUser] CU ON CU.[CollectionId] = CC.[CollectionId] AND CU.[OrganizationUserId] = OU.[Id]
WHERE WHERE
C.[Type] = @Type C.[Type] = @Type
AND ( AND (
@ -25,7 +25,7 @@ BEGIN
C.[UserId] IS NULL C.[UserId] IS NULL
AND OU.[Status] = 2 -- 2 = Confirmed AND OU.[Status] = 2 -- 2 = Confirmed
AND O.[Enabled] = 1 AND O.[Enabled] = 1
AND (OU.[AccessAllCollections] = 1 OR SU.[CollectionId] IS NOT NULL) AND (OU.[AccessAllCollections] = 1 OR CU.[CollectionId] IS NOT NULL)
) )
) )
END END

View File

@ -13,15 +13,15 @@ BEGIN
LEFT JOIN LEFT JOIN
[dbo].[OrganizationUser] OU ON OU.[OrganizationId] = O.[Id] AND OU.[UserId] = @UserId [dbo].[OrganizationUser] OU ON OU.[OrganizationId] = O.[Id] AND OU.[UserId] = @UserId
LEFT JOIN LEFT JOIN
[dbo].[CollectionCipher] SC ON C.[UserId] IS NULL AND OU.[AccessAllCollections] = 0 AND SC.[CipherId] = C.[Id] [dbo].[CollectionCipher] CC ON C.[UserId] IS NULL AND OU.[AccessAllCollections] = 0 AND CC.[CipherId] = C.[Id]
LEFT JOIN LEFT JOIN
[dbo].[CollectionUser] SU ON SU.[CollectionId] = SC.[CollectionId] AND SU.[OrganizationUserId] = OU.[Id] [dbo].[CollectionUser] CU ON CU.[CollectionId] = CC.[CollectionId] AND CU.[OrganizationUserId] = OU.[Id]
WHERE WHERE
C.[UserId] = @UserId C.[UserId] = @UserId
OR ( OR (
C.[UserId] IS NULL C.[UserId] IS NULL
AND OU.[Status] = 2 -- 2 = Confirmed AND OU.[Status] = 2 -- 2 = Confirmed
AND O.[Enabled] = 1 AND O.[Enabled] = 1
AND (OU.[AccessAllCollections] = 1 OR SU.[CollectionId] IS NOT NULL) AND (OU.[AccessAllCollections] = 1 OR CU.[CollectionId] IS NOT NULL)
) )
END END

View File

@ -13,11 +13,11 @@ BEGIN
INNER JOIN INNER JOIN
[dbo].[OrganizationUser] OU ON OU.[OrganizationId] = O.[Id] AND OU.[UserId] = @UserId [dbo].[OrganizationUser] OU ON OU.[OrganizationId] = O.[Id] AND OU.[UserId] = @UserId
LEFT JOIN LEFT JOIN
[dbo].[CollectionCipher] SC ON C.[UserId] IS NULL AND OU.[AccessAllCollections] = 0 AND SC.[CipherId] = C.[Id] [dbo].[CollectionCipher] CC ON C.[UserId] IS NULL AND OU.[AccessAllCollections] = 0 AND CC.[CipherId] = C.[Id]
LEFT JOIN LEFT JOIN
[dbo].[CollectionUser] SU ON SU.[CollectionId] = SC.[CollectionId] AND SU.[OrganizationUserId] = OU.[Id] [dbo].[CollectionUser] CU ON CU.[CollectionId] = CC.[CollectionId] AND CU.[OrganizationUserId] = OU.[Id]
WHERE WHERE
OU.[Status] = 2 -- 2 = Confirmed OU.[Status] = 2 -- 2 = Confirmed
AND O.[Enabled] = 1 AND O.[Enabled] = 1
AND (OU.[AccessAllCollections] = 1 OR SU.[CollectionId] IS NOT NULL) AND (OU.[AccessAllCollections] = 1 OR CU.[CollectionId] IS NOT NULL)
END END

View File

@ -18,9 +18,9 @@ BEGIN
LEFT JOIN LEFT JOIN
[dbo].[OrganizationUser] OU ON OU.[OrganizationId] = O.[Id] AND OU.[UserId] = @UserId [dbo].[OrganizationUser] OU ON OU.[OrganizationId] = O.[Id] AND OU.[UserId] = @UserId
LEFT JOIN LEFT JOIN
[dbo].[CollectionCipher] SC ON C.[UserId] IS NULL AND OU.[AccessAllCollections] = 0 AND SC.[CipherId] = C.[Id] [dbo].[CollectionCipher] CC ON C.[UserId] IS NULL AND OU.[AccessAllCollections] = 0 AND CC.[CipherId] = C.[Id]
LEFT JOIN LEFT JOIN
[dbo].[CollectionUser] SU ON SU.[CollectionId] = SC.[CollectionId] AND SU.[OrganizationUserId] = OU.[Id] [dbo].[CollectionUser] CU ON CU.[CollectionId] = CC.[CollectionId] AND CU.[OrganizationUserId] = OU.[Id]
WHERE WHERE
C.Id = @Id C.Id = @Id
AND ( AND (
@ -29,7 +29,7 @@ BEGIN
C.[UserId] IS NULL C.[UserId] IS NULL
AND OU.[Status] = 2 -- 2 = Confirmed AND OU.[Status] = 2 -- 2 = Confirmed
AND O.[Enabled] = 1 AND O.[Enabled] = 1
AND (OU.[AccessAllCollections] = 1 OR SU.[CollectionId] IS NOT NULL) AND (OU.[AccessAllCollections] = 1 OR CU.[CollectionId] IS NOT NULL)
) )
) )
END END

View File

@ -34,12 +34,12 @@ BEGIN
INNER JOIN INNER JOIN
[dbo].[OrganizationUser] OU ON OU.[OrganizationId] = O.[Id] AND OU.[UserId] = @UserId [dbo].[OrganizationUser] OU ON OU.[OrganizationId] = O.[Id] AND OU.[UserId] = @UserId
LEFT JOIN LEFT JOIN
[dbo].[CollectionUser] SU ON OU.[AccessAllCollections] = 0 AND SU.[CollectionId] = S.[Id] AND SU.[OrganizationUserId] = OU.[Id] [dbo].[CollectionUser] CU ON OU.[AccessAllCollections] = 0 AND CU.[CollectionId] = S.[Id] AND CU.[OrganizationUserId] = OU.[Id]
WHERE WHERE
O.[Id] = @OrganizationId O.[Id] = @OrganizationId
AND O.[Enabled] = 1 AND O.[Enabled] = 1
AND OU.[Status] = 2 -- Confirmed AND OU.[Status] = 2 -- Confirmed
AND (OU.[AccessAllCollections] = 1 OR SU.[ReadOnly] = 0) AND (OU.[AccessAllCollections] = 1 OR CU.[ReadOnly] = 0)
) )
INSERT INTO [dbo].[CollectionCipher] INSERT INTO [dbo].[CollectionCipher]
( (

View File

@ -13,8 +13,8 @@ BEGIN
INNER JOIN INNER JOIN
[dbo].[OrganizationUser] OU ON OU.[OrganizationId] = S.[OrganizationId] AND OU.[UserId] = @UserId [dbo].[OrganizationUser] OU ON OU.[OrganizationId] = S.[OrganizationId] AND OU.[UserId] = @UserId
LEFT JOIN LEFT JOIN
[dbo].[CollectionUser] SU ON OU.[AccessAllCollections] = 0 AND SU.[CollectionId] = S.[Id] AND SU.[OrganizationUserId] = OU.[Id] [dbo].[CollectionUser] CU ON OU.[AccessAllCollections] = 0 AND CU.[CollectionId] = S.[Id] AND CU.[OrganizationUserId] = OU.[Id]
WHERE WHERE
OU.[Status] = 2 -- Confirmed OU.[Status] = 2 -- Confirmed
AND (OU.[AccessAllCollections] = 1 OR SU.[CollectionId] IS NOT NULL) AND (OU.[AccessAllCollections] = 1 OR CU.[CollectionId] IS NOT NULL)
END END

View File

@ -14,9 +14,9 @@ BEGIN
INNER JOIN INNER JOIN
[dbo].[OrganizationUser] OU ON OU.[OrganizationId] = S.[OrganizationId] AND OU.[UserId] = @UserId [dbo].[OrganizationUser] OU ON OU.[OrganizationId] = S.[OrganizationId] AND OU.[UserId] = @UserId
LEFT JOIN LEFT JOIN
[dbo].[CollectionUser] SU ON OU.[AccessAllCollections] = 0 AND SU.[CollectionId] = S.[Id] AND SU.[OrganizationUserId] = OU.[Id] [dbo].[CollectionUser] CU ON OU.[AccessAllCollections] = 0 AND CU.[CollectionId] = S.[Id] AND CU.[OrganizationUserId] = OU.[Id]
WHERE WHERE
SC.[CipherId] = @CipherId SC.[CipherId] = @CipherId
AND OU.[Status] = 2 -- Confirmed AND OU.[Status] = 2 -- Confirmed
AND (OU.[AccessAllCollections] = 1 OR SU.[CollectionId] IS NOT NULL) AND (OU.[AccessAllCollections] = 1 OR CU.[CollectionId] IS NOT NULL)
END END

View File

@ -25,12 +25,12 @@ BEGIN
INNER JOIN INNER JOIN
[dbo].[OrganizationUser] OU ON OU.[OrganizationId] = O.[Id] AND OU.[UserId] = @UserId [dbo].[OrganizationUser] OU ON OU.[OrganizationId] = O.[Id] AND OU.[UserId] = @UserId
LEFT JOIN LEFT JOIN
[dbo].[CollectionUser] SU ON OU.[AccessAllCollections] = 0 AND SU.[CollectionId] = S.[Id] AND SU.[OrganizationUserId] = OU.[Id] [dbo].[CollectionUser] CU ON OU.[AccessAllCollections] = 0 AND CU.[CollectionId] = S.[Id] AND CU.[OrganizationUserId] = OU.[Id]
WHERE WHERE
O.[Id] = @OrgId O.[Id] = @OrgId
AND O.[Enabled] = 1 AND O.[Enabled] = 1
AND OU.[Status] = 2 -- Confirmed AND OU.[Status] = 2 -- Confirmed
AND (OU.[AccessAllCollections] = 1 OR SU.[ReadOnly] = 0) AND (OU.[AccessAllCollections] = 1 OR CU.[ReadOnly] = 0)
) )
MERGE MERGE
[dbo].[CollectionCipher] AS [Target] [dbo].[CollectionCipher] AS [Target]

View File

@ -5,11 +5,11 @@ BEGIN
SET NOCOUNT ON SET NOCOUNT ON
SELECT SELECT
SU.* CU.*
FROM FROM
[dbo].[CollectionUserCollectionDetailsView] SU [dbo].[CollectionUserCollectionDetailsView] CU
INNER JOIN INNER JOIN
[OrganizationUser] OU ON SU.[OrganizationUserId] = OU.[Id] [OrganizationUser] OU ON CU.[OrganizationUserId] = OU.[Id]
WHERE WHERE
OU.[UserId] = @UserId OU.[UserId] = @UserId
END END

View File

@ -13,9 +13,9 @@ BEGIN
INNER JOIN INNER JOIN
[dbo].[OrganizationUser] OU ON OU.[OrganizationId] = O.[Id] AND OU.[UserId] = @UserId [dbo].[OrganizationUser] OU ON OU.[OrganizationId] = O.[Id] AND OU.[UserId] = @UserId
LEFT JOIN LEFT JOIN
[dbo].[CollectionUser] SU ON OU.[AccessAllCollections] = 0 AND SU.[CollectionId] = S.[Id] AND SU.[OrganizationUserId] = OU.[Id] [dbo].[CollectionUser] CU ON OU.[AccessAllCollections] = 0 AND CU.[CollectionId] = S.[Id] AND CU.[OrganizationUserId] = OU.[Id]
WHERE WHERE
OU.[Status] = 2 -- Confirmed OU.[Status] = 2 -- Confirmed
AND O.[Enabled] = 1 AND O.[Enabled] = 1
AND (OU.[AccessAllCollections] = 1 OR SU.[CollectionId] IS NOT NULL) AND (OU.[AccessAllCollections] = 1 OR CU.[CollectionId] IS NOT NULL)
END END

View File

@ -18,14 +18,14 @@ BEGIN
LEFT JOIN LEFT JOIN
[dbo].[OrganizationUser] OU ON OU.[OrganizationId] = O.[Id] AND OU.[UserId] = @UserId [dbo].[OrganizationUser] OU ON OU.[OrganizationId] = O.[Id] AND OU.[UserId] = @UserId
LEFT JOIN LEFT JOIN
[dbo].[CollectionCipher] SC ON C.[UserId] IS NULL AND OU.[AccessAllCollections] = 0 AND SC.[CipherId] = C.[Id] [dbo].[CollectionCipher] CC ON C.[UserId] IS NULL AND OU.[AccessAllCollections] = 0 AND CC.[CipherId] = C.[Id]
LEFT JOIN LEFT JOIN
[dbo].[CollectionUser] SU ON SU.[CollectionId] = SC.[CollectionId] AND SU.[OrganizationUserId] = OU.[Id] [dbo].[CollectionUser] CU ON CU.[CollectionId] = CC.[CollectionId] AND CU.[OrganizationUserId] = OU.[Id]
WHERE WHERE
C.[UserId] = @UserId C.[UserId] = @UserId
OR ( OR (
C.[UserId] IS NULL C.[UserId] IS NULL
AND (OU.[AccessAllCollections] = 1 OR SU.[CollectionId] IS NOT NULL) AND (OU.[AccessAllCollections] = 1 OR CU.[CollectionId] IS NOT NULL)
) )
AND C.[Folders] IS NOT NULL AND C.[Folders] IS NOT NULL
AND JSON_VALUE(C.[Folders], @UserIdPath) = @Id AND JSON_VALUE(C.[Folders], @UserIdPath) = @Id

View File

@ -1,12 +1,12 @@
CREATE VIEW [dbo].[CollectionUserCollectionDetailsView] CREATE VIEW [dbo].[CollectionUserCollectionDetailsView]
AS AS
SELECT SELECT
SU.[Id], CU.[Id],
SU.[OrganizationUserId], CU.[OrganizationUserId],
S.[Name], S.[Name],
S.[Id] CollectionId, S.[Id] CollectionId,
SU.[ReadOnly] CU.[ReadOnly]
FROM FROM
[dbo].[CollectionUser] SU [dbo].[CollectionUser] CU
INNER JOIN INNER JOIN
[dbo].[Collection] S ON S.[Id] = SU.[CollectionId] [dbo].[Collection] S ON S.[Id] = CU.[CollectionId]

View File

@ -3,16 +3,16 @@ AS
SELECT SELECT
OU.[Id] AS [OrganizationUserId], OU.[Id] AS [OrganizationUserId],
OU.[AccessAllCollections], OU.[AccessAllCollections],
SU.[Id], CU.[Id],
SU.[CollectionId], CU.[CollectionId],
U.[Name], U.[Name],
ISNULL(U.[Email], OU.[Email]) Email, ISNULL(U.[Email], OU.[Email]) Email,
OU.[Status], OU.[Status],
OU.[Type], OU.[Type],
CASE WHEN OU.[AccessAllCollections] = 0 AND SU.[ReadOnly] = 1 THEN 1 ELSE 0 END [ReadOnly] CASE WHEN OU.[AccessAllCollections] = 0 AND CU.[ReadOnly] = 1 THEN 1 ELSE 0 END [ReadOnly]
FROM FROM
[dbo].[OrganizationUser] OU [dbo].[OrganizationUser] OU
LEFT JOIN LEFT JOIN
[dbo].[CollectionUser] SU ON OU.[AccessAllCollections] = 0 AND SU.[OrganizationUserId] = OU.[Id] [dbo].[CollectionUser] CU ON OU.[AccessAllCollections] = 0 AND CU.[OrganizationUserId] = OU.[Id]
LEFT JOIN LEFT JOIN
[dbo].[User] U ON U.[Id] = OU.[UserId] [dbo].[User] U ON U.[Id] = OU.[UserId]