Search in sources :

Example 1 with WhitelistService

use of org.spongepowered.api.service.whitelist.WhitelistService in project LanternServer by LanternPowered.

the class CommandWhitelist method completeSpec.

@Override
public void completeSpec(PluginContainer pluginContainer, CommandSpec.Builder specBuilder) {
    specBuilder.child(CommandSpec.builder().arguments(GenericArguments.string(Text.of("player"))).executor((src, args) -> {
        final String playerName = args.<String>getOne("player").get();
        final WhitelistService service = Sponge.getServiceManager().provideUnchecked(WhitelistService.class);
        Lantern.getGame().getGameProfileManager().get(playerName).whenComplete((profile, error) -> {
            if (error != null) {
                src.sendMessage(t("commands.whitelist.add.failed", playerName));
            } else {
                src.sendMessage(t("commands.whitelist.add.success", playerName));
                service.addProfile(((LanternGameProfile) profile).withoutProperties());
            }
        });
        return CommandResult.success();
    }).build(), "add").child(CommandSpec.builder().arguments(GenericArguments.string(Text.of("player"))).executor((src, args) -> {
        final String playerName = args.<String>getOne("player").get();
        final WhitelistService service = Sponge.getServiceManager().provideUnchecked(WhitelistService.class);
        Lantern.getGame().getGameProfileManager().get(playerName).whenComplete((profile, error) -> {
            if (error != null || !service.isWhitelisted(profile = ((LanternGameProfile) profile).withoutProperties())) {
                src.sendMessage(t("commands.whitelist.remove.failed", playerName));
            } else {
                src.sendMessage(t("commands.whitelist.remove.success", playerName));
                service.removeProfile(profile);
            }
        });
        return CommandResult.success();
    }).build(), "remove").child(CommandSpec.builder().executor((src, args) -> {
        final WhitelistService service = Sponge.getServiceManager().provideUnchecked(WhitelistService.class);
        final List<String> whitelisted = service.getWhitelistedProfiles().stream().map(p -> p.getName().get()).collect(Collectors.toList());
        src.sendMessage(t("commands.whitelist.list", whitelisted.size(), Sponge.getServer().getOnlinePlayers().size()));
        src.sendMessage(Text.of(Joiner.on(", ").join(whitelisted)));
        return CommandResult.success();
    }).build(), "list").child(CommandSpec.builder().executor((src, args) -> {
        Lantern.getGame().getGlobalConfig().setWhitelistEnabled(true);
        src.sendMessage(t("commands.whitelist.enabled"));
        return CommandResult.success();
    }).build(), "on").child(CommandSpec.builder().executor((src, args) -> {
        Lantern.getGame().getGlobalConfig().setWhitelistEnabled(false);
        src.sendMessage(t("commands.whitelist.disabled"));
        return CommandResult.success();
    }).build(), "off").child(CommandSpec.builder().executor((src, args) -> {
        final WhitelistService service = Sponge.getServiceManager().provideUnchecked(WhitelistService.class);
        if (service instanceof Reloadable) {
            try {
                ((Reloadable) service).reload();
            } catch (Exception e) {
                throw new CommandException(t("commands.whitelist.reload.failed", e.getMessage()), e);
            }
        } else {
            src.sendMessage(t("commands.whitelist.reload.not_supported"));
            return CommandResult.empty();
        }
        return CommandResult.success();
    }).build(), "reload");
}
Also used : CommandResult(org.spongepowered.api.command.CommandResult) TranslationHelper.t(org.lanternpowered.server.text.translation.TranslationHelper.t) Sponge(org.spongepowered.api.Sponge) GenericArguments(org.spongepowered.api.command.args.GenericArguments) Collectors(java.util.stream.Collectors) LanternGameProfile(org.lanternpowered.server.profile.LanternGameProfile) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) WhitelistService(org.spongepowered.api.service.whitelist.WhitelistService) CommandException(org.spongepowered.api.command.CommandException) List(java.util.List) Text(org.spongepowered.api.text.Text) Lantern(org.lanternpowered.server.game.Lantern) Reloadable(org.lanternpowered.server.util.Reloadable) PluginContainer(org.spongepowered.api.plugin.PluginContainer) Joiner(com.google.common.base.Joiner) WhitelistService(org.spongepowered.api.service.whitelist.WhitelistService) LanternGameProfile(org.lanternpowered.server.profile.LanternGameProfile) List(java.util.List) CommandException(org.spongepowered.api.command.CommandException) Reloadable(org.lanternpowered.server.util.Reloadable) CommandException(org.spongepowered.api.command.CommandException)

