Skip to content

Commit f7d6ce2

Browse files
committed
Only allow punished users to be pardoned
1 parent 20ae5ea commit f7d6ce2

3 files changed

Lines changed: 44 additions & 3 deletions

File tree

Refresh.Interfaces.APIv3/Endpoints/Admin/AdminUserPunishmentApiEndpoints.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,16 @@ public ApiOkResponse RestrictUser(RequestContext context, GameDatabaseContext da
5555
[ApiV3Endpoint("admin/users/{idType}/{id}/pardon", HttpMethods.Post), MinimumRole(GameUserRole.Moderator)]
5656
[DocSummary("Pardons all punishments for the given user.")]
5757
[DocError(typeof(ApiNotFoundError), ApiNotFoundError.UserMissingErrorWhen)]
58-
[DocError(typeof(ApiValidationError), ApiValidationError.MayNotModifyUserDueToLowRoleErrorWhen)]
58+
[DocError(typeof(ApiValidationError), ApiValidationError.UserIsAlreadyPardonedErrorWhen)]
5959
public ApiOkResponse PardonUser(RequestContext context, GameDatabaseContext database, GameUser user,
6060
[DocSummary(SharedParamDescriptions.UserIdParam)] string id,
6161
[DocSummary(SharedParamDescriptions.UserIdTypeParam)] string idType)
6262
{
6363
GameUser? targetUser = database.GetUserByIdAndType(idType, id);
6464
if (targetUser == null) return ApiNotFoundError.UserMissingError;
6565

66-
if (!user.MayModifyUser(targetUser))
67-
return ApiValidationError.MayNotModifyUserDueToLowRoleError;
66+
if (targetUser.Role > GameUserRole.Restricted)
67+
return ApiValidationError.UserIsAlreadyPardonedError;
6868

6969
database.SetUserRole(targetUser, GameUserRole.User);
7070
return new ApiOkResponse();

Refresh.Interfaces.APIv3/Endpoints/ApiTypes/Errors/ApiValidationError.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ public class ApiValidationError : ApiError
7171
public const string WrongRoleUpdateMethodErrorWhen = "The specified role cannot be assigned to the user using this endpoint.";
7272
public static readonly ApiValidationError WrongRoleUpdateMethodError = new(WrongRoleUpdateMethodErrorWhen);
7373

74+
public const string UserIsAlreadyPardonedErrorWhen = "This user has no punishments, they are already pardoned.";
75+
public static readonly ApiValidationError UserIsAlreadyPardonedError = new(UserIsAlreadyPardonedErrorWhen);
76+
7477
public const string RoleMissingErrorWhen = "The specified role does not exist.";
7578
public static readonly ApiValidationError RoleMissingError = new(RoleMissingErrorWhen);
7679

RefreshTests.GameServer/Tests/ApiV3/AdminUserManagementApiTests.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,44 @@ public async Task ModeratorsMayNotRestrictAdminsAndModerators()
223223
Assert.That(updated!.Role, Is.EqualTo(GameUserRole.Restricted));
224224
}
225225

226+
[Test]
227+
public async Task CanOnlyPardonPunishedUsers()
228+
{
229+
using TestContext context = this.GetServer();
230+
GameUser admin = context.CreateUser(role: GameUserRole.Admin);
231+
GameUser mod = context.CreateUser(role: GameUserRole.Moderator);
232+
GameUser user = context.CreateUser(role: GameUserRole.User);
233+
GameUser restricted = context.CreateUser(role: GameUserRole.Restricted);
234+
HttpClient client = context.GetAuthenticatedClient(TokenType.Api, mod);
235+
236+
// Admin
237+
HttpResponseMessage response = await client.PostAsync($"/api/v3/admin/users/uuid/{admin.UserId}/pardon", null);
238+
Assert.That(response.IsSuccessStatusCode, Is.False);
239+
context.Database.Refresh();
240+
241+
GameUser? updated = context.Database.GetUserByObjectId(admin.UserId);
242+
Assert.That(updated, Is.Not.Null);
243+
Assert.That(updated!.Role, Is.EqualTo(GameUserRole.Admin));
244+
245+
// User
246+
response = await client.PostAsync($"/api/v3/admin/users/uuid/{user.UserId}/pardon", null);
247+
Assert.That(response.IsSuccessStatusCode, Is.False);
248+
context.Database.Refresh();
249+
250+
updated = context.Database.GetUserByObjectId(user.UserId);
251+
Assert.That(updated, Is.Not.Null);
252+
Assert.That(updated!.Role, Is.EqualTo(GameUserRole.User));
253+
254+
// Restricted
255+
response = await client.PostAsync($"/api/v3/admin/users/uuid/{restricted.UserId}/pardon", null);
256+
Assert.That(response.IsSuccessStatusCode, Is.True);
257+
context.Database.Refresh();
258+
259+
updated = context.Database.GetUserByObjectId(restricted.UserId);
260+
Assert.That(updated, Is.Not.Null);
261+
Assert.That(updated!.Role, Is.EqualTo(GameUserRole.User));
262+
}
263+
226264
[Test]
227265
public async Task ModeratorsMayNotResetPasswordOfAdminsAndModerators()
228266
{

0 commit comments

Comments
 (0)