Admin Panel Addition

This commit is contained in:
dingo
2025-12-26 22:30:39 -05:00
parent 021d9b8230
commit 9b757bf5ff
3 changed files with 106 additions and 2 deletions
+8
View File
@@ -204,14 +204,22 @@ public sealed partial class DungeonHandler : IClientMessageHandler
// Payload classes for Redis pub/sub
private class BroadcastPayload
{
[System.Text.Json.Serialization.JsonPropertyName("type")]
public string Type { get; set; } = "system";
[System.Text.Json.Serialization.JsonPropertyName("message")]
public string Message { get; set; } = "";
[System.Text.Json.Serialization.JsonPropertyName("duration")]
public int Duration { get; set; } = 10;
[System.Text.Json.Serialization.JsonPropertyName("timestamp")]
public long Timestamp { get; set; }
}
private class CommandPayload
{
[System.Text.Json.Serialization.JsonPropertyName("command")]
public string Command { get; set; } = "";
public long Timestamp { get; set; }
}
+19 -1
View File
@@ -10,12 +10,30 @@
<div class="flex items-center justify-between">
<div>
<a href="/admin/players" class="text-gray-400 hover:text-white text-sm">← Back to Players</a>
<h1 class="text-3xl font-bold text-white mt-2">@(Model.IsNew ? "New Account" : Model.Account.Username)</h1>
<h1 class="text-3xl font-bold text-white mt-2">
@(Model.IsNew ? "New Account" : Model.Account.Username)
@if (Model.IsOnline)
{
<span class="ml-2 inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-green-900 text-green-300">
🟢 Online
</span>
}
</h1>
@if (!Model.IsNew)
{
<p class="text-gray-400 mt-1">Account ID: @Model.Account.Id • Created: @Model.Account.CreatedAt.ToString("yyyy-MM-dd HH:mm")</p>
}
</div>
@if (Model.IsOnline)
{
<form method="post" asp-page-handler="ForceLogout">
@Html.AntiForgeryToken()
<input type="hidden" name="accountId" value="@Model.Account.Id" />
<button type="submit" class="btn bg-orange-700 hover:bg-orange-600 text-white" onclick="return confirm('Force logout this user? They will be disconnected immediately.')">
⚡ Force Logout
</button>
</form>
}
</div>
@if (!string.IsNullOrEmpty(Model.Message))
+79 -1
View File
@@ -3,6 +3,7 @@ using Microsoft.EntityFrameworkCore;
using DataServer.Data;
using DataServer.Data.Entities;
using Server.Services.Auth;
using Server.Redis;
namespace Server.Services.Admin.Pages.Players;
@@ -10,15 +11,20 @@ public class EditModel : AdminPageModel
{
private readonly GameDbContext _db;
private readonly IPasswordHasher _passwordHasher;
private readonly IRedisManager _redis;
private readonly ILogger<EditModel> _logger;
public EditModel(GameDbContext db, IPasswordHasher passwordHasher)
public EditModel(GameDbContext db, IPasswordHasher passwordHasher, IRedisManager redis, ILogger<EditModel> logger)
{
_db = db;
_passwordHasher = passwordHasher;
_redis = redis;
_logger = logger;
}
public Account Account { get; set; } = new();
public List<DungeonCharacter> Characters { get; set; } = new();
public bool IsOnline { get; set; }
public bool IsNew => Account.Id == 0;
@@ -43,6 +49,14 @@ public class EditModel : AdminPageModel
Account = account;
// Check if player is online (has online:acct key in Redis)
try
{
var onlineKey = await _redis.Db.StringGetAsync($"online:acct:{account.Id}");
IsOnline = !onlineKey.IsNullOrEmpty;
}
catch { IsOnline = false; }
// Load characters
Characters = await _db.DungeonCharacters
.Where(c => c.AccountId == id.Value && !c.IsDeleted)
@@ -172,4 +186,68 @@ public class EditModel : AdminPageModel
IsError = false;
return RedirectToPage(new { id = accountId });
}
public async Task<IActionResult> OnPostForceLogoutAsync(ulong accountId)
{
var account = await _db.Set<Account>().FirstOrDefaultAsync(a => a.Id == accountId);
if (account == null)
{
Message = "Account not found.";
IsError = true;
return RedirectToPage("/Players/Index");
}
try
{
// Get the current session token from Redis
var onlineKey = $"online:acct:{accountId}";
var token = await _redis.Db.StringGetAsync(onlineKey);
if (!token.IsNullOrEmpty)
{
// Delete the session key
var sessKey = $"sess:{token}";
await _redis.Db.KeyDeleteAsync(sessKey);
// Delete the online marker
await _redis.Db.KeyDeleteAsync(onlineKey);
// Also delete player tracking key if exists
var characters = await _db.DungeonCharacters
.Where(c => c.AccountId == accountId && !c.IsDeleted)
.ToListAsync();
foreach (var character in characters)
{
await _redis.Db.KeyDeleteAsync($"player:{character.Id}");
}
// Decrement player count
var count = await _redis.Db.StringDecrementAsync("stats:players");
if (count < 0) await _redis.Db.StringSetAsync("stats:players", 0);
_logger.LogInformation("Admin force logged out account {AccountId} ({Username})", accountId, account.Username);
}
// Revoke all active sessions in database
var activeSessions = await _db.Sessions
.Where(s => s.AccountId == accountId && s.RevokedAt == null && s.ExpiresAt > DateTime.UtcNow)
.ToListAsync();
foreach (var session in activeSessions)
{
session.RevokedAt = DateTime.UtcNow;
}
await _db.SaveChangesAsync();
Message = $"User {account.Username} has been logged out. {activeSessions.Count} session(s) revoked.";
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to force logout account {AccountId}", accountId);
Message = $"Error: {ex.Message}";
IsError = true;
}
return RedirectToPage(new { id = accountId });
}
}