From 8bf1df3ba4c0eb0d507a3f81434b0fd32b1d6969 Mon Sep 17 00:00:00 2001 From: Daniel Habenicht Date: Fri, 20 Jun 2025 12:33:40 +0200 Subject: [PATCH] Routes fixed, new ui --- Watcher/Program.cs | 128 +++++++-------- Watcher/Views/Auth/Info.cshtml | 195 ++++++++++++----------- Watcher/Views/Auth/Login.cshtml | 83 ++++++++-- Watcher/Views/Home/Index.cshtml | 58 ++++--- Watcher/Views/Server/AddServer.cshtml | 62 +++---- Watcher/Views/Server/overview.cshtml | 133 +++++++++++++--- Watcher/Views/Shared/_Layout.cshtml | 16 +- Watcher/Views/Shared/_LoginLayout.cshtml | 24 +++ Watcher/persistence/watcher.db-shm | Bin 0 -> 32768 bytes Watcher/persistence/watcher.db-wal | 0 Watcher/wwwroot/css/ServerOverview.css | 0 Watcher/wwwroot/css/site.css | 82 +++++++--- 12 files changed, 512 insertions(+), 269 deletions(-) create mode 100644 Watcher/Views/Shared/_LoginLayout.cshtml create mode 100644 Watcher/persistence/watcher.db-shm create mode 100644 Watcher/persistence/watcher.db-wal create mode 100644 Watcher/wwwroot/css/ServerOverview.css diff --git a/Watcher/Program.cs b/Watcher/Program.cs index 3f301cb..4765b87 100644 --- a/Watcher/Program.cs +++ b/Watcher/Program.cs @@ -33,7 +33,7 @@ var connectionString = configuration["Database:ConnectionString"]; builder.Services.AddDbContext((serviceProvider, options) => { var config = serviceProvider.GetRequiredService(); - var provider = config["Database:Provider"]; + var provider = dbProvider; if (provider == "MySql") { @@ -52,81 +52,77 @@ builder.Services.AddDbContext((serviceProvider, options) => }); // ---------- Authentifizierung konfigurieren ---------- -builder.Services.AddAuthentication(options => -{ - options.DefaultScheme = "Cookies"; - options.DefaultChallengeScheme = "Cookies"; // Nur wenn kein OIDC erzwungen wird -}) -.AddCookie("Cookies", options => -{ - options.LoginPath = "/Auth/Login"; -}); - // PocketID nur konfigurieren, wenn aktiviert var pocketIdSection = builder.Configuration.GetSection("Authentication:PocketID"); var pocketIdEnabled = pocketIdSection.GetValue("Enabled"); -if (pocketIdEnabled) +var auth = builder.Services.AddAuthentication("Cookies"); + +auth.AddCookie("Cookies", options => { - builder.Services.AddAuthentication() - .AddOpenIdConnect("oidc", options => + options.LoginPath = "/Auth/Login"; + options.AccessDeniedPath = "/Auth/AccessDenied"; +}); + +builder.Services.AddAuthentication() +.AddOpenIdConnect("oidc", options => +{ + options.Authority = pocketIdSection["Authority"]; + options.ClientId = pocketIdSection["ClientId"]; + options.ClientSecret = pocketIdSection["ClientSecret"]; + options.ResponseType = "code"; + options.CallbackPath = pocketIdSection["CallbackPath"]; + options.SaveTokens = true; + + options.GetClaimsFromUserInfoEndpoint = true; + + options.Scope.Clear(); + options.Scope.Add("openid"); + options.Scope.Add("profile"); + options.Scope.Add("email"); + + options.Events = new OpenIdConnectEvents { - options.Authority = pocketIdSection["Authority"]; - options.ClientId = pocketIdSection["ClientId"]; - options.ClientSecret = pocketIdSection["ClientSecret"]; - options.ResponseType = "code"; - options.CallbackPath = pocketIdSection["CallbackPath"]; - options.SaveTokens = true; - - options.GetClaimsFromUserInfoEndpoint = true; - - options.Scope.Clear(); - options.Scope.Add("openid"); - options.Scope.Add("profile"); - options.Scope.Add("email"); - - options.Events = new OpenIdConnectEvents + OnTokenValidated = async ctx => { - OnTokenValidated = async ctx => + var db = ctx.HttpContext.RequestServices.GetRequiredService(); + + var principal = ctx.Principal; + var pocketId = principal.FindFirst("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier")?.Value; + var preferredUsername = principal.FindFirst("preferred_username")?.Value; + var email = principal.FindFirst("email")?.Value; + + if (string.IsNullOrEmpty(pocketId)) + return; + + var user = await db.Users.FirstOrDefaultAsync(u => u.PocketId == pocketId); + + if (user == null) { - var db = ctx.HttpContext.RequestServices.GetRequiredService(); - - var principal = ctx.Principal; - var pocketId = principal.FindFirst("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier")?.Value; - var preferredUsername = principal.FindFirst("preferred_username")?.Value; - var email = principal.FindFirst("email")?.Value; - - if (string.IsNullOrEmpty(pocketId)) - return; - - var user = await db.Users.FirstOrDefaultAsync(u => u.PocketId == pocketId); - - if (user == null) + user = new User { - user = new User - { - PocketId = pocketId, - PreferredUsername = preferredUsername ?? "", - Email = email, - LastLogin = DateTime.UtcNow, - IdentityProvider = "oidc", - Password = string.Empty - }; - db.Users.Add(user); - } - else - { - user.LastLogin = DateTime.UtcNow; - user.PreferredUsername = preferredUsername ?? user.PreferredUsername; - user.Email = email ?? user.Email; - db.Users.Update(user); - } - - await db.SaveChangesAsync(); + PocketId = pocketId, + PreferredUsername = preferredUsername ?? "", + Email = email, + LastLogin = DateTime.UtcNow, + IdentityProvider = "oidc", + Password = string.Empty + }; + db.Users.Add(user); } - }; - }); -} + else + { + user.LastLogin = DateTime.UtcNow; + user.PreferredUsername = preferredUsername ?? user.PreferredUsername; + user.Email = email ?? user.Email; + db.Users.Update(user); + } + + await db.SaveChangesAsync(); + } + }; +}); + diff --git a/Watcher/Views/Auth/Info.cshtml b/Watcher/Views/Auth/Info.cshtml index 709c002..fd257a3 100644 --- a/Watcher/Views/Auth/Info.cshtml +++ b/Watcher/Views/Auth/Info.cshtml @@ -1,104 +1,115 @@ @{ ViewData["Title"] = "Account Info"; - var pictureUrl = User.Claims.FirstOrDefault(c => c.Type == "picture")?.Value ?? "123"; + var pictureUrl = User.Claims.FirstOrDefault(c => c.Type == "picture")?.Value ?? ""; + var preferredUsername = User.Claims.FirstOrDefault(c => c.Type == "preferred_username")?.Value ?? "admin"; + var isAdmin = preferredUsername == "admin"; } -

Account Info

+
+
+
+ @if (!string.IsNullOrEmpty(pictureUrl)) + { + Profilbild + } + else + { +
+ @(User.Identity?.Name?.Substring(0,1).ToUpper() ?? "?") +
+ } +

@(User.FindFirst("name")?.Value ?? "Unbekannter Nutzer")

+
-
- @if (!string.IsNullOrEmpty(pictureUrl)) + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Username@preferredUsername
E-Mail@(User.FindFirst("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress")?.Value ?? "Nicht verfügbar")
Benutzer-ID@(User.FindFirst("sub")?.Value ?? "Nicht verfügbar")
Login-Zeit + @(User.FindFirst("iat") != null + ? DateTimeOffset.FromUnixTimeSeconds(long.Parse(User.FindFirst("iat").Value)).ToLocalTime().ToString() + : "Nicht verfügbar") +
Token läuft ab + @(User.FindFirst("exp") != null + ? DateTimeOffset.FromUnixTimeSeconds(long.Parse(User.FindFirst("exp").Value)).ToLocalTime().ToString() + : "Nicht verfügbar") +
Rollen + @{ + var roles = User.FindAll("role").Select(r => r.Value); + if (!roles.Any()) + { + Keine Rollen + } + else + { +
    + @foreach (var role in roles) + { +
  • @role
  • + } +
+ } + } +
+ +
+ +
+
+ + @if (isAdmin) { - Profilbild +
+

Benutzerdaten ändern

+
+
+ + +
+
+ + +
+
+ + +
+ +
+
} else { -
- @(User.Identity?.Name?.Substring(0,1).ToUpper() ?? "?") +
+ Benutzerdaten können nur für lokal angemeldete Nutzer geändert werden.
} - -

@(User.FindFirst("name")?.Value ?? "Unbekannter Nutzer")

- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Username@(@User.Claims.FirstOrDefault(c => c.Type == "preferred_username")?.Value ?? "Nicht verfügbar")
E-Mail@(@User.Claims.FirstOrDefault(c => c.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress")?.Value ?? "Nicht verfügbar")
Benutzer-ID@(User.FindFirst("sub")?.Value ?? "Nicht verfügbar")
Login-Zeit@(User.FindFirst("iat") != null - ? DateTimeOffset.FromUnixTimeSeconds(long.Parse(User.FindFirst("iat").Value)).ToLocalTime().ToString() - : "Nicht verfügbar") -
Token läuft ab@(User.FindFirst("exp") != null - ? DateTimeOffset.FromUnixTimeSeconds(long.Parse(User.FindFirst("exp").Value)).ToLocalTime().ToString() - : "Nicht verfügbar") -
Rollen - @{ - var roles = User.FindAll("role").Select(r => r.Value); - if (!roles.Any()) - { - Keine Rollen - } - else - { -
    - @foreach (var role in roles) - { -
  • @role
  • - } -
- } - } -
- -
- -
- -@{ - -} -@if (User.Identity.Name == "admin") -{ -

Benutzerdaten ändern

-
-
- - -
-
- - -
-
- - -
- -
-} -else -{ -

Benutzerdaten können nur für lokal angemeldete Nutzer geändert werden.

-} diff --git a/Watcher/Views/Auth/Login.cshtml b/Watcher/Views/Auth/Login.cshtml index 9d5a5c7..7147327 100644 --- a/Watcher/Views/Auth/Login.cshtml +++ b/Watcher/Views/Auth/Login.cshtml @@ -1,30 +1,87 @@ @model Watcher.ViewModels.LoginViewModel @{ + Layout = "~/Views/Shared/_LoginLayout.cshtml"; ViewData["Title"] = "Login"; } -