1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-28 13:15:12 +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 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;

View File

@ -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<ICollection<Cipher>> GetManyByUserIdAsync(Guid userId)
public async Task<ICollection<CipherDetails>> GetManyByUserIdAsync(Guid userId)
{
using(var connection = new SqlConnection(ConnectionString))
{
var results = await connection.QueryAsync<Cipher>(
$"[{Schema}].[{Table}_ReadByUserId]",
var results = await connection.QueryAsync<CipherDetails>(
$"[{Schema}].[CipherDetails_ReadByUserId]",
new { UserId = userId },
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))
{
var results = await connection.QueryAsync<Cipher>(
$"[{Schema}].[{Table}_ReadByTypeUserId]",
var results = await connection.QueryAsync<CipherDetails>(
$"[{Schema}].[CipherDetails_ReadByTypeUserId]",
new
{
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)
{
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<Cipher>();
var ciphers = await results.ReadAsync<CipherDetails>();
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\DeviceView.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\OrganizationView.sql" />
<Build Include="dbo\Views\UserView.sql" />
@ -99,11 +99,11 @@
<Build Include="dbo\Stored Procedures\Favorite_Create.sql" />
<Build Include="dbo\Stored Procedures\Cipher_ReadById.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\Cipher_ReadByTypeUserId.sql" />
<Build Include="dbo\Stored Procedures\CipherDetails_ReadByTypeUserId.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\Cipher_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\Subvault_ReadByOrganizationIdAdminUserId.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\Views\CipherView.sql" />
<Build Include="dbo\Stored Procedures\CipherDetails_ReadById.sql" />
</ItemGroup>
</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),
@UserId UNIQUEIDENTIFIER
AS
@ -8,7 +8,7 @@ BEGIN
SELECT
*
FROM
[dbo].[CipherView]
[dbo].[CipherDetailsView]
WHERE
[RevisionDate] > @SinceRevisionDate
AND [UserId] = @UserId

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,14 +1,12 @@
CREATE TABLE [dbo].[Cipher] (
[Id] UNIQUEIDENTIFIER NOT NULL,
[UserId] UNIQUEIDENTIFIER NOT NULL,
[FolderId] UNIQUEIDENTIFIER NULL,
[UserId] UNIQUEIDENTIFIER NULL,
[OrganizationId] 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,
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])
);

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