Example 2 with WhitelistService

use of org.spongepowered.api.service.whitelist.WhitelistService in project LanternServer by LanternPowered.

the class NetworkSession method isWhitelisted.

private static boolean isWhitelisted(GameProfile gameProfile) {
    if (!Lantern.getGame().getGlobalConfig().isWhitelistEnabled()) {
        return true;
    }
    final WhitelistService whitelistService = Sponge.getServiceManager().provideUnchecked(WhitelistService.class);
    if (whitelistService.isWhitelisted(gameProfile)) {
        return true;
    }
    final PermissionService permissionService = Sponge.getServiceManager().provideUnchecked(PermissionService.class);
    return permissionService.getUserSubjects().getSubject(gameProfile.getUniqueId().toString()).map(subject -> subject.hasPermission(Permissions.Login.BYPASS_WHITELIST_PERMISSION)).orElse(false);
}
Also used : PermissionService(org.spongepowered.api.service.permission.PermissionService) AttributeKey(io.netty.util.AttributeKey) LanternEntity(org.lanternpowered.server.entity.LanternEntity) EventContextKeys(org.spongepowered.api.event.cause.EventContextKeys) SocketAddress(java.net.SocketAddress) ScheduledFuture(io.netty.util.concurrent.ScheduledFuture) EntityProtocolManager(org.lanternpowered.server.network.entity.EntityProtocolManager) LanternTexts(org.lanternpowered.server.text.LanternTexts) PermissionService(org.spongepowered.api.service.permission.PermissionService) WorldConfig(org.lanternpowered.server.config.world.WorldConfig) Ban(org.spongepowered.api.util.ban.Ban) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) CodecException(io.netty.handler.codec.CodecException) ChannelPromise(io.netty.channel.ChannelPromise) MessageInOutKeepAlive(org.lanternpowered.server.network.vanilla.message.type.connection.MessageInOutKeepAlive) GameProfile(org.spongepowered.api.profile.GameProfile) DecoderException(io.netty.handler.codec.DecoderException) TranslationHelper.t(org.lanternpowered.server.text.translation.TranslationHelper.t) LanternServer(org.lanternpowered.server.LanternServer) Sponge(org.spongepowered.api.Sponge) Set(java.util.Set) EventLoop(io.netty.channel.EventLoop) Instant(java.time.Instant) InetSocketAddress(java.net.InetSocketAddress) Protocol(org.lanternpowered.server.network.protocol.Protocol) PlayerConnection(org.spongepowered.api.network.PlayerConnection) Preconditions.checkState(com.google.common.base.Preconditions.checkState) LanternWorldProperties(org.lanternpowered.server.world.LanternWorldProperties) ClientConnectionEvent(org.spongepowered.api.event.network.ClientConnectionEvent) Cause(org.spongepowered.api.event.cause.Cause) List(java.util.List) Lantern(org.lanternpowered.server.game.Lantern) Transform(org.spongepowered.api.entity.Transform) ReferenceCountUtil(io.netty.util.ReferenceCountUtil) World(org.spongepowered.api.world.World) Optional(java.util.Optional) Queue(java.util.Queue) MessageRegistration(org.lanternpowered.server.network.message.MessageRegistration) EventContext(org.spongepowered.api.event.cause.EventContext) EntityProtocolTypes(org.lanternpowered.server.network.entity.EntityProtocolTypes) Permissions(org.lanternpowered.server.permission.Permissions) Keys(org.spongepowered.api.data.key.Keys) Handler(org.lanternpowered.server.network.message.handler.Handler) Vector3d(com.flowpowered.math.vector.Vector3d) MessageOutDisconnect(org.lanternpowered.server.network.vanilla.message.type.connection.MessageOutDisconnect) GameModes(org.spongepowered.api.entity.living.player.gamemode.GameModes) ProtocolState(org.lanternpowered.server.network.protocol.ProtocolState) WhitelistService(org.spongepowered.api.service.whitelist.WhitelistService) HashSet(java.util.HashSet) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) HandlerMessage(org.lanternpowered.server.network.message.HandlerMessage) CauseStack(org.lanternpowered.server.event.CauseStack) ImmutableList(com.google.common.collect.ImmutableList) Text(org.spongepowered.api.text.Text) ChannelFutureListener(io.netty.channel.ChannelFutureListener) LanternWorld(org.lanternpowered.server.world.LanternWorld) GlobalTabList(org.lanternpowered.server.entity.living.player.tab.GlobalTabList) Nullable(javax.annotation.Nullable) AsyncHelper(org.lanternpowered.server.network.message.AsyncHelper) TranslationHelper.tr(org.lanternpowered.server.text.translation.TranslationHelper.tr) NullMessage(org.lanternpowered.server.network.message.NullMessage) Iterator(java.util.Iterator) SpongeEventFactory(org.spongepowered.api.event.SpongeEventFactory) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) MoreObjects(com.google.common.base.MoreObjects) TimeoutException(io.netty.handler.timeout.TimeoutException) IOException(java.io.IOException) LanternGameProfile(org.lanternpowered.server.profile.LanternGameProfile) ConcurrentLinkedDeque(java.util.concurrent.ConcurrentLinkedDeque) ChannelFuture(io.netty.channel.ChannelFuture) Message(org.lanternpowered.server.network.message.Message) Channel(io.netty.channel.Channel) TimeUnit(java.util.concurrent.TimeUnit) SimpleChannelInboundHandler(io.netty.channel.SimpleChannelInboundHandler) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer) DateTimeFormatter(java.time.format.DateTimeFormatter) MessageChannel(org.spongepowered.api.text.channel.MessageChannel) MessageEvent(org.spongepowered.api.event.message.MessageEvent) BulkMessage(org.lanternpowered.server.network.message.BulkMessage) BanService(org.spongepowered.api.service.ban.BanService) WhitelistService(org.spongepowered.api.service.whitelist.WhitelistService)

