mirror of
https://github.com/bitwarden/server.git
synced 2025-04-06 05:28:15 -05:00
fixes for databasecontext and json types
This commit is contained in:
parent
002efaafd4
commit
8026912eeb
@ -39,7 +39,7 @@
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.2.4" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="2.2.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Identity.Stores" Version="2.2.0" />
|
||||
<PackageReference Include="Npgsql" Version="4.0.9" />
|
||||
<PackageReference Include="Npgsql" Version="4.1.2" />
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="2.2.4" />
|
||||
<PackageReference Include="Quartz" Version="3.0.7" />
|
||||
<PackageReference Include="Serilog.AspNetCore" Version="3.0.0" />
|
||||
|
@ -10,6 +10,7 @@ namespace Bit.Core.Models.EntityFramework
|
||||
|
||||
public User User { get; set; }
|
||||
public Organization Organization { get; set; }
|
||||
[IgnoreMap]
|
||||
public JsonDocument DataJson
|
||||
{
|
||||
get => _dataJson;
|
||||
@ -19,6 +20,7 @@ namespace Bit.Core.Models.EntityFramework
|
||||
_dataJson = value;
|
||||
}
|
||||
}
|
||||
[IgnoreMap]
|
||||
public JsonDocument AttachmentsJson
|
||||
{
|
||||
get => _attachmentsJson;
|
||||
|
@ -10,6 +10,7 @@ namespace Bit.Core.Models.EntityFramework
|
||||
|
||||
public ICollection<Cipher> Ciphers { get; set; }
|
||||
|
||||
[IgnoreMap]
|
||||
public JsonDocument TwoFactorProvidersJson
|
||||
{
|
||||
get => _twoFactorProvidersJson;
|
||||
|
@ -10,6 +10,7 @@ namespace Bit.Core.Models.EntityFramework
|
||||
|
||||
public ICollection<Cipher> Ciphers { get; set; }
|
||||
|
||||
[IgnoreMap]
|
||||
public JsonDocument TwoFactorProvidersJson
|
||||
{
|
||||
get => _twoFactorProvidersJson;
|
||||
|
@ -1,16 +1,22 @@
|
||||
using AutoMapper;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Bit.Core.Repositories.EntityFramework
|
||||
{
|
||||
public abstract class BaseEntityFrameworkRepository
|
||||
{
|
||||
public BaseEntityFrameworkRepository(DatabaseContext databaseContext, IMapper mapper)
|
||||
public BaseEntityFrameworkRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
{
|
||||
DatabaseContext = databaseContext;
|
||||
ServiceScopeFactory = serviceScopeFactory;
|
||||
Mapper = mapper;
|
||||
}
|
||||
|
||||
protected DatabaseContext DatabaseContext { get; private set; }
|
||||
protected IServiceScopeFactory ServiceScopeFactory { get; private set; }
|
||||
protected IMapper Mapper { get; private set; }
|
||||
|
||||
public DatabaseContext GetDatabaseContext(IServiceScope serviceScope)
|
||||
{
|
||||
return serviceScope.ServiceProvider.GetRequiredService<DatabaseContext>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -6,34 +6,20 @@ namespace Bit.Core.Repositories.EntityFramework
|
||||
{
|
||||
public class DatabaseContext : DbContext
|
||||
{
|
||||
private readonly GlobalSettings _globalSettings;
|
||||
|
||||
public DatabaseContext(
|
||||
DbContextOptions<DatabaseContext> options,
|
||||
GlobalSettings globalSettings)
|
||||
public DatabaseContext(DbContextOptions<DatabaseContext> options)
|
||||
: base(options)
|
||||
{
|
||||
_globalSettings = globalSettings;
|
||||
}
|
||||
{ }
|
||||
|
||||
public DbSet<User> Users { get; set; }
|
||||
public DbSet<Cipher> Ciphers { get; set; }
|
||||
public DbSet<Organization> Organizations { get; set; }
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder builder)
|
||||
{
|
||||
if(!string.IsNullOrWhiteSpace(_globalSettings.PostgreSql?.ConnectionString))
|
||||
{
|
||||
builder.UseNpgsql(_globalSettings.PostgreSql.ConnectionString);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder builder)
|
||||
{
|
||||
builder.Entity<Cipher>().Ignore(e => e.Data);
|
||||
builder.Entity<Cipher>().Property(e => e.Data).HasColumnName("Data");
|
||||
builder.Entity<Cipher>().Property(e => e.DataJson).HasColumnName("Data");
|
||||
builder.Entity<Cipher>().Ignore(e => e.Attachments);
|
||||
builder.Entity<Cipher>().Property(e => e.Attachments).HasColumnName("Attachments");
|
||||
builder.Entity<Cipher>().Property(e => e.AttachmentsJson).HasColumnName("Attachments");
|
||||
|
||||
builder.Entity<User>().Ignore(e => e.TwoFactorProviders);
|
||||
builder.Entity<User>().Property(e => e.TwoFactorProvidersJson).HasColumnName("TwoFactorProviders");
|
||||
|
@ -7,20 +7,25 @@ using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using AutoMapper;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Bit.Core.Repositories.EntityFramework
|
||||
{
|
||||
public class OrganizationRepository : Repository<TableModel.Organization, EFModel.Organization, Guid>, IOrganizationRepository
|
||||
{
|
||||
public OrganizationRepository(DatabaseContext databaseContext, IMapper mapper)
|
||||
: base(databaseContext, mapper, () => databaseContext.Organizations)
|
||||
public OrganizationRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper, (DatabaseContext context) => context.Organizations)
|
||||
{ }
|
||||
|
||||
public async Task<ICollection<TableModel.Organization>> GetManyByEnabledAsync()
|
||||
{
|
||||
var organizations = await GetDbSet().Where(e => e.Enabled).ToListAsync();
|
||||
using(var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var organizations = await GetDbSet(dbContext).Where(e => e.Enabled).ToListAsync();
|
||||
return Mapper.Map<List<TableModel.Organization>>(organizations);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<TableModel.Organization>> GetManyByUserIdAsync(Guid userId)
|
||||
{
|
||||
@ -31,14 +36,18 @@ namespace Bit.Core.Repositories.EntityFramework
|
||||
public async Task<ICollection<TableModel.Organization>> SearchAsync(string name, string userEmail, bool? paid,
|
||||
int skip, int take)
|
||||
{
|
||||
using(var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
// TODO: more filters
|
||||
var organizations = await GetDbSet()
|
||||
var organizations = await GetDbSet(dbContext)
|
||||
.Where(e => name == null || e.Name.StartsWith(name))
|
||||
.OrderBy(e => e.Name)
|
||||
.Skip(skip).Take(take)
|
||||
.ToListAsync();
|
||||
return Mapper.Map<List<TableModel.Organization>>(organizations);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task UpdateStorageAsync(Guid id)
|
||||
{
|
||||
@ -47,7 +56,10 @@ namespace Bit.Core.Repositories.EntityFramework
|
||||
|
||||
public async Task<ICollection<DataModel.OrganizationAbility>> GetManyAbilitiesAsync()
|
||||
{
|
||||
return await GetDbSet()
|
||||
using(var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
return await GetDbSet(dbContext)
|
||||
.Select(e => new DataModel.OrganizationAbility
|
||||
{
|
||||
Enabled = e.Enabled,
|
||||
@ -60,3 +72,4 @@ namespace Bit.Core.Repositories.EntityFramework
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ using System.Threading.Tasks;
|
||||
using Bit.Core.Models.Table;
|
||||
using AutoMapper;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Bit.Core.Repositories.EntityFramework
|
||||
{
|
||||
@ -11,35 +12,47 @@ namespace Bit.Core.Repositories.EntityFramework
|
||||
where T : class, ITableObject<TId>
|
||||
where TEntity : class, ITableObject<TId>
|
||||
{
|
||||
public Repository(DatabaseContext databaseContext, IMapper mapper, Func<DbSet<TEntity>> getDbSet)
|
||||
: base(databaseContext, mapper)
|
||||
public Repository(IServiceScopeFactory serviceScopeFactory, IMapper mapper, Func<DatabaseContext, DbSet<TEntity>> getDbSet)
|
||||
: base(serviceScopeFactory, mapper)
|
||||
{
|
||||
GetDbSet = getDbSet;
|
||||
}
|
||||
|
||||
protected Func<DbSet<TEntity>> GetDbSet { get; private set; }
|
||||
protected Func<DatabaseContext, DbSet<TEntity>> GetDbSet { get; private set; }
|
||||
|
||||
public virtual async Task<T> GetByIdAsync(TId id)
|
||||
{
|
||||
var entity = await GetDbSet().FindAsync(id);
|
||||
using(var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var entity = await GetDbSet(dbContext).FindAsync(id);
|
||||
return entity as T;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual async Task CreateAsync(T obj)
|
||||
{
|
||||
using(var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var entity = Mapper.Map<TEntity>(obj);
|
||||
DatabaseContext.Add(entity);
|
||||
await DatabaseContext.SaveChangesAsync();
|
||||
dbContext.Add(entity);
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public virtual async Task ReplaceAsync(T obj)
|
||||
{
|
||||
var entity = await GetDbSet().FindAsync(obj.Id);
|
||||
using(var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var entity = await GetDbSet(dbContext).FindAsync(obj.Id);
|
||||
if(entity != null)
|
||||
{
|
||||
var mappedEntity = Mapper.Map<TEntity>(obj);
|
||||
DatabaseContext.Entry(entity).CurrentValues.SetValues(mappedEntity);
|
||||
await DatabaseContext.SaveChangesAsync();
|
||||
dbContext.Entry(entity).CurrentValues.SetValues(mappedEntity);
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -57,9 +70,13 @@ namespace Bit.Core.Repositories.EntityFramework
|
||||
|
||||
public virtual async Task DeleteAsync(T obj)
|
||||
{
|
||||
using(var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var entity = Mapper.Map<TEntity>(obj);
|
||||
DatabaseContext.Entry(entity).State = EntityState.Deleted;
|
||||
await DatabaseContext.SaveChangesAsync();
|
||||
dbContext.Entry(entity).State = EntityState.Deleted;
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,54 +7,80 @@ using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Bit.Core.Repositories.EntityFramework
|
||||
{
|
||||
public class UserRepository : Repository<TableModel.User, EFModel.User, Guid>, IUserRepository
|
||||
{
|
||||
public UserRepository(DatabaseContext databaseContext, IMapper mapper)
|
||||
: base(databaseContext, mapper, () => databaseContext.Users)
|
||||
public UserRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper, (DatabaseContext context) => context.Users)
|
||||
{ }
|
||||
|
||||
public async Task<TableModel.User> GetByEmailAsync(string email)
|
||||
{
|
||||
return await GetDbSet().FirstOrDefaultAsync(e => e.Email == email);
|
||||
using(var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
return await GetDbSet(dbContext).FirstOrDefaultAsync(e => e.Email == email);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<DataModel.UserKdfInformation> GetKdfInformationByEmailAsync(string email)
|
||||
{
|
||||
return await GetDbSet().Where(e => e.Email == email)
|
||||
using(var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
return await GetDbSet(dbContext).Where(e => e.Email == email)
|
||||
.Select(e => new DataModel.UserKdfInformation
|
||||
{
|
||||
Kdf = e.Kdf,
|
||||
KdfIterations = e.KdfIterations
|
||||
}).SingleOrDefaultAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<TableModel.User>> SearchAsync(string email, int skip, int take)
|
||||
{
|
||||
var users = await GetDbSet()
|
||||
using(var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var users = await GetDbSet(dbContext)
|
||||
.Where(e => email == null || e.Email.StartsWith(email))
|
||||
.OrderBy(e => e.Email)
|
||||
.Skip(skip).Take(take)
|
||||
.ToListAsync();
|
||||
return Mapper.Map<List<TableModel.User>>(users);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<TableModel.User>> GetManyByPremiumAsync(bool premium)
|
||||
{
|
||||
var users = await GetDbSet().Where(e => e.Premium == premium).ToListAsync();
|
||||
using(var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var users = await GetDbSet(dbContext).Where(e => e.Premium == premium).ToListAsync();
|
||||
return Mapper.Map<List<TableModel.User>>(users);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<string> GetPublicKeyAsync(Guid id)
|
||||
{
|
||||
return await GetDbSet().Where(e => e.Id == id).Select(e => e.PublicKey).SingleOrDefaultAsync();
|
||||
using(var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
return await GetDbSet(dbContext).Where(e => e.Id == id).Select(e => e.PublicKey).SingleOrDefaultAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<DateTime> GetAccountRevisionDateAsync(Guid id)
|
||||
{
|
||||
return await GetDbSet().Where(e => e.Id == id).Select(e => e.AccountRevisionDate).SingleOrDefaultAsync();
|
||||
using(var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
return await GetDbSet(dbContext).Where(e => e.Id == id).Select(e => e.AccountRevisionDate)
|
||||
.SingleOrDefaultAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task UpdateStorageAsync(Guid id)
|
||||
@ -64,15 +90,19 @@ namespace Bit.Core.Repositories.EntityFramework
|
||||
|
||||
public async Task UpdateRenewalReminderDateAsync(Guid id, DateTime renewalReminderDate)
|
||||
{
|
||||
using(var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var user = new EFModel.User
|
||||
{
|
||||
Id = id,
|
||||
RenewalReminderDate = renewalReminderDate
|
||||
};
|
||||
var set = GetDbSet();
|
||||
var set = GetDbSet(dbContext);
|
||||
set.Attach(user);
|
||||
DatabaseContext.Entry(user).Property(e => e.RenewalReminderDate).IsModified = true;
|
||||
await DatabaseContext.SaveChangesAsync();
|
||||
dbContext.Entry(user).Property(e => e.RenewalReminderDate).IsModified = true;
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user