2023-11-22 15:44:25 -05:00
|
|
|
|
#nullable enable
|
|
|
|
|
|
|
|
|
|
|
|
using System.Data;
|
2026-01-28 07:13:25 -07:00
|
|
|
|
using Bit.Core;
|
2024-11-21 10:17:04 -08:00
|
|
|
|
using Bit.Core.KeyManagement.UserKey;
|
2021-02-22 15:35:16 -06:00
|
|
|
|
using Bit.Core.Settings;
|
2023-04-18 14:05:17 +02:00
|
|
|
|
using Bit.Core.Tools.Entities;
|
|
|
|
|
|
using Bit.Core.Tools.Repositories;
|
|
|
|
|
|
using Bit.Infrastructure.Dapper.Repositories;
|
2023-12-12 11:58:34 -05:00
|
|
|
|
using Bit.Infrastructure.Dapper.Tools.Helpers;
|
2020-11-02 15:55:49 -05:00
|
|
|
|
using Dapper;
|
2026-01-28 07:13:25 -07:00
|
|
|
|
using Microsoft.AspNetCore.DataProtection;
|
2023-01-10 15:58:41 -05:00
|
|
|
|
using Microsoft.Data.SqlClient;
|
2020-11-02 15:55:49 -05:00
|
|
|
|
|
2023-04-18 14:05:17 +02:00
|
|
|
|
namespace Bit.Infrastructure.Dapper.Tools.Repositories;
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2023-11-22 15:44:25 -05:00
|
|
|
|
/// <inheritdoc cref="ISendRepository" />
|
2022-01-11 10:40:51 +01:00
|
|
|
|
public class SendRepository : Repository<Send, Guid>, ISendRepository
|
2020-11-02 15:55:49 -05:00
|
|
|
|
{
|
2026-01-28 07:13:25 -07:00
|
|
|
|
private readonly IDataProtector _dataProtector;
|
|
|
|
|
|
|
|
|
|
|
|
public SendRepository(GlobalSettings globalSettings, IDataProtectionProvider dataProtectionProvider)
|
|
|
|
|
|
: this(globalSettings.SqlServer.ConnectionString, globalSettings.SqlServer.ReadOnlyConnectionString, dataProtectionProvider)
|
2020-11-02 15:55:49 -05:00
|
|
|
|
{ }
|
|
|
|
|
|
|
2026-01-28 07:13:25 -07:00
|
|
|
|
public SendRepository(string connectionString, string readOnlyConnectionString, IDataProtectionProvider dataProtectionProvider)
|
2020-11-02 15:55:49 -05:00
|
|
|
|
: base(connectionString, readOnlyConnectionString)
|
2026-01-28 07:13:25 -07:00
|
|
|
|
{
|
|
|
|
|
|
_dataProtector = dataProtectionProvider.CreateProtector(Constants.DatabaseFieldProtectorPurpose);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override async Task<Send?> GetByIdAsync(Guid id)
|
|
|
|
|
|
{
|
|
|
|
|
|
var send = await base.GetByIdAsync(id);
|
|
|
|
|
|
UnprotectData(send);
|
|
|
|
|
|
return send;
|
|
|
|
|
|
}
|
2020-11-02 15:55:49 -05:00
|
|
|
|
|
2023-11-22 15:44:25 -05:00
|
|
|
|
/// <inheritdoc />
|
2020-11-02 15:55:49 -05:00
|
|
|
|
public async Task<ICollection<Send>> GetManyByUserIdAsync(Guid userId)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2020-11-02 15:55:49 -05:00
|
|
|
|
using (var connection = new SqlConnection(ConnectionString))
|
|
|
|
|
|
{
|
|
|
|
|
|
var results = await connection.QueryAsync<Send>(
|
|
|
|
|
|
$"[{Schema}].[Send_ReadByUserId]",
|
|
|
|
|
|
new { UserId = userId },
|
|
|
|
|
|
commandType: CommandType.StoredProcedure);
|
|
|
|
|
|
|
2026-01-28 07:13:25 -07:00
|
|
|
|
var sends = results.ToList();
|
|
|
|
|
|
UnprotectData(sends);
|
|
|
|
|
|
return sends;
|
2020-11-02 15:55:49 -05:00
|
|
|
|
}
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
2020-11-02 15:55:49 -05:00
|
|
|
|
|
2023-11-22 15:44:25 -05:00
|
|
|
|
/// <inheritdoc />
|
2020-11-02 15:55:49 -05:00
|
|
|
|
public async Task<ICollection<Send>> GetManyByDeletionDateAsync(DateTime deletionDateBefore)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2020-11-02 15:55:49 -05:00
|
|
|
|
using (var connection = new SqlConnection(ConnectionString))
|
|
|
|
|
|
{
|
|
|
|
|
|
var results = await connection.QueryAsync<Send>(
|
|
|
|
|
|
$"[{Schema}].[Send_ReadByDeletionDateBefore]",
|
|
|
|
|
|
new { DeletionDate = deletionDateBefore },
|
|
|
|
|
|
commandType: CommandType.StoredProcedure);
|
|
|
|
|
|
|
2026-01-28 07:13:25 -07:00
|
|
|
|
var sends = results.ToList();
|
|
|
|
|
|
UnprotectData(sends);
|
|
|
|
|
|
return sends;
|
2020-11-02 15:55:49 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-12-12 11:58:34 -05:00
|
|
|
|
|
2026-01-28 07:13:25 -07:00
|
|
|
|
public override async Task<Send> CreateAsync(Send send)
|
|
|
|
|
|
{
|
|
|
|
|
|
await ProtectDataAndSaveAsync(send, async () => await base.CreateAsync(send));
|
|
|
|
|
|
return send;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override async Task ReplaceAsync(Send send)
|
|
|
|
|
|
{
|
|
|
|
|
|
await ProtectDataAndSaveAsync(send, async () => await base.ReplaceAsync(send));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-12 11:58:34 -05:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public UpdateEncryptedDataForKeyRotation UpdateForKeyRotation(Guid userId, IEnumerable<Send> sends)
|
|
|
|
|
|
{
|
|
|
|
|
|
return async (connection, transaction) =>
|
|
|
|
|
|
{
|
2026-01-28 07:13:25 -07:00
|
|
|
|
// Protect all sends before bulk update
|
|
|
|
|
|
var sendsList = sends.ToList();
|
|
|
|
|
|
foreach (var send in sendsList)
|
|
|
|
|
|
{
|
|
|
|
|
|
ProtectData(send);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-12 11:58:34 -05:00
|
|
|
|
// Create temp table
|
|
|
|
|
|
var sqlCreateTemp = @"
|
|
|
|
|
|
SELECT TOP 0 *
|
|
|
|
|
|
INTO #TempSend
|
|
|
|
|
|
FROM [dbo].[Send]";
|
|
|
|
|
|
|
|
|
|
|
|
await using (var cmd = new SqlCommand(sqlCreateTemp, connection, transaction))
|
|
|
|
|
|
{
|
|
|
|
|
|
cmd.ExecuteNonQuery();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Bulk copy data into temp table
|
|
|
|
|
|
using (var bulkCopy = new SqlBulkCopy(connection, SqlBulkCopyOptions.KeepIdentity, transaction))
|
|
|
|
|
|
{
|
|
|
|
|
|
bulkCopy.DestinationTableName = "#TempSend";
|
2026-01-28 07:13:25 -07:00
|
|
|
|
var sendsTable = sendsList.ToDataTable();
|
2023-12-12 11:58:34 -05:00
|
|
|
|
foreach (DataColumn col in sendsTable.Columns)
|
|
|
|
|
|
{
|
|
|
|
|
|
bulkCopy.ColumnMappings.Add(col.ColumnName, col.ColumnName);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sendsTable.PrimaryKey = new DataColumn[] { sendsTable.Columns[0] };
|
|
|
|
|
|
await bulkCopy.WriteToServerAsync(sendsTable);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Update send table from temp table
|
|
|
|
|
|
var sql = @"
|
|
|
|
|
|
UPDATE
|
|
|
|
|
|
[dbo].[Send]
|
|
|
|
|
|
SET
|
|
|
|
|
|
[Key] = TS.[Key],
|
|
|
|
|
|
[RevisionDate] = TS.[RevisionDate]
|
|
|
|
|
|
FROM
|
|
|
|
|
|
[dbo].[Send] S
|
|
|
|
|
|
INNER JOIN
|
|
|
|
|
|
#TempSend TS ON S.Id = TS.Id
|
|
|
|
|
|
WHERE
|
|
|
|
|
|
S.[UserId] = @UserId
|
|
|
|
|
|
DROP TABLE #TempSend";
|
|
|
|
|
|
|
|
|
|
|
|
await using (var cmd = new SqlCommand(sql, connection, transaction))
|
|
|
|
|
|
{
|
|
|
|
|
|
cmd.Parameters.Add("@UserId", SqlDbType.UniqueIdentifier).Value = userId;
|
|
|
|
|
|
cmd.ExecuteNonQuery();
|
|
|
|
|
|
}
|
2026-01-28 07:13:25 -07:00
|
|
|
|
|
|
|
|
|
|
// Unprotect after save
|
|
|
|
|
|
foreach (var send in sendsList)
|
|
|
|
|
|
{
|
|
|
|
|
|
UnprotectData(send);
|
|
|
|
|
|
}
|
2023-12-12 11:58:34 -05:00
|
|
|
|
};
|
|
|
|
|
|
}
|
2026-01-28 07:13:25 -07:00
|
|
|
|
|
|
|
|
|
|
private async Task ProtectDataAndSaveAsync(Send send, Func<Task> saveTask)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (send == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
await saveTask();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Capture original value
|
|
|
|
|
|
var originalEmailHashes = send.EmailHashes;
|
|
|
|
|
|
|
|
|
|
|
|
// Protect value
|
|
|
|
|
|
ProtectData(send);
|
|
|
|
|
|
|
|
|
|
|
|
// Save
|
|
|
|
|
|
await saveTask();
|
|
|
|
|
|
|
|
|
|
|
|
// Restore original value
|
|
|
|
|
|
send.EmailHashes = originalEmailHashes;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void ProtectData(Send send)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!send.EmailHashes?.StartsWith(Constants.DatabaseFieldProtectedPrefix) ?? false)
|
|
|
|
|
|
{
|
|
|
|
|
|
send.EmailHashes = string.Concat(Constants.DatabaseFieldProtectedPrefix,
|
|
|
|
|
|
_dataProtector.Protect(send.EmailHashes!));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void UnprotectData(Send? send)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (send == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (send.EmailHashes?.StartsWith(Constants.DatabaseFieldProtectedPrefix) ?? false)
|
|
|
|
|
|
{
|
|
|
|
|
|
send.EmailHashes = _dataProtector.Unprotect(
|
|
|
|
|
|
send.EmailHashes.Substring(Constants.DatabaseFieldProtectedPrefix.Length));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void UnprotectData(IEnumerable<Send> sends)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (sends == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var send in sends)
|
|
|
|
|
|
{
|
|
|
|
|
|
UnprotectData(send);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-11-02 15:55:49 -05:00
|
|
|
|
}
|