Example 3 with WhitelistService

use of org.spongepowered.api.service.whitelist.WhitelistService in project LanternServer by LanternPowered.

the class LanternUserStorageService method getFromWhitelistService.

/**
 * Attempts to get a {@link User} from the {@link WhitelistService}.
 *
 * @param uniqueId The unique id
 * @return The user
 */
@Nullable
private ProxyUser getFromWhitelistService(UUID uniqueId) {
    final LanternGameProfile gameProfile;
    final WhitelistService whitelistService = this.whitelistService.get();
    if (whitelistService instanceof WhitelistConfig) {
        gameProfile = ((WhitelistConfig) whitelistService).getEntryByUUID(uniqueId).map(UserEntry::getProfile).orElse(null);
    } else {
        gameProfile = (LanternGameProfile) whitelistService.getWhitelistedProfiles().stream().filter(profile -> profile.getUniqueId().equals(uniqueId)).findFirst().orElse(null);
    }
    return gameProfile == null ? null : new ProxyUser(gameProfile);
}
Also used : WhitelistService(org.spongepowered.api.service.whitelist.WhitelistService) WhitelistConfig(org.lanternpowered.server.config.user.WhitelistConfig) LanternGameProfile(org.lanternpowered.server.profile.LanternGameProfile) UserEntry(org.lanternpowered.server.config.user.UserEntry) ProxyUser(org.lanternpowered.server.entity.living.player.ProxyUser) Nullable(javax.annotation.Nullable)

Aggregations

LanternGameProfile (org.lanternpowered.server.profile.LanternGameProfile)3 WhitelistService (org.spongepowered.api.service.whitelist.WhitelistService)3 List (java.util.List)2 Nullable (javax.annotation.Nullable)2 Lantern (org.lanternpowered.server.game.Lantern)2 TranslationHelper.t (org.lanternpowered.server.text.translation.TranslationHelper.t)2 Sponge (org.spongepowered.api.Sponge)2 Text (org.spongepowered.api.text.Text)2 Vector3d (com.flowpowered.math.vector.Vector3d)1 Joiner (com.google.common.base.Joiner)1 MoreObjects (com.google.common.base.MoreObjects)1 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 ImmutableList (com.google.common.collect.ImmutableList)1 Channel (io.netty.channel.Channel)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 ChannelPromise (io.netty.channel.ChannelPromise)1