Search in sources :

Example 31 with Group

use of me.lucko.luckperms.common.model.Group in project LuckPerms by lucko.

the class HolderShowTracks method execute.

@Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, T holder, List<String> args, String label) {
    if (ArgumentPermissions.checkViewPerms(plugin, sender, getPermission().get(), holder)) {
        Message.COMMAND_NO_PERMISSION.send(sender);
        return CommandResult.NO_PERMISSION;
    }
    try {
        plugin.getStorage().loadAllTracks().get();
    } catch (Exception e) {
        e.printStackTrace();
        Message.TRACKS_LOAD_ERROR.send(sender);
        return CommandResult.LOADING_ERROR;
    }
    List<Map.Entry<Track, String>> lines = new ArrayList<>();
    if (holder.getType().isUser()) {
        // if the holder is a user, we want to query parent groups for tracks
        Set<Node> nodes = holder.getEnduringNodes().values().stream().filter(Node::isGroupNode).filter(Node::getValuePrimitive).filter(Node::isPermanent).collect(Collectors.toSet());
        for (Node node : nodes) {
            String groupName = node.getGroupName();
            List<Track> tracks = plugin.getTrackManager().getAll().values().stream().filter(t -> t.containsGroup(groupName)).collect(Collectors.toList());
            for (Track t : tracks) {
                lines.add(Maps.immutableEntry(t, MessageUtils.getAppendableNodeContextString(node) + "\n" + MessageUtils.listToArrowSep(t.getGroups(), groupName)));
            }
        }
    } else {
        // otherwise, just lookup for the actual group
        String groupName = ((Group) holder).getName();
        List<Track> tracks = plugin.getTrackManager().getAll().values().stream().filter(t -> t.containsGroup(groupName)).collect(Collectors.toList());
        for (Track t : tracks) {
            lines.add(Maps.immutableEntry(t, MessageUtils.listToArrowSep(t.getGroups(), groupName)));
        }
    }
    if (lines.isEmpty()) {
        Message.LIST_TRACKS_EMPTY.send(sender, holder.getFriendlyName());
        return CommandResult.SUCCESS;
    }
    Message.LIST_TRACKS.send(sender, holder.getFriendlyName());
    for (Map.Entry<Track, String> line : lines) {
        Message.LIST_TRACKS_ENTRY.send(sender, line.getKey().getName(), line.getValue());
    }
    return CommandResult.SUCCESS;
}
Also used : PermissionHolder(me.lucko.luckperms.common.model.PermissionHolder) SubCommand(me.lucko.luckperms.common.command.abstraction.SubCommand) MessageUtils(me.lucko.luckperms.common.command.utils.MessageUtils) LocaleManager(me.lucko.luckperms.common.locale.LocaleManager) Set(java.util.Set) Sender(me.lucko.luckperms.common.sender.Sender) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) CommandResult(me.lucko.luckperms.common.command.CommandResult) ArrayList(java.util.ArrayList) CommandPermission(me.lucko.luckperms.common.command.access.CommandPermission) CommandSpec(me.lucko.luckperms.common.locale.command.CommandSpec) Track(me.lucko.luckperms.common.model.Track) List(java.util.List) Map(java.util.Map) Message(me.lucko.luckperms.common.locale.message.Message) Group(me.lucko.luckperms.common.model.Group) LuckPermsPlugin(me.lucko.luckperms.common.plugin.LuckPermsPlugin) Node(me.lucko.luckperms.api.Node) ArgumentPermissions(me.lucko.luckperms.common.command.access.ArgumentPermissions) Predicates(me.lucko.luckperms.common.utils.Predicates) Group(me.lucko.luckperms.common.model.Group) Node(me.lucko.luckperms.api.Node) ArrayList(java.util.ArrayList) Map(java.util.Map) Track(me.lucko.luckperms.common.model.Track)

Example 32 with Group

use of me.lucko.luckperms.common.model.Group in project LuckPerms by lucko.

the class ParentAdd method execute.

@Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List<String> args, String label, CommandPermission permission) throws CommandException {
    if (ArgumentPermissions.checkModifyPerms(plugin, sender, permission, holder)) {
        Message.COMMAND_NO_PERMISSION.send(sender);
        return CommandResult.NO_PERMISSION;
    }
    String groupName = ArgumentParser.parseName(0, args);
    MutableContextSet context = ArgumentParser.parseContext(1, args, plugin);
    Group group = StorageAssistant.loadGroup(groupName, sender, plugin, false);
    if (group == null) {
        return CommandResult.INVALID_ARGS;
    }
    if (ArgumentPermissions.checkContext(plugin, sender, permission, context)) {
        Message.COMMAND_NO_PERMISSION.send(sender);
        return CommandResult.NO_PERMISSION;
    }
    if (ArgumentPermissions.checkArguments(plugin, sender, permission, group.getName())) {
        Message.COMMAND_NO_PERMISSION.send(sender);
        return CommandResult.NO_PERMISSION;
    }
    DataMutateResult result = holder.setPermission(NodeFactory.buildGroupNode(group.getName()).withExtraContext(context).build());
    if (result.asBoolean()) {
        Message.SET_INHERIT_SUCCESS.send(sender, holder.getFriendlyName(), group.getFriendlyName(), MessageUtils.contextSetToString(context));
        ExtendedLogEntry.build().actor(sender).acted(holder).action("parent", "add", group.getName(), context).build().submit(plugin, sender);
        StorageAssistant.save(holder, sender, plugin);
        return CommandResult.SUCCESS;
    } else {
        Message.ALREADY_INHERITS.send(sender, holder.getFriendlyName(), group.getFriendlyName(), MessageUtils.contextSetToString(context));
        return CommandResult.STATE_ERROR;
    }
}
Also used : Group(me.lucko.luckperms.common.model.Group) MutableContextSet(me.lucko.luckperms.api.context.MutableContextSet) DataMutateResult(me.lucko.luckperms.api.DataMutateResult)

