Skip to content

Commit

Permalink
feat: adjust configuration handling
Browse files Browse the repository at this point in the history
Refs: #1172
  • Loading branch information
Phil91 committed Nov 26, 2024
1 parent 7b1ef43 commit b45b1b2
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public interface ISeedDataHandler

IEnumerable<(string ProviderType, ComponentModel ComponentModel)> RealmComponents { get; }
IEnumerable<(string Locale, IEnumerable<KeyValuePair<string, string>> Translations)> RealmLocalizations { get; }
KeycloakRealmSettings Configuration { get; }
SeederConfiguration Configuration { get; }

Task SetClientInternalIds(IAsyncEnumerable<(string ClientId, string Id)> clientInternalIds);

Expand Down
17 changes: 11 additions & 6 deletions src/keycloak/Keycloak.Seeding/BusinessLogic/SeedDataHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
using Org.Eclipse.TractusX.Portal.Backend.Framework.Async;
using Org.Eclipse.TractusX.Portal.Backend.Framework.ErrorHandling;
using Org.Eclipse.TractusX.Portal.Backend.Framework.Linq;
using Org.Eclipse.TractusX.Portal.Backend.Framework.Models;
using Org.Eclipse.TractusX.Portal.Backend.Keycloak.Seeding.Models;
using System.Collections.Immutable;
using System.Text.Json;
Expand All @@ -40,7 +39,7 @@ public class SeedDataHandler : ISeedDataHandler

private KeycloakRealm? _keycloakRealm;
private IReadOnlyDictionary<string, string>? _idOfClients;
private KeycloakRealmSettings? _realmConfiguration;
private SeederConfiguration? _defaultConfiguration;

public async Task Import(KeycloakRealmSettings realmSettings, CancellationToken cancellationToken)
{
Expand All @@ -50,7 +49,13 @@ public async Task Import(KeycloakRealmSettings realmSettings, CancellationToken
async (importRealm, path) => importRealm.Merge(await ReadJsonRealm(path, realmSettings.Realm, cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None)),
cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None))
.Merge(realmSettings.ToModel());
_realmConfiguration = realmSettings;
_defaultConfiguration = new SeederConfiguration
{
Create = realmSettings.Create,
Update = realmSettings.Update,
Delete = realmSettings.Delete,
SeederConfigurations = realmSettings.SeederConfigurations
};
_idOfClients = null;
}

Expand All @@ -74,9 +79,9 @@ public string Realm
get => _keycloakRealm?.Realm ?? throw new ConflictException("realm must not be null");
}

public KeycloakRealmSettings Configuration
public SeederConfiguration Configuration
{
get => _realmConfiguration ?? throw new ConflictException("configuration must not be null");
get => _defaultConfiguration ?? throw new ConflictException("configuration must not be null");
}

