Skip to content

Commit

Permalink
Merge pull request #370 from ix-ax/admin_roles
Browse files Browse the repository at this point in the history
Admin roles
  • Loading branch information
PTKu authored Apr 11, 2024
2 parents c283578 + 826b4f0 commit 9f713dd
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/security/src/AXOpen.Security/Services/ServicesConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static class ServicesConfiguration
{
public static void ConfigureAxBlazorSecurity(this IServiceCollection services,
(IRepository<User> userRepo, IRepository<Group> groupRepo) repos,
List<Role>? roles = null)
List<Role>? roles = null, bool addAllRolesToAdminGroup = false)
{
services.AddTransient<IUserStore<User>, UserStore>();
services.AddTransient<IRoleStore<Role>, RoleStore>();
Expand All @@ -36,9 +36,20 @@ public static void ConfigureAxBlazorSecurity(this IServiceCollection services,


RoleGroupManager roleGroupManager = new RoleGroupManager(repos.groupRepo);
if (roles != null)
if (roles != null )
{
roleGroupManager.CreateRoles(roles);
if (addAllRolesToAdminGroup)
{
List<string> currentAdminRoles = roleGroupManager.GetRolesFromGroup("AdminGroup").Where(c => ! c.Equals("Administrator")).ToList();
List<string?>? requiredAdminRoles = roles.Select(c => c.Name).ToList();
List<string?>? adminRolesToAdd = requiredAdminRoles?.Where(p => currentAdminRoles.All(p2 => p2 != p)).ToList();
List<string>? adminRolesToRemove = currentAdminRoles?.Where(p => requiredAdminRoles.All(p2 => p2 != p)).ToList();

roleGroupManager.AddRolesToGroup("AdminGroup", adminRolesToAdd);
roleGroupManager.RemoveRolesFromGroup("AdminGroup", adminRolesToRemove);

}
}

services.AddScoped<IRepositoryService, RepositoryService>(provider => new RepositoryService(repos.userRepo, roleGroupManager));
Expand Down

0 comments on commit 9f713dd

Please sign in to comment.