Skip to content

Commit

Permalink
Disallowed seeding of database depending on environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
erland-syafiq committed Oct 18, 2023
1 parent 638f54e commit 28b4bb4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
26 changes: 14 additions & 12 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,27 @@ public static async Task Main(string[] args)
app.MapRazorPages();

// Seeding Roles
using (var scope = app.Services.CreateScope())
System.Diagnostics.Debug.WriteLine("SEED DATABASE STATUS: ");
System.Diagnostics.Debug.WriteLine(builder.Configuration["SEED_DATABASE"]);
if (builder.Configuration["SEED_DATABASE"] != "false")
{
var roleManager = scope.ServiceProvider.GetRequiredService<RoleManager<IdentityRole>>();
using (var scope = app.Services.CreateScope())
{
var roleManager = scope.ServiceProvider.GetRequiredService<RoleManager<IdentityRole>>();

var roles = new[] { "Admin", "Secretariat" };
var roles = new[] { "Admin", "Secretariat" };

foreach (var role in roles)
{
if (!await roleManager.RoleExistsAsync(role))
foreach (var role in roles)
{
await roleManager.CreateAsync(new IdentityRole(role));
if (!await roleManager.RoleExistsAsync(role))
{
await roleManager.CreateAsync(new IdentityRole(role));
}
}
}
}

// Decides to create admin based on preferences
if (builder.Configuration["CREATE_ADMIN"] != "false")
{
System.Diagnostics.Debug.WriteLine("Admin Created");
// Decides to create admin based on preferences

using (var scope = app.Services.CreateScope())
{
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>();
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,12 @@ Set the admin password using the Secrets Manager. To do this, run the following
```bash
dotnet user-secrets init
dotnet user-secrets set "ADMIN_PASSWORD" "<INSERT_PASSWORD>"
dotnet user-secrets set "CREATE_ADMIN" "<true or false>"
dotnet user-secrets set "SEED_DATABASE" "<true or false>"
dotnet user-secrets list
```

These environment variables also need to be set on Azure.

On the

### Build and Run

Expand Down

0 comments on commit 28b4bb4

Please sign in to comment.