Skip to content

Commit

Permalink
Add temporary node merge strategy to node post/patch permission methods
Browse files Browse the repository at this point in the history
  • Loading branch information
kezz committed Aug 25, 2023
1 parent 78e85a0 commit dd2c94c
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@
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.util.ParamUtils;
import net.luckperms.api.cacheddata.CachedMetaData;
import net.luckperms.api.messaging.MessagingService;
import net.luckperms.api.model.data.TemporaryNodeMergeStrategy;
import net.luckperms.api.model.group.Group;
import net.luckperms.api.model.group.GroupManager;
import net.luckperms.api.node.Node;
Expand Down Expand Up @@ -181,12 +183,13 @@ public void nodesGet(Context ctx) {
public void nodesAddMultiple(Context ctx) throws JsonProcessingException {
String name = ctx.pathParam("id");
List<Node> nodes = this.objectMapper.readValue(ctx.body(), new TypeReference<>(){});
TemporaryNodeMergeStrategy mergeStrategy = ParamUtils.queryParamAsTemporaryNodeMergeStrategy(this.objectMapper, ctx);

CompletableFuture<Collection<Node>> future = this.groupManager.loadGroup(name).thenCompose(opt -> {
if (opt.isPresent()) {
Group group = opt.get();
for (Node node : nodes) {
group.data().add(node);
group.data().add(node, mergeStrategy);
}
return this.groupManager.saveGroup(group).thenApply(v -> {
this.messagingService.pushUpdate();
Expand Down Expand Up @@ -244,14 +247,15 @@ public void nodesDeleteAll(Context ctx) throws JsonProcessingException {

// POST /group/{id}/nodes
@Override
public void nodesAddSingle(Context ctx) {
public void nodesAddSingle(Context ctx) throws JsonProcessingException {
String name = ctx.pathParam("id");
Node node = ctx.bodyAsClass(Node.class);
TemporaryNodeMergeStrategy mergeStrategy = ParamUtils.queryParamAsTemporaryNodeMergeStrategy(this.objectMapper, ctx);

CompletableFuture<Collection<Node>> future = this.groupManager.loadGroup(name).thenCompose(opt -> {
if (opt.isPresent()) {
Group group = opt.get();
group.data().add(node);
group.data().add(node, mergeStrategy);
return this.groupManager.saveGroup(group).thenApply(v -> {
this.messagingService.pushUpdate();
return group.getNodes();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@
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;
Expand Down Expand Up @@ -224,10 +226,11 @@ public void nodesGet(Context ctx) throws JsonProcessingException {
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);
user.data().add(node, mergeStrategy);
}
return this.userManager.saveUser(user).thenApply(v -> {
this.messagingService.pushUserUpdate(user);
Expand Down Expand Up @@ -263,9 +266,10 @@ public void nodesDeleteAll(Context ctx) throws JsonProcessingException {
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);
user.data().add(node, mergeStrategy);
return this.userManager.saveUser(user).thenApply(v -> {
this.messagingService.pushUserUpdate(user);
return user.getNodes();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* This file is part of LuckPerms, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <[email protected]>
* 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.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.javalin.http.Context;
import net.luckperms.api.model.data.TemporaryNodeMergeStrategy;

/**
* Parameter reading utilities.
*/
public final class ParamUtils {

private ParamUtils() {
}

public static TemporaryNodeMergeStrategy queryParamAsTemporaryNodeMergeStrategy(ObjectMapper objectMapper, Context ctx) throws JsonProcessingException {
final String string = ctx.queryParam("temporaryNodeMergeStrategy");

if (string == null) {
return TemporaryNodeMergeStrategy.NONE;
} else {
return objectMapper.readValue("\"" + string + "\"", TemporaryNodeMergeStrategy.class);
}
}
}
24 changes: 24 additions & 0 deletions src/main/resources/luckperms-openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@ paths:
post:
summary: Add a node to a user
operationId: add-user-node
parameters:
- $ref: '#/components/parameters/temporaryNodeMergeStrategy'
responses:
'200':
description: Ok - returns the updated nodes
Expand Down Expand Up @@ -353,6 +355,8 @@ paths:
patch:
summary: Add multiple nodes to a user
operationId: add-user-nodes
parameters:
- $ref: '#/components/parameters/temporaryNodeMergeStrategy'
responses:
'200':
description: Ok - returns the updated nodes
Expand Down Expand Up @@ -849,6 +853,8 @@ paths:
post:
summary: Add a node to a group
operationId: add-group-node
parameters:
- $ref: '#/components/parameters/temporaryNodeMergeStrategy'
responses:
'200':
description: Ok - returns the updated nodes
Expand Down Expand Up @@ -890,6 +896,8 @@ paths:
patch:
summary: Add multiple Group Nodes
operationId: add-group-nodes
parameters:
- $ref: '#/components/parameters/temporaryNodeMergeStrategy'
responses:
'200':
description: Ok - returns the updated nodes
Expand Down Expand Up @@ -1555,6 +1563,15 @@ components:
results:
$ref: '#/components/schemas/NodeMap'
description: ''
TemporaryNodeMergeStrategy:
title: TemporaryNodeMergeStrategy
type: string
description: Controls how the implementation should behave when new temporary nodes are set that would otherwise conflict with existing entries.
enum:
- add_new_duration_to_existing
- replace_existing_if_duration_longer
- none
default: 'none'
securitySchemes:
apikey:
type: http
Expand Down Expand Up @@ -1582,5 +1599,12 @@ components:
minLength: 1
required: true
description: A group name
temporaryNodeMergeStrategy:
name: temporaryNodeMergeStrategy
in: query
schema:
$ref: '#/components/schemas/TemporaryNodeMergeStrategy'
required: false
description: The node merge strategy
security:
- apikey: []

0 comments on commit dd2c94c

Please sign in to comment.