From ebab3a2760c788f8f20b9ab866a001766028f0b1 Mon Sep 17 00:00:00 2001 From: TK <61820360+TomKovac@users.noreply.github.com> Date: Thu, 11 Apr 2024 12:01:41 +0200 Subject: [PATCH] automatic roles assignement --- .../Services/ServicesConfiguration.cs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/security/src/AXOpen.Security/Services/ServicesConfiguration.cs b/src/security/src/AXOpen.Security/Services/ServicesConfiguration.cs index da0f4dc6b..93a8ec8f8 100644 --- a/src/security/src/AXOpen.Security/Services/ServicesConfiguration.cs +++ b/src/security/src/AXOpen.Security/Services/ServicesConfiguration.cs @@ -17,7 +17,7 @@ public static class ServicesConfiguration { public static void ConfigureAxBlazorSecurity(this IServiceCollection services, (IRepository userRepo, IRepository groupRepo) repos, - List? roles = null) + List? roles = null, bool addAllRolesToAdminGroup = false) { services.AddTransient, UserStore>(); services.AddTransient, RoleStore>(); @@ -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 currentAdminRoles = roleGroupManager.GetRolesFromGroup("AdminGroup").Where(c => ! c.Equals("Administrator")).ToList(); + List? requiredAdminRoles = roles.Select(c => c.Name).ToList(); + List? adminRolesToAdd = requiredAdminRoles?.Where(p => currentAdminRoles.All(p2 => p2 != p)).ToList(); + List? adminRolesToRemove = currentAdminRoles?.Where(p => requiredAdminRoles.All(p2 => p2 != p)).ToList(); + + roleGroupManager.AddRolesToGroup("AdminGroup", adminRolesToAdd); + roleGroupManager.RemoveRolesFromGroup("AdminGroup", adminRolesToRemove); + + } } services.AddScoped(provider => new RepositoryService(repos.userRepo, roleGroupManager));