1
0
mirror of https://github.com/bitwarden/server synced 2025-12-24 20:23:21 +00:00

PM-20574 & PM-20575 Adding Risk Insight Report tables, repositories, and migrations (#5839)

* PM-20574 fixing namespaces on reporting work that got moved over from tools

* PM-20574 adding tables, stored procedures, and migration files

* PM-20574 adding dapper and ef repos and migrations

* PM-20574 changing table and repo names as requested

* PM-20574 updating sql scripts to new names

* PM-20574 updating sql scripts

* PM-20574 updating migration script for org delete by id

* PM-20574 adding mysql migration

* PM-20574 updating sql migration to fix database test

* PM-20574 fixing migration script

* PM-20574 fixing migration script

* PM-20574 fixing table scripts

* PM-20574 fixing table scripts

* PM-20574 fixing migration script formatting

* PM-20574 fixing syntax in migration script

* PM-20574 fixing file names and extensions

* PM-20574 fixing sql file

* PM-20574 fixing sql

* PM-20574 fixing directory for entities and removing scripts from other databases

* PM-20574 generating new migration scripts

* PM-20574 fixed reference to a stored proc

* PM-20574 adding index in scripts and missing table

* PM-20574 fixing merge conflicts

* PM-20574 set OUTPUT param for Id property in create and update proc

* PM-20574 add CreateDate to the update proc

* PM-20574 amend update proc for OrganizationApplication by adding createDate

* PM-20574 formatted sql and updated as per PR comments

* PM-20574 updated script to fix build error

* PM-20574 fixed inconsistency in db script

* PM-20574 removed revisionDate, update procedures and used views

* PM-20574 removed RevisionDate from designer files

* PM-20574 removed revisionDate column that was missed previously

* PM-20574 added revision date back into the mix

* PM-20574 updated database script to fix build error

* PM-20574 fixed a procedure issue

* PM-20574 fix dB build error

* PM-020574 fixed additional PR comments - files cleaned up

* PM-20574 updated procedure was inconsistent

* Update 2025-06-13-00_OrganizationReport.sql

---------

Co-authored-by: voommen-livefront <voommen@livefront.com>
This commit is contained in:
Graham Walker
2025-06-23 12:12:04 -05:00
committed by GitHub
parent d2410747d0
commit 173db0a2dd
66 changed files with 11103 additions and 33 deletions

View File

@@ -0,0 +1,30 @@
using Bit.Infrastructure.EntityFramework.Dirt.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Bit.Infrastructure.EntityFramework.Dirt.Configurations;
public class OrganizationApplicationEntityTypeConfiguration : IEntityTypeConfiguration<OrganizationApplication>
{
public void Configure(EntityTypeBuilder<OrganizationApplication> builder)
{
builder
.Property(s => s.Id)
.ValueGeneratedNever();
builder.HasIndex(s => s.Id)
.IsClustered(true);
builder
.HasIndex(s => s.OrganizationId)
.IsClustered(false);
builder
.HasOne(s => s.Organization)
.WithMany()
.HasForeignKey(s => s.OrganizationId)
.OnDelete(DeleteBehavior.Cascade);
builder.ToTable(nameof(OrganizationApplication));
}
}

View File

@@ -0,0 +1,30 @@
using Bit.Infrastructure.EntityFramework.Dirt.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Bit.Infrastructure.EntityFramework.Dirt.Configurations;
public class OrganizationReportEntityTypeConfiguration : IEntityTypeConfiguration<OrganizationReport>
{
public void Configure(EntityTypeBuilder<OrganizationReport> builder)
{
builder
.Property(s => s.Id)
.ValueGeneratedNever();
builder.HasIndex(s => s.Id)
.IsClustered(true);
builder
.HasIndex(s => s.OrganizationId)
.IsClustered(false);
builder
.HasOne(s => s.Organization)
.WithMany()
.HasForeignKey(s => s.OrganizationId)
.OnDelete(DeleteBehavior.Cascade);
builder.ToTable(nameof(OrganizationReport));
}
}

View File

@@ -0,0 +1,17 @@
using AutoMapper;
using Bit.Infrastructure.EntityFramework.AdminConsole.Models;
namespace Bit.Infrastructure.EntityFramework.Dirt.Models;
public class OrganizationApplication : Core.Dirt.Entities.OrganizationApplication
{
public virtual Organization Organization { get; set; }
}
public class OrganizationApplicationProfile : Profile
{
public OrganizationApplicationProfile()
{
CreateMap<Core.Dirt.Entities.OrganizationApplication, OrganizationApplication>()
.ReverseMap();
}
}

View File

@@ -0,0 +1,17 @@
using AutoMapper;
using Bit.Infrastructure.EntityFramework.AdminConsole.Models;
namespace Bit.Infrastructure.EntityFramework.Dirt.Models;
public class OrganizationReport : Core.Dirt.Entities.OrganizationReport
{
public virtual Organization Organization { get; set; }
}
public class OrganizationReportProfile : Profile
{
public OrganizationReportProfile()
{
CreateMap<Core.Dirt.Entities.OrganizationReport, OrganizationReport>()
.ReverseMap();
}
}

View File

@@ -3,7 +3,7 @@ using Bit.Infrastructure.EntityFramework.AdminConsole.Models;
namespace Bit.Infrastructure.EntityFramework.Dirt.Models;
public class PasswordHealthReportApplication : Core.Dirt.Reports.Entities.PasswordHealthReportApplication
public class PasswordHealthReportApplication : Core.Dirt.Entities.PasswordHealthReportApplication
{
public virtual Organization Organization { get; set; }
}
@@ -12,7 +12,7 @@ public class PasswordHealthReportApplicationProfile : Profile
{
public PasswordHealthReportApplicationProfile()
{
CreateMap<Core.Dirt.Reports.Entities.PasswordHealthReportApplication, PasswordHealthReportApplication>()
CreateMap<Core.Dirt.Entities.PasswordHealthReportApplication, PasswordHealthReportApplication>()
.ReverseMap();
}
}

View File

@@ -0,0 +1,29 @@
using AutoMapper;
using Bit.Core.Dirt.Repositories;
using Bit.Infrastructure.EntityFramework.Dirt.Models;
using Bit.Infrastructure.EntityFramework.Repositories;
using LinqToDB;
using Microsoft.Extensions.DependencyInjection;
namespace Bit.Infrastructure.EntityFramework.Dirt.Repositories;
public class OrganizationApplicationRepository :
Repository<Core.Dirt.Entities.OrganizationApplication, OrganizationApplication, Guid>,
IOrganizationApplicationRepository
{
public OrganizationApplicationRepository(IServiceScopeFactory serviceScopeFactory,
IMapper mapper) : base(serviceScopeFactory, mapper, (DatabaseContext context) => context.OrganizationApplications)
{ }
public async Task<ICollection<Core.Dirt.Entities.OrganizationApplication>> GetByOrganizationIdAsync(Guid organizationId)
{
using (var scope = ServiceScopeFactory.CreateScope())
{
var dbContext = GetDatabaseContext(scope);
var results = await dbContext.OrganizationApplications
.Where(p => p.OrganizationId == organizationId)
.ToListAsync();
return Mapper.Map<ICollection<Core.Dirt.Entities.OrganizationApplication>>(results);
}
}
}

View File

@@ -0,0 +1,30 @@
using AutoMapper;
using Bit.Core.Dirt.Entities;
using Bit.Core.Dirt.Repositories;
using Bit.Infrastructure.EntityFramework.Repositories;
using LinqToDB;
using Microsoft.Extensions.DependencyInjection;
namespace Bit.Infrastructure.EntityFramework.Dirt.Repositories;
public class OrganizationReportRepository :
Repository<OrganizationReport, Models.OrganizationReport, Guid>,
IOrganizationReportRepository
{
public OrganizationReportRepository(IServiceScopeFactory serviceScopeFactory,
IMapper mapper) : base(serviceScopeFactory, mapper, (DatabaseContext context) => context.OrganizationReports)
{ }
public async Task<ICollection<OrganizationReport>> GetByOrganizationIdAsync(Guid organizationId)
{
using (var scope = ServiceScopeFactory.CreateScope())
{
var dbContext = GetDatabaseContext(scope);
var results = await dbContext.OrganizationReports
.Where(p => p.OrganizationId == organizationId)
.ToListAsync();
return Mapper.Map<ICollection<OrganizationReport>>(results);
}
}
}

View File

@@ -1,5 +1,5 @@
using AutoMapper;
using Bit.Core.Dirt.Reports.Repositories;
using Bit.Core.Dirt.Repositories;
using Bit.Infrastructure.EntityFramework.Dirt.Models;
using Bit.Infrastructure.EntityFramework.Repositories;
using LinqToDB;
@@ -8,14 +8,14 @@ using Microsoft.Extensions.DependencyInjection;
namespace Bit.Infrastructure.EntityFramework.Dirt.Repositories;
public class PasswordHealthReportApplicationRepository :
Repository<Core.Dirt.Reports.Entities.PasswordHealthReportApplication, PasswordHealthReportApplication, Guid>,
Repository<Core.Dirt.Entities.PasswordHealthReportApplication, PasswordHealthReportApplication, Guid>,
IPasswordHealthReportApplicationRepository
{
public PasswordHealthReportApplicationRepository(IServiceScopeFactory serviceScopeFactory,
IMapper mapper) : base(serviceScopeFactory, mapper, (DatabaseContext context) => context.PasswordHealthReportApplications)
{ }
public async Task<ICollection<Core.Dirt.Reports.Entities.PasswordHealthReportApplication>> GetByOrganizationIdAsync(Guid organizationId)
public async Task<ICollection<Core.Dirt.Entities.PasswordHealthReportApplication>> GetByOrganizationIdAsync(Guid organizationId)
{
using (var scope = ServiceScopeFactory.CreateScope())
{
@@ -23,7 +23,7 @@ public class PasswordHealthReportApplicationRepository :
var results = await dbContext.PasswordHealthReportApplications
.Where(p => p.OrganizationId == organizationId)
.ToListAsync();
return Mapper.Map<ICollection<Core.Dirt.Reports.Entities.PasswordHealthReportApplication>>(results);
return Mapper.Map<ICollection<Core.Dirt.Entities.PasswordHealthReportApplication>>(results);
}
}
}

View File

@@ -3,6 +3,7 @@ using Bit.Core.Auth.Repositories;
using Bit.Core.Billing.Providers.Repositories;
using Bit.Core.Billing.Repositories;
using Bit.Core.Dirt.Reports.Repositories;
using Bit.Core.Dirt.Repositories;
using Bit.Core.Enums;
using Bit.Core.KeyManagement.Repositories;
using Bit.Core.NotificationCenter.Repositories;
@@ -108,6 +109,8 @@ public static class EntityFrameworkServiceCollectionExtensions
services.AddSingleton<ISecurityTaskRepository, SecurityTaskRepository>();
services.AddSingleton<IUserAsymmetricKeysRepository, UserAsymmetricKeysRepository>();
services.AddSingleton<IOrganizationInstallationRepository, OrganizationInstallationRepository>();
services.AddSingleton<IOrganizationReportRepository, OrganizationReportRepository>();
services.AddSingleton<IOrganizationApplicationRepository, OrganizationApplicationRepository>();
services.AddSingleton<IOrganizationMemberBaseDetailRepository, OrganizationMemberBaseDetailRepository>();
if (selfHosted)

View File

@@ -16,6 +16,7 @@ using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using DP = Microsoft.AspNetCore.DataProtection;
#nullable enable
namespace Bit.Infrastructure.EntityFramework.Repositories;
@@ -84,6 +85,8 @@ public class DatabaseContext : DbContext
public DbSet<OrganizationMemberBaseDetail> OrganizationMemberBaseDetails { get; set; }
public DbSet<SecurityTask> SecurityTasks { get; set; }
public DbSet<OrganizationInstallation> OrganizationInstallations { get; set; }
public DbSet<OrganizationReport> OrganizationReports { get; set; }
public DbSet<OrganizationApplication> OrganizationApplications { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{