1
0
mirror of https://github.com/bitwarden/server synced 2025-12-15 15:53:59 +00:00

Stricter scene and query types

SeederAPI only serves Scenes, Recipes are inteded to be used locally only.
This commit is contained in:
Matt Gibson
2025-10-29 12:27:15 -07:00
parent 16ee5cfaad
commit 878b78b51e
14 changed files with 261 additions and 204 deletions

View File

@@ -1,9 +1,10 @@
using System.Text.Json;
using Bit.Infrastructure.EntityFramework.Models;
using Bit.SeederApi.Models.Response;
namespace Bit.SeederApi.Services;
public interface IRecipeService
public interface ISeedService
{
/// <summary>
/// Executes a scene with the given template name and arguments.
@@ -13,7 +14,7 @@ public interface IRecipeService
/// <returns>A tuple containing the result and optional seed ID for tracked entities</returns>
/// <exception cref="RecipeNotFoundException">Thrown when the scene template is not found</exception>
/// <exception cref="RecipeExecutionException">Thrown when there's an error executing the scene</exception>
(object? Result, Guid? SeedId) ExecuteRecipe(string templateName, JsonElement? arguments);
SceneResponseModel ExecuteScene(string templateName, JsonElement? arguments);
/// <summary>
/// Destroys data created by a scene using the seeded data ID.

View File

@@ -3,16 +3,17 @@ using Bit.Core.Repositories;
using Bit.Infrastructure.EntityFramework.Models;
using Bit.Infrastructure.EntityFramework.Repositories;
using Bit.Seeder;
using Bit.SeederApi.Models.Response;
namespace Bit.SeederApi.Services;
public class RecipeService(
public class SeedService(
DatabaseContext databaseContext,
ILogger<RecipeService> logger,
ILogger<SeedService> logger,
IServiceProvider serviceProvider,
IUserRepository userRepository,
IOrganizationRepository organizationRepository)
: IRecipeService
: ISeedService
{
private static readonly JsonSerializerOptions _jsonOptions = new()
{
@@ -25,13 +26,13 @@ public class RecipeService(
return databaseContext.SeededData.ToList();
}
public (object? Result, Guid? SeedId) ExecuteRecipe(string templateName, JsonElement? arguments)
public SceneResponseModel ExecuteScene(string templateName, JsonElement? arguments)
{
var result = ExecuteRecipeMethod(templateName, arguments, "Seed");
var result = ExecuteSceneMethod(templateName, arguments, "Seed");
if (result.TrackedEntities.Count == 0)
{
return (Result: result.Result, SeedId: null);
return SceneResponseModel.FromSceneResult(result, seedId: null);
}
var seededData = new SeededData
@@ -48,7 +49,7 @@ public class RecipeService(
logger.LogInformation("Saved seeded data with ID {SeedId} for scene {RecipeName}",
seededData.Id, templateName);
return (Result: result.Result, SeedId: seededData.Id);
return SceneResponseModel.FromSceneResult(result, seededData.Id);
}
public object ExecuteQuery(string queryName, JsonElement? arguments)
@@ -166,7 +167,7 @@ public class RecipeService(
return new { SeedId = seedId, RecipeName = seededData.RecipeName };
}
private SceneResult ExecuteRecipeMethod(string templateName, JsonElement? arguments, string methodName)
private SceneResult<object?> ExecuteSceneMethod(string templateName, JsonElement? arguments, string methodName)
{
try
{