2022-06-29 19:46:41 -04:00
|
|
|
|
using System.Data;
|
2022-01-11 10:40:51 +01:00
|
|
|
|
using Bit.Core.Entities;
|
2018-08-14 15:30:04 -04:00
|
|
|
|
using Bit.Core.Models.Data;
|
2022-01-11 10:40:51 +01:00
|
|
|
|
using Bit.Core.Repositories;
|
2021-02-22 15:35:16 -06:00
|
|
|
|
using Bit.Core.Settings;
|
2016-02-06 01:18:25 -05:00
|
|
|
|
using Dapper;
|
2023-01-10 15:58:41 -05:00
|
|
|
|
using Microsoft.Data.SqlClient;
|
2016-02-06 01:18:25 -05:00
|
|
|
|
|
2022-01-11 10:40:51 +01:00
|
|
|
|
namespace Bit.Infrastructure.Dapper.Repositories;
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2022-01-11 10:40:51 +01:00
|
|
|
|
public class UserRepository : Repository<User, Guid>, IUserRepository
|
2016-02-06 01:18:25 -05:00
|
|
|
|
{
|
2016-07-30 16:37:52 -04:00
|
|
|
|
public UserRepository(GlobalSettings globalSettings)
|
2018-10-09 17:21:12 -04:00
|
|
|
|
: this(globalSettings.SqlServer.ConnectionString, globalSettings.SqlServer.ReadOnlyConnectionString)
|
2016-06-17 17:42:22 -04:00
|
|
|
|
{ }
|
|
|
|
|
|
|
2020-03-27 14:36:37 -04:00
|
|
|
|
public UserRepository(string connectionString, string readOnlyConnectionString)
|
2016-05-21 17:16:22 -04:00
|
|
|
|
: base(connectionString, readOnlyConnectionString)
|
2016-02-06 01:18:25 -05:00
|
|
|
|
{ }
|
2016-06-17 17:42:22 -04:00
|
|
|
|
|
2020-07-28 10:03:09 -04:00
|
|
|
|
public override async Task<User> GetByIdAsync(Guid id)
|
2022-08-29 15:53:48 -04:00
|
|
|
|
{
|
2020-07-28 10:03:09 -04:00
|
|
|
|
return await base.GetByIdAsync(id);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-14 15:30:04 -04:00
|
|
|
|
public async Task<User> GetByEmailAsync(string email)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2018-08-14 15:30:04 -04:00
|
|
|
|
using (var connection = new SqlConnection(ConnectionString))
|
|
|
|
|
|
{
|
|
|
|
|
|
var results = await connection.QueryAsync<User>(
|
|
|
|
|
|
$"[{Schema}].[{Table}_ReadByEmail]",
|
|
|
|
|
|
new { Email = email },
|
|
|
|
|
|
commandType: CommandType.StoredProcedure);
|
|
|
|
|
|
|
2018-03-21 16:24:10 -04:00
|
|
|
|
return results.SingleOrDefault();
|
|
|
|
|
|
}
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
2018-03-21 16:24:10 -04:00
|
|
|
|
|
2017-08-22 15:27:29 -04:00
|
|
|
|
public async Task<User> GetBySsoUserAsync(string externalId, Guid? organizationId)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2017-08-22 15:27:29 -04:00
|
|
|
|
using (var connection = new SqlConnection(ConnectionString))
|
|
|
|
|
|
{
|
|
|
|
|
|
var results = await connection.QueryAsync<User>(
|
|
|
|
|
|
$"[{Schema}].[{Table}_ReadBySsoUserOrganizationIdExternalId]",
|
|
|
|
|
|
new { OrganizationId = organizationId, ExternalId = externalId },
|
|
|
|
|
|
commandType: CommandType.StoredProcedure);
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2017-08-22 15:27:29 -04:00
|
|
|
|
return results.SingleOrDefault();
|
|
|
|
|
|
}
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
2017-08-22 15:27:29 -04:00
|
|
|
|
|
2017-03-04 21:28:41 -05:00
|
|
|
|
public async Task<UserKdfInformation> GetKdfInformationByEmailAsync(string email)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2017-03-04 21:28:41 -05:00
|
|
|
|
using (var connection = new SqlConnection(ConnectionString))
|
|
|
|
|
|
{
|
|
|
|
|
|
var results = await connection.QueryAsync<UserKdfInformation>(
|
|
|
|
|
|
$"[{Schema}].[{Table}_ReadKdfByEmail]",
|
|
|
|
|
|
new { Email = email },
|
|
|
|
|
|
commandType: CommandType.StoredProcedure);
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2017-03-04 21:28:41 -05:00
|
|
|
|
return results.SingleOrDefault();
|
|
|
|
|
|
}
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
2017-03-04 21:28:41 -05:00
|
|
|
|
|
2017-01-14 10:02:37 -05:00
|
|
|
|
public async Task<ICollection<User>> SearchAsync(string email, int skip, int take)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2017-01-14 10:02:37 -05:00
|
|
|
|
using (var connection = new SqlConnection(ReadOnlyConnectionString))
|
|
|
|
|
|
{
|
|
|
|
|
|
var results = await connection.QueryAsync<User>(
|
|
|
|
|
|
$"[{Schema}].[{Table}_Search]",
|
|
|
|
|
|
new { Email = email, Skip = skip, Take = take },
|
|
|
|
|
|
commandType: CommandType.StoredProcedure,
|
|
|
|
|
|
commandTimeout: 120);
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2017-01-14 10:02:37 -05:00
|
|
|
|
return results.ToList();
|
|
|
|
|
|
}
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
2017-01-14 10:02:37 -05:00
|
|
|
|
|
2016-06-17 17:42:22 -04:00
|
|
|
|
public async Task<ICollection<User>> GetManyByPremiumAsync(bool premium)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2016-06-17 17:42:22 -04:00
|
|
|
|
using (var connection = new SqlConnection(ConnectionString))
|
|
|
|
|
|
{
|
|
|
|
|
|
var results = await connection.QueryAsync<User>(
|
|
|
|
|
|
"[dbo].[User_ReadByPremium]",
|
2017-07-10 22:08:52 -04:00
|
|
|
|
new { Premium = premium },
|
2016-06-17 17:42:22 -04:00
|
|
|
|
commandType: CommandType.StoredProcedure);
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2016-05-21 17:16:22 -04:00
|
|
|
|
return results.ToList();
|
2016-06-17 17:42:22 -04:00
|
|
|
|
}
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
2016-06-17 17:42:22 -04:00
|
|
|
|
|
2018-07-12 23:23:41 -04:00
|
|
|
|
public async Task<string> GetPublicKeyAsync(Guid id)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2018-07-12 23:23:41 -04:00
|
|
|
|
using (var connection = new SqlConnection(ConnectionString))
|
2022-08-29 15:53:48 -04:00
|
|
|
|
{
|
2021-05-25 19:23:47 +02:00
|
|
|
|
var results = await connection.QueryAsync<string>(
|
|
|
|
|
|
$"[{Schema}].[{Table}_ReadPublicKeyById]",
|
2017-07-10 22:08:52 -04:00
|
|
|
|
new { Id = id },
|
2021-05-25 19:23:47 +02:00
|
|
|
|
commandType: CommandType.StoredProcedure);
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2020-07-28 10:03:09 -04:00
|
|
|
|
return results.SingleOrDefault();
|
2022-08-29 15:53:48 -04:00
|
|
|
|
}
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
2022-08-29 14:53:16 -04:00
|
|
|
|
|
2016-06-17 17:42:22 -04:00
|
|
|
|
public async Task<DateTime> GetAccountRevisionDateAsync(Guid id)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2016-06-17 17:42:22 -04:00
|
|
|
|
using (var connection = new SqlConnection(ReadOnlyConnectionString))
|
|
|
|
|
|
{
|
2016-10-29 02:59:17 -04:00
|
|
|
|
var results = await connection.QueryAsync<DateTime>(
|
|
|
|
|
|
$"[{Schema}].[{Table}_ReadAccountRevisionDateById]",
|
2018-07-12 23:23:41 -04:00
|
|
|
|
new { Id = id },
|
2016-10-29 02:59:17 -04:00
|
|
|
|
commandType: CommandType.StoredProcedure);
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2018-08-14 15:30:04 -04:00
|
|
|
|
return results.SingleOrDefault();
|
2016-06-17 17:42:22 -04:00
|
|
|
|
}
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
2017-07-10 16:21:18 -04:00
|
|
|
|
|
2021-05-25 19:23:47 +02:00
|
|
|
|
public override async Task ReplaceAsync(User user)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2016-05-21 17:16:22 -04:00
|
|
|
|
await base.ReplaceAsync(user);
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-07-10 22:08:52 -04:00
|
|
|
|
public override async Task DeleteAsync(User user)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
using (var connection = new SqlConnection(ConnectionString))
|
2017-07-10 16:21:18 -04:00
|
|
|
|
{
|
|
|
|
|
|
await connection.ExecuteAsync(
|
|
|
|
|
|
$"[{Schema}].[{Table}_DeleteById]",
|
2017-07-10 22:08:52 -04:00
|
|
|
|
new { Id = user.Id },
|
2017-07-10 16:21:18 -04:00
|
|
|
|
commandType: CommandType.StoredProcedure,
|
|
|
|
|
|
commandTimeout: 180);
|
|
|
|
|
|
}
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
2018-07-12 23:23:41 -04:00
|
|
|
|
|
|
|
|
|
|
public async Task UpdateStorageAsync(Guid id)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
using (var connection = new SqlConnection(ConnectionString))
|
2018-07-12 23:23:41 -04:00
|
|
|
|
{
|
|
|
|
|
|
await connection.ExecuteAsync(
|
|
|
|
|
|
$"[{Schema}].[{Table}_UpdateStorage]",
|
|
|
|
|
|
new { Id = id },
|
|
|
|
|
|
commandType: CommandType.StoredProcedure,
|
|
|
|
|
|
commandTimeout: 180);
|
|
|
|
|
|
}
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
2021-05-25 19:23:47 +02:00
|
|
|
|
|
|
|
|
|
|
public async Task UpdateRenewalReminderDateAsync(Guid id, DateTime renewalReminderDate)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2021-05-25 19:23:47 +02:00
|
|
|
|
using (var connection = new SqlConnection(ConnectionString))
|
|
|
|
|
|
{
|
|
|
|
|
|
await connection.ExecuteAsync(
|
|
|
|
|
|
$"[{Schema}].[User_UpdateRenewalReminderDate]",
|
|
|
|
|
|
new { Id = id, RenewalReminderDate = renewalReminderDate },
|
|
|
|
|
|
commandType: CommandType.StoredProcedure);
|
2022-08-29 15:53:48 -04:00
|
|
|
|
}
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
2022-08-29 14:53:16 -04:00
|
|
|
|
|
2016-06-17 17:42:22 -04:00
|
|
|
|
public async Task<IEnumerable<User>> GetManyAsync(IEnumerable<Guid> ids)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2021-05-25 19:23:47 +02:00
|
|
|
|
using (var connection = new SqlConnection(ReadOnlyConnectionString))
|
2022-08-29 15:53:48 -04:00
|
|
|
|
{
|
2016-05-21 17:16:22 -04:00
|
|
|
|
var results = await connection.QueryAsync<User>(
|
2016-02-06 01:18:25 -05:00
|
|
|
|
$"[{Schema}].[{Table}_ReadByIds]",
|
2021-05-25 19:23:47 +02:00
|
|
|
|
new { Ids = ids.ToGuidIdArrayTVP() },
|
2016-02-06 01:18:25 -05:00
|
|
|
|
commandType: CommandType.StoredProcedure);
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2021-05-25 19:23:47 +02:00
|
|
|
|
return results.ToList();
|
|
|
|
|
|
}
|
2016-02-06 01:18:25 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|