2222using Org . Eclipse . TractusX . Portal . Backend . Keycloak . Factory ;
2323using Org . Eclipse . TractusX . Portal . Backend . Keycloak . Library ;
2424using Org . Eclipse . TractusX . Portal . Backend . Keycloak . Library . Models . Roles ;
25+ using Org . Eclipse . TractusX . Portal . Backend . Keycloak . Seeding . Extensions ;
26+ using Org . Eclipse . TractusX . Portal . Backend . Keycloak . Seeding . Models ;
2527
2628namespace Org . Eclipse . TractusX . Portal . Backend . Keycloak . Seeding . BusinessLogic ;
2729
28- public class ClientScopeMapperUpdater : IClientScopeMapperUpdater
30+ public class ClientScopeMapperUpdater ( IKeycloakFactory keycloakFactory , ISeedDataHandler seedDataHandler )
31+ : IClientScopeMapperUpdater
2932{
30- private readonly IKeycloakFactory _keycloakFactory ;
31- private readonly ISeedDataHandler _seedData ;
32-
33- public ClientScopeMapperUpdater ( IKeycloakFactory keycloakFactory , ISeedDataHandler seedDataHandler )
34- {
35- _keycloakFactory = keycloakFactory ;
36- _seedData = seedDataHandler ;
37- }
38-
3933 public async Task UpdateClientScopeMapper ( string instanceName , CancellationToken cancellationToken )
4034 {
41- var keycloak = _keycloakFactory . CreateKeycloakClient ( instanceName ) ;
42- var realm = _seedData . Realm ;
35+ var keycloak = keycloakFactory . CreateKeycloakClient ( instanceName ) ;
36+ var realm = seedDataHandler . Realm ;
37+ var seederConfig = seedDataHandler . GetSpecificConfiguration ( ConfigurationKey . ClientScopes ) ;
4338
4439 var clients = await keycloak . GetClientsAsync ( realm , null , true , cancellationToken ) . ConfigureAwait ( ConfigureAwaitOptions . None ) ;
45- foreach ( var ( clientName , mappingModels ) in _seedData . ClientScopeMappings )
40+ foreach ( var ( clientName , mappingModels ) in seedDataHandler . ClientScopeMappings )
4641 {
4742 var client = clients . SingleOrDefault ( x => x . ClientId == clientName ) ;
4843 if ( client ? . Id is null )
@@ -60,17 +55,23 @@ public async Task UpdateClientScopeMapper(string instanceName, CancellationToken
6055 }
6156 var clientRoles = await keycloak . GetClientRolesScopeMappingsForClientAsync ( realm , clientScope . Id , client . Id , cancellationToken ) . ConfigureAwait ( ConfigureAwaitOptions . None ) ;
6257 var mappingModelRoles = mappingModel . Roles ? . Select ( roleName => roles . SingleOrDefault ( r => r . Name == roleName ) ?? throw new ConflictException ( $ "No role with name { roleName } found") ) ?? Enumerable . Empty < Role > ( ) ;
63- await AddAndDeleteRoles ( keycloak , realm , clientScope . Id , client . Id , clientRoles , mappingModelRoles , cancellationToken ) . ConfigureAwait ( ConfigureAwaitOptions . None ) ;
58+ await AddAndDeleteRoles ( keycloak , realm , clientScope . Id , client . Id , clientRoles , mappingModelRoles , seederConfig , cancellationToken ) . ConfigureAwait ( ConfigureAwaitOptions . None ) ;
6459 }
6560 }
6661 }
6762
68- private static async Task AddAndDeleteRoles ( KeycloakClient keycloak , string realm , string clientScopeId , string clientId , IEnumerable < Role > roles , IEnumerable < Role > updateRoles , CancellationToken cancellationToken )
63+ private static async Task AddAndDeleteRoles ( KeycloakClient keycloak , string realm , string clientScopeId , string clientId , IEnumerable < Role > roles , IEnumerable < Role > updateRoles , KeycloakSeederConfigModel seederConfig , CancellationToken cancellationToken )
6964 {
70- await updateRoles . ExceptBy ( roles . Select ( role => role . Name ) , roleModel => roleModel . Name ) . IfAnyAwait ( rolesToAdd =>
71- keycloak . AddClientRolesScopeMappingToClientAsync ( realm , clientScopeId , clientId , rolesToAdd , cancellationToken ) ) . ConfigureAwait ( false ) ;
65+ await updateRoles
66+ . Where ( x => seederConfig . ModificationAllowed ( ModificationType . Create , x . Name ) )
67+ . ExceptBy ( roles . Select ( role => role . Name ) , roleModel => roleModel . Name )
68+ . IfAnyAwait ( rolesToAdd =>
69+ keycloak . AddClientRolesScopeMappingToClientAsync ( realm , clientScopeId , clientId , rolesToAdd , cancellationToken ) ) . ConfigureAwait ( false ) ;
7270
73- await roles . ExceptBy ( updateRoles . Select ( roleModel => roleModel . Name ) , role => role . Name ) . IfAnyAwait ( rolesToDelete =>
74- keycloak . RemoveClientRolesFromClientScopeForClientAsync ( realm , clientScopeId , clientId , rolesToDelete , cancellationToken ) ) . ConfigureAwait ( false ) ;
71+ await roles
72+ . Where ( x => seederConfig . ModificationAllowed ( ModificationType . Delete , x . Name ) )
73+ . ExceptBy ( updateRoles . Select ( roleModel => roleModel . Name ) , role => role . Name )
74+ . IfAnyAwait ( rolesToDelete =>
75+ keycloak . RemoveClientRolesFromClientScopeForClientAsync ( realm , clientScopeId , clientId , rolesToDelete , cancellationToken ) ) . ConfigureAwait ( false ) ;
7576 }
7677}
0 commit comments