mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-05-18 23:42:28 +00:00
Add admin button to wipe a user's earth decorations
This commit is contained in:
parent
9f1547e344
commit
33f344f200
3 changed files with 79 additions and 2 deletions
|
@ -1,8 +1,10 @@
|
||||||
#nullable enable
|
#nullable enable
|
||||||
|
using LBPUnion.ProjectLighthouse.Files;
|
||||||
|
using LBPUnion.ProjectLighthouse.Logging;
|
||||||
using LBPUnion.ProjectLighthouse.PlayerData.Profiles;
|
using LBPUnion.ProjectLighthouse.PlayerData.Profiles;
|
||||||
using LBPUnion.ProjectLighthouse.Types;
|
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using IOFile = System.IO.File;
|
||||||
|
|
||||||
namespace LBPUnion.ProjectLighthouse.Servers.Website.Controllers.Admin;
|
namespace LBPUnion.ProjectLighthouse.Servers.Website.Controllers.Admin;
|
||||||
|
|
||||||
|
@ -24,7 +26,6 @@ public class AdminUserController : ControllerBase
|
||||||
if (user == null || !user.IsAdmin) return this.NotFound();
|
if (user == null || !user.IsAdmin) return this.NotFound();
|
||||||
|
|
||||||
User? targetedUser = await this.database.Users.FirstOrDefaultAsync(u => u.UserId == id);
|
User? targetedUser = await this.database.Users.FirstOrDefaultAsync(u => u.UserId == id);
|
||||||
|
|
||||||
if (targetedUser == null) return this.NotFound();
|
if (targetedUser == null) return this.NotFound();
|
||||||
|
|
||||||
targetedUser.Banned = false;
|
targetedUser.Banned = false;
|
||||||
|
@ -33,4 +34,70 @@ public class AdminUserController : ControllerBase
|
||||||
await this.database.SaveChangesAsync();
|
await this.database.SaveChangesAsync();
|
||||||
return this.Redirect($"/user/{targetedUser.UserId}");
|
return this.Redirect($"/user/{targetedUser.UserId}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Resets the user's earth decorations to a blank state. Useful for users who abuse audio for example.
|
||||||
|
/// </summary>
|
||||||
|
[HttpGet("wipePlanets")]
|
||||||
|
public async Task<IActionResult> WipePlanets([FromRoute] int id) {
|
||||||
|
User? user = this.database.UserFromWebRequest(this.Request);
|
||||||
|
if (user == null || !user.IsAdmin) return this.NotFound();
|
||||||
|
|
||||||
|
User? targetedUser = await this.database.Users.FirstOrDefaultAsync(u => u.UserId == id);
|
||||||
|
if (targetedUser == null) return this.NotFound();
|
||||||
|
|
||||||
|
string[] hashes = {
|
||||||
|
targetedUser.PlanetHashLBP2,
|
||||||
|
targetedUser.PlanetHashLBP3,
|
||||||
|
targetedUser.PlanetHashLBPVita,
|
||||||
|
};
|
||||||
|
|
||||||
|
// This will also wipe users' earth with the same hashes.
|
||||||
|
foreach (string hash in hashes)
|
||||||
|
{
|
||||||
|
// Don't try to remove empty hashes. That's a horrible idea.
|
||||||
|
if (string.IsNullOrWhiteSpace(hash)) continue;
|
||||||
|
|
||||||
|
// Find users with a matching hash
|
||||||
|
List<User> users = await this.database.Users
|
||||||
|
.Where(u => u.PlanetHashLBP2 == hash ||
|
||||||
|
u.PlanetHashLBP3 == hash ||
|
||||||
|
u.PlanetHashLBPVita == hash)
|
||||||
|
.ToListAsync();
|
||||||
|
|
||||||
|
// We should match at least the targeted user...
|
||||||
|
System.Diagnostics.Debug.Assert(users.Count != 0);
|
||||||
|
|
||||||
|
// Reset each users' hash.
|
||||||
|
foreach (User userWithPlanet in users)
|
||||||
|
{
|
||||||
|
userWithPlanet.PlanetHashLBP2 = "";
|
||||||
|
userWithPlanet.PlanetHashLBP3 = "";
|
||||||
|
userWithPlanet.PlanetHashLBPVita = "";
|
||||||
|
Logger.Success($"Deleted planets for {userWithPlanet.Username} (id:{userWithPlanet.UserId})", LogArea.Admin);
|
||||||
|
}
|
||||||
|
|
||||||
|
// And finally, attempt to remove the resource from the filesystem. We don't want that taking up space.
|
||||||
|
try
|
||||||
|
{
|
||||||
|
IOFile.Delete(FileHelper.GetResourcePath(hash));
|
||||||
|
Logger.Success($"Deleted planet resource {hash}",
|
||||||
|
LogArea.Admin);
|
||||||
|
}
|
||||||
|
catch(DirectoryNotFoundException)
|
||||||
|
{
|
||||||
|
// This is certainly a strange case, but it's not worth doing anything about since we were about
|
||||||
|
// to delete the file anyways. Carry on~
|
||||||
|
}
|
||||||
|
catch(Exception e)
|
||||||
|
{
|
||||||
|
// Welp, guess I'll die then. We tried~
|
||||||
|
Logger.Error($"Failed to delete planet resource {hash}\n{e}", LogArea.Admin);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.database.SaveChangesAsync();
|
||||||
|
|
||||||
|
return this.Redirect($"/user/{targetedUser.UserId}");
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -130,6 +130,15 @@
|
||||||
</div>
|
</div>
|
||||||
<div class="ui fitted hidden divider"></div>
|
<div class="ui fitted hidden divider"></div>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<a class="ui red button" href="/admin/user/@Model.ProfileUser.UserId/wipePlanets">
|
||||||
|
<i class="trash alternate icon"></i>
|
||||||
|
<span>Wipe user's earth decorations</span>
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
<div class="ui fitted hidden divider"></div>
|
||||||
|
|
||||||
@await Html.PartialAsync("Partials/AdminSetGrantedSlotsFormPartial", Model.ProfileUser)
|
@await Html.PartialAsync("Partials/AdminSetGrantedSlotsFormPartial", Model.ProfileUser)
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
|
@ -20,4 +20,5 @@ public enum LogArea
|
||||||
Logger,
|
Logger,
|
||||||
Redis,
|
Redis,
|
||||||
Command,
|
Command,
|
||||||
|
Admin,
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue