1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-26 12:55:17 +01:00
This commit is contained in:
Kyle Spearrin 2020-01-08 21:34:49 -05:00
parent 46c258428e
commit 9fa7f335bd
7 changed files with 115 additions and 4 deletions

View File

@ -50,6 +50,7 @@
<PackageReference Include="IdentityServer4" Version="2.5.3" />
<PackageReference Include="Dapper" Version="1.60.6" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
<PackageReference Include="System.Text.Json" Version="4.7.0" />
<PackageReference Include="WindowsAzure.Storage" Version="9.3.3" />
<PackageReference Include="AspNetCoreRateLimit" Version="2.1.0" />
<PackageReference Include="Braintree" Version="4.15.0" />

View File

@ -1,17 +1,40 @@
using AutoMapper;
using System.Text.Json;
using AutoMapper;
namespace Bit.Core.Models.EntityFramework
{
public class Cipher : Table.Cipher
{
private JsonDocument _dataJson;
private JsonDocument _attachmentsJson;
public User User { get; set; }
public Organization Organization { get; set; }
public JsonDocument DataJson
{
get => _dataJson;
set
{
Data = value.ToString();
_dataJson = value;
}
}
public JsonDocument AttachmentsJson
{
get => _attachmentsJson;
set
{
Attachments = value.ToString();
_attachmentsJson = value;
}
}
}
public class CipherMapperProfile : Profile
{
public CipherMapperProfile()
{
CreateMap<Table.Cipher, Cipher>();
CreateMap<Table.Cipher, Cipher>().ReverseMap();
}
}
}

View File

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

View File

@ -12,7 +12,7 @@ namespace Bit.Core.Models.EntityFramework
{
public UserMapperProfile()
{
CreateMap<Table.User, User>();
CreateMap<Table.User, User>().ReverseMap();
}
}
}

View File

@ -18,6 +18,7 @@ namespace Bit.Core.Repositories.EntityFramework
public DbSet<User> Users { get; set; }
public DbSet<Cipher> Ciphers { get; set; }
public DbSet<Organization> Organizations { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder builder)
{
@ -29,8 +30,14 @@ namespace Bit.Core.Repositories.EntityFramework
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>().Ignore(e => e.Attachments);
builder.Entity<Cipher>().Property(e => e.Attachments).HasColumnName("Attachments");
builder.Entity<User>().ToTable(nameof(User));
builder.Entity<Cipher>().ToTable(nameof(Cipher));
builder.Entity<Organization>().ToTable(nameof(Organization));
}
}
}

View File

@ -0,0 +1,62 @@
using System;
using System.Threading.Tasks;
using TableModel = Bit.Core.Models.Table;
using DataModel = Bit.Core.Models.Data;
using EFModel = Bit.Core.Models.EntityFramework;
using System.Linq;
using System.Collections.Generic;
using AutoMapper;
using Microsoft.EntityFrameworkCore;
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 async Task<ICollection<TableModel.Organization>> GetManyByEnabledAsync()
{
var organizations = await GetDbSet().Where(e => e.Enabled).ToListAsync();
return Mapper.Map<List<TableModel.Organization>>(organizations);
}
public async Task<ICollection<TableModel.Organization>> GetManyByUserIdAsync(Guid userId)
{
// TODO
return await Task.FromResult(null as ICollection<TableModel.Organization>);
}
public async Task<ICollection<TableModel.Organization>> SearchAsync(string name, string userEmail, bool? paid,
int skip, int take)
{
// TODO: more filters
var organizations = await GetDbSet()
.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)
{
// TODO
}
public async Task<ICollection<DataModel.OrganizationAbility>> GetManyAbilitiesAsync()
{
return await GetDbSet()
.Select(e => new DataModel.OrganizationAbility
{
Enabled = e.Enabled,
Id = e.Id,
Use2fa = e.Use2fa,
UseEvents = e.UseEvents,
UsersGetPremium = e.UsersGetPremium,
Using2fa = e.Use2fa && e.TwoFactorProviders != null && e.TwoFactorProviders != "{}",
}).ToListAsync();
}
}
}

View File

@ -34,7 +34,7 @@ namespace Bit.Core.Repositories.EntityFramework
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))
.Where(e => email == null || e.Email.StartsWith(email))
.OrderBy(e => e.Email)
.Skip(skip).Take(take)
.ToListAsync();