From ef8d89214b873bf5af5075ad35417f2300bb7680 Mon Sep 17 00:00:00 2001 From: Shane Melton Date: Thu, 30 Nov 2023 13:09:09 -0800 Subject: [PATCH] [AC-1868] Fix UserCipherDetails_V2 Function (#3491) * [AC-1868] Re-introduce case statement for Edit and ViewPassword selections * Formatting * Formatting again * Remove one more hidden tab --- .../dbo/Functions/UserCipherDetails_V2.sql | 12 +++- .../2023-11-29_00_FixUserCipherDetails_V2.sql | 62 +++++++++++++++++++ 2 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 util/Migrator/DbScripts/2023-11-29_00_FixUserCipherDetails_V2.sql diff --git a/src/Sql/Vault/dbo/Functions/UserCipherDetails_V2.sql b/src/Sql/Vault/dbo/Functions/UserCipherDetails_V2.sql index 1b22bb3de0..203d360d39 100644 --- a/src/Sql/Vault/dbo/Functions/UserCipherDetails_V2.sql +++ b/src/Sql/Vault/dbo/Functions/UserCipherDetails_V2.sql @@ -13,8 +13,16 @@ WITH [CTE] AS ( ) SELECT C.*, - COALESCE(CU.[ReadOnly], CG.[ReadOnly], 0) AS [Edit], - COALESCE(CU.[HidePasswords], CG.[HidePasswords], 0) AS [ViewPassword], + CASE + WHEN COALESCE(CU.[ReadOnly], CG.[ReadOnly], 0) = 0 + THEN 1 + ELSE 0 + END [Edit], + CASE + WHEN COALESCE(CU.[HidePasswords], CG.[HidePasswords], 0) = 0 + THEN 1 + ELSE 0 + END [ViewPassword], CASE WHEN O.[UseTotp] = 1 THEN 1 diff --git a/util/Migrator/DbScripts/2023-11-29_00_FixUserCipherDetails_V2.sql b/util/Migrator/DbScripts/2023-11-29_00_FixUserCipherDetails_V2.sql new file mode 100644 index 0000000000..200678e76c --- /dev/null +++ b/util/Migrator/DbScripts/2023-11-29_00_FixUserCipherDetails_V2.sql @@ -0,0 +1,62 @@ +CREATE OR ALTER FUNCTION [dbo].[UserCipherDetails_V2](@UserId UNIQUEIDENTIFIER) +RETURNS TABLE +AS RETURN +WITH [CTE] AS ( + SELECT + [Id], + [OrganizationId] + FROM + [OrganizationUser] + WHERE + [UserId] = @UserId + AND [Status] = 2 -- Confirmed +) +SELECT + C.*, + CASE + WHEN COALESCE(CU.[ReadOnly], CG.[ReadOnly], 0) = 0 + THEN 1 + ELSE 0 + END [Edit], + CASE + WHEN COALESCE(CU.[HidePasswords], CG.[HidePasswords], 0) = 0 + THEN 1 + ELSE 0 + END [ViewPassword], + CASE + WHEN O.[UseTotp] = 1 + THEN 1 + ELSE 0 + END [OrganizationUseTotp] +FROM + [dbo].[CipherDetails](@UserId) C +INNER JOIN + [CTE] OU ON C.[UserId] IS NULL AND C.[OrganizationId] IN (SELECT [OrganizationId] FROM [CTE]) +INNER JOIN + [dbo].[Organization] O ON O.[Id] = OU.[OrganizationId] AND O.[Id] = C.[OrganizationId] AND O.[Enabled] = 1 +LEFT JOIN + [dbo].[CollectionCipher] CC ON CC.[CipherId] = C.[Id] +LEFT JOIN + [dbo].[CollectionUser] CU ON CU.[CollectionId] = CC.[CollectionId] 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 + CU.[CollectionId] IS NOT NULL + OR CG.[CollectionId] IS NOT NULL + +UNION ALL + +SELECT + *, + 1 [Edit], + 1 [ViewPassword], + 0 [OrganizationUseTotp] +FROM + [dbo].[CipherDetails](@UserId) +WHERE + [UserId] = @UserId +GO