mirror of
https://github.com/LuckPerms/LuckPerms.git
synced 2024-11-24 03:25:19 +01:00
Account for 'allow-invalid-usernames' setting in API calls
This commit is contained in:
parent
7e50c3c777
commit
879a88a398
@ -27,19 +27,24 @@ package me.lucko.luckperms.common.api;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
|
||||
import me.lucko.luckperms.common.config.ConfigKeys;
|
||||
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
|
||||
import me.lucko.luckperms.common.storage.misc.DataConstraints;
|
||||
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public final class ApiUtils {
|
||||
|
||||
public static String checkUsername(String s) {
|
||||
public static String checkUsername(String s, LuckPermsPlugin plugin) {
|
||||
if (s == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Preconditions.checkArgument(
|
||||
DataConstraints.PLAYER_USERNAME_TEST.test(s),
|
||||
"Invalid username entry '" + s + "'. Usernames must be less than 16 chars and only contain 'a-z A-Z 1-9 _'."
|
||||
);
|
||||
Predicate<String> test = plugin.getConfiguration().get(ConfigKeys.ALLOW_INVALID_USERNAMES) ?
|
||||
DataConstraints.PLAYER_USERNAME_TEST_LENIENT :
|
||||
DataConstraints.PLAYER_USERNAME_TEST;
|
||||
|
||||
Preconditions.checkArgument(test.test(s), "Invalid username entry: " + s);
|
||||
return s;
|
||||
}
|
||||
|
||||
@ -48,10 +53,7 @@ public final class ApiUtils {
|
||||
return null;
|
||||
}
|
||||
|
||||
Preconditions.checkArgument(
|
||||
DataConstraints.GROUP_NAME_TEST.test(s),
|
||||
"Invalid name entry '" + s + "'. Names must be less than 37 chars and only contain 'a-z A-Z 1-9'."
|
||||
);
|
||||
Preconditions.checkArgument(DataConstraints.GROUP_NAME_TEST.test(s), "Invalid name entry: " + s);
|
||||
return s.toLowerCase();
|
||||
}
|
||||
|
||||
|
@ -27,6 +27,7 @@ package me.lucko.luckperms.common.api.implementation;
|
||||
|
||||
import me.lucko.luckperms.api.LogEntry;
|
||||
import me.lucko.luckperms.common.actionlog.Log;
|
||||
import me.lucko.luckperms.common.api.ApiUtils;
|
||||
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
@ -34,8 +35,6 @@ import java.util.Objects;
|
||||
import java.util.SortedSet;
|
||||
import java.util.UUID;
|
||||
|
||||
import static me.lucko.luckperms.common.api.ApiUtils.checkName;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public class ApiLog implements me.lucko.luckperms.api.Log {
|
||||
private final Log handle;
|
||||
@ -64,12 +63,12 @@ public class ApiLog implements me.lucko.luckperms.api.Log {
|
||||
@Override
|
||||
public @NonNull SortedSet<LogEntry> getGroupHistory(@NonNull String name) {
|
||||
Objects.requireNonNull(name, "name");
|
||||
return (SortedSet) this.handle.getGroupHistory(checkName(name));
|
||||
return (SortedSet) this.handle.getGroupHistory(ApiUtils.checkName(name));
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull SortedSet<LogEntry> getTrackHistory(@NonNull String name) {
|
||||
Objects.requireNonNull(name, "name");
|
||||
return (SortedSet) this.handle.getTrackHistory(checkName(name));
|
||||
return (SortedSet) this.handle.getTrackHistory(ApiUtils.checkName(name));
|
||||
}
|
||||
}
|
||||
|
@ -33,6 +33,7 @@ import me.lucko.luckperms.api.Track;
|
||||
import me.lucko.luckperms.api.User;
|
||||
import me.lucko.luckperms.api.event.cause.CreationCause;
|
||||
import me.lucko.luckperms.api.event.cause.DeletionCause;
|
||||
import me.lucko.luckperms.common.api.ApiUtils;
|
||||
import me.lucko.luckperms.common.bulkupdate.comparison.Constraint;
|
||||
import me.lucko.luckperms.common.bulkupdate.comparison.StandardComparison;
|
||||
import me.lucko.luckperms.common.node.factory.NodeFactory;
|
||||
@ -50,9 +51,6 @@ import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static me.lucko.luckperms.common.api.ApiUtils.checkName;
|
||||
import static me.lucko.luckperms.common.api.ApiUtils.checkUsername;
|
||||
|
||||
public class ApiStorage implements me.lucko.luckperms.api.Storage {
|
||||
private static final Function<Throwable, Boolean> CONSUME_EXCEPTION = throwable -> {
|
||||
throwable.printStackTrace();
|
||||
@ -114,7 +112,7 @@ public class ApiStorage implements me.lucko.luckperms.api.Storage {
|
||||
@Override
|
||||
public @NonNull CompletableFuture<Boolean> loadUser(@NonNull UUID uuid, String username) {
|
||||
Objects.requireNonNull(uuid, "uuid");
|
||||
username = checkUsername(username);
|
||||
username = ApiUtils.checkUsername(username, this.plugin);
|
||||
|
||||
if (this.plugin.getUserManager().getIfLoaded(uuid) == null) {
|
||||
this.plugin.getUserManager().getHouseKeeper().registerApiUsage(uuid);
|
||||
@ -147,7 +145,7 @@ public class ApiStorage implements me.lucko.luckperms.api.Storage {
|
||||
@Override
|
||||
public @NonNull CompletableFuture<Boolean> createAndLoadGroup(@NonNull String name) {
|
||||
Objects.requireNonNull(name, "name");
|
||||
return this.handle.createAndLoadGroup(checkName(name), CreationCause.API)
|
||||
return this.handle.createAndLoadGroup(ApiUtils.checkName(name), CreationCause.API)
|
||||
.thenApply(r -> true)
|
||||
.exceptionally(consumeExceptionToFalse());
|
||||
}
|
||||
@ -155,7 +153,7 @@ public class ApiStorage implements me.lucko.luckperms.api.Storage {
|
||||
@Override
|
||||
public @NonNull CompletableFuture<Boolean> loadGroup(@NonNull String name) {
|
||||
Objects.requireNonNull(name, "name");
|
||||
return this.handle.loadGroup(checkName(name))
|
||||
return this.handle.loadGroup(ApiUtils.checkName(name))
|
||||
.thenApply(Optional::isPresent)
|
||||
.exceptionally(consumeExceptionToFalse());
|
||||
}
|
||||
@ -195,7 +193,7 @@ public class ApiStorage implements me.lucko.luckperms.api.Storage {
|
||||
@Override
|
||||
public @NonNull CompletableFuture<Boolean> createAndLoadTrack(@NonNull String name) {
|
||||
Objects.requireNonNull(name, "name");
|
||||
return this.handle.createAndLoadTrack(checkName(name), CreationCause.API)
|
||||
return this.handle.createAndLoadTrack(ApiUtils.checkName(name), CreationCause.API)
|
||||
.thenApply(r -> true)
|
||||
.exceptionally(consumeExceptionToFalse());
|
||||
}
|
||||
@ -203,7 +201,7 @@ public class ApiStorage implements me.lucko.luckperms.api.Storage {
|
||||
@Override
|
||||
public @NonNull CompletableFuture<Boolean> loadTrack(@NonNull String name) {
|
||||
Objects.requireNonNull(name, "name");
|
||||
return this.handle.loadTrack(checkName(name))
|
||||
return this.handle.loadTrack(ApiUtils.checkName(name))
|
||||
.thenApply(Optional::isPresent)
|
||||
.exceptionally(consumeExceptionToFalse());
|
||||
}
|
||||
@ -235,7 +233,7 @@ public class ApiStorage implements me.lucko.luckperms.api.Storage {
|
||||
public @NonNull CompletableFuture<Boolean> saveUUIDData(@NonNull String username, @NonNull UUID uuid) {
|
||||
Objects.requireNonNull(username, "username");
|
||||
Objects.requireNonNull(uuid, "uuid");
|
||||
return this.handle.savePlayerData(uuid, checkUsername(username))
|
||||
return this.handle.savePlayerData(uuid, ApiUtils.checkUsername(username, this.plugin))
|
||||
.thenApply(r -> true)
|
||||
.exceptionally(consumeExceptionToFalse());
|
||||
}
|
||||
@ -243,7 +241,7 @@ public class ApiStorage implements me.lucko.luckperms.api.Storage {
|
||||
@Override
|
||||
public @NonNull CompletableFuture<UUID> getUUID(@NonNull String username) {
|
||||
Objects.requireNonNull(username, "username");
|
||||
return this.handle.getPlayerUuid(checkUsername(username));
|
||||
return this.handle.getPlayerUuid(ApiUtils.checkUsername(username, this.plugin));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -61,7 +61,7 @@ public class ApiUserManager extends ApiAbstractManager<User, me.lucko.luckperms.
|
||||
@Override
|
||||
public @NonNull CompletableFuture<me.lucko.luckperms.api.User> loadUser(@NonNull UUID uuid, @Nullable String username) {
|
||||
Objects.requireNonNull(uuid, "uuid");
|
||||
ApiUtils.checkUsername(username);
|
||||
ApiUtils.checkUsername(username, this.plugin);
|
||||
|
||||
if (this.plugin.getUserManager().getIfLoaded(uuid) == null) {
|
||||
this.plugin.getUserManager().getHouseKeeper().registerApiUsage(uuid);
|
||||
|
Loading…
Reference in New Issue
Block a user