yavsc/src/Yavsc.Org/Controllers/Administration/ClientController.cs

173 lines
5.3 KiB
C#
Raw Normal View History

2025-08-24 16:07:53 +01:00
using IdentityServer8.EntityFramework.DbContexts;
using IdentityServer8.EntityFramework.Entities;
2026-03-09 02:07:09 +00:00
using IdentityServer8.EntityFramework.Stores;
2025-08-24 16:07:53 +01:00
using Microsoft.AspNetCore.Authorization;
2023-03-19 17:57:55 +00:00
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
2018-05-04 13:56:22 +02:00
using Yavsc.Models;
using Yavsc.Models.Auth;
namespace Yavsc.Controllers
{
2025-08-24 16:07:53 +01:00
[Authorize("AdministratorOnly")]
2018-05-04 13:56:22 +02:00
public class ClientController : Controller
{
2026-03-09 02:07:09 +00:00
private readonly ApplicationDbContext context;
private readonly ClientStore clientStore;
2018-05-04 13:56:22 +02:00
2026-03-09 02:07:09 +00:00
public ClientController(ApplicationDbContext context,
ClientStore clientStore,
IdentityServer8.Stores.ValidatingClientStore<ClientStore> validatingClientStore
)
2018-05-04 13:56:22 +02:00
{
2026-03-09 02:07:09 +00:00
this.context = context;
this.clientStore = clientStore;
2018-05-04 13:56:22 +02:00
}
// GET: Client
public async Task<IActionResult> Index()
{
2026-03-09 02:07:09 +00:00
return View(await context.Clients.Include(c => c.AllowedGrantTypes)
.Include(c => c.RedirectUris).ToListAsync());
2018-05-04 13:56:22 +02:00
}
// GET: Client/Details/5
2026-03-09 02:07:09 +00:00
public async Task<IActionResult> Details(int id)
2018-05-04 13:56:22 +02:00
{
2026-03-09 02:07:09 +00:00
Client client = await context.Clients.Include(
2025-08-24 16:07:53 +01:00
c => c.ClientSecrets
2026-03-09 02:07:09 +00:00
).Include(c => c.AllowedGrantTypes)
.Include(c => c.RedirectUris)
.Include(c=>c.ClientSecrets)
.Include(c=>c.AllowedCorsOrigins)
.Include(c=>c.AllowedScopes)
.Include(c=>c.IdentityProviderRestrictions)
.Include(c=>c.PostLogoutRedirectUris)
.SingleAsync(m => m.Id == id);
2018-05-04 13:56:22 +02:00
if (client == null)
{
2023-03-19 17:57:55 +00:00
return NotFound();
2018-05-04 13:56:22 +02:00
}
return View(client);
}
// GET: Client/Create
public IActionResult Create()
{
2026-03-09 02:07:09 +00:00
Secret s;
2018-05-04 13:56:22 +02:00
SetAppTypesInputValues();
return View();
}
// POST: Client/Create
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(Client client)
{
if (ModelState.IsValid)
{
2026-03-09 02:07:09 +00:00
var model = await clientStore.FindClientByIdAsync(client.ClientId);
if (model != null)
{
ModelState.AddModelError("ClientId", "existent");
return BadRequest(ModelState);
}
context.Clients.Add(client);
if (client.ClientSecrets != null)
{
foreach (var secret in client.ClientSecrets)
{
context.ClientSecrets.Add(secret);
}
}
await context.SaveChangesAsync();
2018-05-04 13:56:22 +02:00
return RedirectToAction("Index");
}
SetAppTypesInputValues();
return View(client);
}
2026-03-09 02:07:09 +00:00
2018-05-04 13:56:22 +02:00
private void SetAppTypesInputValues()
{
2025-08-24 16:07:53 +01:00
IEnumerable<SelectListItem> types = new SelectListItem[] {
2018-05-04 13:56:22 +02:00
new SelectListItem {
Text = ApplicationTypes.JavaScript.ToString(),
Value = ((int) ApplicationTypes.JavaScript).ToString() },
new SelectListItem {
Text = ApplicationTypes.NativeConfidential.ToString(),
2025-08-24 16:07:53 +01:00
Value = ((int) ApplicationTypes.NativeConfidential).ToString()
2018-05-04 13:56:22 +02:00
}
};
2025-08-24 16:07:53 +01:00
ViewData["AccessTokenType"] = types;
2018-05-04 13:56:22 +02:00
}
// GET: Client/Edit/5
2026-03-09 02:07:09 +00:00
public async Task<IActionResult> Edit(int id)
2018-05-04 13:56:22 +02:00
{
2026-03-09 02:07:09 +00:00
Client client = await context.Clients.SingleOrDefaultAsync(m => m.Id == id);
2018-05-04 13:56:22 +02:00
if (client == null)
{
2023-03-19 17:57:55 +00:00
return NotFound();
2018-05-04 13:56:22 +02:00
}
SetAppTypesInputValues();
return View(client);
}
// POST: Client/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(Client client)
{
if (ModelState.IsValid)
{
2026-03-09 02:07:09 +00:00
if (client.ClientSecrets != null)
{
foreach (var secret in client.ClientSecrets)
{
context.Update(secret);
}
}
context.Update(client);
await context.SaveChangesAsync();
2018-05-04 13:56:22 +02:00
return RedirectToAction("Index");
}
return View(client);
}
// GET: Client/Delete/5
[ActionName("Delete")]
2026-03-09 02:07:09 +00:00
public async Task<IActionResult> Delete(int id)
2018-05-04 13:56:22 +02:00
{
2026-03-09 02:07:09 +00:00
Client client = await context.Clients.SingleOrDefaultAsync(m => m.Id == id);
2018-05-04 13:56:22 +02:00
if (client == null)
{
2023-03-19 17:57:55 +00:00
return NotFound();
2018-05-04 13:56:22 +02:00
}
return View(client);
}
// POST: Client/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
2026-03-09 02:07:09 +00:00
public async Task<IActionResult> DeleteConfirmed(int id)
2018-05-04 13:56:22 +02:00
{
2026-03-09 02:07:09 +00:00
Client client = await context.Clients
.Include(client => client.ClientSecrets)
.SingleAsync(m => m.Id == id);
context.Clients.Remove(client);
await context.SaveChangesAsync();
2018-05-04 13:56:22 +02:00
return RedirectToAction("Index");
}
}
}