2222using Org . Eclipse . TractusX . Portal . Backend . Keycloak . Library ;
2323using Org . Eclipse . TractusX . Portal . Backend . Keycloak . Library . Models . ClientScopes ;
2424using Org . Eclipse . TractusX . Portal . Backend . Keycloak . Library . Models . ProtocolMappers ;
25+ using Org . Eclipse . TractusX . Portal . Backend . Keycloak . Seeding . Extensions ;
2526using Org . Eclipse . TractusX . Portal . Backend . Keycloak . Seeding . Models ;
2627
2728namespace Org . Eclipse . TractusX . Portal . Backend . Keycloak . Seeding . BusinessLogic ;
2829
29- public class ClientScopesUpdater : IClientScopesUpdater
30+ public class ClientScopesUpdater ( IKeycloakFactory keycloakFactory , ISeedDataHandler seedDataHandler )
31+ : IClientScopesUpdater
3032{
31- private readonly IKeycloakFactory _keycloakFactory ;
32- private readonly ISeedDataHandler _seedData ;
33-
34- public ClientScopesUpdater ( IKeycloakFactory keycloakFactory , ISeedDataHandler seedDataHandler )
35- {
36- _keycloakFactory = keycloakFactory ;
37- _seedData = seedDataHandler ;
38- }
39-
4033 public async Task UpdateClientScopes ( string instanceName , CancellationToken cancellationToken )
4134 {
42- var keycloak = _keycloakFactory . CreateKeycloakClient ( instanceName ) ;
43- var realm = _seedData . Realm ;
35+ var keycloak = keycloakFactory . CreateKeycloakClient ( instanceName ) ;
36+ var realm = seedDataHandler . Realm ;
37+ var seederConfig = seedDataHandler . GetSpecificConfiguration ( ConfigurationKey . ClientScopes ) ;
4438
4539 var clientScopes = await keycloak . GetClientScopesAsync ( realm , cancellationToken ) . ConfigureAwait ( ConfigureAwaitOptions . None ) ;
46- var seedClientScopes = _seedData . ClientScopes ;
40+ var seedClientScopes = seedDataHandler . ClientScopes ;
4741
48- await RemoveObsoleteClientScopes ( keycloak , realm , clientScopes , seedClientScopes , cancellationToken ) . ConfigureAwait ( ConfigureAwaitOptions . None ) ;
49- await CreateMissingClientScopes ( keycloak , realm , clientScopes , seedClientScopes , cancellationToken ) . ConfigureAwait ( ConfigureAwaitOptions . None ) ;
50- await UpdateExistingClientScopes ( keycloak , realm , clientScopes , seedClientScopes , cancellationToken ) . ConfigureAwait ( ConfigureAwaitOptions . None ) ;
42+ await CheckAndExecute ( ModificationType . Delete , keycloak , realm , clientScopes , seedClientScopes , seederConfig , cancellationToken , RemoveObsoleteClientScopes ) . ConfigureAwait ( ConfigureAwaitOptions . None ) ;
43+ await CheckAndExecute ( ModificationType . Create , keycloak , realm , clientScopes , seedClientScopes , seederConfig , cancellationToken , CreateMissingClientScopes ) . ConfigureAwait ( ConfigureAwaitOptions . None ) ;
44+ await CheckAndExecute ( ModificationType . Update , keycloak , realm , clientScopes , seedClientScopes , seederConfig , cancellationToken , UpdateExistingClientScopes ) . ConfigureAwait ( ConfigureAwaitOptions . None ) ;
5145 }
5246
53- private static async Task RemoveObsoleteClientScopes ( KeycloakClient keycloak , string realm , IEnumerable < ClientScope > clientScopes , IEnumerable < ClientScopeModel > seedClientScopes , CancellationToken cancellationToken )
47+ private static Task CheckAndExecute ( ModificationType modificationType , KeycloakClient keycloak , string realm , IEnumerable < ClientScope > clientScopes , IEnumerable < ClientScopeModel > seedClientScopes , KeycloakSeederConfigModel seederConfig , CancellationToken cancellationToken , Func < KeycloakClient , string , IEnumerable < ClientScope > , IEnumerable < ClientScopeModel > , KeycloakSeederConfigModel , CancellationToken , Task > executeLogic ) =>
48+ seederConfig . ModificationAllowed ( modificationType )
49+ ? executeLogic ( keycloak , realm , clientScopes , seedClientScopes , seederConfig , cancellationToken )
50+ : Task . CompletedTask ;
51+
52+ private static async Task RemoveObsoleteClientScopes ( KeycloakClient keycloak , string realm , IEnumerable < ClientScope > clientScopes , IEnumerable < ClientScopeModel > seedClientScopes , KeycloakSeederConfigModel seederConfig , CancellationToken cancellationToken )
5453 {
55- foreach ( var deleteScope in clientScopes . ExceptBy ( seedClientScopes . Select ( x => x . Name ) , x => x . Name ) )
54+ foreach ( var deleteScope in clientScopes
55+ . Where ( x => seederConfig . ModificationAllowed ( ModificationType . Delete , x . Name ) )
56+ . ExceptBy ( seedClientScopes . Select ( x => x . Name ) , x => x . Name ) )
5657 {
5758 await keycloak . DeleteClientScopeAsync (
5859 realm ,
@@ -61,32 +62,38 @@ await keycloak.DeleteClientScopeAsync(
6162 }
6263 }
6364
64- private static async Task CreateMissingClientScopes ( KeycloakClient keycloak , string realm , IEnumerable < ClientScope > clientScopes , IEnumerable < ClientScopeModel > seedClientScopes , CancellationToken cancellationToken )
65+ private static async Task CreateMissingClientScopes ( KeycloakClient keycloak , string realm , IEnumerable < ClientScope > clientScopes , IEnumerable < ClientScopeModel > seedClientScopes , KeycloakSeederConfigModel seederConfig , CancellationToken cancellationToken )
6566 {
66- foreach ( var addScope in seedClientScopes . ExceptBy ( clientScopes . Select ( x => x . Name ) , x => x . Name ) )
67+ foreach ( var addScope in seedClientScopes
68+ . Where ( x => seederConfig . ModificationAllowed ( ModificationType . Create , x . Name ) )
69+ . ExceptBy ( clientScopes . Select ( x => x . Name ) , x => x . Name ) )
6770 {
6871 await keycloak . CreateClientScopeAsync ( realm , CreateClientScope ( null , addScope , true ) , cancellationToken ) . ConfigureAwait ( ConfigureAwaitOptions . None ) ;
6972 }
7073 }
7174
72- private static async Task UpdateExistingClientScopes ( KeycloakClient keycloak , string realm , IEnumerable < ClientScope > clientScopes , IEnumerable < ClientScopeModel > seedClientScopes , CancellationToken cancellationToken )
75+ private static async Task UpdateExistingClientScopes ( KeycloakClient keycloak , string realm , IEnumerable < ClientScope > clientScopes , IEnumerable < ClientScopeModel > seedClientScopes , KeycloakSeederConfigModel seederConfig , CancellationToken cancellationToken )
7376 {
7477 foreach ( var ( clientScope , update ) in clientScopes
78+ . Where ( x => seederConfig . ModificationAllowed ( ModificationType . Update , x . Name ) )
7579 . Join (
7680 seedClientScopes ,
7781 x => x . Name ,
7882 x => x . Name ,
7983 ( clientScope , update ) => ( ClientScope : clientScope , Update : update ) ) )
8084 {
81- await UpdateClientScopeWithProtocolMappers ( keycloak , realm , clientScope , update , cancellationToken ) . ConfigureAwait ( ConfigureAwaitOptions . None ) ;
85+ await UpdateClientScopeWithProtocolMappers ( keycloak , realm , clientScope , update , seederConfig , cancellationToken ) . ConfigureAwait ( ConfigureAwaitOptions . None ) ;
8286 }
8387 }
8488
85- private static async Task UpdateClientScopeWithProtocolMappers ( KeycloakClient keycloak , string realm , ClientScope clientScope , ClientScopeModel update , CancellationToken cancellationToken )
89+ private static async Task UpdateClientScopeWithProtocolMappers ( KeycloakClient keycloak , string realm , ClientScope clientScope , ClientScopeModel update , KeycloakSeederConfigModel seederConfig , CancellationToken cancellationToken )
8690 {
8791 if ( clientScope . Id == null )
8892 throw new ConflictException ( $ "clientScope.Id is null: { clientScope . Name } ") ;
8993
94+ if ( clientScope . Name == null )
95+ throw new ConflictException ( $ "clientScope.Name is null: { clientScope . Name } ") ;
96+
9097 if ( ! CompareClientScope ( clientScope , update ) )
9198 {
9299 await keycloak . UpdateClientScopeAsync (
@@ -99,14 +106,16 @@ await keycloak.UpdateClientScopeAsync(
99106 var mappers = clientScope . ProtocolMappers ?? Enumerable . Empty < ProtocolMapper > ( ) ;
100107 var updateMappers = update . ProtocolMappers ?? Enumerable . Empty < ProtocolMapperModel > ( ) ;
101108
102- await DeleteObsoleteProtocolMappers ( keycloak , realm , clientScope . Id , mappers , updateMappers , cancellationToken ) . ConfigureAwait ( ConfigureAwaitOptions . None ) ;
103- await CreateMissingProtocolMappers ( keycloak , realm , clientScope . Id , mappers , updateMappers , cancellationToken ) . ConfigureAwait ( ConfigureAwaitOptions . None ) ;
104- await UpdateExistingProtocolMappers ( keycloak , realm , clientScope . Id , mappers , updateMappers , cancellationToken ) . ConfigureAwait ( ConfigureAwaitOptions . None ) ;
109+ await DeleteObsoleteProtocolMappers ( keycloak , realm , clientScope . Name , clientScope . Id , mappers , updateMappers , seederConfig , cancellationToken ) . ConfigureAwait ( ConfigureAwaitOptions . None ) ;
110+ await CreateMissingProtocolMappers ( keycloak , realm , clientScope . Name , clientScope . Id , mappers , updateMappers , seederConfig , cancellationToken ) . ConfigureAwait ( ConfigureAwaitOptions . None ) ;
111+ await UpdateExistingProtocolMappers ( keycloak , realm , clientScope . Name , clientScope . Id , mappers , updateMappers , seederConfig , cancellationToken ) . ConfigureAwait ( ConfigureAwaitOptions . None ) ;
105112 }
106113
107- private static async Task DeleteObsoleteProtocolMappers ( KeycloakClient keycloak , string realm , string clientScopeId , IEnumerable < ProtocolMapper > mappers , IEnumerable < ProtocolMapperModel > updateMappers , CancellationToken cancellationToken )
114+ private static async Task DeleteObsoleteProtocolMappers ( KeycloakClient keycloak , string realm , string clientScopeName , string clientScopeId , IEnumerable < ProtocolMapper > mappers , IEnumerable < ProtocolMapperModel > updateMappers , KeycloakSeederConfigModel seederConfig , CancellationToken cancellationToken )
108115 {
109- foreach ( var mapper in mappers . ExceptBy ( updateMappers . Select ( x => x . Name ) , x => x . Name ) )
116+ foreach ( var mapper in mappers
117+ . Where ( x => seederConfig . ModificationAllowed ( clientScopeName , ConfigurationKey . ProtocolMappers , ModificationType . Delete , x . Name ) )
118+ . ExceptBy ( updateMappers . Select ( x => x . Name ) , x => x . Name ) )
110119 {
111120 await keycloak . DeleteProtocolMapperAsync (
112121 realm ,
@@ -116,9 +125,11 @@ await keycloak.DeleteProtocolMapperAsync(
116125 }
117126 }
118127
119- private static async Task CreateMissingProtocolMappers ( KeycloakClient keycloak , string realm , string clientScopeId , IEnumerable < ProtocolMapper > mappers , IEnumerable < ProtocolMapperModel > updateMappers , CancellationToken cancellationToken )
128+ private static async Task CreateMissingProtocolMappers ( KeycloakClient keycloak , string realm , string clientScopeName , string clientScopeId , IEnumerable < ProtocolMapper > mappers , IEnumerable < ProtocolMapperModel > updateMappers , KeycloakSeederConfigModel seederConfig , CancellationToken cancellationToken )
120129 {
121- foreach ( var update in updateMappers . ExceptBy ( mappers . Select ( x => x . Name ) , x => x . Name ) )
130+ foreach ( var update in updateMappers
131+ . Where ( x => seederConfig . ModificationAllowed ( clientScopeName , ConfigurationKey . ProtocolMappers , ModificationType . Create , x . Name ) )
132+ . ExceptBy ( mappers . Select ( x => x . Name ) , x => x . Name ) )
122133 {
123134 await keycloak . CreateProtocolMapperAsync (
124135 realm ,
@@ -128,13 +139,15 @@ await keycloak.CreateProtocolMapperAsync(
128139 }
129140 }
130141
131- private static async Task UpdateExistingProtocolMappers ( KeycloakClient keycloak , string realm , string clientScopeId , IEnumerable < ProtocolMapper > mappers , IEnumerable < ProtocolMapperModel > updateMappers , CancellationToken cancellationToken )
142+ private static async Task UpdateExistingProtocolMappers ( KeycloakClient keycloak , string realm , string clientScopeName , string clientScopeId , IEnumerable < ProtocolMapper > mappers , IEnumerable < ProtocolMapperModel > updateMappers , KeycloakSeederConfigModel seederConfig , CancellationToken cancellationToken )
132143 {
133- foreach ( var ( mapper , update ) in mappers . Join (
134- updateMappers ,
135- x => x . Name ,
136- x => x . Name ,
137- ( mapper , update ) => ( Mapper : mapper , Update : update ) )
144+ foreach ( var ( mapper , update ) in mappers
145+ . Where ( x => seederConfig . ModificationAllowed ( clientScopeName , ConfigurationKey . ProtocolMappers , ModificationType . Update , x . Name ) )
146+ . Join (
147+ updateMappers ,
148+ x => x . Name ,
149+ x => x . Name ,
150+ ( mapper , update ) => ( Mapper : mapper , Update : update ) )
138151 . Where ( x => ! ProtocolMappersUpdater . CompareProtocolMapper ( x . Mapper , x . Update ) ) )
139152 {
140153 await keycloak . UpdateProtocolMapperAsync (
0 commit comments