diff --git a/src/Core/Core.csproj b/src/Core/Core.csproj index 718b7a1d35..dd750a49ca 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 4eb27ebe81..72ae917346 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 0000000000..c90a78b5c0 --- /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 eaecc6a152..88640db99f 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 e49854838c..ed93e06c2b 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 0000000000..79961543eb --- /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 29c6439466..973055d7b8 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();