1
0
mirror of https://github.com/bitwarden/server.git synced 2025-04-13 00:58:13 -05:00

Stub out EF repo base with user repo

This commit is contained in:
Kyle Spearrin 2020-01-08 20:28:16 -05:00
parent 9caaab0537
commit 7c5be176fa
9 changed files with 243 additions and 1 deletions

View File

@ -0,0 +1,6 @@
namespace Bit.Core.Models.Table
{
internal class Ciper
{
}
}

View File

@ -21,6 +21,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="7.0.0" />
<PackageReference Include="AWSSDK.SimpleEmail" Version="3.3.101.38" />
<PackageReference Include="AWSSDK.SQS" Version="3.3.102" />
<PackageReference Include="BitPay.Light" Version="1.0.1907" />
@ -39,6 +40,7 @@
<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.EntityFrameworkCore.PostgreSQL" Version="2.2.4" />
<PackageReference Include="Quartz" Version="3.0.7" />
<PackageReference Include="Serilog.AspNetCore" Version="3.0.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="3.0.1" />

View File

@ -0,0 +1,17 @@
using AutoMapper;
namespace Bit.Core.Models.EntityFramework
{
public class Cipher : Table.Cipher
{
public User User { get; set; }
}
public class CipherMapperProfile : Profile
{
public CipherMapperProfile()
{
CreateMap<Table.Cipher, Cipher>();
}
}
}

View File

@ -0,0 +1,18 @@
using System.Collections.Generic;
using AutoMapper;
namespace Bit.Core.Models.EntityFramework
{
public class User : Table.User
{
public ICollection<Cipher> Ciphers { get; set; }
}
public class UserMapperProfile : Profile
{
public UserMapperProfile()
{
CreateMap<Table.User, User>();
}
}
}

View File

@ -0,0 +1,16 @@
using AutoMapper;
namespace Bit.Core.Repositories.EntityFramework
{
public abstract class BaseEntityFrameworkRepository
{
public BaseEntityFrameworkRepository(DatabaseContext databaseContext, IMapper mapper)
{
DatabaseContext = databaseContext;
Mapper = mapper;
}
protected DatabaseContext DatabaseContext { get; private set; }
protected IMapper Mapper { get; private set; }
}
}

View File

@ -0,0 +1,36 @@
using System;
using Bit.Core.Models.EntityFramework;
using Microsoft.EntityFrameworkCore;
namespace Bit.Core.Repositories.EntityFramework
{
public class DatabaseContext : DbContext
{
private readonly GlobalSettings _globalSettings;
public DatabaseContext(
DbContextOptions<DatabaseContext> options,
GlobalSettings globalSettings)
: base(options)
{
_globalSettings = globalSettings;
}
public DbSet<User> Users { get; set; }
public DbSet<Cipher> Ciphers { 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<User>().ToTable(nameof(User));
builder.Entity<Cipher>().ToTable(nameof(Cipher));
}
}
}

View File

@ -0,0 +1,65 @@
using System;
using System.Threading.Tasks;
using Bit.Core.Models.Table;
using AutoMapper;
using Microsoft.EntityFrameworkCore;
namespace Bit.Core.Repositories.EntityFramework
{
public abstract class Repository<T, TEntity, TId> : BaseEntityFrameworkRepository, IRepository<T, TId>
where TId : IEquatable<TId>
where T : class, ITableObject<TId>
where TEntity : class, ITableObject<TId>
{
public Repository(DatabaseContext databaseContext, IMapper mapper, Func<DbSet<TEntity>> getDbSet)
: base(databaseContext, mapper)
{
GetDbSet = getDbSet;
}
protected Func<DbSet<TEntity>> GetDbSet { get; private set; }
public virtual async Task<T> GetByIdAsync(TId id)
{
var entity = await GetDbSet().FindAsync(id);
return entity as T;
}
public virtual async Task CreateAsync(T obj)
{
var entity = Mapper.Map<TEntity>(obj);
DatabaseContext.Add(entity);
await DatabaseContext.SaveChangesAsync();
}
public virtual async Task ReplaceAsync(T obj)
{
var entity = await GetDbSet().FindAsync(obj.Id);
if(entity != null)
{
var mappedEntity = Mapper.Map<TEntity>(obj);
DatabaseContext.Entry(entity).CurrentValues.SetValues(mappedEntity);
await DatabaseContext.SaveChangesAsync();
}
}
public virtual async Task UpsertAsync(T obj)
{
if(obj.Id.Equals(default(T)))
{
await CreateAsync(obj);
}
else
{
await ReplaceAsync(obj);
}
}
public virtual async Task DeleteAsync(T obj)
{
var entity = Mapper.Map<TEntity>(obj);
DatabaseContext.Entry(entity).State = EntityState.Deleted;
await DatabaseContext.SaveChangesAsync();
}
}
}

View File

@ -0,0 +1,78 @@
using System;
using TableModel = Bit.Core.Models.Table;
using EFModel = Bit.Core.Models.EntityFramework;
using DataModel = Bit.Core.Models.Data;
using AutoMapper;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
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 async Task<TableModel.User> GetByEmailAsync(string email)
{
return await GetDbSet().FirstOrDefaultAsync(e => e.Email == email);
}
public async Task<DataModel.UserKdfInformation> GetKdfInformationByEmailAsync(string email)
{
return await GetDbSet().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()
.Where(e => 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();
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();
}
public async Task<DateTime> GetAccountRevisionDateAsync(Guid id)
{
return await GetDbSet().Where(e => e.Id == id).Select(e => e.AccountRevisionDate).SingleOrDefaultAsync();
}
public async Task UpdateStorageAsync(Guid id)
{
// TODO
}
public async Task UpdateRenewalReminderDateAsync(Guid id, DateTime renewalReminderDate)
{
var user = new EFModel.User
{
Id = id,
RenewalReminderDate = renewalReminderDate
};
var set = GetDbSet();
set.Attach(user);
DatabaseContext.Entry(user).Property(e => e.RenewalReminderDate).IsModified = true;
await DatabaseContext.SaveChangesAsync();
}
}
}

View File

@ -20,6 +20,7 @@ using System;
using System.IO;
using SqlServerRepos = Bit.Core.Repositories.SqlServer;
using PostgreSqlRepos = Bit.Core.Repositories.PostgreSql;
using EntityFrameworkRepos = Bit.Core.Repositories.EntityFramework;
using NoopRepos = Bit.Core.Repositories.Noop;
using System.Threading.Tasks;
using TableStorageRepos = Bit.Core.Repositories.TableStorage;
@ -32,6 +33,7 @@ using System.Linq;
using System.Security.Cryptography.X509Certificates;
using Bit.Core.Utilities;
using Serilog.Context;
using AutoMapper;
namespace Bit.Core.Utilities
{
@ -41,7 +43,9 @@ namespace Bit.Core.Utilities
{
if(!string.IsNullOrWhiteSpace(globalSettings.PostgreSql?.ConnectionString))
{
services.AddSingleton<IUserRepository, PostgreSqlRepos.UserRepository>();
services.AddAutoMapper(typeof(EntityFrameworkRepos.UserRepository));
services.AddDbContext<EntityFrameworkRepos.DatabaseContext>();
services.AddSingleton<IUserRepository, EntityFrameworkRepos.UserRepository>();
}
else
{