private void AddBasicUser() { Task.Run(async() => { //Check if Role Exists var basicRole = new BlazorHeroRole(RoleConstants.BasicRole, _localizer["Basic role with default permissions"]); var basicRoleInDb = await _roleManager.FindByNameAsync(RoleConstants.BasicRole); if (basicRoleInDb == null) { await _roleManager.CreateAsync(basicRole); _logger.LogInformation(_localizer["Seeded Basic Role."]); } //Check if User Exists var basicUser = new BlazorHeroUser { FirstName = "John", LastName = "Doe", Email = "*****@*****.**", UserName = "******", EmailConfirmed = true, PhoneNumberConfirmed = true, CreatedOn = DateTime.Now, IsActive = true }; var basicUserInDb = await _userManager.FindByEmailAsync(basicUser.Email); if (basicUserInDb == null) { await _userManager.CreateAsync(basicUser, UserConstants.DefaultPassword); await _userManager.AddToRoleAsync(basicUser, RoleConstants.BasicRole); _logger.LogInformation(_localizer["Seeded User with Basic Role."]); } }).GetAwaiter().GetResult(); }
private void AddAdministrator() { Task.Run(async() => { //Check if Role Exists var adminRole = new BlazorHeroRole(RoleConstants.AdministratorRole, _localizer["Administrator role with full permissions"]); var adminRoleInDb = await _roleManager.FindByNameAsync(RoleConstants.AdministratorRole); if (adminRoleInDb == null) { await _roleManager.CreateAsync(adminRole); adminRoleInDb = await _roleManager.FindByNameAsync(RoleConstants.AdministratorRole); _logger.LogInformation(_localizer["Seeded Administrator Role."]); } //Check if User Exists var superUser = new BlazorHeroUser { FirstName = "Mukesh", LastName = "Murugan", Email = "*****@*****.**", UserName = "******", EmailConfirmed = true, PhoneNumberConfirmed = true, CreatedOn = DateTime.Now, IsActive = true }; var superUserInDb = await _userManager.FindByEmailAsync(superUser.Email); if (superUserInDb == null) { await _userManager.CreateAsync(superUser, UserConstants.DefaultPassword); var result = await _userManager.AddToRoleAsync(superUser, RoleConstants.AdministratorRole); if (result.Succeeded) { _logger.LogInformation(_localizer["Seeded Default SuperAdmin User."]); } else { foreach (var error in result.Errors) { _logger.LogError(error.Description); } } } foreach (var permission in Permissions.GetRegisteredPermissions()) { await _roleManager.AddPermissionClaim(adminRoleInDb, permission); } }).GetAwaiter().GetResult(); }
private void AddAdministrator() { Task.Run(async() => { //Check if Role Exists var adminRole = new BlazorHeroRole(RoleConstants.AdministratorRole, _localizer["Administrator role with full permissions"]); var adminRoleInDb = await _roleManager.FindByNameAsync(RoleConstants.AdministratorRole); if (adminRoleInDb == null) { await _roleManager.CreateAsync(adminRole); _logger.LogInformation(_localizer["Seeded Administrator Role."]); } //Check if User Exists var superUser = new BlazorHeroUser { FirstName = "Mukesh", LastName = "Murugan", Email = "*****@*****.**", UserName = "******", EmailConfirmed = true, PhoneNumberConfirmed = true, CreatedOn = DateTime.Now, IsActive = true }; var superUserInDb = await _userManager.FindByEmailAsync(superUser.Email); if (superUserInDb == null) { await _userManager.CreateAsync(superUser, UserConstants.DefaultPassword); var result = await _userManager.AddToRoleAsync(superUser, RoleConstants.AdministratorRole); if (result.Succeeded) { foreach (var prop in typeof(Permissions).GetNestedTypes().SelectMany(c => c.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy))) { var propertyValue = prop.GetValue(null); if (propertyValue is not null) { await _roleManager.AddPermissionClaim(adminRole, propertyValue.ToString()); } } } _logger.LogInformation(_localizer["Seeded User with Administrator Role."]); } }).GetAwaiter().GetResult(); }
public static async Task <IdentityResult> AddPermissionClaim(this RoleManager <BlazorHeroRole> roleManager, BlazorHeroRole role, string permission) { var allClaims = await roleManager.GetClaimsAsync(role); if (!allClaims.Any(a => a.Type == ApplicationClaimTypes.Permission && a.Value == permission)) { return(await roleManager.AddClaimAsync(role, new Claim(ApplicationClaimTypes.Permission, permission))); } return(IdentityResult.Failed()); }
public static async Task AddCustomPermissionClaim(this RoleManager <BlazorHeroRole> roleManager, BlazorHeroRole role, string permission) { var allClaims = await roleManager.GetClaimsAsync(role); if (!allClaims.Any(a => a.Type == ApplicationClaimTypes.Permission && a.Value == permission)) { await roleManager.AddClaimAsync(role, new Claim(ApplicationClaimTypes.Permission, permission)); } }