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

147 lines
4.4 KiB
C#
Raw Normal View History

2025-08-24 16:07:53 +01:00
using IdentityServer8.EntityFramework.DbContexts;
using IdentityServer8.EntityFramework.Entities;
using Microsoft.AspNetCore.Authorization;
2023-03-19 17:57:55 +00:00
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using Yavsc.Helpers;
2018-05-04 13:56:22 +02:00
using Yavsc.Models;
using Yavsc.Models.Auth;
using Yavsc.Server.Helpers;
2018-05-04 13:56:22 +02:00
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
{
2025-08-24 16:07:53 +01:00
private readonly ConfigurationDbContext _context;
2018-05-04 13:56:22 +02:00
2025-08-24 16:07:53 +01:00
public ClientController(ConfigurationDbContext context)
2018-05-04 13:56:22 +02:00
{
2025-08-24 16:07:53 +01:00
_context = context;
2018-05-04 13:56:22 +02:00
}
// GET: Client
public async Task<IActionResult> Index()
{
2025-08-24 16:07:53 +01: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
public async Task<IActionResult> Details(string id)
{
if (id == null)
{
2023-03-19 17:57:55 +00:00
return NotFound();
2018-05-04 13:56:22 +02:00
}
2025-08-24 16:07:53 +01:00
Client client = await _context.Clients.Include(
c => c.ClientSecrets
).Include(c=>c.AllowedGrantTypes)
.Include(c=>c.RedirectUris)
.SingleAsync(m => m.ClientId == 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()
{
SetAppTypesInputValues();
return View();
}
// POST: Client/Create
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(Client client)
{
if (ModelState.IsValid)
{
2025-08-24 16:07:53 +01:00
if (string.IsNullOrWhiteSpace(client.ClientId))
client.ClientId = Guid.NewGuid().ToString();
_context.Clients.Add(client);
await _context.SaveChangesAsync();
2018-05-04 13:56:22 +02:00
return RedirectToAction("Index");
}
SetAppTypesInputValues();
return View(client);
}
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
public async Task<IActionResult> Edit(string id)
{
if (id == null)
{
2023-03-19 17:57:55 +00:00
return NotFound();
2018-05-04 13:56:22 +02:00
}
2025-08-24 16:07:53 +01:00
Client client = await _context.Clients.SingleAsync(m => m.ClientId == 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)
{
_context.Update(client);
2025-08-24 16:07:53 +01:00
await _context.SaveChangesAsync();
2018-05-04 13:56:22 +02:00
return RedirectToAction("Index");
}
return View(client);
}
// GET: Client/Delete/5
[ActionName("Delete")]
public async Task<IActionResult> Delete(string id)
{
if (id == null)
{
2023-03-19 17:57:55 +00:00
return NotFound();
2018-05-04 13:56:22 +02:00
}
2025-08-24 16:07:53 +01:00
Client client = await _context.Clients.SingleAsync(m => m.ClientId == 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]
public async Task<IActionResult> DeleteConfirmed(string id)
{
2025-08-24 16:07:53 +01:00
Client client = await _context.Clients.SingleAsync(m => m.ClientId == id);
_context.Clients.Remove(client);
await _context.SaveChangesAsync();
2018-05-04 13:56:22 +02:00
return RedirectToAction("Index");
}
}
}