From 9fa7f335bd7b87c6227811e8eb091ec4fcbcfeeb Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Wed, 8 Jan 2020 21:34:49 -0500 Subject: [PATCH] org repo --- src/Core/Core.csproj | 1 + src/Core/Models/EntityFramework/Cipher.cs | 27 +++++++- .../Models/EntityFramework/Organization.cs | 18 ++++++ src/Core/Models/EntityFramework/User.cs | 2 +- .../EntityFramework/DatabaseContext.cs | 7 +++ .../EntityFramework/OrganizationRepository.cs | 62 +++++++++++++++++++ .../EntityFramework/UserRepository.cs | 2 +- 7 files changed, 115 insertions(+), 4 deletions(-) create mode 100644 src/Core/Models/EntityFramework/Organization.cs create mode 100644 src/Core/Repositories/EntityFramework/OrganizationRepository.cs diff --git a/src/Core/Core.csproj b/src/Core/Core.csproj index 718b7a1d3..dd750a49c 100644 --- a/src/Core/Core.csproj +++ b/src/Core/Core.csproj @@ -50,6 +50,7 @@ + diff --git a/src/Core/Models/EntityFramework/Cipher.cs b/src/Core/Models/EntityFramework/Cipher.cs index 4eb27ebe8..72ae91734 100644 --- a/src/Core/Models/EntityFramework/Cipher.cs +++ b/src/Core/Models/EntityFramework/Cipher.cs @@ -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(); + CreateMap().ReverseMap(); } } } diff --git a/src/Core/Models/EntityFramework/Organization.cs b/src/Core/Models/EntityFramework/Organization.cs new file mode 100644 index 000000000..c90a78b5c --- /dev/null +++ b/src/Core/Models/EntityFramework/Organization.cs @@ -0,0 +1,18 @@ +using System.Collections.Generic; +using AutoMapper; + +namespace Bit.Core.Models.EntityFramework +{ + public class Organization : Table.Organization + { + public ICollection Ciphers { get; set; } + } + + public class OrganizationMapperProfile : Profile + { + public OrganizationMapperProfile() + { + CreateMap().ReverseMap(); + } + } +} diff --git a/src/Core/Models/EntityFramework/User.cs b/src/Core/Models/EntityFramework/User.cs index eaecc6a15..88640db99 100644 --- a/src/Core/Models/EntityFramework/User.cs +++ b/src/Core/Models/EntityFramework/User.cs @@ -12,7 +12,7 @@ namespace Bit.Core.Models.EntityFramework { public UserMapperProfile() { - CreateMap(); + CreateMap().ReverseMap(); } } } diff --git a/src/Core/Repositories/EntityFramework/DatabaseContext.cs b/src/Core/Repositories/EntityFramework/DatabaseContext.cs index e49854838..ed93e06c2 100644 --- a/src/Core/Repositories/EntityFramework/DatabaseContext.cs +++ b/src/Core/Repositories/EntityFramework/DatabaseContext.cs @@ -18,6 +18,7 @@ namespace Bit.Core.Repositories.EntityFramework public DbSet Users { get; set; } public DbSet Ciphers { get; set; } + public DbSet 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().Ignore(e => e.Data); + builder.Entity().Property(e => e.Data).HasColumnName("Data"); + builder.Entity().Ignore(e => e.Attachments); + builder.Entity().Property(e => e.Attachments).HasColumnName("Attachments"); + builder.Entity().ToTable(nameof(User)); builder.Entity().ToTable(nameof(Cipher)); + builder.Entity().ToTable(nameof(Organization)); } } } diff --git a/src/Core/Repositories/EntityFramework/OrganizationRepository.cs b/src/Core/Repositories/EntityFramework/OrganizationRepository.cs new file mode 100644 index 000000000..79961543e --- /dev/null +++ b/src/Core/Repositories/EntityFramework/OrganizationRepository.cs @@ -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, IOrganizationRepository + { + public OrganizationRepository(DatabaseContext databaseContext, IMapper mapper) + : base(databaseContext, mapper, () => databaseContext.Organizations) + { } + + public async Task> GetManyByEnabledAsync() + { + var organizations = await GetDbSet().Where(e => e.Enabled).ToListAsync(); + return Mapper.Map>(organizations); + } + + public async Task> GetManyByUserIdAsync(Guid userId) + { + // TODO + return await Task.FromResult(null as ICollection); + } + + public async Task> 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>(organizations); + } + + public async Task UpdateStorageAsync(Guid id) + { + // TODO + } + + public async Task> 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(); + } + } +} diff --git a/src/Core/Repositories/EntityFramework/UserRepository.cs b/src/Core/Repositories/EntityFramework/UserRepository.cs index 29c643946..973055d7b 100644 --- a/src/Core/Repositories/EntityFramework/UserRepository.cs +++ b/src/Core/Repositories/EntityFramework/UserRepository.cs @@ -34,7 +34,7 @@ namespace Bit.Core.Repositories.EntityFramework public async Task> 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();