Skip to content

Commit 80d1c3a

Browse files
add Archives column to ciphers table
1 parent de5a81b commit 80d1c3a

File tree

3 files changed

+203
-9
lines changed

3 files changed

+203
-9
lines changed

src/Sql/dbo/Vault/Functions/CipherDetails.sql

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ SELECT
2828
C.[DeletedDate],
2929
C.[Reprompt],
3030
C.[Key],
31-
C.[ArchivedDate]
31+
CASE
32+
WHEN
33+
@UserId IS NULL
34+
OR C.[Archives] IS NULL
35+
THEN NULL
36+
ELSE TRY_CONVERT(DATETIME2(7), JSON_VALUE(C.[Archives], CONCAT('$."', @UserId, '"')))
37+
END [ArchivedDate]
3238
FROM
33-
[dbo].[Cipher] C
39+
[dbo].[Cipher] C;

src/Sql/dbo/Vault/Functions/UserCipherDetails.sql

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,29 @@ WITH [CTE] AS (
1212
AND [Status] = 2 -- Confirmed
1313
)
1414
SELECT
15-
C.*,
15+
C.Id,
16+
C.UserId,
17+
C.OrganizationId,
18+
C.Type,
19+
C.Data,
20+
C.Attachments,
21+
C.CreationDate,
22+
C.RevisionDate,
23+
C.Favorite,
24+
C.FolderId,
25+
C.DeletedDate,
26+
C.ArchivedDate,
27+
C.Reprompt,
28+
C.[Key],
1629
CASE
1730
WHEN COALESCE(CU.[ReadOnly], CG.[ReadOnly], 0) = 0
1831
THEN 1
1932
ELSE 0
2033
END [Edit],
2134
CASE
22-
WHEN COALESCE(CU.[HidePasswords], CG.[HidePasswords], 0) = 0
23-
THEN 1
24-
ELSE 0
35+
WHEN COALESCE(CU.[HidePasswords], CG.[HidePasswords], 0) = 0
36+
THEN 1
37+
ELSE 0
2538
END [ViewPassword],
2639
CASE
2740
WHEN COALESCE(CU.[Manage], CG.[Manage], 0) = 1
@@ -56,12 +69,25 @@ WHERE
5669
UNION ALL
5770

