Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(seeding): add userProfile and localizationTexts to seeding #1154

Merged
merged 2 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 12 additions & 40 deletions src/keycloak/Keycloak.Library/Components/KeycloakClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ namespace Org.Eclipse.TractusX.Portal.Backend.Keycloak.Library;

public partial class KeycloakClient
{
public async Task CreateComponentAsync(string realm, Component componentRepresentation) =>
await (await GetBaseUrlAsync(realm).ConfigureAwait(ConfigureAwaitOptions.None))
public async Task CreateComponentAsync(string realm, Component componentRepresentation, CancellationToken cancellationToken) =>
await (await GetBaseUrlAsync(realm, cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None))
.AppendPathSegment("/admin/realms/")
.AppendPathSegment(realm, true)
.AppendPathSegment("/components")
.PostJsonAsync(componentRepresentation)
.PostJsonAsync(componentRepresentation, cancellationToken: cancellationToken)
.ConfigureAwait(ConfigureAwaitOptions.None);

public async Task<IEnumerable<Component>> GetComponentsAsync(string realm, string? name = null, string? parent = null, string? type = null)
public async Task<IEnumerable<Component>> GetComponentsAsync(string realm, string? name = null, string? parent = null, string? type = null, CancellationToken cancellationToken = default)
{
var queryParams = new Dictionary<string, object?>
{
Expand All @@ -47,58 +47,30 @@ public async Task<IEnumerable<Component>> GetComponentsAsync(string realm, strin
[nameof(type)] = type
};

return await (await GetBaseUrlAsync(realm).ConfigureAwait(ConfigureAwaitOptions.None))
return await (await GetBaseUrlAsync(realm, cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None))
.AppendPathSegment("/admin/realms/")
.AppendPathSegment(realm, true)
.AppendPathSegment("/components")
.SetQueryParams(queryParams)
.GetJsonAsync<IEnumerable<Component>>()
.GetJsonAsync<IEnumerable<Component>>(cancellationToken: cancellationToken)
.ConfigureAwait(ConfigureAwaitOptions.None);
}

public async Task<Component> GetComponentAsync(string realm, string componentId) =>
await (await GetBaseUrlAsync(realm).ConfigureAwait(ConfigureAwaitOptions.None))
public async Task UpdateComponentAsync(string realm, string componentId, Component componentRepresentation, CancellationToken cancellationToken) =>
await (await GetBaseUrlAsync(realm, cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None))
.AppendPathSegment("/admin/realms/")
.AppendPathSegment(realm, true)
.AppendPathSegment("/components/")
.AppendPathSegment(componentId, true)
.GetJsonAsync<Component>()
.PutJsonAsync(componentRepresentation, cancellationToken: cancellationToken)
.ConfigureAwait(ConfigureAwaitOptions.None);

public async Task UpdateComponentAsync(string realm, string componentId, Component componentRepresentation) =>
await (await GetBaseUrlAsync(realm).ConfigureAwait(ConfigureAwaitOptions.None))
public async Task DeleteComponentAsync(string realm, string componentId, CancellationToken cancellationToken) =>
await (await GetBaseUrlAsync(realm, cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None))
.AppendPathSegment("/admin/realms/")
.AppendPathSegment(realm, true)
.AppendPathSegment("/components/")
.AppendPathSegment(componentId, true)
.PutJsonAsync(componentRepresentation)
.DeleteAsync(cancellationToken: cancellationToken)
.ConfigureAwait(ConfigureAwaitOptions.None);

public async Task DeleteComponentAsync(string realm, string componentId) =>
await (await GetBaseUrlAsync(realm).ConfigureAwait(ConfigureAwaitOptions.None))
.AppendPathSegment("/admin/realms/")
.AppendPathSegment(realm, true)
.AppendPathSegment("/components/")
.AppendPathSegment(componentId, true)
.DeleteAsync()
.ConfigureAwait(ConfigureAwaitOptions.None);

public async Task<IEnumerable<ComponentType>> GetSubcomponentTypesAsync(string realm, string componentId, string? type = null)
{
var queryParams = new Dictionary<string, object?>
{
[nameof(type)] = type
};

var result = await (await GetBaseUrlAsync(realm).ConfigureAwait(ConfigureAwaitOptions.None))
.AppendPathSegment("/admin/realms/")
.AppendPathSegment(realm, true)
.AppendPathSegment("/components/")
.AppendPathSegment(componentId, true)
.AppendPathSegment("/sub-component-types")
.SetQueryParams(queryParams)
.GetJsonAsync<IEnumerable<ComponentType>>()
.ConfigureAwait(ConfigureAwaitOptions.None);
return result;
}
}
1 change: 1 addition & 0 deletions src/keycloak/Keycloak.Library/Keycloak.Library.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>ef300f2e-b1c3-4ce1-b028-92533b71aa73</UserSecretsId>
</PropertyGroup>

<ItemGroup>
Expand Down
87 changes: 87 additions & 0 deletions src/keycloak/Keycloak.Library/Localization/KeycloakClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/********************************************************************************
* Copyright (c) 2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/

using Flurl.Http;
using Org.Eclipse.TractusX.Portal.Backend.Framework.Linq;
using System.Net.Http.Headers;

namespace Org.Eclipse.TractusX.Portal.Backend.Keycloak.Library;

public partial class KeycloakClient
{
private const string AdminUrlSegment = "/admin/realms/";
private const string LocalizationUrlSegment = "/localization/";

public async Task<IEnumerable<string>> GetLocaleAsync(string realm, CancellationToken cancellationToken = default)
{
return await (await GetBaseUrlAsync(realm, cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None))
.AppendPathSegment(AdminUrlSegment)
.AppendPathSegment(realm, true)
.AppendPathSegment("/localization")
.GetJsonAsync<IEnumerable<string>>(cancellationToken: cancellationToken)
.ConfigureAwait(ConfigureAwaitOptions.None);
}

public async Task<IEnumerable<KeyValuePair<string, string>>> GetLocaleAsync(string realm, string locale, CancellationToken cancellationToken = default)
{
var response = await (await GetBaseUrlAsync(realm, cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None))
.AppendPathSegment(AdminUrlSegment)
.AppendPathSegment(realm, true)
.AppendPathSegment(LocalizationUrlSegment)
.AppendPathSegment(locale, true)
.GetJsonAsync<IDictionary<string, string>?>(cancellationToken: cancellationToken)
.ConfigureAwait(ConfigureAwaitOptions.None);

return response == null
? Enumerable.Empty<KeyValuePair<string, string>>()
: response.FilterNotNull();
}

public async Task UpdateLocaleAsync(string realm, string locale, string key, string translation, CancellationToken cancellationToken)
{
using var content = new StringContent(translation, MediaTypeHeaderValue.Parse("text/plain"));
await (await GetBaseUrlAsync(realm, cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None))
.AppendPathSegment(AdminUrlSegment)
.AppendPathSegment(realm, true)
.AppendPathSegment(LocalizationUrlSegment)
.AppendPathSegment(locale, true)
.AppendPathSegment(key, true)
.PutAsync(content, cancellationToken: cancellationToken)
.ConfigureAwait(ConfigureAwaitOptions.None);
}

public async Task DeleteLocaleAsync(string realm, string locale, string key, CancellationToken cancellationToken) =>
await (await GetBaseUrlAsync(realm, cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None))
.AppendPathSegment(AdminUrlSegment)
.AppendPathSegment(realm, true)
.AppendPathSegment(LocalizationUrlSegment)
.AppendPathSegment(locale, true)
.AppendPathSegment(key, true)
.DeleteAsync(cancellationToken: cancellationToken)
.ConfigureAwait(ConfigureAwaitOptions.None);

public async Task DeleteLocaleAsync(string realm, string locale, CancellationToken cancellationToken) =>
await (await GetBaseUrlAsync(realm, cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None))
.AppendPathSegment(AdminUrlSegment)
.AppendPathSegment(realm, true)
.AppendPathSegment(LocalizationUrlSegment)
.AppendPathSegment(locale, true)
.DeleteAsync(cancellationToken: cancellationToken)
.ConfigureAwait(ConfigureAwaitOptions.None);
}
20 changes: 13 additions & 7 deletions src/keycloak/Keycloak.Library/Models/Components/Component.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,23 @@ namespace Org.Eclipse.TractusX.Portal.Backend.Keycloak.Library.Models.Components
public class Component
{
[JsonPropertyName("id")]
public string Id { get; set; }
public string? Id { get; set; }

[JsonPropertyName("name")]
public string Name { get; set; }
public string? Name { get; set; }

[JsonPropertyName("providerId")]
public string ProviderId { get; set; }
public string? ProviderId { get; set; }

[JsonPropertyName("providerType")]
public string ProviderType { get; set; }
public string? ProviderType { get; set; }

[JsonPropertyName("parentId")]
public string ParentId { get; set; }
public string? ParentId { get; set; }

[JsonPropertyName("config")]
public Config Config { get; set; }
public IReadOnlyDictionary<string, IEnumerable<string>?>? Config { get; set; }

[JsonPropertyName("subType")]
public string SubType { get; set; }
public string? SubType { get; set; }
}
47 changes: 0 additions & 47 deletions src/keycloak/Keycloak.Library/Models/Components/Config.cs

This file was deleted.

4 changes: 4 additions & 0 deletions src/keycloak/Keycloak.Library/Models/RealmsAdmin/Realm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,8 @@ public class Realm
public bool? UserManagedAccessAllowed { get; set; }
[JsonPropertyName("passwordPolicy")]
public string? PasswordPolicy { get; set; }
[JsonPropertyName("defaultLocale")]
public string? DefaultLocale { get; set; }
[JsonPropertyName("localizationTexts")]
public IDictionary<string, IDictionary<string, string>?>? LocalizationTexts { get; set; }
}
5 changes: 4 additions & 1 deletion src/keycloak/Keycloak.Library/Models/Root/Locale.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,8 @@ namespace Org.Eclipse.TractusX.Portal.Backend.Keycloak.Library.Models.Root;
public enum Locale
{
[EnumMember(Value = "en")]
En
En,

[EnumMember(Value = "de")]
De
}
Loading
Loading