Files
server/src/Infrastructure.Dapper/Repositories/UserRepository.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

171 lines
5.6 KiB
C#
Raw Normal View History

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