Initial commit
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using RobotNet10.RobotApp.Data;
|
||||
|
||||
namespace RobotNet10.RobotApp.Data;
|
||||
|
||||
public static class ApplicationDbExtensions
|
||||
{
|
||||
extension(IServiceProvider serviceProvider)
|
||||
{
|
||||
public async Task SeedApplicationDbAsync()
|
||||
{
|
||||
using var scope = serviceProvider.CreateScope();
|
||||
|
||||
using var appDb = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
||||
|
||||
await appDb.Database.MigrateAsync();
|
||||
await appDb.Database.EnsureCreatedAsync();
|
||||
await appDb.SaveChangesAsync();
|
||||
|
||||
await scope.ServiceProvider.SeedRolesAsync();
|
||||
await scope.ServiceProvider.SeedUsersAsync();
|
||||
}
|
||||
|
||||
private async Task SeedRolesAsync()
|
||||
{
|
||||
var roleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();
|
||||
|
||||
if (!await roleManager.RoleExistsAsync("Administrator"))
|
||||
{
|
||||
await roleManager.CreateAsync(new IdentityRole()
|
||||
{
|
||||
Name = "Administrator",
|
||||
NormalizedName = "ADMINISTRATOR",
|
||||
});
|
||||
}
|
||||
|
||||
if (!await roleManager.RoleExistsAsync("Distributor"))
|
||||
{
|
||||
await roleManager.CreateAsync(new IdentityRole()
|
||||
{
|
||||
Name = "Distributor",
|
||||
NormalizedName = "DISTRIBUTOR",
|
||||
});
|
||||
}
|
||||
|
||||
if (!await roleManager.RoleExistsAsync("Operator"))
|
||||
{
|
||||
await roleManager.CreateAsync(new IdentityRole()
|
||||
{
|
||||
Name = "Operator",
|
||||
NormalizedName = "OPERATOR",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private async Task SeedUsersAsync()
|
||||
{
|
||||
using var userManager = serviceProvider.GetRequiredService<UserManager<ApplicationUser>>();
|
||||
if (await userManager.FindByNameAsync("admin") is null)
|
||||
{
|
||||
var admin = new ApplicationUser()
|
||||
{
|
||||
UserName = "admin",
|
||||
Email = "administrator@phenikaa-x.com",
|
||||
NormalizedUserName = "ADMINISTRATOR",
|
||||
NormalizedEmail = "ADMINISTRATOR@PHENIKAA-X.COM",
|
||||
EmailConfirmed = true,
|
||||
};
|
||||
|
||||
await userManager.CreateAsync(admin, "robotics");
|
||||
await userManager.AddToRoleAsync(admin, "Administrator");
|
||||
}
|
||||
|
||||
if (await userManager.FindByNameAsync("distributor") is null)
|
||||
{
|
||||
var admin = new ApplicationUser()
|
||||
{
|
||||
UserName = "distributor",
|
||||
Email = "distributor@phenikaa-x.com",
|
||||
NormalizedUserName = "DISTRIBUTOR",
|
||||
NormalizedEmail = "DISTRIBUTOR@PHENIKAA-X.COM",
|
||||
EmailConfirmed = true,
|
||||
};
|
||||
|
||||
await userManager.CreateAsync(admin, "distributor");
|
||||
await userManager.AddToRoleAsync(admin, "Distributor");
|
||||
}
|
||||
|
||||
if (await userManager.FindByNameAsync("Operator") is null)
|
||||
{
|
||||
var admin = new ApplicationUser()
|
||||
{
|
||||
UserName = "Operator",
|
||||
Email = "Operator@phenikaa-x.com",
|
||||
NormalizedUserName = "OPERATOR",
|
||||
NormalizedEmail = "OPERATOR@PHENIKAA-X.COM",
|
||||
EmailConfirmed = true,
|
||||
};
|
||||
|
||||
await userManager.CreateAsync(admin, "Operator");
|
||||
await userManager.AddToRoleAsync(admin, "Operator");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user