Example 33 with Group

use of me.lucko.luckperms.common.model.Group in project LuckPerms by lucko.

the class ParentSet method execute.

@Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List<String> args, String label, CommandPermission permission) throws CommandException {
    if (ArgumentPermissions.checkModifyPerms(plugin, sender, permission, holder)) {
        Message.COMMAND_NO_PERMISSION.send(sender);
        return CommandResult.NO_PERMISSION;
    }
    String groupName = ArgumentParser.parseName(0, args);
    MutableContextSet context = ArgumentParser.parseContext(1, args, plugin);
    Group group = StorageAssistant.loadGroup(groupName, sender, plugin, false);
    if (group == null) {
        return CommandResult.LOADING_ERROR;
    }
    if (ArgumentPermissions.checkContext(plugin, sender, permission, context)) {
        Message.COMMAND_NO_PERMISSION.send(sender);
        return CommandResult.NO_PERMISSION;
    }
    if (ArgumentPermissions.checkArguments(plugin, sender, permission, group.getName())) {
        Message.COMMAND_NO_PERMISSION.send(sender);
        return CommandResult.NO_PERMISSION;
    }
    holder.clearParents(context, false);
    holder.setPermission(NodeFactory.buildGroupNode(group.getName()).withExtraContext(context).build());
    if (holder.getType().isUser()) {
        ((User) holder).getPrimaryGroup().setStoredValue(group.getName());
    }
    Message.SET_PARENT_SUCCESS.send(sender, holder.getFriendlyName(), group.getFriendlyName(), MessageUtils.contextSetToString(context));
    ExtendedLogEntry.build().actor(sender).acted(holder).action("parent", "set", group.getName(), context).build().submit(plugin, sender);
    StorageAssistant.save(holder, sender, plugin);
    return CommandResult.SUCCESS;
}
Also used : Group(me.lucko.luckperms.common.model.Group) MutableContextSet(me.lucko.luckperms.api.context.MutableContextSet)

Example 34 with Group

use of me.lucko.luckperms.common.model.Group in project LuckPerms by lucko.

the class ParentSetTrack method execute.

@Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List<String> args, String label, CommandPermission permission) throws CommandException {
    if (ArgumentPermissions.checkModifyPerms(plugin, sender, permission, holder)) {
        Message.COMMAND_NO_PERMISSION.send(sender);
        return CommandResult.NO_PERMISSION;
    }
    final String trackName = args.get(0).toLowerCase();
    if (!DataConstraints.TRACK_NAME_TEST.test(trackName)) {
        Message.TRACK_INVALID_ENTRY.send(sender, trackName);
        return CommandResult.INVALID_ARGS;
    }
    Track track = StorageAssistant.loadTrack(trackName, sender, plugin);
    if (track == null) {
        return CommandResult.LOADING_ERROR;
    }
    if (track.getSize() <= 1) {
        Message.TRACK_EMPTY.send(sender, track.getName());
        return CommandResult.STATE_ERROR;
    }
    int index = ArgumentParser.parseIntOrElse(1, args, -1);
    String groupName;
    if (index > 0) {
        List<String> trackGroups = track.getGroups();
        if ((index - 1) >= trackGroups.size()) {
            Message.DOES_NOT_EXIST.send(sender, index);
            return CommandResult.INVALID_ARGS;
        }
        groupName = track.getGroups().get(index - 1);
    } else {
        groupName = ArgumentParser.parseName(1, args);
        if (!track.containsGroup(groupName)) {
            Message.TRACK_DOES_NOT_CONTAIN.send(sender, track.getName(), groupName);
            return CommandResult.INVALID_ARGS;
        }
    }
    MutableContextSet context = ArgumentParser.parseContext(2, args, plugin);
    Group group = StorageAssistant.loadGroup(groupName, sender, plugin, false);
    if (group == null) {
        return CommandResult.LOADING_ERROR;
    }
    if (ArgumentPermissions.checkContext(plugin, sender, permission, context)) {
        Message.COMMAND_NO_PERMISSION.send(sender);
        return CommandResult.NO_PERMISSION;
    }
    if (ArgumentPermissions.checkArguments(plugin, sender, permission, track.getName(), group.getName())) {
        Message.COMMAND_NO_PERMISSION.send(sender);
        return CommandResult.NO_PERMISSION;
    }
    holder.removeIf(node -> node.isGroupNode() && node.getFullContexts().equals(context) && track.containsGroup(node.getGroupName()));
    holder.setPermission(NodeFactory.buildGroupNode(group.getName()).withExtraContext(context).build());
    Message.SET_TRACK_PARENT_SUCCESS.send(sender, holder.getFriendlyName(), track.getName(), group.getFriendlyName(), MessageUtils.contextSetToString(context));
    ExtendedLogEntry.build().actor(sender).acted(holder).action("parent", "settrack", track.getName(), groupName, context).build().submit(plugin, sender);
    StorageAssistant.save(holder, sender, plugin);
    return CommandResult.SUCCESS;
}
Also used : Group(me.lucko.luckperms.common.model.Group) MutableContextSet(me.lucko.luckperms.api.context.MutableContextSet) Track(me.lucko.luckperms.common.model.Track)

