Skip to content

Commit 59b8d86

Browse files
Can update the client type
1 parent edd7bf3 commit 59b8d86

6 files changed

Lines changed: 177 additions & 2 deletions

File tree

src/IdServer/SimpleIdServer.IdServer.Website/Pages/Client/Client.razor

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@
1919
<RadzenBreadCrumbItem Path="@urlHelper.GetUrl("/clients")" Text="@Global.ClientsTitle" />
2020
<RadzenBreadCrumbItem Text="@Global.ClientDetailsTitle" />
2121
</RadzenBreadCrumb>
22-
2322
<RadzenText class="mt-3 mb-3 no-margin" Text="@clientState.Value.Client.ClientId" TextStyle="TextStyle.DisplayH3" />
2423

2524
<div class="mb-1">
26-
<RadzenBadge Text="@(clientState.Value.Client.ClientType == null ? string.Empty : Enum.GetName(typeof(ClientTypes), clientState.Value.Client.ClientType.Value))" IsPill="true" />
25+
<div>
26+
<RadzenLabel>@Global.Type</RadzenLabel>
27+
<RadzenDropDown TValue="SimpleIdServer.IdServer.Domains.ClientTypes" Value=@clientState.Value.Client.ClientType Change=@HandleClientTypeChanged Data="@ClientTypes" ValueProperty="Type" TextProperty="Name" />
28+
</div>
2729
@if(clientState.Value.Client.IsPublic)
2830
{
2931
<RadzenBadge style="margin-left: 5px;" Text="@Global.PublicClient" IsPill="true" />
@@ -82,6 +84,7 @@
8284
public string? action { get; set; } = null;
8385

8486
int selectedIndex = 0;
87+
List<ClientTypeRecord> ClientTypes { get; set; } = new List<ClientTypeRecord>();
8588

8689
protected override void OnAfterRender(bool firstRender)
8790
{
@@ -100,11 +103,37 @@
100103
StateHasChanged();
101104
});
102105
dispatcher.Dispatch(new GetClientAction { Id = id });
106+
ClientTypes = Enum.GetValues(typeof(ClientTypes))
107+
.Cast<ClientTypes>()
108+
.Select(c => new ClientTypeRecord
109+
{
110+
Name = c.ToString(),
111+
Type = c
112+
})
113+
.OrderBy(c => c.Name)
114+
.ToList();
103115
}
104116

105117
void OnChange(int index)
106118
{
107119
var rec = mappingActionNameToIndex.Single(kvp => kvp.Value == index);
108120
navigationManager.NavigateTo(urlHelper.GetUrl($"/clients/{System.Web.HttpUtility.UrlEncode(id)}/{rec.Key}"));
109121
}
122+
123+
void HandleClientTypeChanged(object obj)
124+
{
125+
if(obj == null || clientState.Value.Client == null)
126+
{
127+
return;
128+
}
129+
130+
var clientType = (ClientTypes)obj;
131+
dispatcher.Dispatch(new UpdateClientTypeAction { Id = clientState.Value.Client.Id, ClientType = clientType });
132+
}
133+
134+
record ClientTypeRecord
135+
{
136+
public string Name { get; set; }
137+
public ClientTypes Type { get; set; }
138+
}
110139
}

src/IdServer/SimpleIdServer.IdServer.Website/Stores/ClientStore/ClientEffects.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,28 @@ public async Task Handle(DeleteClientSecretsAction act, IDispatcher dispatcher)
808808
});
809809
}
810810

811+
[EffectMethod]
812+
public async Task Handle(UpdateClientTypeAction act, IDispatcher dispatcher)
813+
{
814+
var baseUrl = await GetClientsUrl();
815+
var httpClient = await _websiteHttpClientFactory.Build();
816+
var requestMessage = new HttpRequestMessage
817+
{
818+
RequestUri = new Uri($"{baseUrl}/{act.Id}/type"),
819+
Method = HttpMethod.Put,
820+
Content = new StringContent(JsonSerializer.Serialize(new UpdateClientTypeRequest
821+
{
822+
ClientType = act.ClientType
823+
}), Encoding.UTF8, "application/json")
824+
};
825+
await httpClient.SendAsync(requestMessage);
826+
dispatcher.Dispatch(new UpdateClientTypeSuccessAction
827+
{
828+
ClientType = act.ClientType,
829+
Id = act.Id
830+
});
831+
}
832+
811833
private async Task CreateClient(Client client, IDispatcher dispatcher, ClientTypes clientType, PemResult pemResult = null, string jsonWebKey = null)
812834
{
813835
var baseUrl = await GetClientsUrl();
@@ -1429,4 +1451,30 @@ public List<ClientSecret> Secrets
14291451
{
14301452
get; set;
14311453
}
1454+
}
1455+
1456+
public class UpdateClientTypeAction
1457+
{
1458+
public string Id
1459+
{
1460+
get; set;
1461+
}
1462+
1463+
public ClientTypes ClientType
1464+
{
1465+
get; set;
1466+
}
1467+
}
1468+
1469+
public class UpdateClientTypeSuccessAction
1470+
{
1471+
public string Id
1472+
{
1473+
get; set;
1474+
}
1475+
1476+
public ClientTypes ClientType
1477+
{
1478+
get; set;
1479+
}
14321480
}

