-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathUserController.java
More file actions
425 lines (372 loc) · 16.5 KB
/
UserController.java
File metadata and controls
425 lines (372 loc) · 16.5 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*
* This file is part of LuckPerms, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <luck@lucko.me>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.lucko.luckperms.extension.rest.controller;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.javalin.http.Context;
import me.lucko.luckperms.extension.rest.RestConfig;
import me.lucko.luckperms.extension.rest.model.PermissionCheckRequest;
import me.lucko.luckperms.extension.rest.model.PermissionCheckResult;
import me.lucko.luckperms.extension.rest.model.SearchRequest;
import me.lucko.luckperms.extension.rest.model.TrackRequest;
import me.lucko.luckperms.extension.rest.model.UserLookupResult;
import me.lucko.luckperms.extension.rest.model.UserSearchResult;
import me.lucko.luckperms.extension.rest.util.ParamUtils;
import net.luckperms.api.cacheddata.CachedMetaData;
import net.luckperms.api.context.ContextSet;
import net.luckperms.api.context.ImmutableContextSet;
import net.luckperms.api.messaging.MessagingService;
import net.luckperms.api.model.PermissionHolder;
import net.luckperms.api.model.PlayerSaveResult;
import net.luckperms.api.model.data.TemporaryNodeMergeStrategy;
import net.luckperms.api.model.user.User;
import net.luckperms.api.model.user.UserManager;
import net.luckperms.api.node.Node;
import net.luckperms.api.node.matcher.NodeMatcher;
import net.luckperms.api.query.QueryMode;
import net.luckperms.api.query.QueryOptions;
import net.luckperms.api.track.DemotionResult;
import net.luckperms.api.track.PromotionResult;
import net.luckperms.api.track.Track;
import net.luckperms.api.track.TrackManager;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
public class UserController implements PermissionHolderController {
private static final boolean CACHE = RestConfig.getBoolean("cache.users", true);
private final UserManager userManager;
private final TrackManager trackManager;
private final MessagingService messagingService;
private final ObjectMapper objectMapper;
public UserController(UserManager userManager, TrackManager trackManager, MessagingService messagingService, ObjectMapper objectMapper) {
this.userManager = userManager;
this.trackManager = trackManager;
this.messagingService = messagingService;
this.objectMapper = objectMapper;
}
private UUID parseUuid(String s) throws JsonProcessingException {
String uuidString = "\"" + s + "\"";
return this.objectMapper.readValue(uuidString, UUID.class);
}
private UUID pathParamAsUuid(Context ctx) throws JsonProcessingException {
return parseUuid(ctx.pathParam("id"));
}
private CompletableFuture<User> loadUserCached(UUID uniqueId) {
if (CACHE) {
User user = this.userManager.getUser(uniqueId);
if (user != null) {
return CompletableFuture.completedFuture(user);
}
}
return this.userManager.loadUser(uniqueId);
}
// POST /user
@Override
public void create(Context ctx) {
CreateReq body = ctx.bodyAsClass(CreateReq.class);
CompletableFuture<PlayerSaveResult> future = this.userManager.savePlayerData(body.uniqueId, body.username);
ctx.future(future, result -> {
if (((PlayerSaveResult) result).includes(PlayerSaveResult.Outcome.CLEAN_INSERT)) {
ctx.status(201);
}
ctx.json(result);
});
}
record CreateReq(@JsonProperty(required = true) UUID uniqueId, @JsonProperty(required = true) String username) { }
// GET /user
@Override
public void getAll(Context ctx) {
CompletableFuture<Set<UUID>> future = this.userManager.getUniqueUsers();
ctx.future(future);
}
// GET /user/search
@Override
public void search(Context ctx) throws Exception {
NodeMatcher<? extends Node> matcher = SearchRequest.parse(ctx);
CompletableFuture<List<UserSearchResult>> future = this.userManager.<Node>searchAll(matcher)
.thenApply(map -> map.entrySet().stream()
.map(e -> new UserSearchResult(e.getKey(), e.getValue()))
.toList()
);
ctx.future(future);
}
// GET /user/lookup
public void lookup(Context ctx) throws Exception {
String usernameParam = ctx.queryParam("username");
String uniqueIdParam = ctx.queryParam("uniqueId");
CompletableFuture<UserLookupResult> future;
if (usernameParam != null && !usernameParam.isEmpty()) {
future = this.userManager.lookupUniqueId(usernameParam)
.thenApply(uniqueId -> uniqueId == null
? null
: new UserLookupResult(usernameParam, uniqueId));
} else if (uniqueIdParam != null && !uniqueIdParam.isEmpty()) {
UUID parsedUniqueId = parseUuid(uniqueIdParam);
future = this.userManager.lookupUsername(parsedUniqueId)
.thenApply(username -> username == null || username.isEmpty()
? null
: new UserLookupResult(username, parsedUniqueId));
} else {
throw new IllegalArgumentException("Must specify username or unique id");
}
ctx.future(future, result -> {
if (result == null) {
ctx.status(404);
} else {
ctx.json(result);
}
});
}
// GET /user/{id}
@Override
public void get(Context ctx) throws JsonProcessingException {
UUID uniqueId = pathParamAsUuid(ctx);
ctx.future(loadUserCached(uniqueId), result -> {
if (result == null) {
ctx.status(404);
} else {
ctx.json(result);
}
});
}
// PATCH /user/{id}
@Override
public void update(Context ctx) throws JsonProcessingException {
UUID uniqueId = pathParamAsUuid(ctx);
UpdateReq body = ctx.bodyAsClass(UpdateReq.class);
ctx.future(this.userManager.savePlayerData(uniqueId, body.username), result -> ctx.result("ok"));
}
record UpdateReq(@JsonProperty(required = true) String username) { }
// DELETE /user/{id}
@Override
public void delete(Context ctx) throws JsonProcessingException {
UUID uniqueId = pathParamAsUuid(ctx);
boolean playerDataOnly = ctx.queryParamAsClass("playerDataOnly", Boolean.class).getOrDefault(false);
CompletableFuture<Void> future;
if (playerDataOnly) {
future = this.userManager.deletePlayerData(uniqueId);
} else {
future = this.userManager.loadUser(uniqueId).thenCompose(user -> {
user.data().clear();
return this.userManager.saveUser(user).thenCompose(ignored -> {
this.messagingService.pushUserUpdate(user);
return this.userManager.deletePlayerData(uniqueId);
});
});
}
ctx.future(future, result -> ctx.result("ok"));
}
// GET /user/{id}/nodes
@Override
public void nodesGet(Context ctx) throws JsonProcessingException {
UUID uniqueId = pathParamAsUuid(ctx);
CompletableFuture<Collection<Node>> future = loadUserCached(uniqueId).thenApply(PermissionHolder::getNodes);
ctx.future(future, result -> {
if (result == null) {
ctx.status(404);
} else {
ctx.json(result);
}
});
}
// PATCH /user/{id}/nodes
@Override
public void nodesAddMultiple(Context ctx) throws JsonProcessingException {
UUID uniqueId = pathParamAsUuid(ctx);
List<Node> nodes = this.objectMapper.readValue(ctx.body(), new TypeReference<>(){});
TemporaryNodeMergeStrategy mergeStrategy = ParamUtils.queryParamAsTemporaryNodeMergeStrategy(this.objectMapper, ctx);
CompletableFuture<Collection<Node>> future = this.userManager.loadUser(uniqueId).thenCompose(user -> {
for (Node node : nodes) {
user.data().add(node, mergeStrategy);
}
return this.userManager.saveUser(user).thenApply(v -> {
this.messagingService.pushUserUpdate(user);
return user.getNodes();
});
});
ctx.future(future);
}
// DELETE /user/{id}/nodes
@Override
public void nodesDeleteAll(Context ctx) throws JsonProcessingException {
UUID uniqueId = pathParamAsUuid(ctx);
List<Node> nodes = ctx.body().isEmpty()
? null
: this.objectMapper.readValue(ctx.body(), new TypeReference<>(){});
CompletableFuture<?> future = this.userManager.loadUser(uniqueId).thenCompose(user -> {
if (nodes == null) {
user.data().clear();
} else {
for (Node node : nodes) {
user.data().remove(node);
}
}
return this.userManager.saveUser(user).thenApply(v -> {
this.messagingService.pushUserUpdate(user);
return user.getNodes();
});
});
ctx.future(future, result -> ctx.result("ok"));
}
// POST /user/{id}/nodes
@Override
public void nodesAddSingle(Context ctx) throws JsonProcessingException {
UUID uniqueId = pathParamAsUuid(ctx);
Node node = ctx.bodyAsClass(Node.class);
TemporaryNodeMergeStrategy mergeStrategy = ParamUtils.queryParamAsTemporaryNodeMergeStrategy(this.objectMapper, ctx);
CompletableFuture<Collection<Node>> future = this.userManager.loadUser(uniqueId).thenCompose(user -> {
user.data().add(node, mergeStrategy);
return this.userManager.saveUser(user).thenApply(v -> {
this.messagingService.pushUserUpdate(user);
return user.getNodes();
});
});
ctx.future(future);
}
// PUT /user/{id}/nodes
@Override
public void nodesSet(Context ctx) throws JsonProcessingException {
UUID uniqueId = pathParamAsUuid(ctx);
List<Node> nodes = this.objectMapper.readValue(ctx.body(), new TypeReference<>(){});
CompletableFuture<Collection<Node>> future = this.userManager.loadUser(uniqueId).thenCompose(user -> {
user.data().clear();
for (Node node : nodes) {
user.data().add(node);
}
return this.userManager.saveUser(user).thenApply(v -> {
this.messagingService.pushUserUpdate(user);
return user.getNodes();
});
});
ctx.future(future);
}
// GET /user/{id}/meta
@Override
public void metaGet(Context ctx) throws JsonProcessingException {
UUID uniqueId = pathParamAsUuid(ctx);
String contextJson = ctx.queryParam("context");
ContextSet contextSet = contextJson != null ? this.objectMapper.readValue(contextJson, ContextSet.class) : null;
CompletableFuture<CachedMetaData> future = loadUserCached(uniqueId)
.thenApply(user -> {
if (contextSet != null) {
return user.getCachedData().getMetaData(QueryOptions.builder(QueryMode.CONTEXTUAL).context(contextSet).build());
}
return user.getCachedData().getMetaData();
});
ctx.future(future);
}
// GET /user/{id}/permission-check
@Override
public void permissionCheck(Context ctx) throws JsonProcessingException {
UUID uniqueId = pathParamAsUuid(ctx);
String permission = ctx.queryParam("permission");
if (permission == null || permission.isEmpty()) {
throw new IllegalArgumentException("Missing permission");
}
CompletableFuture<PermissionCheckResult> future = loadUserCached(uniqueId)
.thenApply(user -> user.getCachedData().getPermissionData().queryPermission(permission))
.thenApply(PermissionCheckResult::from);
ctx.future(future);
}
// POST /user/{id}/permission-check
@Override
public void permissionCheckCustom(Context ctx) throws JsonProcessingException {
UUID uniqueId = pathParamAsUuid(ctx);
PermissionCheckRequest req = ctx.bodyAsClass(PermissionCheckRequest.class);
if (req.permission() == null || req.permission().isEmpty()) {
throw new IllegalArgumentException("Missing permission");
}
CompletableFuture<PermissionCheckResult> future = this.userManager.loadUser(uniqueId)
.thenApply(user -> {
QueryOptions options = req.queryOptions();
if (options == null) {
options = user.getQueryOptions();
}
return user.getCachedData().getPermissionData(options).queryPermission(req.permission());
})
.thenApply(PermissionCheckResult::from);
ctx.future(future);
}
// POST /user/{id}/promote
@Override
public void promote(Context ctx) throws Exception {
UUID uniqueId = pathParamAsUuid(ctx);
TrackRequest req = ctx.bodyAsClass(TrackRequest.class);
if (req.track() == null || req.track().isEmpty()) {
throw new IllegalArgumentException("Missing track");
}
ContextSet context = req.context() == null ? ImmutableContextSet.empty() : req.context();
CompletableFuture<PromotionResult> future = this.trackManager.loadTrack(req.track()).thenCompose(opt -> {
if (opt.isPresent()) {
Track track = opt.get();
return this.userManager.loadUser(uniqueId).thenCompose(user -> {
PromotionResult result = track.promote(user, context);
return this.userManager.saveUser(user).thenApply(x -> result);
});
} else {
return CompletableFuture.completedFuture(null);
}
});
ctx.future(future, result -> {
if (result == null) {
ctx.status(404);
} else {
ctx.json(result);
}
});
}
// POST /user/{id}/demote
@Override
public void demote(Context ctx) throws Exception {
UUID uniqueId = pathParamAsUuid(ctx);
TrackRequest req = ctx.bodyAsClass(TrackRequest.class);
if (req.track() == null || req.track().isEmpty()) {
throw new IllegalArgumentException("Missing track");
}
ContextSet context = req.context() == null ? ImmutableContextSet.empty() : req.context();
CompletableFuture<DemotionResult> future = this.trackManager.loadTrack(req.track()).thenCompose(opt -> {
if (opt.isPresent()) {
Track track = opt.get();
return this.userManager.loadUser(uniqueId).thenCompose(user -> {
DemotionResult result = track.demote(user, context);
return this.userManager.saveUser(user).thenApply(x -> result);
});
} else {
return CompletableFuture.completedFuture(null);
}
});
ctx.future(future, result -> {
if (result == null) {
ctx.status(404);
} else {
ctx.json(result);
}
});
}
}