1
0
mirror of https://github.com/bitwarden/server.git synced 2025-04-06 05:28:15 -05:00

stub out user repo for postgresql

This commit is contained in:
Kyle Spearrin 2019-01-15 22:07:13 -05:00
parent 84800da1fb
commit acef40ee82
4 changed files with 185 additions and 17 deletions

View File

@ -15,7 +15,8 @@ namespace Bit.Core
public virtual bool DisableUserRegistration { get; set; }
public virtual InstallationSettings Installation { get; set; } = new InstallationSettings();
public virtual BaseServiceUriSettings BaseServiceUri { get; set; } = new BaseServiceUriSettings();
public virtual SqlServerSettings SqlServer { get; set; } = new SqlServerSettings();
public virtual SqlSettings SqlServer { get; set; } = new SqlSettings();
public virtual SqlSettings PostgreSql { get; set; } = new SqlSettings();
public virtual MailSettings Mail { get; set; } = new MailSettings();
public virtual StorageSettings Storage { get; set; } = new StorageSettings();
public virtual StorageSettings Events { get; set; } = new StorageSettings();
@ -45,7 +46,7 @@ namespace Bit.Core
public string InternalVault { get; set; }
}
public class SqlServerSettings
public class SqlSettings
{
private string _connectionString;
private string _readOnlyConnectionString;

View File

@ -12,7 +12,7 @@ namespace Bit.Core.Repositories.PostgreSql
where TId : IEquatable<TId>
where T : class, ITableObject<TId>
{
public Repository(string connectionString, string readOnlyConnectionString, string table)
public Repository(string connectionString, string readOnlyConnectionString, string table = null)
: base(connectionString, readOnlyConnectionString)
{
if(!string.IsNullOrWhiteSpace(table))
@ -21,7 +21,7 @@ namespace Bit.Core.Repositories.PostgreSql
}
else
{
Table = SnakeCase(typeof(T).Name);
Table = SnakeCase(typeof(T).Name).ToLowerInvariant();
}
}

View File

@ -0,0 +1,159 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using Bit.Core.Models.Data;
using Bit.Core.Models.Table;
using Dapper;
using Npgsql;
namespace Bit.Core.Repositories.PostgreSql
{
public class UserRepository : Repository<User, Guid>, IUserRepository
{
public UserRepository(GlobalSettings globalSettings)
: this(globalSettings.PostgreSql.ConnectionString, globalSettings.PostgreSql.ReadOnlyConnectionString)
{ }
public UserRepository(string connectionString, string readOnlyConnectionString)
: base(connectionString, readOnlyConnectionString)
{ }
public override async Task<User> GetByIdAsync(Guid id)
{
return await base.GetByIdAsync(id);
}
public async Task<User> GetByEmailAsync(string email)
{
using(var connection = new NpgsqlConnection(ConnectionString))
{
var results = await connection.QueryAsync<User>(
"user_read_by_email",
new { email = email },
commandType: CommandType.StoredProcedure);
return results.SingleOrDefault();
}
}
public async Task<UserKdfInformation> GetKdfInformationByEmailAsync(string email)
{
using(var connection = new NpgsqlConnection(ConnectionString))
{
var results = await connection.QueryAsync<UserKdfInformation>(
"user_read_kdf_by_email",
new { email = email },
commandType: CommandType.StoredProcedure);
return results.SingleOrDefault();
}
}
public async Task<ICollection<User>> SearchAsync(string email, int skip, int take)
{
using(var connection = new NpgsqlConnection(ReadOnlyConnectionString))
{
var results = await connection.QueryAsync<User>(
"user_search",
new { email = email, skip = skip, take = take },
commandType: CommandType.StoredProcedure,
commandTimeout: 120);
return results.ToList();
}
}
public async Task<ICollection<User>> GetManyByPremiumAsync(bool premium)
{
using(var connection = new NpgsqlConnection(ConnectionString))
{
var results = await connection.QueryAsync<User>(
"user_read_by_premium",
new { premium = premium },
commandType: CommandType.StoredProcedure);
return results.ToList();
}
}
public async Task<ICollection<User>> GetManyByPremiumRenewalAsync()
{
using(var connection = new NpgsqlConnection(ConnectionString))
{
var results = await connection.QueryAsync<User>(
"user_read_by_premium_renewal",
commandType: CommandType.StoredProcedure);
return results.ToList();
}
}
public async Task<string> GetPublicKeyAsync(Guid id)
{
using(var connection = new NpgsqlConnection(ConnectionString))
{
var results = await connection.QueryAsync<string>(
"user_read_public_key_by_id",
new { id = id },
commandType: CommandType.StoredProcedure);
return results.SingleOrDefault();
}
}
public async Task<DateTime> GetAccountRevisionDateAsync(Guid id)
{
using(var connection = new NpgsqlConnection(ConnectionString))
{
var results = await connection.QueryAsync<DateTime>(
"user_read_account_revision_date_by_id",
new { id = id },
commandType: CommandType.StoredProcedure);
return results.SingleOrDefault();
}
}
public override async Task ReplaceAsync(User user)
{
await base.ReplaceAsync(user);
}
public override async Task DeleteAsync(User user)
{
using(var connection = new NpgsqlConnection(ConnectionString))
{
await connection.ExecuteAsync(
$"user_delete_by_id",
new { id = user.Id },
commandType: CommandType.StoredProcedure,
commandTimeout: 180);
}
}
public async Task UpdateStorageAsync(Guid id)
{
using(var connection = new NpgsqlConnection(ConnectionString))
{
await connection.ExecuteAsync(
"user_update_storage",
new { id = id },
commandType: CommandType.StoredProcedure,
commandTimeout: 180);
}
}
public async Task UpdateRenewalReminderDateAsync(Guid id, DateTime renewalReminderDate)
{
using(var connection = new NpgsqlConnection(ConnectionString))
{
await connection.ExecuteAsync(
"user_update_renewal_reminder_date",
new { id = id, renewal_reminder_date = renewalReminderDate },
commandType: CommandType.StoredProcedure);
}
}
}
}

View File

@ -19,6 +19,7 @@ using Microsoft.WindowsAzure.Storage;
using System;
using System.IO;
using SqlServerRepos = Bit.Core.Repositories.SqlServer;
using PostgreSqlRepos = Bit.Core.Repositories.PostgreSql;
using System.Threading.Tasks;
using TableStorageRepos = Bit.Core.Repositories.TableStorage;
using Microsoft.Extensions.DependencyInjection.Extensions;
@ -32,19 +33,26 @@ namespace Bit.Core.Utilities
{
public static void AddSqlServerRepositories(this IServiceCollection services, GlobalSettings globalSettings)
{
services.AddSingleton<IUserRepository, SqlServerRepos.UserRepository>();
services.AddSingleton<ICipherRepository, SqlServerRepos.CipherRepository>();
services.AddSingleton<IDeviceRepository, SqlServerRepos.DeviceRepository>();
services.AddSingleton<IGrantRepository, SqlServerRepos.GrantRepository>();
services.AddSingleton<IOrganizationRepository, SqlServerRepos.OrganizationRepository>();
services.AddSingleton<IOrganizationUserRepository, SqlServerRepos.OrganizationUserRepository>();
services.AddSingleton<ICollectionRepository, SqlServerRepos.CollectionRepository>();
services.AddSingleton<IFolderRepository, SqlServerRepos.FolderRepository>();
services.AddSingleton<ICollectionCipherRepository, SqlServerRepos.CollectionCipherRepository>();
services.AddSingleton<IGroupRepository, SqlServerRepos.GroupRepository>();
services.AddSingleton<IU2fRepository, SqlServerRepos.U2fRepository>();
services.AddSingleton<IInstallationRepository, SqlServerRepos.InstallationRepository>();
services.AddSingleton<IMaintenanceRepository, SqlServerRepos.MaintenanceRepository>();
if(!string.IsNullOrWhiteSpace(globalSettings.PostgreSql?.ConnectionString))
{
services.AddSingleton<IUserRepository, PostgreSqlRepos.UserRepository>();
}
else
{
services.AddSingleton<IUserRepository, SqlServerRepos.UserRepository>();
services.AddSingleton<ICipherRepository, SqlServerRepos.CipherRepository>();
services.AddSingleton<IDeviceRepository, SqlServerRepos.DeviceRepository>();
services.AddSingleton<IGrantRepository, SqlServerRepos.GrantRepository>();
services.AddSingleton<IOrganizationRepository, SqlServerRepos.OrganizationRepository>();
services.AddSingleton<IOrganizationUserRepository, SqlServerRepos.OrganizationUserRepository>();
services.AddSingleton<ICollectionRepository, SqlServerRepos.CollectionRepository>();
services.AddSingleton<IFolderRepository, SqlServerRepos.FolderRepository>();
services.AddSingleton<ICollectionCipherRepository, SqlServerRepos.CollectionCipherRepository>();
services.AddSingleton<IGroupRepository, SqlServerRepos.GroupRepository>();
services.AddSingleton<IU2fRepository, SqlServerRepos.U2fRepository>();
services.AddSingleton<IInstallationRepository, SqlServerRepos.InstallationRepository>();
services.AddSingleton<IMaintenanceRepository, SqlServerRepos.MaintenanceRepository>();
}
if(globalSettings.SelfHosted)
{