src/IdServer/SimpleIdServer.IdServer.Website/Stores/ClientStore/ClientReducers.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using SimpleIdServer.IdServer.Helpers.Models;
1111
using SimpleIdServer.IdServer.Saml.Idp.Extensions;
1212
using SimpleIdServer.IdServer.Website.Stores.ScopeStore;
13+
using System.Xml;
1314

1415
namespace SimpleIdServer.IdServer.Website.Stores.ClientStore
1516
{
@@ -108,6 +109,28 @@ public static SearchClientsState ReduceToggleAllClientSelectionAction(SearchClie
108109
};
109110
}
110111

112+
[ReducerMethod]
113+
public static SearchClientsState ReduceUpdateClientTypeSuccessAction(SearchClientsState state, UpdateClientTypeSuccessAction act)
114+
{
115+
if(state.Clients == null)
116+
{
117+
return state;
118+
}
119+
120+
var clients = state.Clients;
121+
var selectedClient = clients.SingleOrDefault(c => c.Value.Id == act.Id);
122+
if(selectedClient != null)
123+
{
124+
selectedClient.Value.ClientType = act.ClientType;
125+
}
126+
127+
return state with
128+
{
129+
IsLoading = false,
130+
Clients = clients
131+
};
132+
}
133+
111134
#endregion
112135

113136
#region AddClientState
@@ -225,6 +248,18 @@ public static UpdateClientState ReduceUpdateAdvancedClientSettingsSuccessAction(
225248
IsUpdating = false
226249
};
227250

251+
[ReducerMethod]
252+
public static UpdateClientState ReduceUpdateClientTypeAction(UpdateClientState state, UpdateClientTypeAction act) => state with
253+
{
254+
IsUpdating = true
255+
};
256+
257+
[ReducerMethod]
258+
public static UpdateClientState ReduceUpdateClientTypeSuccessAction(UpdateClientState state, UpdateClientTypeSuccessAction act) => state with
259+
{
260+
IsUpdating = false
261+
};
262+
228263
#endregion
229264

230265
#region ClientState
@@ -368,6 +403,24 @@ public static ClientState ReduceUpdateAdvancedClientSettingsSuccessAction(Client
368403
};
369404
}
370405

406+
[ReducerMethod]
407+
public static ClientState ReduceUpdateClientTypeAction(ClientState state, UpdateClientTypeAction act) => state with
408+
{
409+
IsLoading = true
410+
};
411+
412+
[ReducerMethod]
413+
public static ClientState ReduceUpdateClientTypeSuccessAction(ClientState state, UpdateClientTypeSuccessAction act)
414+
{
415+
var client = state.Client;
416+
client.ClientType = act.ClientType;
417+
return state with
418+
{
419+
Client = client,
420+
IsLoading = false
421+
};
422+
}
423+
371424
#endregion
372425

373426
#region ClientScopesState

src/IdServer/SimpleIdServer.IdServer/Api/Clients/ClientsController.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,35 @@ await _busControl.Publish(new AddClientRoleFailureEvent
782782
}
783783
}
784784

785+
[HttpPut]
786+
public async Task<IActionResult> UpdateClientType([FromRoute] string prefix, string id, [FromBody] UpdateClientTypeRequest request, CancellationToken cancellationToken)
787+
{
788+
prefix = prefix ?? Constants.DefaultRealm;
789+
try
790+
{
791+
using (var transaction = _transactionBuilder.Build())
792+
{
793+
await CheckAccessToken(prefix, DefaultScopes.Clients.Name);
794+
var result = await _clientRepository.GetById(prefix, id, cancellationToken);
795+
if (result == null)
796+
{
797+
throw new OAuthException(HttpStatusCode.NotFound, ErrorCodes.NOT_FOUND, string.Format(Global.UnknownClient, id));
798+
}
799+
800+
result.ClientType = request.ClientType;
801+
result.UpdateDateTime = DateTime.UtcNow;
802+
_clientRepository.Update(result);
803+
await transaction.Commit(cancellationToken);
804+
return new NoContentResult();
805+
}
806+
}
807+
catch (OAuthException ex)
808+
{
809+
_logger.LogError(ex.ToString());
810+
return BuildError(ex);
811+
}
812+
}
813+
785814
[HttpPut]
786815
public async Task<IActionResult> UpdateRealms([FromRoute] string prefix, string id, [FromBody] UpdateClientRealmsRequest request, CancellationToken cancellationToken)
787816
{
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (c) SimpleIdServer. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
using SimpleIdServer.IdServer.Domains;
4+
5+
namespace SimpleIdServer.IdServer.Api.Clients;
6+
7+
public class UpdateClientTypeRequest
8+
{
9+
public ClientTypes ClientType
10+
{
11+
get; set;
12+
}
13+
}

src/IdServer/SimpleIdServer.IdServer/WebApplicationExtensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,9 @@ public static WebApplication UseSid(this WebApplication webApplication, bool coo
581581
webApplication.SidMapControllerRoute("RemoveClientSecret",
582582
pattern: (usePrefix ? "{prefix}/" : string.Empty) + DefaultEndpoints.Clients + "/{id}/secrets/{secretId}",
583583
defaults: new { controller = "Clients", action = "RemoveSecret" });
584+
webApplication.SidMapControllerRoute("UpdateClientType",
585+
pattern: (usePrefix ? "{prefix}/" : string.Empty) + DefaultEndpoints.Clients + "/{id}/type",
586+
defaults: new { controller = "Clients", action = "UpdateClientType" });
584587

585588

586589
webApplication.SidMapControllerRoute("getActiveTemplate",

0 commit comments

Comments
 (0)