254 lines
8.2 KiB
C#
254 lines
8.2 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using DataServer.Data;
|
|
using DataServer.Data.Entities;
|
|
using GameServices.Auth;
|
|
using Server.Redis;
|
|
|
|
namespace GameServices.Admin.Pages.Players;
|
|
|
|
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, 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;
|
|
|
|
[TempData]
|
|
public string? Message { get; set; }
|
|
|
|
[TempData]
|
|
public bool IsError { get; set; }
|
|
|
|
public async Task<IActionResult> OnGetAsync(ulong? id)
|
|
{
|
|
if (!id.HasValue)
|
|
{
|
|
return RedirectToPage("/Players/Index");
|
|
}
|
|
|
|
var account = await _db.Set<Account>().FirstOrDefaultAsync(a => a.Id == id.Value);
|
|
if (account == null)
|
|
{
|
|
return RedirectToPage("/Players/Index");
|
|
}
|
|
|
|
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)
|
|
.OrderByDescending(c => c.Level)
|
|
.ToListAsync();
|
|
|
|
return Page();
|
|
}
|
|
|
|
public async Task<IActionResult> OnPostSetRoleAsync(ulong accountId, AccountRole newRole)
|
|
{
|
|
var account = await _db.Set<Account>().FirstOrDefaultAsync(a => a.Id == accountId);
|
|
if (account == null)
|
|
{
|
|
Message = "Account not found.";
|
|
IsError = true;
|
|
return RedirectToPage("/Players/Index");
|
|
}
|
|
|
|
var oldRole = account.Role;
|
|
account.Role = newRole;
|
|
await _db.SaveChangesAsync();
|
|
|
|
Account = account;
|
|
Characters = await _db.DungeonCharacters
|
|
.Where(c => c.AccountId == accountId && !c.IsDeleted)
|
|
.OrderByDescending(c => c.Level)
|
|
.ToListAsync();
|
|
|
|
Message = $"Role changed from {oldRole} to {newRole}.";
|
|
return Page();
|
|
}
|
|
|
|
public async Task<IActionResult> OnPostBanAsync(ulong accountId, string banType, DateTime? bannedUntil)
|
|
{
|
|
var account = await _db.Set<Account>().FirstOrDefaultAsync(a => a.Id == accountId);
|
|
if (account == null)
|
|
{
|
|
Message = "Account not found.";
|
|
IsError = true;
|
|
return RedirectToPage("/Players/Index");
|
|
}
|
|
|
|
if (banType == "permanent")
|
|
{
|
|
account.IsBanned = true;
|
|
account.BannedUntil = null;
|
|
Message = "Account permanently banned.";
|
|
}
|
|
else if (banType == "temporary" && bannedUntil.HasValue)
|
|
{
|
|
account.IsBanned = false;
|
|
account.BannedUntil = bannedUntil.Value.ToUniversalTime();
|
|
Message = $"Account banned until {account.BannedUntil.Value:yyyy-MM-dd HH:mm} UTC.";
|
|
}
|
|
else
|
|
{
|
|
Message = "Invalid ban parameters.";
|
|
IsError = true;
|
|
}
|
|
|
|
await _db.SaveChangesAsync();
|
|
|
|
Account = account;
|
|
Characters = await _db.DungeonCharacters
|
|
.Where(c => c.AccountId == accountId && !c.IsDeleted)
|
|
.OrderByDescending(c => c.Level)
|
|
.ToListAsync();
|
|
|
|
return Page();
|
|
}
|
|
|
|
public async Task<IActionResult> OnPostUnbanAsync(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");
|
|
}
|
|
|
|
account.IsBanned = false;
|
|
account.BannedUntil = null;
|
|
await _db.SaveChangesAsync();
|
|
|
|
Account = account;
|
|
Characters = await _db.DungeonCharacters
|
|
.Where(c => c.AccountId == accountId && !c.IsDeleted)
|
|
.OrderByDescending(c => c.Level)
|
|
.ToListAsync();
|
|
|
|
Message = "Ban removed. Account is now active.";
|
|
return Page();
|
|
}
|
|
|
|
public async Task<IActionResult> OnPostResetPasswordAsync(ulong accountId, string newPassword, string confirmPassword)
|
|
{
|
|
var account = await _db.Set<Account>().FirstOrDefaultAsync(a => a.Id == accountId);
|
|
if (account == null)
|
|
{
|
|
Message = "Account not found.";
|
|
IsError = true;
|
|
return RedirectToPage("/Players/Index");
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(newPassword) || newPassword.Length < 6)
|
|
{
|
|
Message = "Password must be at least 6 characters.";
|
|
IsError = true;
|
|
return RedirectToPage(new { id = accountId });
|
|
}
|
|
|
|
if (newPassword != confirmPassword)
|
|
{
|
|
Message = "Passwords do not match.";
|
|
IsError = true;
|
|
return RedirectToPage(new { id = accountId });
|
|
}
|
|
|
|
_passwordHasher.Hash(newPassword, out var hash, out var salt);
|
|
account.PasswordHash = hash;
|
|
account.PasswordSalt = salt;
|
|
await _db.SaveChangesAsync();
|
|
|
|
Message = "Password has been reset successfully.";
|
|
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 });
|
|
}
|
|
}
|