diff --git a/src/Sql/Sql.sqlproj b/src/Sql/Sql.sqlproj
index 8e1a17205d..92f43b9992 100644
--- a/src/Sql/Sql.sqlproj
+++ b/src/Sql/Sql.sqlproj
@@ -193,5 +193,8 @@
+
+
+
\ No newline at end of file
diff --git a/src/Sql/dbo/Functions/UserCipherDetails.sql b/src/Sql/dbo/Functions/UserCipherDetails.sql
new file mode 100644
index 0000000000..73a24d8349
--- /dev/null
+++ b/src/Sql/dbo/Functions/UserCipherDetails.sql
@@ -0,0 +1,38 @@
+CREATE FUNCTION [dbo].[UserCipherDetails](@UserId UNIQUEIDENTIFIER)
+RETURNS TABLE
+AS RETURN
+SELECT
+ C.*,
+ CASE
+ WHEN C.[UserId] IS NOT NULL OR OU.[AccessAll] = 1 OR CU.[ReadOnly] = 0 OR G.[AccessAll] = 1 OR CG.[ReadOnly] = 0 THEN 1
+ ELSE 0
+ END [Edit]
+FROM
+ [dbo].[CipherDetails](@UserId) C
+LEFT JOIN
+ [dbo].[Organization] O ON C.[UserId] IS NULL AND O.[Id] = C.[OrganizationId]
+LEFT JOIN
+ [dbo].[OrganizationUser] OU ON OU.[OrganizationId] = O.[Id] AND OU.[UserId] = @UserId
+LEFT JOIN
+ [dbo].[CollectionCipher] CC ON C.[UserId] IS NULL AND OU.[AccessAll] = 0 AND 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 C.[UserId] IS NULL AND CU.[CollectionId] IS NULL AND OU.[AccessAll] = 0 AND GU.[OrganizationUserId] = OU.[Id]
+LEFT JOIN
+ [dbo].[Group] G ON G.[Id] = GU.[GroupId]
+LEFT JOIN
+ [dbo].[CollectionGroup] CG ON G.[AccessAll] = 0 AND CG.[CollectionId] = CC.[CollectionId] AND CG.[GroupId] = GU.[GroupId]
+WHERE
+ C.[UserId] = @UserId
+ OR (
+ C.[UserId] IS NULL
+ AND OU.[Status] = 2 -- 2 = Confirmed
+ AND O.[Enabled] = 1
+ AND (
+ OU.[AccessAll] = 1
+ OR CU.[CollectionId] IS NOT NULL
+ OR G.[AccessAll] = 1
+ OR CG.[CollectionId] IS NOT NULL
+ )
+ )
\ No newline at end of file
diff --git a/src/Sql/dbo/Stored Procedures/CipherDetails_ReadByIdUserId.sql b/src/Sql/dbo/Stored Procedures/CipherDetails_ReadByIdUserId.sql
index 1d3cb1ea5c..e2fb2629bd 100644
--- a/src/Sql/dbo/Stored Procedures/CipherDetails_ReadByIdUserId.sql
+++ b/src/Sql/dbo/Stored Procedures/CipherDetails_ReadByIdUserId.sql
@@ -6,43 +6,11 @@ BEGIN
SET NOCOUNT ON
SELECT TOP 1
- C.*,
- CASE
- WHEN C.[UserId] IS NOT NULL OR OU.[AccessAll] = 1 OR CU.[ReadOnly] = 0 OR G.[AccessAll] = 1 OR CG.[ReadOnly] = 0 THEN 1
- ELSE 0
- END [Edit]
+ *
FROM
- [dbo].[CipherDetails](@UserId) C
- LEFT JOIN
- [dbo].[Organization] O ON C.[UserId] IS NULL AND O.[Id] = C.[OrganizationId]
- LEFT JOIN
- [dbo].[OrganizationUser] OU ON OU.[OrganizationId] = O.[Id] AND OU.[UserId] = @UserId
- LEFT JOIN
- [dbo].[CollectionCipher] CC ON C.[UserId] IS NULL AND OU.[AccessAll] = 0 AND 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 C.[UserId] IS NULL AND CU.[CollectionId] IS NULL AND OU.[AccessAll] = 0 AND GU.[OrganizationUserId] = OU.[Id]
- LEFT JOIN
- [dbo].[Group] G ON G.[Id] = GU.[GroupId]
- LEFT JOIN
- [dbo].[CollectionGroup] CG ON G.[AccessAll] = 0 AND CG.[CollectionId] = CC.[CollectionId] AND CG.[GroupId] = GU.[GroupId]
+ [dbo].[UserCipherDetails](@UserId)
WHERE
- C.Id = @Id
- AND (
- C.[UserId] = @UserId
- OR (
- C.[UserId] IS NULL
- AND OU.[Status] = 2 -- 2 = Confirmed
- AND O.[Enabled] = 1
- AND (
- OU.[AccessAll] = 1
- OR CU.[CollectionId] IS NOT NULL
- OR G.[AccessAll] = 1
- OR CG.[CollectionId] IS NOT NULL
- )
- )
- )
+ [Id] = @Id
ORDER BY
[Edit] DESC
END
\ No newline at end of file
diff --git a/src/Sql/dbo/Stored Procedures/CipherDetails_ReadByTypeUserId.sql b/src/Sql/dbo/Stored Procedures/CipherDetails_ReadByTypeUserId.sql
index a8245a17d2..7c0dff1e74 100644
--- a/src/Sql/dbo/Stored Procedures/CipherDetails_ReadByTypeUserId.sql
+++ b/src/Sql/dbo/Stored Procedures/CipherDetails_ReadByTypeUserId.sql
@@ -6,41 +6,9 @@ BEGIN
SET NOCOUNT ON
SELECT
- C.*,
- CASE
- WHEN C.[UserId] IS NOT NULL OR OU.[AccessAll] = 1 OR CU.[ReadOnly] = 0 OR G.[AccessAll] = 1 OR CG.[ReadOnly] = 0 THEN 1
- ELSE 0
- END [Edit]
+ *
FROM
- [dbo].[CipherDetails](@UserId) C
- LEFT JOIN
- [dbo].[Organization] O ON C.[UserId] IS NULL AND O.[Id] = C.[OrganizationId]
- LEFT JOIN
- [dbo].[OrganizationUser] OU ON OU.[OrganizationId] = O.[Id] AND OU.[UserId] = @UserId
- LEFT JOIN
- [dbo].[CollectionCipher] CC ON C.[UserId] IS NULL AND OU.[AccessAll] = 0 AND 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 C.[UserId] IS NULL AND CU.[CollectionId] IS NULL AND OU.[AccessAll] = 0 AND GU.[OrganizationUserId] = OU.[Id]
- LEFT JOIN
- [dbo].[Group] G ON G.[Id] = GU.[GroupId]
- LEFT JOIN
- [dbo].[CollectionGroup] CG ON G.[AccessAll] = 0 AND CG.[CollectionId] = CC.[CollectionId] AND CG.[GroupId] = GU.[GroupId]
+ [dbo].[UserCipherDetails](@UserId)
WHERE
- C.[Type] = @Type
- AND (
- C.[UserId] = @UserId
- OR (
- C.[UserId] IS NULL
- AND OU.[Status] = 2 -- 2 = Confirmed
- AND O.[Enabled] = 1
- AND (
- OU.[AccessAll] = 1
- OR CU.[CollectionId] IS NOT NULL
- OR G.[AccessAll] = 1
- OR CG.[CollectionId] IS NOT NULL
- )
- )
- )
+ [Type] = @Type
END
\ No newline at end of file
diff --git a/src/Sql/dbo/Stored Procedures/CipherDetails_ReadByUserId.sql b/src/Sql/dbo/Stored Procedures/CipherDetails_ReadByUserId.sql
index c3ac47e4a2..00f265b3e7 100644
--- a/src/Sql/dbo/Stored Procedures/CipherDetails_ReadByUserId.sql
+++ b/src/Sql/dbo/Stored Procedures/CipherDetails_ReadByUserId.sql
@@ -5,38 +5,7 @@ BEGIN
SET NOCOUNT ON
SELECT
- C.*,
- CASE
- WHEN C.[UserId] IS NOT NULL OR OU.[AccessAll] = 1 OR CU.[ReadOnly] = 0 OR G.[AccessAll] = 1 OR CG.[ReadOnly] = 0 THEN 1
- ELSE 0
- END [Edit]
+ *
FROM
- [dbo].[CipherDetails](@UserId) C
- LEFT JOIN
- [dbo].[Organization] O ON C.[UserId] IS NULL AND O.[Id] = C.[OrganizationId]
- LEFT JOIN
- [dbo].[OrganizationUser] OU ON OU.[OrganizationId] = O.[Id] AND OU.[UserId] = @UserId
- LEFT JOIN
- [dbo].[CollectionCipher] CC ON C.[UserId] IS NULL AND OU.[AccessAll] = 0 AND 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 C.[UserId] IS NULL AND CU.[CollectionId] IS NULL AND OU.[AccessAll] = 0 AND GU.[OrganizationUserId] = OU.[Id]
- LEFT JOIN
- [dbo].[Group] G ON G.[Id] = GU.[GroupId]
- LEFT JOIN
- [dbo].[CollectionGroup] CG ON G.[AccessAll] = 0 AND CG.[CollectionId] = CC.[CollectionId] AND CG.[GroupId] = GU.[GroupId]
- WHERE
- C.[UserId] = @UserId
- OR (
- C.[UserId] IS NULL
- AND OU.[Status] = 2 -- 2 = Confirmed
- AND O.[Enabled] = 1
- AND (
- OU.[AccessAll] = 1
- OR CU.[CollectionId] IS NOT NULL
- OR G.[AccessAll] = 1
- OR CG.[CollectionId] IS NOT NULL
- )
- )
+ [dbo].[UserCipherDetails](@UserId)
END
\ No newline at end of file
diff --git a/src/Sql/dbo/Stored Procedures/Cipher_Delete.sql b/src/Sql/dbo/Stored Procedures/Cipher_Delete.sql
new file mode 100644
index 0000000000..15701bce6e
--- /dev/null
+++ b/src/Sql/dbo/Stored Procedures/Cipher_Delete.sql
@@ -0,0 +1,24 @@
+CREATE PROCEDURE [dbo].[Cipher_Delete]
+ @Ids AS [dbo].[GuidIdArray] READONLY,
+ @UserId AS UNIQUEIDENTIFIER
+AS
+BEGIN
+ SET NOCOUNT ON
+
+ ;WITH [CTE] AS (
+ SELECT
+ [Id],
+ [Edit]
+ FROM
+ [dbo].[UserCipherDetails](@UserId)
+ )
+ DELETE
+ FROM
+ [CTE]
+ WHERE
+ [Edit] = 1
+ AND [Id] IN (@Ids)
+
+ EXEC [dbo].[User_BumpAccountRevisionDate] @UserId
+ -- TODO: What if some that were deleted were organization ciphers? Then bump by org ids.
+END
\ No newline at end of file
diff --git a/src/Sql/dbo/Stored Procedures/Cipher_Move.sql b/src/Sql/dbo/Stored Procedures/Cipher_Move.sql
new file mode 100644
index 0000000000..06360aed47
--- /dev/null
+++ b/src/Sql/dbo/Stored Procedures/Cipher_Move.sql
@@ -0,0 +1,27 @@
+CREATE PROCEDURE [dbo].[Cipher_Move]
+ @Ids AS [dbo].[GuidIdArray] READONLY,
+ @FolderId AS UNIQUEIDENTIFIER,
+ @UserId AS UNIQUEIDENTIFIER
+AS
+BEGIN
+ SET NOCOUNT ON
+
+ ;WITH [CTE] AS (
+ SELECT
+ [Id],
+ [Edit],
+ [FolderId]
+ FROM
+ [dbo].[UserCipherDetails](@UserId)
+ )
+ UPDATE
+ [CTE]
+ SET
+ [FolderId] = @FolderId
+ WHERE
+ [Edit] = 1
+ AND [Id] IN (@Ids)
+
+ EXEC [dbo].[User_BumpAccountRevisionDate] @UserId
+ -- TODO: What if some that were updated were organization ciphers? Then bump by org ids.
+END
\ No newline at end of file