2022-06-29 19:46:41 -04:00
|
|
|
|
using Bit.Core.Enums;
|
2022-01-11 10:40:51 +01:00
|
|
|
|
using Bit.Core.Repositories;
|
|
|
|
|
|
using Bit.Infrastructure.EntityFramework.Repositories;
|
|
|
|
|
|
using LinqToDB.EntityFrameworkCore;
|
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Bit.Infrastructure.EntityFramework;
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2022-01-11 10:40:51 +01:00
|
|
|
|
public static class EntityFrameworkServiceCollectionExtensions
|
|
|
|
|
|
{
|
|
|
|
|
|
public static void AddEFRepositories(this IServiceCollection services, bool selfHosted, string connectionString,
|
|
|
|
|
|
SupportedDatabaseProviders provider)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(connectionString))
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2022-01-11 10:40:51 +01:00
|
|
|
|
throw new Exception($"Database provider type {provider} was selected but no connection string was found.");
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
2022-01-11 10:40:51 +01:00
|
|
|
|
LinqToDBForEFTools.Initialize();
|
|
|
|
|
|
services.AddAutoMapper(typeof(UserRepository));
|
|
|
|
|
|
services.AddDbContext<DatabaseContext>(options =>
|
|
|
|
|
|
{
|
|
|
|
|
|
if (provider == SupportedDatabaseProviders.Postgres)
|
|
|
|
|
|
{
|
|
|
|
|
|
options.UseNpgsql(connectionString);
|
|
|
|
|
|
// Handle NpgSql Legacy Support for `timestamp without timezone` issue
|
|
|
|
|
|
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (provider == SupportedDatabaseProviders.MySql)
|
|
|
|
|
|
{
|
|
|
|
|
|
options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString));
|
2022-08-29 15:53:48 -04:00
|
|
|
|
}
|
2022-08-29 16:06:55 -04:00
|
|
|
|
});
|
2022-01-11 10:40:51 +01:00
|
|
|
|
services.AddSingleton<ICipherRepository, CipherRepository>();
|
|
|
|
|
|
services.AddSingleton<ICollectionCipherRepository, CollectionCipherRepository>();
|
|
|
|
|
|
services.AddSingleton<ICollectionRepository, CollectionRepository>();
|
|
|
|
|
|
services.AddSingleton<IDeviceRepository, DeviceRepository>();
|
|
|
|
|
|
services.AddSingleton<IEmergencyAccessRepository, EmergencyAccessRepository>();
|
|
|
|
|
|
services.AddSingleton<IFolderRepository, FolderRepository>();
|
|
|
|
|
|
services.AddSingleton<IGrantRepository, GrantRepository>();
|
|
|
|
|
|
services.AddSingleton<IGroupRepository, GroupRepository>();
|
|
|
|
|
|
services.AddSingleton<IInstallationRepository, InstallationRepository>();
|
|
|
|
|
|
services.AddSingleton<IMaintenanceRepository, MaintenanceRepository>();
|
|
|
|
|
|
services.AddSingleton<IOrganizationRepository, OrganizationRepository>();
|
2022-05-10 17:12:09 -04:00
|
|
|
|
services.AddSingleton<IOrganizationApiKeyRepository, OrganizationApiKeyRepository>();
|
2022-01-11 10:40:51 +01:00
|
|
|
|
services.AddSingleton<IOrganizationConnectionRepository, OrganizationConnectionRepository>();
|
|
|
|
|
|
services.AddSingleton<IOrganizationSponsorshipRepository, OrganizationSponsorshipRepository>();
|
|
|
|
|
|
services.AddSingleton<IOrganizationUserRepository, OrganizationUserRepository>();
|
|
|
|
|
|
services.AddSingleton<IPolicyRepository, PolicyRepository>();
|
|
|
|
|
|
services.AddSingleton<ISendRepository, SendRepository>();
|
|
|
|
|
|
services.AddSingleton<ISsoConfigRepository, SsoConfigRepository>();
|
|
|
|
|
|
services.AddSingleton<ISsoUserRepository, SsoUserRepository>();
|
|
|
|
|
|
services.AddSingleton<ITaxRateRepository, TaxRateRepository>();
|
|
|
|
|
|
services.AddSingleton<ITransactionRepository, TransactionRepository>();
|
|
|
|
|
|
services.AddSingleton<IUserRepository, UserRepository>();
|
|
|
|
|
|
services.AddSingleton<IProviderRepository, ProviderRepository>();
|
|
|
|
|
|
services.AddSingleton<IProviderUserRepository, ProviderUserRepository>();
|
|
|
|
|
|
services.AddSingleton<IProviderOrganizationRepository, ProviderOrganizationRepository>();
|
2022-09-26 13:21:13 -04:00
|
|
|
|
services.AddSingleton<IAuthRequestRepository, AuthRequestRepository>();
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2022-01-11 10:40:51 +01:00
|
|
|
|
if (selfHosted)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2022-01-11 10:40:51 +01:00
|
|
|
|
services.AddSingleton<IEventRepository, EventRepository>();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|