mirror of
https://github.com/bitwarden/server
synced 2025-12-16 08:13:33 +00:00
Refactor to track entities rather than manually writing destroy
This commit is contained in:
9
src/Infrastructure.EntityFramework/Models/SeededData.cs
Normal file
9
src/Infrastructure.EntityFramework/Models/SeededData.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
namespace Bit.Infrastructure.EntityFramework.Models;
|
||||||
|
|
||||||
|
public class SeededData
|
||||||
|
{
|
||||||
|
public Guid Id { get; set; }
|
||||||
|
public required string RecipeName { get; set; }
|
||||||
|
public required string Data { get; set; } // JSON blob with entity tracking info
|
||||||
|
public DateTime CreationDate { get; set; }
|
||||||
|
}
|
||||||
@@ -87,6 +87,7 @@ public class DatabaseContext : DbContext
|
|||||||
public DbSet<OrganizationInstallation> OrganizationInstallations { get; set; }
|
public DbSet<OrganizationInstallation> OrganizationInstallations { get; set; }
|
||||||
public DbSet<OrganizationReport> OrganizationReports { get; set; }
|
public DbSet<OrganizationReport> OrganizationReports { get; set; }
|
||||||
public DbSet<OrganizationApplication> OrganizationApplications { get; set; }
|
public DbSet<OrganizationApplication> OrganizationApplications { get; set; }
|
||||||
|
public DbSet<SeededData> SeededData { get; set; }
|
||||||
|
|
||||||
protected override void OnModelCreating(ModelBuilder builder)
|
protected override void OnModelCreating(ModelBuilder builder)
|
||||||
{
|
{
|
||||||
|
|||||||
6
src/Sql/dbo/Tables/SeededData.sql
Normal file
6
src/Sql/dbo/Tables/SeededData.sql
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
CREATE TABLE [dbo].[SeededData] (
|
||||||
|
[Id] UNIQUEIDENTIFIER NOT NULL,
|
||||||
|
[RecipeName] NVARCHAR (MAX) NOT NULL,
|
||||||
|
[Data] NVARCHAR (MAX) NULL,
|
||||||
|
[CreationDate] DATETIME2 (7) NOT NULL,
|
||||||
|
);
|
||||||
10
util/Migrator/DbScripts/2025-10-07_00_SeededData.sql
Normal file
10
util/Migrator/DbScripts/2025-10-07_00_SeededData.sql
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
IF OBJECT_ID('dbo.SeededData') IS NULL
|
||||||
|
BEGIN
|
||||||
|
CREATE TABLE [dbo].[SeededData] (
|
||||||
|
[Id] UNIQUEIDENTIFIER NOT NULL,
|
||||||
|
[RecipeName] NVARCHAR (MAX) NOT NULL,
|
||||||
|
[Data] NVARCHAR (MAX) NULL,
|
||||||
|
[CreationDate] DATETIME2 (7) NOT NULL,
|
||||||
|
);
|
||||||
|
END
|
||||||
|
GO
|
||||||
7
util/Seeder/RecipeResult.cs
Normal file
7
util/Seeder/RecipeResult.cs
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
namespace Bit.Seeder;
|
||||||
|
|
||||||
|
public class RecipeResult
|
||||||
|
{
|
||||||
|
public required object Result { get; init; }
|
||||||
|
public Dictionary<string, List<Guid>> TrackedEntities { get; init; } = new();
|
||||||
|
}
|
||||||
@@ -1,15 +1,13 @@
|
|||||||
using Bit.Infrastructure.EntityFramework.AdminConsole.Models;
|
using Bit.Infrastructure.EntityFramework.Models;
|
||||||
using Bit.Infrastructure.EntityFramework.Models;
|
|
||||||
using Bit.Infrastructure.EntityFramework.Repositories;
|
using Bit.Infrastructure.EntityFramework.Repositories;
|
||||||
using Bit.Seeder.Factories;
|
using Bit.Seeder.Factories;
|
||||||
using LinqToDB.EntityFrameworkCore;
|
using LinqToDB.EntityFrameworkCore;
|
||||||
using Microsoft.EntityFrameworkCore;
|
|
||||||
|
|
||||||
namespace Bit.Seeder.Recipes;
|
namespace Bit.Seeder.Recipes;
|
||||||
|
|
||||||
public class OrganizationWithUsersRecipe(DatabaseContext db)
|
public class OrganizationWithUsersRecipe(DatabaseContext db)
|
||||||
{
|
{
|
||||||
public Guid Seed(string name, int users, string domain)
|
public RecipeResult Seed(string name, int users, string domain)
|
||||||
{
|
{
|
||||||
var organization = OrganizationSeeder.CreateEnterprise(name, domain, users);
|
var organization = OrganizationSeeder.CreateEnterprise(name, domain, users);
|
||||||
var user = UserSeeder.CreateUser($"admin@{domain}");
|
var user = UserSeeder.CreateUser($"admin@{domain}");
|
||||||
@@ -34,20 +32,14 @@ public class OrganizationWithUsersRecipe(DatabaseContext db)
|
|||||||
db.BulkCopy(additionalUsers);
|
db.BulkCopy(additionalUsers);
|
||||||
db.BulkCopy(additionalOrgUsers);
|
db.BulkCopy(additionalOrgUsers);
|
||||||
|
|
||||||
return organization.Id;
|
return new RecipeResult
|
||||||
}
|
|
||||||
|
|
||||||
public void Destroy(Guid organizationId)
|
|
||||||
{
|
|
||||||
var organization = db.Organizations.Include(o => o.OrganizationUsers)
|
|
||||||
.ThenInclude(ou => ou.User).FirstOrDefault(p => p.Id == organizationId);
|
|
||||||
if (organization == null)
|
|
||||||
{
|
{
|
||||||
throw new Exception($"Organization with ID {organizationId} not found.");
|
Result = organization.Id,
|
||||||
}
|
TrackedEntities = new Dictionary<string, List<Guid>>
|
||||||
var users = organization.OrganizationUsers.Select(u => u.User);
|
{
|
||||||
|
["Organization"] = [organization.Id],
|
||||||
db.RemoveRange(users);
|
["User"] = [user.Id, .. additionalUsers.Select(u => u.Id)]
|
||||||
db.Remove(organization);
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,13 +31,14 @@ public class SeedController : Controller
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var result = _recipeService.ExecuteRecipe(request.Template, request.Arguments);
|
var (result, seedId) = _recipeService.ExecuteRecipe(request.Template, request.Arguments);
|
||||||
|
|
||||||
return Ok(new
|
return Ok(new
|
||||||
{
|
{
|
||||||
Message = "Seed completed successfully",
|
Message = "Seed completed successfully",
|
||||||
request.Template,
|
request.Template,
|
||||||
Result = result
|
Result = result,
|
||||||
|
SeedId = seedId
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
catch (RecipeNotFoundException ex)
|
catch (RecipeNotFoundException ex)
|
||||||
@@ -64,29 +65,24 @@ public class SeedController : Controller
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpDelete("/delete")]
|
[HttpDelete("/seed/{seedId}")]
|
||||||
public IActionResult Delete([FromBody] SeedRequestModel request)
|
public IActionResult Delete([FromRoute] Guid seedId)
|
||||||
{
|
{
|
||||||
_logger.LogInformation("Deleting with template: {Template}", request.Template);
|
_logger.LogInformation("Deleting seeded data with ID: {SeedId}", seedId);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var result = _recipeService.DestroyRecipe(request.Template, request.Arguments);
|
var result = _recipeService.DestroyRecipe(seedId);
|
||||||
|
|
||||||
return Ok(new
|
return Ok(new
|
||||||
{
|
{
|
||||||
Message = "Delete completed successfully",
|
Message = "Delete completed successfully",
|
||||||
request.Template,
|
|
||||||
Result = result
|
Result = result
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
catch (RecipeNotFoundException ex)
|
|
||||||
{
|
|
||||||
return NotFound(new { Error = ex.Message });
|
|
||||||
}
|
|
||||||
catch (RecipeExecutionException ex)
|
catch (RecipeExecutionException ex)
|
||||||
{
|
{
|
||||||
_logger.LogError(ex, "Error executing recipe delete: {Template}", request.Template);
|
_logger.LogError(ex, "Error deleting seeded data: {SeedId}", seedId);
|
||||||
return BadRequest(new
|
return BadRequest(new
|
||||||
{
|
{
|
||||||
Error = ex.Message,
|
Error = ex.Message,
|
||||||
@@ -95,7 +91,7 @@ public class SeedController : Controller
|
|||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_logger.LogError(ex, "Unexpected error deleting with template: {Template}", request.Template);
|
_logger.LogError(ex, "Unexpected error deleting seeded data: {SeedId}", seedId);
|
||||||
return StatusCode(500, new
|
return StatusCode(500, new
|
||||||
{
|
{
|
||||||
Error = "An unexpected error occurred while deleting",
|
Error = "An unexpected error occurred while deleting",
|
||||||
|
|||||||
@@ -9,18 +9,16 @@ public interface IRecipeService
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="templateName">The name of the recipe template (e.g., "OrganizationWithUsersRecipe")</param>
|
/// <param name="templateName">The name of the recipe template (e.g., "OrganizationWithUsersRecipe")</param>
|
||||||
/// <param name="arguments">Optional JSON arguments to pass to the recipe's Seed method</param>
|
/// <param name="arguments">Optional JSON arguments to pass to the recipe's Seed method</param>
|
||||||
/// <returns>The result returned by the recipe's Seed method</returns>
|
/// <returns>A tuple containing the result and optional seed ID for tracked entities</returns>
|
||||||
/// <exception cref="RecipeNotFoundException">Thrown when the recipe template is not found</exception>
|
/// <exception cref="RecipeNotFoundException">Thrown when the recipe template is not found</exception>
|
||||||
/// <exception cref="RecipeExecutionException">Thrown when there's an error executing the recipe</exception>
|
/// <exception cref="RecipeExecutionException">Thrown when there's an error executing the recipe</exception>
|
||||||
object? ExecuteRecipe(string templateName, JsonElement? arguments);
|
(object? Result, Guid? SeedId) ExecuteRecipe(string templateName, JsonElement? arguments);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Destroys data created by a recipe with the given template name and arguments.
|
/// Destroys data created by a recipe using the seeded data ID.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="templateName">The name of the recipe template (e.g., "OrganizationWithUsersRecipe")</param>
|
/// <param name="seedId">The ID of the seeded data to destroy</param>
|
||||||
/// <param name="arguments">Optional JSON arguments to pass to the recipe's Destroy method</param>
|
/// <returns>The result of the destroy operation</returns>
|
||||||
/// <returns>The result returned by the recipe's Destroy method</returns>
|
/// <exception cref="RecipeExecutionException">Thrown when there's an error destroying the seeded data</exception>
|
||||||
/// <exception cref="RecipeNotFoundException">Thrown when the recipe template is not found</exception>
|
object? DestroyRecipe(Guid seedId);
|
||||||
/// <exception cref="RecipeExecutionException">Thrown when there's an error executing the recipe</exception>
|
|
||||||
object? DestroyRecipe(string templateName, JsonElement? arguments);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
|
using Bit.Infrastructure.EntityFramework.Models;
|
||||||
using Bit.Infrastructure.EntityFramework.Repositories;
|
using Bit.Infrastructure.EntityFramework.Repositories;
|
||||||
|
using Bit.Seeder;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
|
||||||
@@ -15,14 +17,71 @@ public class RecipeService : IRecipeService
|
|||||||
_logger = logger;
|
_logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
public object? ExecuteRecipe(string templateName, JsonElement? arguments)
|
public (object? Result, Guid? SeedId) ExecuteRecipe(string templateName, JsonElement? arguments)
|
||||||
{
|
{
|
||||||
return ExecuteRecipeMethod(templateName, arguments, "Seed");
|
var result = ExecuteRecipeMethod(templateName, arguments, "Seed");
|
||||||
|
|
||||||
|
if (result is not RecipeResult recipeResult)
|
||||||
|
{
|
||||||
|
return (Result: result, SeedId: null);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (recipeResult.TrackedEntities.Count == 0)
|
||||||
|
{
|
||||||
|
return (Result: recipeResult.Result, SeedId: null);
|
||||||
|
}
|
||||||
|
|
||||||
|
var seededData = new SeededData
|
||||||
|
{
|
||||||
|
Id = Guid.NewGuid(),
|
||||||
|
RecipeName = templateName,
|
||||||
|
Data = JsonSerializer.Serialize(recipeResult.TrackedEntities),
|
||||||
|
CreationDate = DateTime.UtcNow
|
||||||
|
};
|
||||||
|
|
||||||
|
_databaseContext.Add(seededData);
|
||||||
|
_databaseContext.SaveChanges();
|
||||||
|
|
||||||
|
_logger.LogInformation("Saved seeded data with ID {SeedId} for recipe {RecipeName}",
|
||||||
|
seededData.Id, templateName);
|
||||||
|
|
||||||
|
return (Result: recipeResult.Result, SeedId: seededData.Id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public object? DestroyRecipe(string templateName, JsonElement? arguments)
|
public object? DestroyRecipe(Guid seedId)
|
||||||
{
|
{
|
||||||
return ExecuteRecipeMethod(templateName, arguments, "Destroy");
|
var seededData = _databaseContext.SeededData.FirstOrDefault(s => s.Id == seedId);
|
||||||
|
if (seededData == null)
|
||||||
|
{
|
||||||
|
throw new RecipeExecutionException($"Seeded data with ID {seedId} not found");
|
||||||
|
}
|
||||||
|
|
||||||
|
var trackedEntities = JsonSerializer.Deserialize<Dictionary<string, List<Guid>>>(seededData.Data);
|
||||||
|
if (trackedEntities == null)
|
||||||
|
{
|
||||||
|
throw new RecipeExecutionException($"Failed to deserialize tracked entities for seed ID {seedId}");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete in reverse order to respect foreign key constraints
|
||||||
|
if (trackedEntities.TryGetValue("User", out var userIds))
|
||||||
|
{
|
||||||
|
var users = _databaseContext.Users.Where(u => userIds.Contains(u.Id));
|
||||||
|
_databaseContext.RemoveRange(users);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (trackedEntities.TryGetValue("Organization", out var orgIds))
|
||||||
|
{
|
||||||
|
var organizations = _databaseContext.Organizations.Where(o => orgIds.Contains(o.Id));
|
||||||
|
_databaseContext.RemoveRange(organizations);
|
||||||
|
}
|
||||||
|
|
||||||
|
_databaseContext.Remove(seededData);
|
||||||
|
_databaseContext.SaveChanges();
|
||||||
|
|
||||||
|
_logger.LogInformation("Successfully destroyed seeded data with ID {SeedId} for recipe {RecipeName}",
|
||||||
|
seedId, seededData.RecipeName);
|
||||||
|
|
||||||
|
return new { SeedId = seedId, RecipeName = seededData.RecipeName };
|
||||||
}
|
}
|
||||||
|
|
||||||
private object? ExecuteRecipeMethod(string templateName, JsonElement? arguments, string methodName)
|
private object? ExecuteRecipeMethod(string templateName, JsonElement? arguments, string methodName)
|
||||||
|
|||||||
Reference in New Issue
Block a user