mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-05-18 07:32:27 +00:00
Use permission level model for permissions
This is better. Because having 20 fucking columns for permissions is STUPID.
This commit is contained in:
parent
1b5e58dd80
commit
8301418085
16 changed files with 542 additions and 433 deletions
|
@ -74,7 +74,7 @@ public class LoginController : ControllerBase
|
|||
|
||||
User? user = await this.database.UserFromGameToken(token, true);
|
||||
|
||||
if (user == null || user.Banned)
|
||||
if (user == null || user.IsBanned)
|
||||
{
|
||||
Logger.Error($"Unable to find user {npTicket.Username} from token", LogArea.Login);
|
||||
return this.StatusCode(403, "");
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#nullable enable
|
||||
using LBPUnion.ProjectLighthouse.Administration;
|
||||
using LBPUnion.ProjectLighthouse.Files;
|
||||
using LBPUnion.ProjectLighthouse.Logging;
|
||||
using LBPUnion.ProjectLighthouse.PlayerData.Profiles;
|
||||
|
@ -28,7 +29,7 @@ public class AdminUserController : ControllerBase
|
|||
User? targetedUser = await this.database.Users.FirstOrDefaultAsync(u => u.UserId == id);
|
||||
if (targetedUser == null) return this.NotFound();
|
||||
|
||||
targetedUser.Banned = false;
|
||||
targetedUser.PermissionLevel = PermissionLevel.Default;
|
||||
targetedUser.BannedReason = null;
|
||||
|
||||
await this.database.SaveChangesAsync();
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#nullable enable
|
||||
using LBPUnion.ProjectLighthouse.Administration;
|
||||
using LBPUnion.ProjectLighthouse.PlayerData.Profiles;
|
||||
using LBPUnion.ProjectLighthouse.Servers.Website.Pages.Layouts;
|
||||
using LBPUnion.ProjectLighthouse.Types;
|
||||
|
@ -33,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.Banned = true;
|
||||
this.TargetedUser.PermissionLevel = PermissionLevel.Default;
|
||||
this.TargetedUser.BannedReason = reason;
|
||||
|
||||
// invalidate all currently active gametokens
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
@page "/admin/users"
|
||||
@using LBPUnion.ProjectLighthouse.Administration
|
||||
@using LBPUnion.ProjectLighthouse.PlayerData.Profiles
|
||||
@using LBPUnion.ProjectLighthouse.Types
|
||||
@model LBPUnion.ProjectLighthouse.Servers.Website.Pages.Admin.AdminPanelUsersPage
|
||||
|
@ -14,17 +15,37 @@
|
|||
<div class="ui grid">
|
||||
@foreach (User user in Model.Users)
|
||||
{
|
||||
string color = "blue";
|
||||
string subtitle = "User";
|
||||
if (user.IsAdmin)
|
||||
string color;
|
||||
string subtitle;
|
||||
|
||||
switch (user.PermissionLevel)
|
||||
{
|
||||
color = "yellow";
|
||||
subtitle = "Admin";
|
||||
}
|
||||
if (user.Banned)
|
||||
// jank but works
|
||||
case PermissionLevel.Banned:
|
||||
{
|
||||
color = "red";
|
||||
subtitle = $"Banned user! Reason: {user.BannedReason}";
|
||||
break;
|
||||
}
|
||||
case PermissionLevel.Moderator:
|
||||
{
|
||||
color = "green";
|
||||
subtitle = "Moderator";
|
||||
break;
|
||||
}
|
||||
case PermissionLevel.Administrator:
|
||||
{
|
||||
color = "yellow";
|
||||
subtitle = "Admin";
|
||||
break;
|
||||
}
|
||||
case PermissionLevel.Default:
|
||||
default:
|
||||
{
|
||||
color = "blue";
|
||||
subtitle = "User";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
subtitle += $" (id: {user.UserId})";
|
||||
|
|
|
@ -57,7 +57,7 @@ public class LoginForm : BaseLayout
|
|||
return this.Page();
|
||||
}
|
||||
|
||||
if (user.Banned)
|
||||
if (user.IsBanned)
|
||||
{
|
||||
Logger.Warn($"User {user.Username} (id: {user.UserId}) failed to login on web due to being banned", LogArea.Login);
|
||||
this.Error = "You have been banned. Please contact an administrator for more information.\nReason: " + user.BannedReason;
|
||||
|
|
|
@ -13,7 +13,7 @@
|
|||
Model.Description = Model.ProfileUser!.Biography;
|
||||
}
|
||||
|
||||
@if (Model.ProfileUser.Banned)
|
||||
@if (Model.ProfileUser.IsBanned)
|
||||
{
|
||||
<div class="ui inverted red segment">
|
||||
<h2>User is currently banned!</h2>
|
||||
|
@ -120,7 +120,7 @@
|
|||
<div class="ui yellow segment">
|
||||
<h2>Admin Options</h2>
|
||||
|
||||
@if (!Model.ProfileUser.Banned)
|
||||
@if (!Model.ProfileUser.IsBanned)
|
||||
{
|
||||
<div>
|
||||
<a class="ui red button" href="/admin/user/@Model.ProfileUser.UserId/ban">
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#nullable enable
|
||||
using LBPUnion.ProjectLighthouse.Administration;
|
||||
using LBPUnion.ProjectLighthouse.Configuration;
|
||||
using LBPUnion.ProjectLighthouse.PlayerData.Profiles;
|
||||
using LBPUnion.ProjectLighthouse.Servers.Website.Pages.Layouts;
|
||||
|
@ -29,14 +30,14 @@ public class UsersPage : BaseLayout
|
|||
|
||||
this.SearchValue = name.Replace(" ", string.Empty);
|
||||
|
||||
this.UserCount = await this.Database.Users.CountAsync(u => !u.Banned && u.Username.Contains(this.SearchValue));
|
||||
this.UserCount = await this.Database.Users.CountAsync(u => u.PermissionLevel != PermissionLevel.Banned && u.Username.Contains(this.SearchValue));
|
||||
|
||||
this.PageNumber = pageNumber;
|
||||
this.PageAmount = Math.Max(1, (int)Math.Ceiling((double)this.UserCount / ServerStatics.PageSize));
|
||||
|
||||
if (this.PageNumber < 0 || this.PageNumber >= this.PageAmount) return this.Redirect($"/users/{Math.Clamp(this.PageNumber, 0, this.PageAmount - 1)}");
|
||||
|
||||
this.Users = await this.Database.Users.Where(u => !u.Banned && u.Username.Contains(this.SearchValue))
|
||||
this.Users = await this.Database.Users.Where(u => u.PermissionLevel != PermissionLevel.Banned && u.Username.Contains(this.SearchValue))
|
||||
.OrderByDescending(b => b.UserId)
|
||||
.Skip(pageNumber * ServerStatics.PageSize)
|
||||
.Take(ServerStatics.PageSize)
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using LBPUnion.ProjectLighthouse;
|
||||
using LBPUnion.ProjectLighthouse.Administration;
|
||||
using LBPUnion.ProjectLighthouse.Helpers;
|
||||
using LBPUnion.ProjectLighthouse.PlayerData;
|
||||
using LBPUnion.ProjectLighthouse.PlayerData.Profiles;
|
||||
|
@ -28,7 +29,7 @@ public class AdminTests : LighthouseWebTest
|
|||
};
|
||||
|
||||
database.WebTokens.Add(webToken);
|
||||
user.IsAdmin = true;
|
||||
user.PermissionLevel = PermissionLevel.Administrator;
|
||||
await database.SaveChangesAsync();
|
||||
|
||||
this.Driver.Navigate().GoToUrl(this.BaseAddress + "/");
|
||||
|
@ -52,7 +53,7 @@ public class AdminTests : LighthouseWebTest
|
|||
};
|
||||
|
||||
database.WebTokens.Add(webToken);
|
||||
user.IsAdmin = false;
|
||||
user.PermissionLevel = PermissionLevel.Default;
|
||||
await database.SaveChangesAsync();
|
||||
|
||||
this.Driver.Navigate().GoToUrl(this.BaseAddress + "/");
|
||||
|
|
|
@ -35,7 +35,7 @@ public class MakeUserAdminCommand : ICommand
|
|||
return;
|
||||
}
|
||||
|
||||
user.IsAdmin = true;
|
||||
user.PermissionLevel = PermissionLevel.Administrator;
|
||||
await this.database.SaveChangesAsync();
|
||||
|
||||
logger.LogSuccess($"The user {user.Username} (id: {user.UserId}) is now an admin.", LogArea.Command);
|
||||
|
|
9
ProjectLighthouse/Administration/PermissionLevel.cs
Normal file
9
ProjectLighthouse/Administration/PermissionLevel.cs
Normal file
|
@ -0,0 +1,9 @@
|
|||
namespace LBPUnion.ProjectLighthouse.Administration;
|
||||
|
||||
public enum PermissionLevel
|
||||
{
|
||||
Banned = 0,
|
||||
Default = 1,
|
||||
Moderator = 2,
|
||||
Administrator = 3,
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using LBPUnion.ProjectLighthouse.Administration;
|
||||
using LBPUnion.ProjectLighthouse.PlayerData;
|
||||
using LBPUnion.ProjectLighthouse.Types;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
@ -18,7 +19,7 @@ public static class StatisticsHelper
|
|||
|
||||
public static async Task<int> SlotCount() => await database.Slots.CountAsync();
|
||||
|
||||
public static async Task<int> UserCount() => await database.Users.CountAsync(u => !u.Banned);
|
||||
public static async Task<int> UserCount() => await database.Users.CountAsync(u => u.PermissionLevel != PermissionLevel.Banned);
|
||||
|
||||
public static async Task<int> TeamPickCount() => await database.Slots.CountAsync(s => s.TeamPick);
|
||||
|
||||
|
|
|
@ -0,0 +1,56 @@
|
|||
|
||||
|
||||
#nullable disable
|
||||
|
||||
using LBPUnion.ProjectLighthouse;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
namespace ProjectLighthouse.Migrations
|
||||
{
|
||||
[DbContext(typeof(Database))]
|
||||
[Migration("20220522192158_SwitchToPermissionLevels")]
|
||||
public class SwitchToPermissionLevels : Migration
|
||||
{
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.AddColumn<int>(name: "PermissionLevel",
|
||||
table: "Users",
|
||||
type: "int",
|
||||
nullable: false,
|
||||
defaultValue: 1);
|
||||
|
||||
migrationBuilder.Sql("UPDATE Users SET PermissionLevel = 3 WHERE IsAdmin");
|
||||
migrationBuilder.Sql("UPDATE Users SET PermissionLevel = 0 WHERE Banned");
|
||||
|
||||
migrationBuilder.DropColumn(
|
||||
name: "Banned",
|
||||
table: "Users");
|
||||
|
||||
migrationBuilder.DropColumn(
|
||||
name: "IsAdmin",
|
||||
table: "Users");
|
||||
}
|
||||
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropColumn(
|
||||
name: "PermissionLevel",
|
||||
table: "Users");
|
||||
|
||||
migrationBuilder.AddColumn<bool>(
|
||||
name: "Banned",
|
||||
table: "Users",
|
||||
type: "tinyint(1)",
|
||||
nullable: false,
|
||||
defaultValue: false);
|
||||
|
||||
migrationBuilder.AddColumn<bool>(
|
||||
name: "IsAdmin",
|
||||
table: "Users",
|
||||
type: "tinyint(1)",
|
||||
nullable: false,
|
||||
defaultValue: false);
|
||||
}
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load diff
|
@ -3,6 +3,7 @@ using System.Diagnostics.CodeAnalysis;
|
|||
using System.Linq;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Xml.Serialization;
|
||||
using LBPUnion.ProjectLighthouse.Administration;
|
||||
using LBPUnion.ProjectLighthouse.Configuration;
|
||||
using LBPUnion.ProjectLighthouse.Serialization;
|
||||
using LBPUnion.ProjectLighthouse.Types;
|
||||
|
@ -15,7 +16,6 @@ public class User
|
|||
[NotMapped]
|
||||
[JsonIgnore]
|
||||
private Database? _database;
|
||||
#nullable disable
|
||||
|
||||
[NotMapped]
|
||||
[JsonIgnore]
|
||||
|
@ -123,9 +123,6 @@ public class User
|
|||
[JsonIgnore]
|
||||
public int Hearts => this.database.HeartedProfiles.Count(s => s.HeartedUserId == this.UserId);
|
||||
|
||||
[JsonIgnore]
|
||||
public bool IsAdmin { get; set; } = false;
|
||||
|
||||
[JsonIgnore]
|
||||
public bool PasswordResetRequired { get; set; }
|
||||
|
||||
|
@ -138,10 +135,19 @@ public class User
|
|||
public UserStatus Status => new(this.database, this.UserId);
|
||||
|
||||
[JsonIgnore]
|
||||
public bool Banned { get; set; }
|
||||
public bool IsBanned => this.PermissionLevel == PermissionLevel.Banned;
|
||||
|
||||
[JsonIgnore]
|
||||
public string BannedReason { get; set; }
|
||||
public bool IsModerator => this.PermissionLevel == PermissionLevel.Moderator;
|
||||
|
||||
[JsonIgnore]
|
||||
public bool IsAdmin => this.PermissionLevel == PermissionLevel.Administrator;
|
||||
|
||||
[JsonIgnore]
|
||||
public PermissionLevel PermissionLevel { get; set; } = PermissionLevel.Default;
|
||||
|
||||
[JsonIgnore]
|
||||
public string? BannedReason { get; set; }
|
||||
|
||||
public string Serialize(GameVersion gameVersion = GameVersion.LittleBigPlanet1)
|
||||
{
|
||||
|
|
|
@ -50,6 +50,10 @@
|
|||
</EmbeddedResource>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Remove="Migrations\20220522192158_SwitchToPermissionLevels.Designer.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
|
||||
<Exec Command="git describe --long --always --dirty --exclude=\* --abbrev=8 > "$(ProjectDir)/gitVersion.txt"" />
|
||||
<Exec Command="git branch --show-current > "$(ProjectDir)/gitBranch.txt"" />
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
#!/bin/bash
|
||||
|
||||
export LIGHTHOUSE_DB_CONNECTION_STRING='server=127.0.0.1;uid=root;pwd=lighthouse;database=lighthouse'
|
||||
dotnet ef migrations add "$1" --project ProjectLighthouse
|
||||
dotnet ef migrations add "$1" --project ../ProjectLighthouse
|
Loading…
Add table
Add a link
Reference in a new issue