diff --git a/src/Core/Models/Data/CipherDetails.cs b/src/Core/Models/Data/CipherDetails.cs new file mode 100644 index 000000000..ad04868a8 --- /dev/null +++ b/src/Core/Models/Data/CipherDetails.cs @@ -0,0 +1,18 @@ +using Bit.Core.Enums; +using System; + +namespace Core.Models.Data +{ + class CipherDetails + { + public Guid Id { get; set; } + public Guid? UserId { get; set; } + public Guid? OrganizationId { get; set; } + public Guid? FolderId { get; set; } + public CipherType Type { get; set; } + public bool Favorite { get; set; } + public string Data { get; set; } + public DateTime CreationDate { get; set; } + public DateTime RevisionDate { get; set; } + } +} diff --git a/src/Core/Models/Table/Cipher.cs b/src/Core/Models/Table/Cipher.cs index ccd53e168..0d7cc3d04 100644 --- a/src/Core/Models/Table/Cipher.cs +++ b/src/Core/Models/Table/Cipher.cs @@ -7,9 +7,8 @@ namespace Bit.Core.Models.Table { public Guid Id { get; set; } public Guid UserId { get; set; } - public Guid? FolderId { get; set; } + public Guid? OrganizationId { get; set; } public Enums.CipherType Type { get; set; } - public bool Favorite { get; set; } public string Data { get; set; } public DateTime CreationDate { get; internal set; } = DateTime.UtcNow; public DateTime RevisionDate { get; internal set; } = DateTime.UtcNow; diff --git a/src/Core/Repositories/SqlServer/CipherRepository.cs b/src/Core/Repositories/SqlServer/CipherRepository.cs index 59f410ce1..0527b55a2 100644 --- a/src/Core/Repositories/SqlServer/CipherRepository.cs +++ b/src/Core/Repositories/SqlServer/CipherRepository.cs @@ -7,6 +7,7 @@ using DataTableProxy; using Bit.Core.Models.Table; using System.Data; using Dapper; +using Core.Models.Data; namespace Bit.Core.Repositories.SqlServer { @@ -31,12 +32,12 @@ namespace Bit.Core.Repositories.SqlServer return cipher; } - public async Task> GetManyByUserIdAsync(Guid userId) + public async Task> GetManyByUserIdAsync(Guid userId) { using(var connection = new SqlConnection(ConnectionString)) { - var results = await connection.QueryAsync( - $"[{Schema}].[{Table}_ReadByUserId]", + var results = await connection.QueryAsync( + $"[{Schema}].[CipherDetails_ReadByUserId]", new { UserId = userId }, commandType: CommandType.StoredProcedure); @@ -44,12 +45,12 @@ namespace Bit.Core.Repositories.SqlServer } } - public async Task> GetManyByTypeAndUserIdAsync(Enums.CipherType type, Guid userId) + public async Task> GetManyByTypeAndUserIdAsync(Enums.CipherType type, Guid userId) { using(var connection = new SqlConnection(ConnectionString)) { - var results = await connection.QueryAsync( - $"[{Schema}].[{Table}_ReadByTypeUserId]", + var results = await connection.QueryAsync( + $"[{Schema}].[CipherDetails_ReadByTypeUserId]", new { Type = type, @@ -61,13 +62,13 @@ namespace Bit.Core.Repositories.SqlServer } } - public async Task, ICollection>> + public async Task, ICollection>> GetManySinceRevisionDateAndUserIdWithDeleteHistoryAsync(DateTime sinceRevisionDate, Guid userId) { using(var connection = new SqlConnection(ConnectionString)) { var results = await connection.QueryMultipleAsync( - $"[{Schema}].[{Table}_ReadByRevisionDateUserWithDeleteHistory]", + $"[{Schema}].[CipherDetails_ReadByRevisionDateUserWithDeleteHistory]", new { SinceRevisionDate = sinceRevisionDate, @@ -75,10 +76,10 @@ namespace Bit.Core.Repositories.SqlServer }, commandType: CommandType.StoredProcedure); - var ciphers = await results.ReadAsync(); + var ciphers = await results.ReadAsync(); var deletes = await results.ReadAsync(); - return new Tuple, ICollection>(ciphers.ToList(), deletes.ToList()); + return new Tuple, ICollection>(ciphers.ToList(), deletes.ToList()); } } diff --git a/src/Sql/Sql.sqlproj b/src/Sql/Sql.sqlproj index 84592c2a5..93c24a0e5 100644 --- a/src/Sql/Sql.sqlproj +++ b/src/Sql/Sql.sqlproj @@ -84,7 +84,7 @@ - + @@ -99,11 +99,11 @@ - + - + - + @@ -159,7 +159,9 @@ - + + + \ No newline at end of file diff --git a/src/Sql/dbo/Stored Procedures/CipherDetails_ReadById.sql b/src/Sql/dbo/Stored Procedures/CipherDetails_ReadById.sql new file mode 100644 index 000000000..6463096a0 --- /dev/null +++ b/src/Sql/dbo/Stored Procedures/CipherDetails_ReadById.sql @@ -0,0 +1,13 @@ +CREATE PROCEDURE [dbo].[CipherDetails_ReadById] + @Id UNIQUEIDENTIFIER +AS +BEGIN + SET NOCOUNT ON + + SELECT + * + FROM + [dbo].[CipherDetailsView] + WHERE + [Id] = @Id +END \ No newline at end of file diff --git a/src/Sql/dbo/Stored Procedures/Cipher_ReadByRevisionDateUserWithDeleteHistory.sql b/src/Sql/dbo/Stored Procedures/CipherDetails_ReadByRevisionDateUserWithDeleteHistory.sql similarity index 77% rename from src/Sql/dbo/Stored Procedures/Cipher_ReadByRevisionDateUserWithDeleteHistory.sql rename to src/Sql/dbo/Stored Procedures/CipherDetails_ReadByRevisionDateUserWithDeleteHistory.sql index 64640d46b..310e5fe64 100644 --- a/src/Sql/dbo/Stored Procedures/Cipher_ReadByRevisionDateUserWithDeleteHistory.sql +++ b/src/Sql/dbo/Stored Procedures/CipherDetails_ReadByRevisionDateUserWithDeleteHistory.sql @@ -1,4 +1,4 @@ -CREATE PROCEDURE [dbo].[Cipher_ReadByRevisionDateUserWithDeleteHistory] +CREATE PROCEDURE [dbo].[CipherDetails_ReadByRevisionDateUserWithDeleteHistory] @SinceRevisionDate DATETIME2(7), @UserId UNIQUEIDENTIFIER AS @@ -8,7 +8,7 @@ BEGIN SELECT * FROM - [dbo].[CipherView] + [dbo].[CipherDetailsView] WHERE [RevisionDate] > @SinceRevisionDate AND [UserId] = @UserId diff --git a/src/Sql/dbo/Stored Procedures/Cipher_ReadByTypeUserId.sql b/src/Sql/dbo/Stored Procedures/CipherDetails_ReadByTypeUserId.sql similarity index 65% rename from src/Sql/dbo/Stored Procedures/Cipher_ReadByTypeUserId.sql rename to src/Sql/dbo/Stored Procedures/CipherDetails_ReadByTypeUserId.sql index 5d3f3bd0a..a10772ce6 100644 --- a/src/Sql/dbo/Stored Procedures/Cipher_ReadByTypeUserId.sql +++ b/src/Sql/dbo/Stored Procedures/CipherDetails_ReadByTypeUserId.sql @@ -1,4 +1,4 @@ -CREATE PROCEDURE [dbo].[Cipher_ReadByTypeUserId] +CREATE PROCEDURE [dbo].[CipherDetails_ReadByTypeUserId] @Type TINYINT, @UserId UNIQUEIDENTIFIER AS @@ -8,7 +8,7 @@ BEGIN SELECT * FROM - [dbo].[CipherView] + [dbo].[CipherDetailsView] WHERE [Type] = @Type AND [UserId] = @UserId diff --git a/src/Sql/dbo/Stored Procedures/Cipher_ReadByUserId.sql b/src/Sql/dbo/Stored Procedures/CipherDetails_ReadByUserId.sql similarity index 58% rename from src/Sql/dbo/Stored Procedures/Cipher_ReadByUserId.sql rename to src/Sql/dbo/Stored Procedures/CipherDetails_ReadByUserId.sql index b04592c22..5b6a1e1e8 100644 --- a/src/Sql/dbo/Stored Procedures/Cipher_ReadByUserId.sql +++ b/src/Sql/dbo/Stored Procedures/CipherDetails_ReadByUserId.sql @@ -1,4 +1,4 @@ -CREATE PROCEDURE [dbo].[Cipher_ReadByUserId] +CREATE PROCEDURE [dbo].[CipherDetails_ReadByUserId] @UserId UNIQUEIDENTIFIER AS BEGIN @@ -7,7 +7,7 @@ BEGIN SELECT * FROM - [dbo].[CipherView] + [dbo].[CipherDetailsView] WHERE [UserId] = @UserId END \ No newline at end of file diff --git a/src/Sql/dbo/Stored Procedures/Cipher_ReadByUserIdHasSubvault.sql b/src/Sql/dbo/Stored Procedures/CipherDetails_ReadByUserIdHasSubvault.sql similarity index 79% rename from src/Sql/dbo/Stored Procedures/Cipher_ReadByUserIdHasSubvault.sql rename to src/Sql/dbo/Stored Procedures/CipherDetails_ReadByUserIdHasSubvault.sql index 592bd7184..9507d97af 100644 --- a/src/Sql/dbo/Stored Procedures/Cipher_ReadByUserIdHasSubvault.sql +++ b/src/Sql/dbo/Stored Procedures/CipherDetails_ReadByUserIdHasSubvault.sql @@ -1,4 +1,4 @@ -CREATE PROCEDURE [dbo].[Cipher_ReadByUserIdHasSubvault] +CREATE PROCEDURE [dbo].[CipherDetails_ReadByUserIdHasSubvault] @UserId UNIQUEIDENTIFIER AS BEGIN @@ -7,7 +7,7 @@ BEGIN SELECT DISTINCT C.* FROM - [dbo].[CipherView] C + [dbo].[CipherDetailsView] C INNER JOIN [dbo].[SubvaultCipher] SC ON SC.[CipherId] = C.[Id] INNER JOIN diff --git a/src/Sql/dbo/Stored Procedures/Cipher_Create.sql b/src/Sql/dbo/Stored Procedures/Cipher_Create.sql index 4c7894909..dc3fe7693 100644 --- a/src/Sql/dbo/Stored Procedures/Cipher_Create.sql +++ b/src/Sql/dbo/Stored Procedures/Cipher_Create.sql @@ -1,9 +1,8 @@ CREATE PROCEDURE [dbo].[Cipher_Create] @Id UNIQUEIDENTIFIER, @UserId UNIQUEIDENTIFIER, - @FolderId UNIQUEIDENTIFIER, + @OrganizationId UNIQUEIDENTIFIER, @Type TINYINT, - @Favorite BIT, @Data NVARCHAR(MAX), @CreationDate DATETIME2(7), @RevisionDate DATETIME2(7) @@ -15,9 +14,8 @@ BEGIN ( [Id], [UserId], - [FolderId], + [OrganizationId], [Type], - [Favorite], [Data], [CreationDate], [RevisionDate] @@ -26,9 +24,8 @@ BEGIN ( @Id, @UserId, - @FolderId, + @OrganizationId, @Type, - @Favorite, @Data, @CreationDate, @RevisionDate diff --git a/src/Sql/dbo/Stored Procedures/Cipher_DeleteById.sql b/src/Sql/dbo/Stored Procedures/Cipher_DeleteById.sql index 2e4d0f44a..025ac74af 100644 --- a/src/Sql/dbo/Stored Procedures/Cipher_DeleteById.sql +++ b/src/Sql/dbo/Stored Procedures/Cipher_DeleteById.sql @@ -4,21 +4,9 @@ AS BEGIN SET NOCOUNT ON - BEGIN TRANSACTION Cipher_DeleteById - - UPDATE - [dbo].[Cipher] - SET - [FolderId] = NULL, - [RevisionDate] = GETUTCDATE() - WHERE - [FolderId] = @Id - DELETE FROM [dbo].[Cipher] WHERE [Id] = @Id - - COMMIT TRANSACTION Cipher_DeleteById END \ No newline at end of file diff --git a/src/Sql/dbo/Stored Procedures/Cipher_Update.sql b/src/Sql/dbo/Stored Procedures/Cipher_Update.sql index 19190a73c..8c05b46a3 100644 --- a/src/Sql/dbo/Stored Procedures/Cipher_Update.sql +++ b/src/Sql/dbo/Stored Procedures/Cipher_Update.sql @@ -1,9 +1,8 @@ CREATE PROCEDURE [dbo].[Cipher_Update] @Id UNIQUEIDENTIFIER, @UserId UNIQUEIDENTIFIER, - @FolderId UNIQUEIDENTIFIER, + @OrganizationId UNIQUEIDENTIFIER, @Type TINYINT, - @Favorite BIT, @Data NVARCHAR(MAX), @CreationDate DATETIME2(7), @RevisionDate DATETIME2(7) @@ -15,9 +14,8 @@ BEGIN [dbo].[Cipher] SET [UserId] = @UserId, - [FolderId] = @FolderId, + [OrganizationId] = @OrganizationId, [Type] = @Type, - [Favorite] = @Favorite, [Data] = @Data, [CreationDate] = @CreationDate, [RevisionDate] = @RevisionDate diff --git a/src/Sql/dbo/Stored Procedures/User_DeleteById.sql b/src/Sql/dbo/Stored Procedures/User_DeleteById.sql index 1c1fdb030..9ceac6033 100644 --- a/src/Sql/dbo/Stored Procedures/User_DeleteById.sql +++ b/src/Sql/dbo/Stored Procedures/User_DeleteById.sql @@ -15,7 +15,6 @@ BEGIN [dbo].[Cipher] WHERE [UserId] = @Id - AND [Type] > 0 SET @BatchSize = @@ROWCOUNT @@ -24,13 +23,6 @@ BEGIN BEGIN TRANSACTION User_DeleteById - DELETE - FROM - [dbo].[Cipher] - WHERE - [UserId] = @Id - AND [Type] = 0 - DELETE FROM [dbo].[Device] diff --git a/src/Sql/dbo/Tables/Cipher.sql b/src/Sql/dbo/Tables/Cipher.sql index 719e147b0..f5816377a 100644 --- a/src/Sql/dbo/Tables/Cipher.sql +++ b/src/Sql/dbo/Tables/Cipher.sql @@ -1,14 +1,12 @@ CREATE TABLE [dbo].[Cipher] ( - [Id] UNIQUEIDENTIFIER NOT NULL, - [UserId] UNIQUEIDENTIFIER NOT NULL, - [FolderId] UNIQUEIDENTIFIER NULL, - [Type] TINYINT NOT NULL, - [Favorite] BIT NOT NULL, - [Data] NVARCHAR (MAX) NOT NULL, - [CreationDate] DATETIME2 (7) NOT NULL, - [RevisionDate] DATETIME2 (7) NOT NULL, + [Id] UNIQUEIDENTIFIER NOT NULL, + [UserId] UNIQUEIDENTIFIER NULL, + [OrganizationId] UNIQUEIDENTIFIER NULL, + [Type] TINYINT NOT NULL, + [Data] NVARCHAR (MAX) NOT NULL, + [CreationDate] DATETIME2 (7) NOT NULL, + [RevisionDate] DATETIME2 (7) NOT NULL, CONSTRAINT [PK_Cipher] PRIMARY KEY CLUSTERED ([Id] ASC), - CONSTRAINT [FK_Cipher_Folder] FOREIGN KEY ([FolderId]) REFERENCES [dbo].[Cipher] ([Id]), CONSTRAINT [FK_Cipher_User] FOREIGN KEY ([UserId]) REFERENCES [dbo].[User] ([Id]) ); diff --git a/src/Sql/dbo/Views/CipherDetailsView.sql b/src/Sql/dbo/Views/CipherDetailsView.sql new file mode 100644 index 000000000..4ac8cd8c0 --- /dev/null +++ b/src/Sql/dbo/Views/CipherDetailsView.sql @@ -0,0 +1,12 @@ +CREATE VIEW [dbo].[CipherDetailsView] +AS +SELECT + C.*, + CASE WHEN F.[CipherId] IS NULL THEN 0 ELSE 1 END [Favorite], + FC.[FolderId] +FROM + [dbo].[Cipher] C +LEFT JOIN + [dbo].[Favorite] F ON F.[CipherId] = C.[Id] +LEFT JOIN + [dbo].[FolderCipher] FC ON FC.[CipherId] = C.[Id] \ No newline at end of file diff --git a/src/SqlUpdate/2017-16-03_01_CipherMigration.sql b/src/SqlUpdate/2017-16-03_01_CipherMigration.sql new file mode 100644 index 000000000..b93836ce4 --- /dev/null +++ b/src/SqlUpdate/2017-16-03_01_CipherMigration.sql @@ -0,0 +1,14 @@ +insert into folder +select Id, UserId, JSON_VALUE(Data,'$.Name') AS [Name], CreationDate, RevisionDate +from cipher +where [type] = 0 + +insert into foldercipher +select FolderId, Id +from cipher +where [FolderId] is not null + +insert into favorite +select UserId, [Id] +from cipher +where Favorite = 1