Skip to content

Improvement in source code to scale when adding more user. #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion APIwebASPNETCore6JWT/APIwebASPNETCore6JWT.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
<None Remove="Authorization\" />
</ItemGroup>
<ItemGroup>
<Folder Include="Identity\" />
<Folder Include="Authorization\" />
</ItemGroup>
</Project>
105 changes: 95 additions & 10 deletions APIwebASPNETCore6JWT/Identity/AppIdentityDbContextSeed.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using Microsoft.AspNetCore.Identity;
using System;

namespace APIwebASPNETCore6JWT.Identity;

public static class AppIdentityDbContextSeed
{
#region Public Methods

public static async Task SeedAsync(WebApplication app)
{
using var scope = app.Services.CreateScope();
Expand All @@ -13,23 +16,105 @@ public static async Task SeedAsync(WebApplication app)
var userManager = scopedProvider.GetRequiredService<UserManager<ApplicationUser>>();
var roleManager = scopedProvider.GetRequiredService<RoleManager<IdentityRole>>();

await roleManager.CreateAsync(new IdentityRole("Administrator"));
var roles = GetListOfRolesToSeed();
var users = GetListOfUsersToSeed();

// 1. ASP.NET Core Identity: Add roles.
foreach (var role in roles)
{
// Asp Net Identity: Create roles to assign user after.
var createdRole = await roleManager.CreateAsync(new IdentityRole(role.RoleName));

if (!createdRole.Succeeded)
{
throw new Exception($"role {role.RoleName} not created.");
}
}

// 2. ASP.NET Core Identity: Add users and assigning roles.
foreach (var user in users)
{
var applicationUser = new ApplicationUser()
{
UserName = user.UserName,
Email = user.Email,
PhoneNumber = user.PhoneNumber,
};

// ASP.NET Core Identity: Create user.
var createUserTaskResult = await userManager.CreateAsync(applicationUser, user.Password);

if (!createUserTaskResult.Succeeded)
{
throw new Exception($"User {applicationUser.UserName} not created.");
}

// ASP.NET Core Identity: Finding user to assign role.
var foundUser = await userManager.FindByNameAsync(user.UserName);

var defaultUser = new ApplicationUser { UserName = "[email protected]", Email = "[email protected]" };
await userManager.CreateAsync(defaultUser, "[email protected]");
if (foundUser == null)
{
throw new Exception($"User not created.");
}

string adminUserName = "[email protected]";
var adminUser = new ApplicationUser { UserName = adminUserName, Email = adminUserName };
await userManager.CreateAsync(adminUser, "[email protected]");
// ASP.NET Core Identity: Assigning role to user.
var rolledUser = await userManager.AddToRoleAsync(applicationUser, user.Role);

adminUser = await userManager.FindByNameAsync(adminUserName);
await userManager.AddToRoleAsync(adminUser, "Administrator");
if (!rolledUser.Succeeded)
{
throw new Exception($"{user.UserName} is not enrolled.");
}
}
}
catch (Exception ex)
{
app.Logger.LogError(ex, "An error occurred seeding the DB.");
app.Logger.LogError($"An error occurred seeding the DB: {exception.Message}");
}
}

}
#endregion

#region Private Methods

private static IList<UserToSeed> GetListOfUsersToSeed()
{
return new List<UserToSeed>()
{
new UserToSeed()
{
UserName = "javi.karra",
Password = "[email protected]@javi",
Email = "[email protected]",
PhoneNumber = "1234567890",
Role = RoleDefinition.USER_ROLE,
},
new UserToSeed()
{
UserName = "lucas.perez",
Password = "[email protected]@lucas",
Email = "[email protected]",
PhoneNumber = "6234567890",
Role = RoleDefinition.USER_ROLE,
},
new UserToSeed()
{
UserName = "admin",
Password = "[email protected]@admin",
Email = "[email protected]",
PhoneNumber = "623455699",
Role = RoleDefinition.ADMINISTRATOR_ROLE,
}
};
}

private static IList<RolesToSeed> GetListOfRolesToSeed()
{
return new List<RolesToSeed>()
{
new RolesToSeed() { RoleName = RoleDefinition.ADMINISTRATOR_ROLE },
new RolesToSeed() { RoleName = RoleDefinition.USER_ROLE }
};
}

#endregion
}
13 changes: 13 additions & 0 deletions APIwebASPNETCore6JWT/Identity/RoleDefinition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace APIwebASPNETCore6JWT.Identity
{
public static class RoleDefinition
{
#region Fiedls

public const string ADMINISTRATOR_ROLE = "Administrator";

public const string USER_ROLE = "User";

#endregion
}
}
7 changes: 7 additions & 0 deletions APIwebASPNETCore6JWT/Identity/RolesToSeed.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace APIwebASPNETCore6JWT.Identity
{
public class RolesToSeed
{
public string RoleName { get; set; } = default!;
}
}
13 changes: 13 additions & 0 deletions APIwebASPNETCore6JWT/Identity/UserToSeed.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace APIwebASPNETCore6JWT.Identity
{
public class UserToSeed
{
public string Email { get; set; } = default!;
public string FirstName { get; set; } = default!;
public string LastName { get; set; } = default!;
public string Password { get; set; } = default!;
public string PhoneNumber { get; set; } = default!;
public string Role { get; set; } = default!;
public string UserName { get; set; } = default!;
}
}