1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-29 13:25:17 +01:00

cipher updates

move cipher info to favorites and folders sprocs for getting shared
cipher information
This commit is contained in:
Kyle Spearrin 2017-03-17 09:29:46 -04:00
parent 1d3092b6b2
commit 3e0c0224b5
16 changed files with 96 additions and 64 deletions

View File

@ -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; }
}
}

View File

@ -7,9 +7,8 @@ namespace Bit.Core.Models.Table
{ {
public Guid Id { get; set; } public Guid Id { get; set; }
public Guid UserId { get; set; } public Guid UserId { get; set; }
public Guid? FolderId { get; set; } public Guid? OrganizationId { get; set; }
public Enums.CipherType Type { get; set; } public Enums.CipherType Type { get; set; }
public bool Favorite { get; set; }
public string Data { get; set; } public string Data { get; set; }
public DateTime CreationDate { get; internal set; } = DateTime.UtcNow; public DateTime CreationDate { get; internal set; } = DateTime.UtcNow;
public DateTime RevisionDate { get; internal set; } = DateTime.UtcNow; public DateTime RevisionDate { get; internal set; } = DateTime.UtcNow;

View File

@ -7,6 +7,7 @@ using DataTableProxy;
using Bit.Core.Models.Table; using Bit.Core.Models.Table;
using System.Data; using System.Data;
using Dapper; using Dapper;
using Core.Models.Data;
namespace Bit.Core.Repositories.SqlServer namespace Bit.Core.Repositories.SqlServer
{ {
@ -31,12 +32,12 @@ namespace Bit.Core.Repositories.SqlServer
return cipher; return cipher;
} }
public async Task<ICollection<Cipher>> GetManyByUserIdAsync(Guid userId) public async Task<ICollection<CipherDetails>> GetManyByUserIdAsync(Guid userId)
{ {
using(var connection = new SqlConnection(ConnectionString)) using(var connection = new SqlConnection(ConnectionString))
{ {
var results = await connection.QueryAsync<Cipher>( var results = await connection.QueryAsync<CipherDetails>(
$"[{Schema}].[{Table}_ReadByUserId]", $"[{Schema}].[CipherDetails_ReadByUserId]",
new { UserId = userId }, new { UserId = userId },
commandType: CommandType.StoredProcedure); commandType: CommandType.StoredProcedure);
@ -44,12 +45,12 @@ namespace Bit.Core.Repositories.SqlServer
} }
} }
public async Task<ICollection<Cipher>> GetManyByTypeAndUserIdAsync(Enums.CipherType type, Guid userId) public async Task<ICollection<CipherDetails>> GetManyByTypeAndUserIdAsync(Enums.CipherType type, Guid userId)
{ {
using(var connection = new SqlConnection(ConnectionString)) using(var connection = new SqlConnection(ConnectionString))
{ {
var results = await connection.QueryAsync<Cipher>( var results = await connection.QueryAsync<CipherDetails>(
$"[{Schema}].[{Table}_ReadByTypeUserId]", $"[{Schema}].[CipherDetails_ReadByTypeUserId]",
new new
{ {
Type = type, Type = type,
@ -61,13 +62,13 @@ namespace Bit.Core.Repositories.SqlServer
} }
} }
public async Task<Tuple<ICollection<Cipher>, ICollection<Guid>>> public async Task<Tuple<ICollection<CipherDetails>, ICollection<Guid>>>
GetManySinceRevisionDateAndUserIdWithDeleteHistoryAsync(DateTime sinceRevisionDate, Guid userId) GetManySinceRevisionDateAndUserIdWithDeleteHistoryAsync(DateTime sinceRevisionDate, Guid userId)
{ {
using(var connection = new SqlConnection(ConnectionString)) using(var connection = new SqlConnection(ConnectionString))
{ {
var results = await connection.QueryMultipleAsync( var results = await connection.QueryMultipleAsync(
$"[{Schema}].[{Table}_ReadByRevisionDateUserWithDeleteHistory]", $"[{Schema}].[CipherDetails_ReadByRevisionDateUserWithDeleteHistory]",
new new
{ {
SinceRevisionDate = sinceRevisionDate, SinceRevisionDate = sinceRevisionDate,
@ -75,10 +76,10 @@ namespace Bit.Core.Repositories.SqlServer
}, },
commandType: CommandType.StoredProcedure); commandType: CommandType.StoredProcedure);
var ciphers = await results.ReadAsync<Cipher>(); var ciphers = await results.ReadAsync<CipherDetails>();
var deletes = await results.ReadAsync<Guid>(); var deletes = await results.ReadAsync<Guid>();
return new Tuple<ICollection<Cipher>, ICollection<Guid>>(ciphers.ToList(), deletes.ToList()); return new Tuple<ICollection<CipherDetails>, ICollection<Guid>>(ciphers.ToList(), deletes.ToList());
} }
} }

View File

@ -84,7 +84,7 @@
<Build Include="dbo\Views\SubvaultUserView.sql" /> <Build Include="dbo\Views\SubvaultUserView.sql" />
<Build Include="dbo\Views\DeviceView.sql" /> <Build Include="dbo\Views\DeviceView.sql" />
<Build Include="dbo\Views\HistoryView.sql" /> <Build Include="dbo\Views\HistoryView.sql" />
<Build Include="dbo\Views\CipherView.sql" /> <Build Include="dbo\Views\CipherDetailsView.sql" />
<Build Include="dbo\Views\OrganizationUserView.sql" /> <Build Include="dbo\Views\OrganizationUserView.sql" />
<Build Include="dbo\Views\OrganizationView.sql" /> <Build Include="dbo\Views\OrganizationView.sql" />
<Build Include="dbo\Views\UserView.sql" /> <Build Include="dbo\Views\UserView.sql" />
@ -99,11 +99,11 @@
<Build Include="dbo\Stored Procedures\Favorite_Create.sql" /> <Build Include="dbo\Stored Procedures\Favorite_Create.sql" />
<Build Include="dbo\Stored Procedures\Cipher_ReadById.sql" /> <Build Include="dbo\Stored Procedures\Cipher_ReadById.sql" />
<Build Include="dbo\Stored Procedures\Favorite_Delete.sql" /> <Build Include="dbo\Stored Procedures\Favorite_Delete.sql" />
<Build Include="dbo\Stored Procedures\Cipher_ReadByRevisionDateUserWithDeleteHistory.sql" /> <Build Include="dbo\Stored Procedures\CipherDetails_ReadByRevisionDateUserWithDeleteHistory.sql" />
<Build Include="dbo\Stored Procedures\Folder_Create.sql" /> <Build Include="dbo\Stored Procedures\Folder_Create.sql" />
<Build Include="dbo\Stored Procedures\Cipher_ReadByTypeUserId.sql" /> <Build Include="dbo\Stored Procedures\CipherDetails_ReadByTypeUserId.sql" />
<Build Include="dbo\Stored Procedures\Folder_DeleteById.sql" /> <Build Include="dbo\Stored Procedures\Folder_DeleteById.sql" />
<Build Include="dbo\Stored Procedures\Cipher_ReadByUserId.sql" /> <Build Include="dbo\Stored Procedures\CipherDetails_ReadByUserId.sql" />
<Build Include="dbo\Stored Procedures\Folder_ReadById.sql" /> <Build Include="dbo\Stored Procedures\Folder_ReadById.sql" />
<Build Include="dbo\Stored Procedures\Cipher_Update.sql" /> <Build Include="dbo\Stored Procedures\Cipher_Update.sql" />
<Build Include="dbo\Stored Procedures\Folder_Update.sql" /> <Build Include="dbo\Stored Procedures\Folder_Update.sql" />
@ -159,7 +159,9 @@
<Build Include="dbo\Stored Procedures\SubvaultUserDetails_ReadByUserId.sql" /> <Build Include="dbo\Stored Procedures\SubvaultUserDetails_ReadByUserId.sql" />
<Build Include="dbo\Stored Procedures\Subvault_ReadByOrganizationIdAdminUserId.sql" /> <Build Include="dbo\Stored Procedures\Subvault_ReadByOrganizationIdAdminUserId.sql" />
<Build Include="dbo\Views\SubvaultUserDetailsView.sql" /> <Build Include="dbo\Views\SubvaultUserDetailsView.sql" />
<Build Include="dbo\Stored Procedures\Cipher_ReadByUserIdHasSubvault.sql" /> <Build Include="dbo\Stored Procedures\CipherDetails_ReadByUserIdHasSubvault.sql" />
<Build Include="dbo\Stored Procedures\SubvaultCipher_ReadByUserId.sql" /> <Build Include="dbo\Stored Procedures\SubvaultCipher_ReadByUserId.sql" />
<Build Include="dbo\Views\CipherView.sql" />
<Build Include="dbo\Stored Procedures\CipherDetails_ReadById.sql" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -0,0 +1,13 @@
CREATE PROCEDURE [dbo].[CipherDetails_ReadById]
@Id UNIQUEIDENTIFIER
AS
BEGIN
SET NOCOUNT ON
SELECT
*
FROM
[dbo].[CipherDetailsView]
WHERE
[Id] = @Id
END

View File

@ -1,4 +1,4 @@
CREATE PROCEDURE [dbo].[Cipher_ReadByRevisionDateUserWithDeleteHistory] CREATE PROCEDURE [dbo].[CipherDetails_ReadByRevisionDateUserWithDeleteHistory]
@SinceRevisionDate DATETIME2(7), @SinceRevisionDate DATETIME2(7),
@UserId UNIQUEIDENTIFIER @UserId UNIQUEIDENTIFIER
AS AS
@ -8,7 +8,7 @@ BEGIN
SELECT SELECT
* *
FROM FROM
[dbo].[CipherView] [dbo].[CipherDetailsView]
WHERE WHERE
[RevisionDate] > @SinceRevisionDate [RevisionDate] > @SinceRevisionDate
AND [UserId] = @UserId AND [UserId] = @UserId

View File

@ -1,4 +1,4 @@
CREATE PROCEDURE [dbo].[Cipher_ReadByTypeUserId] CREATE PROCEDURE [dbo].[CipherDetails_ReadByTypeUserId]
@Type TINYINT, @Type TINYINT,
@UserId UNIQUEIDENTIFIER @UserId UNIQUEIDENTIFIER
AS AS
@ -8,7 +8,7 @@ BEGIN
SELECT SELECT
* *
FROM FROM
[dbo].[CipherView] [dbo].[CipherDetailsView]
WHERE WHERE
[Type] = @Type [Type] = @Type
AND [UserId] = @UserId AND [UserId] = @UserId

View File

@ -1,4 +1,4 @@
CREATE PROCEDURE [dbo].[Cipher_ReadByUserId] CREATE PROCEDURE [dbo].[CipherDetails_ReadByUserId]
@UserId UNIQUEIDENTIFIER @UserId UNIQUEIDENTIFIER
AS AS
BEGIN BEGIN
@ -7,7 +7,7 @@ BEGIN
SELECT SELECT
* *
FROM FROM
[dbo].[CipherView] [dbo].[CipherDetailsView]
WHERE WHERE
[UserId] = @UserId [UserId] = @UserId
END END

View File

@ -1,4 +1,4 @@
CREATE PROCEDURE [dbo].[Cipher_ReadByUserIdHasSubvault] CREATE PROCEDURE [dbo].[CipherDetails_ReadByUserIdHasSubvault]
@UserId UNIQUEIDENTIFIER @UserId UNIQUEIDENTIFIER
AS AS
BEGIN BEGIN
@ -7,7 +7,7 @@ BEGIN
SELECT DISTINCT SELECT DISTINCT
C.* C.*
FROM FROM
[dbo].[CipherView] C [dbo].[CipherDetailsView] C
INNER JOIN INNER JOIN
[dbo].[SubvaultCipher] SC ON SC.[CipherId] = C.[Id] [dbo].[SubvaultCipher] SC ON SC.[CipherId] = C.[Id]
INNER JOIN INNER JOIN

View File

@ -1,9 +1,8 @@
CREATE PROCEDURE [dbo].[Cipher_Create] CREATE PROCEDURE [dbo].[Cipher_Create]
@Id UNIQUEIDENTIFIER, @Id UNIQUEIDENTIFIER,
@UserId UNIQUEIDENTIFIER, @UserId UNIQUEIDENTIFIER,
@FolderId UNIQUEIDENTIFIER, @OrganizationId UNIQUEIDENTIFIER,
@Type TINYINT, @Type TINYINT,
@Favorite BIT,
@Data NVARCHAR(MAX), @Data NVARCHAR(MAX),
@CreationDate DATETIME2(7), @CreationDate DATETIME2(7),
@RevisionDate DATETIME2(7) @RevisionDate DATETIME2(7)
@ -15,9 +14,8 @@ BEGIN
( (
[Id], [Id],
[UserId], [UserId],
[FolderId], [OrganizationId],
[Type], [Type],
[Favorite],
[Data], [Data],
[CreationDate], [CreationDate],
[RevisionDate] [RevisionDate]
@ -26,9 +24,8 @@ BEGIN
( (
@Id, @Id,
@UserId, @UserId,
@FolderId, @OrganizationId,
@Type, @Type,
@Favorite,
@Data, @Data,
@CreationDate, @CreationDate,
@RevisionDate @RevisionDate

View File

@ -4,21 +4,9 @@ AS
BEGIN BEGIN
SET NOCOUNT ON SET NOCOUNT ON
BEGIN TRANSACTION Cipher_DeleteById
UPDATE
[dbo].[Cipher]
SET
[FolderId] = NULL,
[RevisionDate] = GETUTCDATE()
WHERE
[FolderId] = @Id
DELETE DELETE
FROM FROM
[dbo].[Cipher] [dbo].[Cipher]
WHERE WHERE
[Id] = @Id [Id] = @Id
COMMIT TRANSACTION Cipher_DeleteById
END END

View File

@ -1,9 +1,8 @@
CREATE PROCEDURE [dbo].[Cipher_Update] CREATE PROCEDURE [dbo].[Cipher_Update]
@Id UNIQUEIDENTIFIER, @Id UNIQUEIDENTIFIER,
@UserId UNIQUEIDENTIFIER, @UserId UNIQUEIDENTIFIER,
@FolderId UNIQUEIDENTIFIER, @OrganizationId UNIQUEIDENTIFIER,
@Type TINYINT, @Type TINYINT,
@Favorite BIT,
@Data NVARCHAR(MAX), @Data NVARCHAR(MAX),
@CreationDate DATETIME2(7), @CreationDate DATETIME2(7),
@RevisionDate DATETIME2(7) @RevisionDate DATETIME2(7)
@ -15,9 +14,8 @@ BEGIN
[dbo].[Cipher] [dbo].[Cipher]
SET SET
[UserId] = @UserId, [UserId] = @UserId,
[FolderId] = @FolderId, [OrganizationId] = @OrganizationId,
[Type] = @Type, [Type] = @Type,
[Favorite] = @Favorite,
[Data] = @Data, [Data] = @Data,
[CreationDate] = @CreationDate, [CreationDate] = @CreationDate,
[RevisionDate] = @RevisionDate [RevisionDate] = @RevisionDate

View File

@ -15,7 +15,6 @@ BEGIN
[dbo].[Cipher] [dbo].[Cipher]
WHERE WHERE
[UserId] = @Id [UserId] = @Id
AND [Type] > 0
SET @BatchSize = @@ROWCOUNT SET @BatchSize = @@ROWCOUNT
@ -24,13 +23,6 @@ BEGIN
BEGIN TRANSACTION User_DeleteById BEGIN TRANSACTION User_DeleteById
DELETE
FROM
[dbo].[Cipher]
WHERE
[UserId] = @Id
AND [Type] = 0
DELETE DELETE
FROM FROM
[dbo].[Device] [dbo].[Device]

View File

@ -1,14 +1,12 @@
CREATE TABLE [dbo].[Cipher] ( CREATE TABLE [dbo].[Cipher] (
[Id] UNIQUEIDENTIFIER NOT NULL, [Id] UNIQUEIDENTIFIER NOT NULL,
[UserId] UNIQUEIDENTIFIER NOT NULL, [UserId] UNIQUEIDENTIFIER NULL,
[FolderId] UNIQUEIDENTIFIER NULL, [OrganizationId] UNIQUEIDENTIFIER NULL,
[Type] TINYINT NOT NULL, [Type] TINYINT NOT NULL,
[Favorite] BIT NOT NULL, [Data] NVARCHAR (MAX) NOT NULL,
[Data] NVARCHAR (MAX) NOT NULL, [CreationDate] DATETIME2 (7) NOT NULL,
[CreationDate] DATETIME2 (7) NOT NULL, [RevisionDate] DATETIME2 (7) NOT NULL,
[RevisionDate] DATETIME2 (7) NOT NULL,
CONSTRAINT [PK_Cipher] PRIMARY KEY CLUSTERED ([Id] ASC), 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]) CONSTRAINT [FK_Cipher_User] FOREIGN KEY ([UserId]) REFERENCES [dbo].[User] ([Id])
); );

View File

@ -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]

View File

@ -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