5871
SELECT
59-
*,
72+
C.Id,
73+
C.UserId,
74+
C.OrganizationId,
75+
C.Type,
76+
C.Data,
77+
C.Attachments,
78+
C.CreationDate,
79+
C.RevisionDate,
80+
C.Favorite,
81+
C.FolderId,
82+
C.DeletedDate,
83+
C.ArchivedDate,
84+
C.Reprompt,
85+
C.[Key],
6086
1 [Edit],
6187
1 [ViewPassword],
6288
1 [Manage],
6389
0 [OrganizationUseTotp]
6490
FROM
65-
[dbo].[CipherDetails](@UserId)
91+
[dbo].[CipherDetails](@UserId) AS C
6692
WHERE
67-
[UserId] = @UserId
93+
C.[UserId] = @UserId;
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
-- Add new JSON column for Archives (similar to Favorites/Folders pattern)
2+
IF NOT EXISTS (
3+
SELECT 1
4+
FROM sys.columns
5+
WHERE object_id = OBJECT_ID(N'[dbo].[Cipher]')
6+
AND name = 'Archives'
7+
)
8+
BEGIN
9+
ALTER TABLE [dbo].[Cipher]
10+
ADD [Archives] NVARCHAR(MAX) NULL;
11+
END;
12+
GO
13+
14+
-- Update CipherDetails function to use JSON column approach
15+
IF OBJECT_ID('[dbo].[CipherDetails]') IS NOT NULL
16+
BEGIN
17+
DROP FUNCTION [dbo].[CipherDetails];
18+
END
19+
GO
20+
21+
CREATE FUNCTION [dbo].[CipherDetails](@UserId UNIQUEIDENTIFIER)
22+
RETURNS TABLE
23+
AS RETURN
24+
SELECT
25+
C.[Id],
26+
C.[UserId],
27+
C.[OrganizationId],
28+
C.[Type],
29+
C.[Data],
30+
C.[Attachments],
31+
C.[CreationDate],
32+
C.[RevisionDate],
33+
CASE
34+
WHEN
35+
@UserId IS NULL
36+
OR C.[Favorites] IS NULL
37+
OR JSON_VALUE(C.[Favorites], CONCAT('$."', @UserId, '"')) IS NULL
38+
THEN 0
39+
ELSE 1
40+
END [Favorite],
41+
CASE
42+
WHEN
43+
@UserId IS NULL
44+
OR C.[Folders] IS NULL
45+
THEN NULL
46+
ELSE TRY_CONVERT(UNIQUEIDENTIFIER, JSON_VALUE(C.[Folders], CONCAT('$."', @UserId, '"')))
47+
END [FolderId],
48+
C.[DeletedDate],
49+
C.[Reprompt],
50+
C.[Key],
51+
CASE
52+
WHEN
53+
@UserId IS NULL
54+
OR C.[Archives] IS NULL
55+
THEN NULL
56+
ELSE TRY_CONVERT(DATETIME2(7), JSON_VALUE(C.[Archives], CONCAT('$."', @UserId, '"')))
57+
END [ArchivedDate]
58+
FROM
59+
[dbo].[Cipher] C;
60+
GO
61+
62+
-- Update UserCipherDetails function
63+
IF OBJECT_ID('[dbo].[UserCipherDetails]') IS NOT NULL
64+
BEGIN
65+
DROP FUNCTION [dbo].[UserCipherDetails];
66+
END
67+
GO
68+
69+
CREATE FUNCTION [dbo].[UserCipherDetails](@UserId UNIQUEIDENTIFIER)
70+
RETURNS TABLE
71+
AS RETURN
72+
WITH [CTE] AS (
73+
SELECT
74+
[Id],
75+
[OrganizationId]
76+
FROM
77+
[OrganizationUser]
78+
WHERE
79+
[UserId] = @UserId
80+
AND [Status] = 2 -- Confirmed
81+
)
82+
SELECT
83+
C.Id,
84+
C.UserId,
85+
C.OrganizationId,
86+
C.Type,
87+
C.Data,
88+
C.Attachments,
89+
C.CreationDate,
90+
C.RevisionDate,
91+
C.Favorite,
92+
C.FolderId,
93+
C.DeletedDate,
94+
C.ArchivedDate,
95+
C.Reprompt,
96+
C.[Key],
97+
CASE
98+
WHEN COALESCE(CU.[ReadOnly], CG.[ReadOnly], 0) = 0
99+
THEN 1
100+
ELSE 0
101+
END [Edit],
102+
CASE
103+
WHEN COALESCE(CU.[HidePasswords], CG.[HidePasswords], 0) = 0
104+
THEN 1
105+
ELSE 0
106+
END [ViewPassword],
107+
CASE
108+
WHEN COALESCE(CU.[Manage], CG.[Manage], 0) = 1
109+
THEN 1
110+
ELSE 0
111+
END [Manage],
112+
CASE
113+
WHEN O.[UseTotp] = 1
114+
THEN 1
115+
ELSE 0
116+
END [OrganizationUseTotp]
117+
FROM
118+
[dbo].[CipherDetails](@UserId) C
119+
INNER JOIN
120+
[CTE] OU ON C.[UserId] IS NULL AND C.[OrganizationId] IN (SELECT [OrganizationId] FROM [CTE])
121+
INNER JOIN
122+
[dbo].[Organization] O ON O.[Id] = OU.[OrganizationId] AND O.[Id] = C.[OrganizationId] AND O.[Enabled] = 1
123+
LEFT JOIN
124+
[dbo].[CollectionCipher] CC ON CC.[CipherId] = C.[Id]
125+
LEFT JOIN
126+
[dbo].[CollectionUser] CU ON CU.[CollectionId] = CC.[CollectionId] AND CU.[OrganizationUserId] = OU.[Id]
127+
LEFT JOIN
128+
[dbo].[GroupUser] GU ON CU.[CollectionId] IS NULL AND GU.[OrganizationUserId] = OU.[Id]
129+
LEFT JOIN
130+
[dbo].[Group] G ON G.[Id] = GU.[GroupId]
131+
LEFT JOIN
132+
[dbo].[CollectionGroup] CG ON CG.[CollectionId] = CC.[CollectionId] AND CG.[GroupId] = GU.[GroupId]
133+
WHERE
134+
CU.[CollectionId] IS NOT NULL
135+
OR CG.[CollectionId] IS NOT NULL
136+
137+
UNION ALL
138+
139+
SELECT
140+
C.Id,
141+
C.UserId,
142+
C.OrganizationId,
143+
C.Type,
144+
C.Data,
145+
C.Attachments,
146+
C.CreationDate,
147+
C.RevisionDate,
148+
C.Favorite,
149+
C.FolderId,
150+
C.DeletedDate,
151+
C.ArchivedDate,
152+
C.Reprompt,
153+
C.[Key],
154+
1 [Edit],
155+
1 [ViewPassword],
156+
1 [Manage],
157+
0 [OrganizationUseTotp]
158+
FROM
159+
[dbo].[CipherDetails](@UserId) AS C
160+
WHERE
161+
C.[UserId] = @UserId;
162+
GO

0 commit comments

Comments
 (0)