public KeycloakRealm KeycloakRealm
Expand Down Expand Up @@ -178,7 +183,7 @@ public KeycloakSeederConfigModel GetSpecificConfiguration(ConfigurationKey confi

var configKeyString = configKey.ToString();

specificConfiguration = _realmConfiguration?.SeederConfigurations?.SingleOrDefault(x => x.Key == configKeyString);
specificConfiguration = Configuration.SeederConfigurations?.SingleOrDefault(x => x.Key == configKeyString);
_specificConfigurations.Add(configKey, specificConfiguration);
return new KeycloakSeederConfigModel(Configuration, specificConfiguration);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,27 @@ namespace Org.Eclipse.TractusX.Portal.Backend.Keycloak.Seeding.Extensions;

public static class SeederConfigurationExtensions
{
public static bool IsModificationAllowed(this KeycloakRealmSettings config, ConfigurationKey configKey) =>
public static bool IsModificationAllowed(this SeederConfiguration config, ConfigurationKey configKey) =>
config.Create || config.Update || config.Delete ||
(config.SeederConfigurations != null &&
IsModificationAllowed(config.SeederConfigurations, configKey.ToString(), out var _));
IsModificationAllowed(config.SeederConfigurations, configKey.ToString(), false, out var _));

private static bool IsModificationAllowed(
IEnumerable<SeederConfiguration> configurations,
string targetKey,
bool isKeyParentConfig,
out SeederConfiguration? matchingConfig)
{
matchingConfig = null;

foreach (var config in configurations)
{
if (config.SeederConfigurations != null && IsModificationAllowed(config.SeederConfigurations, targetKey, out matchingConfig))
if (config.SeederConfigurations != null && IsModificationAllowed(config.SeederConfigurations, targetKey, isKeyParentConfig || config.Key == targetKey, out matchingConfig))
{
return true;
}

if (config.Key != targetKey || config is { Create: false, Update: false, Delete: false })
if ((config.Key != targetKey && !isKeyParentConfig) || config is { Create: false, Update: false, Delete: false })
{
continue;
}
Expand Down Expand Up @@ -93,15 +94,6 @@ public static bool ModificationAllowed(this KeycloakSeederConfigModel config, st
return entity?.ModifyAllowed(modificationType) ?? config.ModificationAllowed(modificationType, entityKey);
}

private static bool ModifyAllowed(this KeycloakRealmSettings configuration, ModificationType modificationType) =>
modificationType switch
{
ModificationType.Create => configuration.Create,
ModificationType.Update => configuration.Update,
ModificationType.Delete => configuration.Delete,
_ => throw new ArgumentOutOfRangeException(nameof(modificationType), modificationType, null)
};

private static bool ModifyAllowed(this SeederConfiguration configuration, ModificationType modificationType) =>
modificationType switch
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class KeycloakRealmSettings
public bool Create { get; set; }
public bool Update { get; set; }
public bool Delete { get; set; }
[DistinctValues]
[DistinctValues("x => x.Key")]
public IEnumerable<SeederConfiguration>? SeederConfigurations { get; set; }
public string? Id { get; set; }
public string? DisplayName { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

namespace Org.Eclipse.TractusX.Portal.Backend.Keycloak.Seeding.Models;

public static class KeycloakRealmSettingsExtentions
public static class KeycloakRealmSettingsExtensions
{
public static KeycloakRealm ToModel(this KeycloakRealmSettings keycloakRealmSettings) =>
new()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
namespace Org.Eclipse.TractusX.Portal.Backend.Keycloak.Seeding.Models;

public record KeycloakSeederConfigModel(
KeycloakRealmSettings DefaultSettings,
SeederConfiguration DefaultSettings,
SeederConfiguration? SpecificConfiguration
);
8 changes: 4 additions & 4 deletions src/keycloak/Keycloak.Seeding/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ In the Seeder configuration you must have one Default entry where the following
"Create": true,
"Update": true,
"Delete": true,
"SeederConfiguration": []
"SeederConfigurations": []
]
```

Expand All @@ -27,7 +27,7 @@ To be able to enable or disable the functionality for specific types the SeederC
**Example**:

```json
"SeederConfiguration": [
"SeederConfigurations": [
{
"Key": "Localizations",
"Create": false,
Expand Down Expand Up @@ -67,7 +67,7 @@ To be able to enable or disable the seeding for specific values the configuratio
**Example**

```json
"SeederConfiguration": [
"SeederConfigurations": [
{
"Key": "Localizations",
"Create": true,
Expand Down Expand Up @@ -97,7 +97,7 @@ For some entities there is a specific entry type configuration in place. E.g. Fe
**Example**

```json
"SeederConfiguration": [
"SeederConfigurations": [
{
"Key": "Users",
"Create": true,
Expand Down
2 changes: 1 addition & 1 deletion src/keycloak/Keycloak.Seeding/appsettings.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"Create": true,
"Update": false,
"Delete": true,
"SeederConfiguration": [
"SeederConfigurations": [
{
"Key": "Roles",
"Create": false,
Expand Down

0 comments on commit b45b1b2

Please sign in to comment.