add roles seeding
All checks were successful
Build and Deploy Frontend / build-and-deploy (push) Successful in 8s
Build and Deploy API / build-and-deploy (push) Successful in 11s

This commit is contained in:
2026-04-21 19:13:44 -05:00
parent 214f1601b5
commit 2f3cb46af3
3 changed files with 36 additions and 19 deletions

View File

@@ -46,16 +46,13 @@ builder.Services.AddAuthentication(options => {
};
});
// TODO: adding roles doesnt work atm because roles need to be seeded in the database first
// maybe programamatically checking them at startup like if(roleManager_.FindAsync("Admin") == null { roleManager_.addAsync("Admin"); })
// or something
builder.Services.AddAuthorization(options => {
options.AddPolicy("RequireAdmin", policy => {
policy.RequireRole("Admin", "Superuser");
});
options.AddPolicy("RequireSuperuser", policy => {
policy.RequireRole("Superuser");
});
options.AddPolicy("SensitiveDataRead", policy =>
policy.RequireRole("admin", "superuser"));
options.AddPolicy("SensitiveDataModify", policy =>
policy.RequireRole("superuser"));
});
// configuration for behind my nginx proxy
@@ -123,6 +120,18 @@ using (var scope = app.Services.CreateScope()) {
Thread.Sleep(5000);
}
}
// TODO: abstract this away
// auto seed Identity roles
var roleManager = scope.ServiceProvider.GetRequiredService<RoleManager<IdentityRole>>();
string[] roles = { "guest", "user", "dev", "mod", "admin", "superuser", "role1", "role2" };
foreach(string role in roles) {
if(!await roleManager.RoleExistsAsync(role)) {
await roleManager.CreateAsync(new IdentityRole(role));
}
}
}
app.Run();