Example 35 with Group

use of me.lucko.luckperms.common.model.Group in project LuckPerms by lucko.

the class ArgumentPermissions method checkViewPerms.

/**
 * Checks if the sender has permission to view the given target
 *
 * @param plugin the plugin instance
 * @param sender the sender to check
 * @param base the base permission for the command
 * @param target the object the sender is truing to view
 * @return true if the sender should NOT be allowed to view the target, true if they should
 */
public static boolean checkViewPerms(LuckPermsPlugin plugin, Sender sender, CommandPermission base, Object target) {
    if (!plugin.getConfiguration().get(ConfigKeys.USE_ARGUMENT_BASED_COMMAND_PERMISSIONS)) {
        return false;
    }
    if (target instanceof User) {
        User targetUser = ((User) target);
        if (targetUser.getUuid().equals(sender.getUuid())) {
            // the sender is trying to view themselves
            Tristate ret = sender.getPermissionValue(base.getPermission() + ".view.self");
            if (ret != Tristate.UNDEFINED) {
                return !ret.asBoolean();
            } else {
                // fallback to the global perm if the one for the specific command is undefined
                Tristate globalRet = sender.getPermissionValue(USER_VIEW_SELF);
                return !globalRet.asBoolean();
            }
        } else {
            // they're trying to view another user
            Tristate ret = sender.getPermissionValue(base.getPermission() + ".view.others");
            if (ret != Tristate.UNDEFINED) {
                return !ret.asBoolean();
            } else {
                // fallback to the global perm if the one for the specific command is undefined
                Tristate globalRet = sender.getPermissionValue(USER_VIEW_OTHERS);
                return !globalRet.asBoolean();
            }
        }
    } else if (target instanceof Group) {
        Group targetGroup = ((Group) target);
        Tristate ret = sender.getPermissionValue(base.getPermission() + ".view." + targetGroup.getName());
        if (ret != Tristate.UNDEFINED) {
            return !ret.asBoolean();
        } else {
            // fallback to the global perm if the one for the specific command is undefined
            Tristate globalRet = sender.getPermissionValue(GROUP_VIEW.apply(targetGroup.getName()));
            return !globalRet.asBoolean();
        }
    } else if (target instanceof Track) {
        Track targetTrack = ((Track) target);
        Tristate ret = sender.getPermissionValue(base.getPermission() + ".view." + targetTrack.getName());
        if (ret != Tristate.UNDEFINED) {
            return !ret.asBoolean();
        } else {
            // fallback to the global perm if the one for the specific command is undefined
            Tristate globalRet = sender.getPermissionValue(TRACK_VIEW.apply(targetTrack.getName()));
            return !globalRet.asBoolean();
        }
    }
    return false;
}
Also used : Group(me.lucko.luckperms.common.model.Group) User(me.lucko.luckperms.common.model.User) Tristate(me.lucko.luckperms.api.Tristate) Track(me.lucko.luckperms.common.model.Track)

Aggregations

Group (me.lucko.luckperms.common.model.Group)52 User (me.lucko.luckperms.common.model.User)20 Node (me.lucko.luckperms.api.Node)16 Set (java.util.Set)10 UUID (java.util.UUID)10 Track (me.lucko.luckperms.common.model.Track)10 NodeFactory (me.lucko.luckperms.common.node.NodeFactory)10 List (java.util.List)9 LuckPermsPlugin (me.lucko.luckperms.common.plugin.LuckPermsPlugin)9 Contexts (me.lucko.luckperms.api.Contexts)8 PermissionHolder (me.lucko.luckperms.common.model.PermissionHolder)8 Sender (me.lucko.luckperms.common.sender.Sender)8 HashSet (java.util.HashSet)7 Map (java.util.Map)7 Collectors (java.util.stream.Collectors)7 MutableContextSet (me.lucko.luckperms.api.context.MutableContextSet)7 CommandResult (me.lucko.luckperms.common.command.CommandResult)7 CommandPermission (me.lucko.luckperms.common.command.access.CommandPermission)7 LocaleManager (me.lucko.luckperms.common.locale.LocaleManager)7 CommandSpec (me.lucko.luckperms.common.locale.command.CommandSpec)7