187 lines
5.8 KiB
C#
187 lines
5.8 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using DataServer.Data;
|
|
using DataServer.Data.Entities;
|
|
using Server.Services.Auth;
|
|
|
|
namespace Server.Services.Admin.Pages.Players;
|
|
|
|
public class EditModel : AdminPageModel
|
|
{
|
|
private readonly GameDbContext _db;
|
|
private readonly IPasswordHasher _passwordHasher;
|
|
|
|
public EditModel(GameDbContext db, IPasswordHasher passwordHasher)
|
|
{
|
|
_db = db;
|
|
_passwordHasher = passwordHasher;
|
|
}
|
|
|
|
public Account Account { get; set; } = new();
|
|
public List<DungeonCharacter> Characters { get; set; } = new();
|
|
|
|
public bool IsNew => Account.Id == 0;
|
|
public string? Message { get; set; }
|
|
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;
|
|
|
|
// 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)
|
|
{
|
|
Account = account;
|
|
Characters = await _db.DungeonCharacters
|
|
.Where(c => c.AccountId == accountId && !c.IsDeleted)
|
|
.OrderByDescending(c => c.Level)
|
|
.ToListAsync();
|
|
Message = "Password must be at least 6 characters.";
|
|
IsError = true;
|
|
return Page();
|
|
}
|
|
|
|
if (newPassword != confirmPassword)
|
|
{
|
|
Account = account;
|
|
Characters = await _db.DungeonCharacters
|
|
.Where(c => c.AccountId == accountId && !c.IsDeleted)
|
|
.OrderByDescending(c => c.Level)
|
|
.ToListAsync();
|
|
Message = "Passwords do not match.";
|
|
IsError = true;
|
|
return Page();
|
|
}
|
|
|
|
_passwordHasher.Hash(newPassword, out var hash, out var salt);
|
|
account.PasswordHash = hash;
|
|
account.PasswordSalt = salt;
|
|
await _db.SaveChangesAsync();
|
|
|
|
Account = account;
|
|
Characters = await _db.DungeonCharacters
|
|
.Where(c => c.AccountId == accountId && !c.IsDeleted)
|
|
.OrderByDescending(c => c.Level)
|
|
.ToListAsync();
|
|
|
|
Message = "Password has been reset.";
|
|
return Page();
|
|
}
|
|
}
|