Add ability to change user's permissions from admin panel user list

This commit is contained in:
jvyden 2022-06-10 17:02:02 -04:00
parent ff12f5f7d5
commit 1037a6eddb
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
6 changed files with 44 additions and 3 deletions

1
.gitignore vendored
View file

@ -24,6 +24,7 @@ png/
/ProjectLighthouse/logs/*
lighthouse.config.json
lighthouse.yml
lighthouse.yml.configme
gitBranch.txt
gitVersion.txt
gitRemotes.txt

View file

@ -10,6 +10,7 @@
<Path>CONTRIBUTING.md</Path>
<Path>DatabaseMigrations</Path>
<Path>LICENSE</Path>
<Path>ProjectLighthouse.Localization</Path>
<Path>ProjectLighthouse.sln.DotSettings</Path>
<Path>ProjectLighthouse.sln.DotSettings.user</Path>
<Path>README.md</Path>

View file

@ -101,4 +101,26 @@ public class AdminUserController : ControllerBase
return this.Redirect($"/user/{targetedUser.UserId}");
}
[HttpPost("setPermissionLevel")]
public async Task<IActionResult> SetUserPermissionLevel([FromRoute] int id, [FromForm] PermissionLevel role)
{
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();
if (role != PermissionLevel.Banned)
{
targetedUser.PermissionLevel = role;
await this.database.SaveChangesAsync();
}
else
{
return this.Redirect($"/admin/user/{id}/ban");
}
return this.Redirect("/admin/users");
}
}

View file

@ -34,7 +34,7 @@ public class AdminBanUserPage : BaseLayout
this.TargetedUser = await this.Database.Users.FirstOrDefaultAsync(u => u.UserId == id);
if (this.TargetedUser == null) return this.NotFound();
this.TargetedUser.PermissionLevel = PermissionLevel.Default;
this.TargetedUser.PermissionLevel = PermissionLevel.Banned;
this.TargetedUser.BannedReason = reason;
// invalidate all currently active gametokens

View file

@ -10,7 +10,7 @@
}
<p>There are currently @Model.UserCount users registered to your instance.</p>
<p><b>Note:</b> Users are ordered by most-recent-first.</p>
<p><b>Note:</b> Users are ordered by their permissions, then by most-recent-first.</p>
<div class="ui grid">
@foreach (User user in Model.Users)
@ -56,6 +56,19 @@
<a href="/user/@user.UserId">@user.Username</a>
</h2>
<h3>@subtitle</h3>
<form method="post" action="/admin/user/@user.UserId/setPermissionLevel">
<div class="ui right action input">
<select name="role" class="ui selection dropdown">
@foreach (PermissionLevel level in Enum.GetValues<PermissionLevel>())
{
string selected = level == user.PermissionLevel ? " selected" : "";
<option value="@((int)level)"@selected>@level.ToString()</option>
}
</select>
<input type="submit" class="ui green button" value="Apply"/>
</div>
</form>
</div>
</div>
}

View file

@ -21,7 +21,11 @@ public class AdminPanelUsersPage : BaseLayout
if (user == null) return this.Redirect("~/login");
if (!user.IsAdmin) return this.NotFound();
this.Users = await this.Database.Users.OrderByDescending(u => u.UserId).ToListAsync();
this.Users = await this.Database.Users
.OrderByDescending(u => u.PermissionLevel)
.ThenByDescending(u => u.UserId)
.ToListAsync();
this.UserCount = this.Users.Count;
return this.Page();