-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathAdminUserApiEndpoints.cs
More file actions
235 lines (199 loc) · 12.7 KB
/
Copy pathAdminUserApiEndpoints.cs
File metadata and controls
235 lines (199 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
using AttribDoc.Attributes;
using Bunkum.Core;
using Bunkum.Core.Endpoints;
using Bunkum.Core.Storage;
using Bunkum.Protocols.Http;
using Refresh.Common.Constants;
using Refresh.Common.Verification;
using Refresh.Core.Authentication.Permission;
using Refresh.Core.Types.Data;
using Refresh.Database;
using Refresh.Database.Models.Moderation;
using Refresh.Database.Models.Users;
using Refresh.Interfaces.APIv3.Documentation.Attributes;
using Refresh.Interfaces.APIv3.Documentation.Descriptions;
using Refresh.Interfaces.APIv3.Endpoints.ApiTypes;
using Refresh.Interfaces.APIv3.Endpoints.ApiTypes.Errors;
using Refresh.Interfaces.APIv3.Endpoints.DataTypes.Request;
using Refresh.Interfaces.APIv3.Endpoints.DataTypes.Response.Admin;
using Refresh.Interfaces.APIv3.Endpoints.DataTypes.Response.Users;
using Refresh.Interfaces.APIv3.Extensions;
namespace Refresh.Interfaces.APIv3.Endpoints.Admin;
using BC = BCrypt.Net.BCrypt;
public class AdminUserApiEndpoints : EndpointGroup
{
[ApiV3Endpoint("admin/users/{idType}/{id}"), MinimumRole(GameUserRole.Moderator)]
[DocSummary("Gets a user by their UUID or name with extended information.")]
[DocError(typeof(ApiNotFoundError), ApiNotFoundError.UserMissingErrorWhen)]
public ApiResponse<ApiExtendedGameUserResponse> GetExtendedUser(RequestContext context,
GameDatabaseContext database, DataContext dataContext,
[DocSummary(SharedParamDescriptions.UserIdParam)] string id,
[DocSummary(SharedParamDescriptions.UserIdTypeParam)] string idType)
{
GameUser? user = database.GetUserByIdAndType(idType, id);
if (user == null) return ApiNotFoundError.UserMissingError;
return ApiExtendedGameUserResponse.FromOld(user, dataContext);
}
[ApiV3Endpoint("admin/users"), MinimumRole(GameUserRole.Moderator)]
[DocSummary("Gets all users with extended information.")]
[DocUsesPageData]
public ApiListResponse<ApiExtendedGameUserResponse> GetExtendedUsers(RequestContext context,
GameDatabaseContext database, IDataStore dataStore, DataContext dataContext)
{
(int skip, int count) = context.GetPageData();
DatabaseList<ApiExtendedGameUserResponse> list = DatabaseListExtensions.FromOldList<ApiExtendedGameUserResponse, GameUser>(database.GetUsers(count, skip), dataContext);
return list;
}
[ApiV3Endpoint("admin/previousUsernames/byName/{username}"), MinimumRole(GameUserRole.Moderator)]
[DocSummary("Gets all users (with extended information) who have once used the specified username.")]
[DocUsesPageData]
public ApiListResponse<ApiExtendedPreviousUsernameResponse> GetExtendedPreviousUsernamesByUsername(RequestContext context,
GameDatabaseContext database, IDataStore dataStore, DataContext dataContext, string username)
{
(int skip, int count) = context.GetPageData();
DatabaseList<PreviousUsername> previousNames = database.GetPreviousUsernameRecordsForUsername(username, skip, count);
return DatabaseListExtensions.FromOldList<ApiExtendedPreviousUsernameResponse, PreviousUsername>(previousNames, dataContext);
}
[ApiV3Endpoint("admin/previousUsernames/byUserUuid/{uuid}"), MinimumRole(GameUserRole.Moderator)]
[DocSummary("Gets all previous usernames which have once been used by the specified user.")]
[DocUsesPageData]
public ApiListResponse<ApiExtendedPreviousUsernameResponse> GetExtendedPreviousUsernamesByUser(RequestContext context,
GameDatabaseContext database, IDataStore dataStore, DataContext dataContext, string uuid)
{
GameUser? user = database.GetUserByUuid(uuid);
if (user == null) return ApiNotFoundError.UserMissingError;
(int skip, int count) = context.GetPageData();
DatabaseList<PreviousUsername> previousNames = database.GetPreviousUsernameRecordsByUser(user, skip, count);
return DatabaseListExtensions.FromOldList<ApiExtendedPreviousUsernameResponse, PreviousUsername>(previousNames, dataContext);
}
[ApiV3Endpoint("admin/users/{idType}/{id}/resetPassword", HttpMethods.Put), MinimumRole(GameUserRole.Moderator)]
[DocSummary("Resets a user's password by their UUID or username.")]
[DocError(typeof(ApiNotFoundError), ApiNotFoundError.UserMissingErrorWhen)]
[DocError(typeof(ApiValidationError), ApiValidationError.MayNotModifyUserDueToLowRoleErrorWhen)]
[DocRequestBody(typeof(ApiResetUserPasswordRequest))]
public ApiOkResponse ResetUserPassword(RequestContext context, GameDatabaseContext database, ApiResetUserPasswordRequest body, GameUser user,
[DocSummary(SharedParamDescriptions.UserIdParam)] string id,
[DocSummary(SharedParamDescriptions.UserIdTypeParam)] string idType)
{
GameUser? targetUser = database.GetUserByIdAndType(idType, id);
if (targetUser == null) return ApiNotFoundError.UserMissingError;
if (!user.MayModifyUser(targetUser))
return ApiValidationError.MayNotModifyUserDueToLowRoleError;
if (body.PasswordSha512.Length != 128 || !CommonPatterns.Sha512Regex().IsMatch(body.PasswordSha512))
return new ApiValidationError("Password is definitely not SHA512. Please hash the password.");
string? passwordBcrypt = BC.HashPassword(body.PasswordSha512, AuthenticationApiEndpoints.WorkFactor);
if (passwordBcrypt == null) return new ApiInternalError("Could not BCrypt the given password.");
database.SetUserPassword(targetUser, passwordBcrypt, true);
return new ApiOkResponse();
}
// TODO: Users should be able to retrieve and reset their own planets
[ApiV3Endpoint("admin/users/{idType}/{id}/planets"), MinimumRole(GameUserRole.Moderator)]
[DocSummary("Retrieves the hashes of a user's planets and whether they're modded. Gets user by their UUID or username.")]
[DocError(typeof(ApiNotFoundError), ApiNotFoundError.UserMissingErrorWhen)]
public ApiResponse<ApiAdminUserPlanetsResponse> GetUserPlanets(RequestContext context, GameDatabaseContext database,
[DocSummary(SharedParamDescriptions.UserIdParam)] string id,
[DocSummary(SharedParamDescriptions.UserIdTypeParam)] string idType)
{
GameUser? user = database.GetUserByIdAndType(idType, id);
if (user == null) return ApiNotFoundError.UserMissingError;
return new ApiAdminUserPlanetsResponse
{
Lbp2PlanetsHash = user.Lbp2PlanetsHash,
Lbp3PlanetsHash = user.Lbp3PlanetsHash,
VitaPlanetsHash = user.VitaPlanetsHash,
BetaPlanetsHash = user.BetaPlanetsHash,
AreLbp2PlanetsModded = user.AreLbp2PlanetsModded,
AreLbp3PlanetsModded = user.AreLbp3PlanetsModded,
AreVitaPlanetsModded = user.AreVitaPlanetsModded,
AreBetaPlanetsModded = user.AreBetaPlanetsModded,
};
}
[ApiV3Endpoint("admin/users/{idType}/{id}/planets", HttpMethods.Delete), MinimumRole(GameUserRole.Moderator)]
[DocSummary("Resets a user's planets. Gets user by their UUID or username.")]
[DocError(typeof(ApiNotFoundError), ApiNotFoundError.UserMissingErrorWhen)]
[DocError(typeof(ApiValidationError), ApiValidationError.MayNotModifyUserDueToLowRoleErrorWhen)]
public ApiOkResponse ResetUserPlanets(RequestContext context, GameDatabaseContext database, GameUser user,
[DocSummary(SharedParamDescriptions.UserIdParam)] string id,
[DocSummary(SharedParamDescriptions.UserIdTypeParam)] string idType)
{
GameUser? targetUser = database.GetUserByIdAndType(idType, id);
if (targetUser == null) return ApiNotFoundError.UserMissingError;
if (!user.MayModifyUser(targetUser))
return ApiValidationError.MayNotModifyUserDueToLowRoleError;
database.ResetUserPlanets(targetUser);
return new ApiOkResponse();
}
[ApiV3Endpoint("admin/users/{idType}/{id}", HttpMethods.Patch), MinimumRole(GameUserRole.Moderator)]
[DocSummary("Updates the specified user's profile with the given data")]
[DocError(typeof(ApiNotFoundError), ApiNotFoundError.UserMissingErrorWhen)]
[DocError(typeof(ApiValidationError), ApiValidationError.MayNotModifyUserDueToLowRoleErrorWhen)]
[DocError(typeof(ApiValidationError), ApiValidationError.MayNotOverwriteRoleErrorWhen)]
[DocError(typeof(ApiValidationError), ApiValidationError.RoleMissingErrorWhen)]
[DocError(typeof(ApiValidationError), ApiValidationError.WrongRoleUpdateMethodErrorWhen)]
[DocError(typeof(ApiNotFoundError), ApiNotFoundError.IconMissingErrorWhen)]
[DocError(typeof(ApiValidationError), ApiValidationError.InvalidUsernameErrorWhen)]
[DocError(typeof(ApiValidationError), ApiValidationError.UsernameTakenErrorWhen)]
public ApiResponse<ApiExtendedGameUserResponse> UpdateUser(RequestContext context, GameDatabaseContext database,
GameUser user, ApiAdminUpdateUserRequest body, DataContext dataContext,
[DocSummary(SharedParamDescriptions.UserIdParam)] string id,
[DocSummary(SharedParamDescriptions.UserIdTypeParam)] string idType)
{
GameUser? targetUser = database.GetUserByIdAndType(idType, id);
if (targetUser == null)
return ApiNotFoundError.UserMissingError;
if (!user.MayModifyUser(targetUser))
return ApiValidationError.MayNotModifyUserDueToLowRoleError;
// Only admins may edit anyone's role.
// TODO: Maybe moderators should also be able to set roles, but only for users below them, and to roles below them?
if (body.Role != null)
{
if (user.Role < GameUserRole.Admin)
return ApiValidationError.MayNotOverwriteRoleError;
if (!Enum.IsDefined(typeof(GameUserRole), body.Role))
return ApiValidationError.RoleMissingError;
// All roles below regular user are special and must be given using different endpoints because they require extra information.
// Incase the implementation of #286 requires a guest role, that one will very likely be below User aswell, and it should also not
// be assignable with this endpoint (when should a user ever be demoted to a temporary guest?)
if (body.Role < GameUserRole.User)
return ApiValidationError.WrongRoleUpdateMethodError;
}
(body.IconHash, ApiError? mainIconError) = body.IconHash.ValidateIcon(dataContext);
if (mainIconError != null) return mainIconError;
(body.VitaIconHash, ApiError? vitaIconError) = body.VitaIconHash.ValidateIcon(dataContext);
if (vitaIconError != null) return vitaIconError;
(body.BetaIconHash, ApiError? betaIconError) = body.BetaIconHash.ValidateIcon(dataContext);
if (betaIconError != null) return betaIconError;
// Do nothing if the username entered is actually the same as the one already set
if (body.Username != null && body.Username != targetUser.Username)
{
if (!body.Username.StartsWith(SystemUsers.SystemPrefix) && !database.IsUsernameValid(body.Username))
return new ApiValidationError(ApiValidationError.InvalidUsernameErrorWhen
+ " Are you sure you used a PSN/RPCN username, or prepended it with ! if it's a fake user?");
if (database.IsUsernameTaken(body.Username, targetUser))
return ApiValidationError.UsernameTakenError;
database.RenameUser(targetUser, body.Username);
}
// Trim description
if (body.Description != null && body.Description.Length > UgcLimits.DescriptionLimit)
body.Description = body.Description[..UgcLimits.DescriptionLimit];
database.UpdateUserData(targetUser, body);
// TODO: In ApiV4, moderation actions should also provide reasons
database.CreateModerationAction(targetUser, ModerationActionType.UserModification, user, "");
return ApiExtendedGameUserResponse.FromOld(targetUser, dataContext);
}
[ApiV3Endpoint("admin/users/{idType}/{id}", HttpMethods.Delete), MinimumRole(GameUserRole.Moderator)]
[DocSummary("Deletes a user by their UUID or username.")]
[DocError(typeof(ApiNotFoundError), ApiNotFoundError.UserMissingErrorWhen)]
[DocError(typeof(ApiValidationError), ApiValidationError.MayNotModifyUserDueToLowRoleErrorWhen)]
public ApiOkResponse DeleteUser(RequestContext context, GameDatabaseContext database, GameUser user,
[DocSummary(SharedParamDescriptions.UserIdParam)] string id,
[DocSummary(SharedParamDescriptions.UserIdTypeParam)] string idType)
{
GameUser? targetUser = database.GetUserByIdAndType(idType, id);
if (targetUser == null) return ApiNotFoundError.UserMissingError;
if (!user.MayModifyUser(targetUser))
return ApiValidationError.MayNotModifyUserDueToLowRoleError;
database.DeleteUser(targetUser);
return new ApiOkResponse();
}
}