2023-11-22 15:44:25 -05:00
|
|
|
|
#nullable enable
|
|
|
|
|
|
|
|
|
|
|
|
using System.Data;
|
2023-12-12 11:58:34 -05:00
|
|
|
|
using Bit.Core.Auth.UserFeatures.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;
|
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
|
|
|
|
{
|
|
|
|
|
|
public SendRepository(GlobalSettings globalSettings)
|
|
|
|
|
|
: this(globalSettings.SqlServer.ConnectionString, globalSettings.SqlServer.ReadOnlyConnectionString)
|
|
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
|
|
public SendRepository(string connectionString, string readOnlyConnectionString)
|
|
|
|
|
|
: base(connectionString, readOnlyConnectionString)
|
|
|
|
|
|
{ }
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
return results.ToList();
|
|
|
|
|
|
}
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
return results.ToList();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-12-12 11:58:34 -05:00
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public UpdateEncryptedDataForKeyRotation UpdateForKeyRotation(Guid userId, IEnumerable<Send> sends)
|
|
|
|
|
|
{
|
|
|
|
|
|
return async (connection, transaction) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
// 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";
|
|
|
|
|
|
var sendsTable = sends.ToDataTable();
|
|
|
|
|
|
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();
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
2020-11-02 15:55:49 -05:00
|
|
|
|
}
|