Files
gott/Endpoints/ProfileEndpoint.cs
T
Meleki d1355dcb0d feat: Login-as-User (Impersonate) + Default-Path pro User
Admin:
- POST /api/admin/users/{userId}/impersonate – erstellt Token für Zieluser
- Login-as-Button (fa-sign-in) in User-Liste (nicht für sich selbst)
- confirm-Dialog, danach location.reload() mit neuem User-Kontext
- Default-Path Feld + Browse-Button im Admin-Formular

Default-Path:
- Neues Feld default_path in users-Tabelle (TryAddColumn Migration)
- UserRecord.DefaultPath Property
- In allen Endpoints: Admin CRUD, Auth /me, Profile Get/Update
- Profil-Seite: Default-Path Feld + Browse-Button
- Session-Seite: Workspace-Browse nutzt defaultPath als Fallback
2026-03-05 20:42:21 +01:00

100 lines
3.5 KiB
C#

using System.Text.Json;
using Gott.Extensions;
using Gott.Services;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
namespace Gott.Endpoints;
public static class ProfileEndpoint
{
public static void MapProfileEndpoints(this WebApplication app)
{
app.MapGet("/api/profile", HandleGet);
app.MapPut("/api/profile", HandleUpdate);
app.MapPut("/api/profile/password", HandlePasswordChange);
}
private static async System.Threading.Tasks.Task HandleGet(HttpContext ctx, DatabaseService db)
{
var userId = ctx.GetUserId();
var user = await db.GetUser(userId);
if (user == null)
{
ctx.Response.StatusCode = 404;
await ctx.Response.WriteAsJsonAsync(new { error = "User not found" });
return;
}
await ctx.Response.WriteAsJsonAsync(new
{
userId = user.UserId,
username = user.Username,
displayName = user.DisplayName,
persona = user.Persona,
defaultPath = user.DefaultPath,
isAdmin = user.IsAdmin
});
}
private static async System.Threading.Tasks.Task HandleUpdate(HttpContext ctx, DatabaseService db)
{
var userId = ctx.GetUserId();
var body = await JsonSerializer.DeserializeAsync<JsonElement>(ctx.Request.Body);
var displayName = body.TryGetProperty("displayName", out var dn) ? dn.GetString() : null;
var persona = body.TryGetProperty("persona", out var p) ? p.GetString() : null;
var defaultPath = body.TryGetProperty("defaultPath", out var dp) ? dp.GetString() : null;
await db.UpdateUserProfile(userId, displayName, persona, defaultPath);
var user = await db.GetUser(userId);
await ctx.Response.WriteAsJsonAsync(new
{
userId = user.UserId,
username = user.Username,
displayName = user.DisplayName,
persona = user.Persona,
defaultPath = user.DefaultPath,
isAdmin = user.IsAdmin
});
}
private static async System.Threading.Tasks.Task HandlePasswordChange(HttpContext ctx, DatabaseService db, AuthService auth)
{
var userId = ctx.GetUserId();
var body = await JsonSerializer.DeserializeAsync<JsonElement>(ctx.Request.Body);
var currentPassword = body.TryGetProperty("currentPassword", out var cp) ? cp.GetString() : null;
var newPassword = body.TryGetProperty("newPassword", out var np) ? np.GetString() : null;
if (string.IsNullOrEmpty(currentPassword) || string.IsNullOrEmpty(newPassword))
{
ctx.Response.StatusCode = 400;
await ctx.Response.WriteAsJsonAsync(new { error = "Aktuelles und neues Passwort erforderlich" });
return;
}
if (newPassword.Length < 4)
{
ctx.Response.StatusCode = 400;
await ctx.Response.WriteAsJsonAsync(new { error = "Passwort muss mindestens 4 Zeichen lang sein" });
return;
}
var user = await db.GetUser(userId);
if (user == null || !auth.VerifyPassword(currentPassword, user.PasswordHash))
{
ctx.Response.StatusCode = 403;
await ctx.Response.WriteAsJsonAsync(new { error = "Aktuelles Passwort ist falsch" });
return;
}
var newHash = auth.HashPassword(newPassword);
await db.UpdateUserPassword(userId, newHash);
await ctx.Response.WriteAsJsonAsync(new { ok = true });
}
}