mirror of
https://github.com/bitwarden/server.git
synced 2025-07-07 10:55:43 -05:00
Stub out EF repo base with user repo
This commit is contained in:
@ -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; }
|
||||
}
|
||||
}
|
36
src/Core/Repositories/EntityFramework/DatabaseContext.cs
Normal file
36
src/Core/Repositories/EntityFramework/DatabaseContext.cs
Normal 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));
|
||||
}
|
||||
}
|
||||
}
|
65
src/Core/Repositories/EntityFramework/Repository.cs
Normal file
65
src/Core/Repositories/EntityFramework/Repository.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
78
src/Core/Repositories/EntityFramework/UserRepository.cs
Normal file
78
src/Core/Repositories/EntityFramework/UserRepository.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user