From 369dfd9d1dfb4194e1f6793cf267f4df3854726f Mon Sep 17 00:00:00 2001 From: Jonas Hendrickx Date: Wed, 5 Jun 2024 09:06:51 +0200 Subject: [PATCH] Refactoring --- .../Pages/App/Settings/CreatePublicKey.razor | 15 +++++---------- .../Pages/App/Settings/CreateSecretKey.razor | 15 +++++---------- .../CreateApiKeyComponent.razor | 12 +++++++----- 3 files changed, 17 insertions(+), 25 deletions(-) diff --git a/src/AdminConsole/Components/Pages/App/Settings/CreatePublicKey.razor b/src/AdminConsole/Components/Pages/App/Settings/CreatePublicKey.razor index 4bd9e8886..4a70af920 100644 --- a/src/AdminConsole/Components/Pages/App/Settings/CreatePublicKey.razor +++ b/src/AdminConsole/Components/Pages/App/Settings/CreatePublicKey.razor @@ -7,7 +7,6 @@ @using Passwordless.AdminConsole.Services.PasswordlessManagement @using Passwordless.Common.Constants @using Passwordless.Common.EventLog.Enums -@using Passwordless.Common.Extensions @using Passwordless.Common.Models.Apps @inherits BaseApplicationPage @@ -19,29 +18,25 @@ @inject ILogger Logger - + @code { - public IReadOnlyCollection? Scopes { get; private set; } + public IReadOnlyCollection? Scopes { get; private set; } public int OrganizationId { get; private set; } protected override void OnInitialized() { - Scopes = Enum.GetValues(typeof(PublicKeyScopes)).Cast().Select(x => x.GetValue()).ToArray(); + Scopes = Enum.GetValues(typeof(PublicKeyScopes)).Cast().ToArray(); OrganizationId = HttpContextAccessor.HttpContext!.User.GetOrgId() ?? throw new ArgumentNullException(nameof(OrganizationId)); } - public async Task OnCreatedAsync(List selectedScopes) + public async Task OnCreatedAsync(HashSet selectedScopes) { - var selectedScopeValues = selectedScopes - .Select(x => x.AsPublicKeyScope()) - .ToHashSet(); - try { - var request = new CreatePublicKeyRequest(selectedScopeValues); + var request = new CreatePublicKeyRequest(selectedScopes); await ManagementClient.CreateApiKeyAsync(AppId, request); var eventDto = new OrganizationEventDto(HttpContextAccessor.HttpContext!.Request.HttpContext.User.GetId(), diff --git a/src/AdminConsole/Components/Pages/App/Settings/CreateSecretKey.razor b/src/AdminConsole/Components/Pages/App/Settings/CreateSecretKey.razor index 28029adf6..7773cafaf 100644 --- a/src/AdminConsole/Components/Pages/App/Settings/CreateSecretKey.razor +++ b/src/AdminConsole/Components/Pages/App/Settings/CreateSecretKey.razor @@ -8,7 +8,6 @@ @using Passwordless.AdminConsole.Services.PasswordlessManagement @using Passwordless.Common.Constants @using Passwordless.Common.EventLog.Enums -@using Passwordless.Common.Extensions @using Passwordless.Common.Models.Apps @inherits BaseApplicationPage @@ -20,30 +19,26 @@ @inject ILogger Logger - + @code { - public IReadOnlyCollection? Scopes { get; private set; } + public IReadOnlyCollection? Scopes { get; private set; } public int OrganizationId { get; private set; } protected override void OnInitialized() { - Scopes = Enum.GetValues(typeof(SecretKeyScopes)).Cast().Select(x => x.GetValue()).ToArray(); + Scopes = Enum.GetValues(typeof(SecretKeyScopes)).Cast().ToArray(); OrganizationId = HttpContextAccessor.HttpContext!.User.GetOrgId() ?? throw new ArgumentNullException(nameof(OrganizationId)); } - public async Task OnCreatedAsync(List selectedScopes) + public async Task OnCreatedAsync(HashSet selectedScopes) { - var selectedScopeValues = selectedScopes - .Select(x => x.AsSecretKeyScope()) - .ToHashSet(); - string? encodedApiKey; try { - var request = new CreateSecretKeyRequest(selectedScopeValues); + var request = new CreateSecretKeyRequest(selectedScopes); var response = await ManagementClient.CreateApiKeyAsync(AppId, request); var eventDto = new OrganizationEventDto(HttpContextAccessor.HttpContext!.Request.HttpContext.User.GetId(), diff --git a/src/AdminConsole/Components/Pages/App/Settings/SettingsComponents/CreateApiKeyComponent.razor b/src/AdminConsole/Components/Pages/App/Settings/SettingsComponents/CreateApiKeyComponent.razor index 37a902804..e25b27fc5 100644 --- a/src/AdminConsole/Components/Pages/App/Settings/SettingsComponents/CreateApiKeyComponent.razor +++ b/src/AdminConsole/Components/Pages/App/Settings/SettingsComponents/CreateApiKeyComponent.razor @@ -1,3 +1,5 @@ +@typeparam TScope where TScope : struct, IConvertible +

Scopes

@@ -21,10 +23,10 @@ private const string FormName = "create-api-key-form"; [Parameter] - public required IReadOnlyCollection Scopes { get; set; } + public required IReadOnlyCollection Scopes { get; set; } [Parameter] - public required EventCallback> OnCreateClicked { get; set; } + public required EventCallback> OnCreateClicked { get; set; } [SupplyParameterFromForm(FormName = FormName)] public CreateApiKeyFormModel? FormModel { get; set; } @@ -42,16 +44,16 @@ private async Task OnValidSubmitAsync() { - if (!FormModel.SelectedScopes.Any()) + if (!FormModel!.SelectedScopes.Any()) { FormValidationMessageStore!.Add(() => FormModel.SelectedScopes, "Please select at least one scope."); return; } - await OnCreateClicked.InvokeAsync(FormModel.SelectedScopes); + await OnCreateClicked.InvokeAsync(FormModel.SelectedScopes.ToHashSet()); } public class CreateApiKeyFormModel { - public List SelectedScopes { get; set; } = new(); + public List SelectedScopes { get; set; } = new(); } } \ No newline at end of file