simpleidserver / SimpleIdServer

OpenID, OAuth 2.0, SCIM2.0, UMA2.0, FAPI, CIBA & OPENBANKING Framework for ASP.NET Core
https://simpleidserver.com/
Apache License 2.0
682 stars 90 forks source link

Allow to seed data from JSON file #752

Closed lechediaz closed 1 month ago

lechediaz commented 1 month ago

Hello,

I'm submitting my contribution as part of the functionality I discussed in Issue #521. Currently, it only allows seeding user data specified in the JSON file.

To make it work, you need to register the service in the Program file as follows:

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddJsonSeeding(builder.Configuration);

After building the application, execute the seeding:

var app = builder.Build();
using (var scope = app.Services.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
    ISeedingService seedingService = scope.ServiceProvider.GetService<ISeedingService>();
    seedingService.SeedDataAsync().Wait();
}

In the configuration, under the key JSON_SEEDS_FILE_PATH, you should specify the path where the JSON file with the information to be seeded is located.

Within the JSON file, you can include an array of users under the key Users. Each user can contain information according to the UserSeedDto class. For example:

{
  "Users": [
    {
      "Login": "user2",
      "Password": "password",
      "FirstName": "User",
      "LastName": "2",
      "Email": "user2@email.com",
      "Roles": ["user", "local"]
    },
    {
      "Login": "user3",
      "Password": "password"
    }
  ]
}

I also added a unit test to validate the deserialization of the JSON file.

In the coming days, I will try to add the ability to seed Clients in a similar manner, at least for the basic ones.

I hope my contribution is taken into account. I remain attentive to any comments or suggestions.

lechediaz commented 1 month ago

Hello. The test When_Resolve_DID_With_Two_VerificationMethod_Then_DID_Document_Is_Resolved is failing but I didn't touched.

lechediaz commented 1 month ago

Hello @thabart , I have a problem trying to resolve the conflicts in this Pull Request, but I can’t figure out how to inject the DbContext instance. Previously, I would simply inject StoreDbContext, but now I’m not sure which one to use because you moved things to a new project 😕. Can you please help me?