Skip to content

Commit

Permalink
New - Ajout d'utilisateur par défaut pour la plateforme de tests
Browse files Browse the repository at this point in the history
  • Loading branch information
noelmugnier committed Nov 4, 2020
1 parent 326914f commit 3a9d69c
Showing 1 changed file with 143 additions and 0 deletions.
143 changes: 143 additions & 0 deletions Sheaft.Identity/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,149 @@ public void Configure(IApplicationBuilder app)
authContext.SaveChanges();
}

if (Env.IsDevelopment())
{
var prod1Email = "[email protected]";
if (!authContext.Users.Any(u => u.UserName == prod1Email))
{
var um = serviceScope.ServiceProvider.GetService<UserManager<AppUser>>();
var user = new AppUser()
{
Id = "5a8f0ae2b70147f0a8efe7c2365a72eb",
UserName = prod1Email,
Email = prod1Email,
LastName = "Piquet",
FirstName = "Arnold"
};

var result = um.CreateAsync(user, "password").Result;

if (result.Succeeded)
{
var userCreated = authContext.Users.FirstOrDefault(u => u.Email == user.Email);

um.AddToRoleAsync(userCreated, Configuration.GetValue<string>("Roles:Producer:value")).Wait();
um.AddToRoleAsync(userCreated, Configuration.GetValue<string>("Roles:Owner:value")).Wait();
um.AddToRoleAsync(userCreated, Configuration.GetValue<string>("Roles:AppUser:value")).Wait();

um.AddClaimAsync(userCreated, new Claim(JwtClaimTypes.Name, $"La Ferme des Piquets")).Wait();
um.AddClaimAsync(userCreated, new Claim(JwtClaimTypes.GivenName, user.FirstName)).Wait();
um.AddClaimAsync(userCreated, new Claim(JwtClaimTypes.FamilyName, user.LastName)).Wait();
um.AddClaimAsync(userCreated, new Claim(JwtClaimTypes.Email, user.Email)).Wait();
um.AddClaimAsync(userCreated, new Claim(JwtClaimTypes.Role, Configuration.GetValue<string>("Roles:Owner:value"))).Wait();
um.AddClaimAsync(userCreated, new Claim(JwtClaimTypes.Role, Configuration.GetValue<string>("Roles:AppUser:value"))).Wait();
um.AddClaimAsync(userCreated, new Claim(JwtClaimTypes.Role, Configuration.GetValue<string>("Roles:Producer:value"))).Wait();
}

authContext.SaveChanges();
}

var prod2Email = "[email protected]";
if (!authContext.Users.Any(u => u.UserName == prod2Email))
{
var um = serviceScope.ServiceProvider.GetService<UserManager<AppUser>>();
var user = new AppUser()
{
Id = "442e31e3eea94aa0b7413245ed1c6f2f",
UserName = prod2Email,
Email = prod2Email,
LastName = "Fotdakor",
FirstName = "Peter"
};

var result = um.CreateAsync(user, "password").Result;

if (result.Succeeded)
{
var userCreated = authContext.Users.FirstOrDefault(u => u.Email == user.Email);

um.AddToRoleAsync(userCreated, Configuration.GetValue<string>("Roles:Producer:value")).Wait();
um.AddToRoleAsync(userCreated, Configuration.GetValue<string>("Roles:Owner:value")).Wait();
um.AddToRoleAsync(userCreated, Configuration.GetValue<string>("Roles:AppUser:value")).Wait();

um.AddClaimAsync(userCreated, new Claim(JwtClaimTypes.Name, $"La ferme pas d'accord")).Wait();
um.AddClaimAsync(userCreated, new Claim(JwtClaimTypes.GivenName, user.FirstName)).Wait();
um.AddClaimAsync(userCreated, new Claim(JwtClaimTypes.FamilyName, user.LastName)).Wait();
um.AddClaimAsync(userCreated, new Claim(JwtClaimTypes.Email, user.Email)).Wait();
um.AddClaimAsync(userCreated, new Claim(JwtClaimTypes.Role, Configuration.GetValue<string>("Roles:Owner:value"))).Wait();
um.AddClaimAsync(userCreated, new Claim(JwtClaimTypes.Role, Configuration.GetValue<string>("Roles:AppUser:value"))).Wait();
um.AddClaimAsync(userCreated, new Claim(JwtClaimTypes.Role, Configuration.GetValue<string>("Roles:Producer:value"))).Wait();
}

authContext.SaveChanges();
}

var mag1Email = "[email protected]";
if (!authContext.Users.Any(u => u.UserName == mag1Email))
{
var um = serviceScope.ServiceProvider.GetService<UserManager<AppUser>>();
var user = new AppUser()
{
Id = "28491432175442859f675386a898a48f",
UserName = mag1Email,
Email = mag1Email,
LastName = "Debussy",
FirstName = "Elia"
};

var result = um.CreateAsync(user, "password").Result;

if (result.Succeeded)
{
var userCreated = authContext.Users.FirstOrDefault(u => u.Email == user.Email);

um.AddToRoleAsync(userCreated, Configuration.GetValue<string>("Roles:Store:value")).Wait();
um.AddToRoleAsync(userCreated, Configuration.GetValue<string>("Roles:Owner:value")).Wait();
um.AddToRoleAsync(userCreated, Configuration.GetValue<string>("Roles:AppUser:value")).Wait();

um.AddClaimAsync(userCreated, new Claim(JwtClaimTypes.Name, $"O'local")).Wait();
um.AddClaimAsync(userCreated, new Claim(JwtClaimTypes.GivenName, user.FirstName)).Wait();
um.AddClaimAsync(userCreated, new Claim(JwtClaimTypes.FamilyName, user.LastName)).Wait();
um.AddClaimAsync(userCreated, new Claim(JwtClaimTypes.Email, user.Email)).Wait();
um.AddClaimAsync(userCreated, new Claim(JwtClaimTypes.Role, Configuration.GetValue<string>("Roles:Owner:value"))).Wait();
um.AddClaimAsync(userCreated, new Claim(JwtClaimTypes.Role, Configuration.GetValue<string>("Roles:AppUser:value"))).Wait();
um.AddClaimAsync(userCreated, new Claim(JwtClaimTypes.Role, Configuration.GetValue<string>("Roles:Store:value"))).Wait();
}

authContext.SaveChanges();
}

var mag2Email = "[email protected]";
if (!authContext.Users.Any(u => u.UserName == mag2Email))
{
var um = serviceScope.ServiceProvider.GetService<UserManager<AppUser>>();
var user = new AppUser()
{
Id = "0eafd299d0e64a63af8d6d154db96f55",
UserName = mag2Email,
Email = mag2Email,
LastName = "Syntax",
FirstName = "John"
};

var result = um.CreateAsync(user, "password").Result;

if (result.Succeeded)
{
var userCreated = authContext.Users.FirstOrDefault(u => u.Email == user.Email);

um.AddToRoleAsync(userCreated, Configuration.GetValue<string>("Roles:Store:value")).Wait();
um.AddToRoleAsync(userCreated, Configuration.GetValue<string>("Roles:Owner:value")).Wait();
um.AddToRoleAsync(userCreated, Configuration.GetValue<string>("Roles:AppUser:value")).Wait();

um.AddClaimAsync(userCreated, new Claim(JwtClaimTypes.Name, $"Mes p'tits plats")).Wait();
um.AddClaimAsync(userCreated, new Claim(JwtClaimTypes.GivenName, user.FirstName)).Wait();
um.AddClaimAsync(userCreated, new Claim(JwtClaimTypes.FamilyName, user.LastName)).Wait();
um.AddClaimAsync(userCreated, new Claim(JwtClaimTypes.Email, user.Email)).Wait();
um.AddClaimAsync(userCreated, new Claim(JwtClaimTypes.Role, Configuration.GetValue<string>("Roles:Owner:value"))).Wait();
um.AddClaimAsync(userCreated, new Claim(JwtClaimTypes.Role, Configuration.GetValue<string>("Roles:AppUser:value"))).Wait();
um.AddClaimAsync(userCreated, new Claim(JwtClaimTypes.Role, Configuration.GetValue<string>("Roles:Store:value"))).Wait();
}

authContext.SaveChanges();
}
}

if (!configContext.IdentityResources.Any())
{
configContext.IdentityResources.AddRange(new List<IdentityResource>
Expand Down

0 comments on commit 3a9d69c

Please sign in to comment.