Search in sources :

Example 1 with Node

use of me.lucko.luckperms.api.Node in project LuckPerms by lucko.

the class ConfigurateDao method createAndLoadGroup.

@Override
public Group createAndLoadGroup(String name) throws Exception {
    Group group = this.plugin.getGroupManager().getOrMake(name);
    group.getIoLock().lock();
    try {
        ConfigurationNode object = readFile(StorageLocation.GROUP, name);
        if (object != null) {
            Set<Node> nodes = readNodes(object).stream().map(NodeModel::toNode).collect(Collectors.toSet());
            group.setEnduringNodes(nodes);
        } else {
            ConfigurationNode data = SimpleConfigurationNode.root();
            data.getNode("name").setValue(group.getName());
            Set<NodeModel> nodes = group.getEnduringNodes().values().stream().map(NodeModel::fromNode).collect(Collectors.toCollection(LinkedHashSet::new));
            writeNodes(data, nodes);
            saveFile(StorageLocation.GROUP, name, data);
        }
    } catch (Exception e) {
        throw reportException(name, e);
    } finally {
        group.getIoLock().unlock();
    }
    group.getRefreshBuffer().requestDirectly();
    return group;
}
Also used : Group(me.lucko.luckperms.common.model.Group) NodeModel(me.lucko.luckperms.common.node.NodeModel) ConfigurationNode(ninja.leaping.configurate.ConfigurationNode) SimpleConfigurationNode(ninja.leaping.configurate.SimpleConfigurationNode) ConfigurationNode(ninja.leaping.configurate.ConfigurationNode) SimpleConfigurationNode(ninja.leaping.configurate.SimpleConfigurationNode) Node(me.lucko.luckperms.api.Node) IOException(java.io.IOException)

Example 2 with Node

use of me.lucko.luckperms.api.Node in project LuckPerms by lucko.

the class ConfigurateDao method loadUser.

@Override
public User loadUser(UUID uuid, String username) throws Exception {
    User user = this.plugin.getUserManager().getOrMake(UserIdentifier.of(uuid, username));
    user.getIoLock().lock();
    try {
        ConfigurationNode object = readFile(StorageLocation.USER, uuid.toString());
        if (object != null) {
            String name = object.getNode("name").getString();
            user.getPrimaryGroup().setStoredValue(object.getNode(this instanceof JsonDao ? "primaryGroup" : "primary-group").getString());
            Set<Node> nodes = readNodes(object).stream().map(NodeModel::toNode).collect(Collectors.toSet());
            user.setEnduringNodes(nodes);
            user.setName(name, true);
            boolean save = this.plugin.getUserManager().giveDefaultIfNeeded(user, false);
            if (user.getName().isPresent() && (name == null || !user.getName().get().equalsIgnoreCase(name))) {
                save = true;
            }
            if (save | user.auditTemporaryPermissions()) {
                saveUser(user);
            }
        } else {
            if (this.plugin.getUserManager().shouldSave(user)) {
                user.clearNodes();
                user.getPrimaryGroup().setStoredValue(null);
                this.plugin.getUserManager().giveDefaultIfNeeded(user, false);
            }
        }
    } catch (Exception e) {
        throw reportException(uuid.toString(), e);
    } finally {
        user.getIoLock().unlock();
    }
    user.getRefreshBuffer().requestDirectly();
    return user;
}
Also used : User(me.lucko.luckperms.common.model.User) ConfigurationNode(ninja.leaping.configurate.ConfigurationNode) SimpleConfigurationNode(ninja.leaping.configurate.SimpleConfigurationNode) ConfigurationNode(ninja.leaping.configurate.ConfigurationNode) SimpleConfigurationNode(ninja.leaping.configurate.SimpleConfigurationNode) Node(me.lucko.luckperms.api.Node) IOException(java.io.IOException)

Example 3 with Node

use of me.lucko.luckperms.api.Node in project LuckPerms by lucko.

the class MongoDao method loadUser.

@Override
public User loadUser(UUID uuid, String username) {
    User user = this.plugin.getUserManager().getOrMake(UserIdentifier.of(uuid, username));
    user.getIoLock().lock();
    try {
        MongoCollection<Document> c = this.database.getCollection(this.prefix + "users");
        try (MongoCursor<Document> cursor = c.find(new Document("_id", user.getUuid())).iterator()) {
            if (cursor.hasNext()) {
                // User exists, let's load.
                Document d = cursor.next();
                String name = d.getString("name");
                user.getPrimaryGroup().setStoredValue(d.getString("primaryGroup"));
                Set<Node> nodes = nodesFromDoc(d).stream().map(NodeModel::toNode).collect(Collectors.toSet());
                user.setEnduringNodes(nodes);
                user.setName(name, true);
                boolean save = this.plugin.getUserManager().giveDefaultIfNeeded(user, false);
                if (user.getName().isPresent() && (name == null || !user.getName().get().equalsIgnoreCase(name))) {
                    save = true;
                }
                if (save | user.auditTemporaryPermissions()) {
                    c.replaceOne(new Document("_id", user.getUuid()), userToDoc(user));
                }
            } else {
                if (this.plugin.getUserManager().shouldSave(user)) {
                    user.clearNodes();
                    user.getPrimaryGroup().setStoredValue(null);
                    this.plugin.getUserManager().giveDefaultIfNeeded(user, false);
                }
            }
        }
    } finally {
        user.getIoLock().unlock();
    }
    user.getRefreshBuffer().requestDirectly();
    return user;
}
Also used : User(me.lucko.luckperms.common.model.User) Node(me.lucko.luckperms.api.Node) Document(org.bson.Document)

Example 4 with Node

use of me.lucko.luckperms.api.Node in project LuckPerms by lucko.

the class UserDemote method execute.

@Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, User user, List<String> args, String label) throws CommandException {
    if (ArgumentPermissions.checkModifyPerms(plugin, sender, getPermission().get(), user)) {
        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;
    }
    boolean silent = args.remove("-s");
    MutableContextSet context = ArgumentParser.parseContext(1, args, plugin);
    if (ArgumentPermissions.checkContext(plugin, sender, getPermission().get(), context)) {
        Message.COMMAND_NO_PERMISSION.send(sender);
        return CommandResult.NO_PERMISSION;
    }
    // Load applicable groups
    Set<Node> nodes = user.getEnduringNodes().values().stream().filter(Node::isGroupNode).filter(Node::getValuePrimitive).filter(node -> node.getFullContexts().makeImmutable().equals(context.makeImmutable())).collect(Collectors.toSet());
    nodes.removeIf(g -> !track.containsGroup(g.getGroupName()));
    if (nodes.isEmpty()) {
        Message.USER_TRACK_ERROR_NOT_CONTAIN_GROUP.send(sender, user.getFriendlyName(), track.getName());
        return CommandResult.FAILURE;
    }
    if (nodes.size() != 1) {
        Message.TRACK_AMBIGUOUS_CALL.send(sender, user.getFriendlyName());
        return CommandResult.FAILURE;
    }
    final Node oldNode = Iterables.getFirst(nodes, null);
    final String old = oldNode.getGroupName();
    final String previous;
    try {
        previous = track.getPrevious(old);
    } catch (IllegalArgumentException e) {
        Message.TRACK_DOES_NOT_CONTAIN.send(sender, track.getName(), old);
        return CommandResult.STATE_ERROR;
    }
    if (ArgumentPermissions.checkArguments(plugin, sender, getPermission().get(), track.getName(), oldNode.getGroupName())) {
        Message.COMMAND_NO_PERMISSION.send(sender);
        return CommandResult.NO_PERMISSION;
    }
    if (previous == null) {
        user.unsetPermission(oldNode);
        Message.USER_DEMOTE_ENDOFTRACK.send(sender, track.getName(), user.getFriendlyName(), old);
        ExtendedLogEntry.build().actor(sender).acted(user).action("demote", track.getName(), context).build().submit(plugin, sender);
        StorageAssistant.save(user, sender, plugin);
        plugin.getEventFactory().handleUserDemote(user, track, old, null, sender);
        return CommandResult.SUCCESS;
    }
    Group previousGroup = plugin.getStorage().loadGroup(previous).join().orElse(null);
    if (previousGroup == null) {
        Message.USER_DEMOTE_ERROR_MALFORMED.send(sender, previous);
        return CommandResult.LOADING_ERROR;
    }
    user.unsetPermission(oldNode);
    user.setPermission(NodeFactory.buildGroupNode(previousGroup.getName()).withExtraContext(context).build());
    if (context.isEmpty() && user.getPrimaryGroup().getStoredValue().orElse(NodeFactory.DEFAULT_GROUP_NAME).equalsIgnoreCase(old)) {
        user.getPrimaryGroup().setStoredValue(previousGroup.getName());
    }
    Message.USER_DEMOTE_SUCCESS.send(sender, user.getFriendlyName(), track.getName(), old, previousGroup.getFriendlyName(), MessageUtils.contextSetToString(context));
    if (!silent) {
        Message.EMPTY.send(sender, MessageUtils.listToArrowSep(track.getGroups(), previousGroup.getName(), old, true));
    }
    ExtendedLogEntry.build().actor(sender).acted(user).action("demote", track.getName(), context).build().submit(plugin, sender);
    StorageAssistant.save(user, sender, plugin);
    plugin.getEventFactory().handleUserDemote(user, track, old, previousGroup.getName(), sender);
    return CommandResult.SUCCESS;
}
Also used : Iterables(com.google.common.collect.Iterables) CommandException(me.lucko.luckperms.common.command.abstraction.CommandException) SubCommand(me.lucko.luckperms.common.command.abstraction.SubCommand) LocaleManager(me.lucko.luckperms.common.locale.LocaleManager) MutableContextSet(me.lucko.luckperms.api.context.MutableContextSet) StorageAssistant(me.lucko.luckperms.common.command.utils.StorageAssistant) ArgumentParser(me.lucko.luckperms.common.command.utils.ArgumentParser) CommandSpec(me.lucko.luckperms.common.locale.command.CommandSpec) Message(me.lucko.luckperms.common.locale.message.Message) LuckPermsPlugin(me.lucko.luckperms.common.plugin.LuckPermsPlugin) ExtendedLogEntry(me.lucko.luckperms.common.actionlog.ExtendedLogEntry) MessageUtils(me.lucko.luckperms.common.command.utils.MessageUtils) Set(java.util.Set) NodeFactory(me.lucko.luckperms.common.node.NodeFactory) Sender(me.lucko.luckperms.common.sender.Sender) Collectors(java.util.stream.Collectors) CommandResult(me.lucko.luckperms.common.command.CommandResult) CommandPermission(me.lucko.luckperms.common.command.access.CommandPermission) Track(me.lucko.luckperms.common.model.Track) List(java.util.List) TabCompletions(me.lucko.luckperms.common.command.utils.TabCompletions) Group(me.lucko.luckperms.common.model.Group) Node(me.lucko.luckperms.api.Node) DataConstraints(me.lucko.luckperms.common.storage.DataConstraints) ArgumentPermissions(me.lucko.luckperms.common.command.access.ArgumentPermissions) User(me.lucko.luckperms.common.model.User) Predicates(me.lucko.luckperms.common.utils.Predicates) Group(me.lucko.luckperms.common.model.Group) MutableContextSet(me.lucko.luckperms.api.context.MutableContextSet) Node(me.lucko.luckperms.api.Node) Track(me.lucko.luckperms.common.model.Track)

Example 5 with Node

use of me.lucko.luckperms.api.Node in project LuckPerms by lucko.

the class UserPromote method execute.

@Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, User user, List<String> args, String label) throws CommandException {
    if (ArgumentPermissions.checkModifyPerms(plugin, sender, getPermission().get(), user)) {
        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;
    }
    boolean silent = args.remove("-s");
    MutableContextSet context = ArgumentParser.parseContext(1, args, plugin);
    if (ArgumentPermissions.checkContext(plugin, sender, getPermission().get(), context)) {
        Message.COMMAND_NO_PERMISSION.send(sender);
        return CommandResult.NO_PERMISSION;
    }
    // Load applicable groups
    Set<Node> nodes = user.getEnduringNodes().values().stream().filter(Node::isGroupNode).filter(Node::getValuePrimitive).filter(node -> node.getFullContexts().makeImmutable().equals(context.makeImmutable())).collect(Collectors.toSet());
    nodes.removeIf(g -> !track.containsGroup(g.getGroupName()));
    if (nodes.isEmpty()) {
        String first = track.getGroups().get(0);
        Group nextGroup = plugin.getGroupManager().getIfLoaded(first);
        if (nextGroup == null) {
            Message.USER_PROMOTE_ERROR_MALFORMED.send(sender, first);
            return CommandResult.LOADING_ERROR;
        }
        if (ArgumentPermissions.checkArguments(plugin, sender, getPermission().get(), track.getName(), nextGroup.getName())) {
            Message.COMMAND_NO_PERMISSION.send(sender);
            return CommandResult.NO_PERMISSION;
        }
        user.setPermission(NodeFactory.buildGroupNode(nextGroup.getId()).withExtraContext(context).build());
        Message.USER_TRACK_ADDED_TO_FIRST.send(sender, user.getFriendlyName(), nextGroup.getFriendlyName(), MessageUtils.contextSetToString(context));
        ExtendedLogEntry.build().actor(sender).acted(user).action("promote", track.getName(), context).build().submit(plugin, sender);
        StorageAssistant.save(user, sender, plugin);
        plugin.getEventFactory().handleUserPromote(user, track, null, first, sender);
        return CommandResult.SUCCESS;
    }
    if (nodes.size() != 1) {
        Message.TRACK_AMBIGUOUS_CALL.send(sender, user.getFriendlyName());
        return CommandResult.FAILURE;
    }
    final Node oldNode = nodes.stream().findAny().get();
    final String old = oldNode.getGroupName();
    final String next;
    try {
        next = track.getNext(old);
    } catch (IllegalArgumentException e) {
        Message.TRACK_DOES_NOT_CONTAIN.send(sender, track.getName(), old);
        return CommandResult.STATE_ERROR;
    }
    if (next == null) {
        Message.USER_PROMOTE_ERROR_ENDOFTRACK.send(sender, track.getName(), user.getFriendlyName());
        return CommandResult.STATE_ERROR;
    }
    if (!plugin.getStorage().loadGroup(next).join().isPresent()) {
        Message.USER_PROMOTE_ERROR_MALFORMED.send(sender, next);
        return CommandResult.STATE_ERROR;
    }
    Group nextGroup = plugin.getGroupManager().getIfLoaded(next);
    if (nextGroup == null) {
        Message.USER_PROMOTE_ERROR_MALFORMED.send(sender, next);
        return CommandResult.LOADING_ERROR;
    }
    if (ArgumentPermissions.checkArguments(plugin, sender, getPermission().get(), track.getName(), nextGroup.getName())) {
        Message.COMMAND_NO_PERMISSION.send(sender);
        return CommandResult.NO_PERMISSION;
    }
    user.unsetPermission(oldNode);
    user.setPermission(NodeFactory.buildGroupNode(nextGroup.getName()).withExtraContext(context).build());
    if (context.isEmpty() && user.getPrimaryGroup().getStoredValue().orElse(NodeFactory.DEFAULT_GROUP_NAME).equalsIgnoreCase(old)) {
        user.getPrimaryGroup().setStoredValue(nextGroup.getName());
    }
    Message.USER_PROMOTE_SUCCESS.send(sender, user.getFriendlyName(), track.getName(), old, nextGroup.getFriendlyName(), MessageUtils.contextSetToString(context));
    if (!silent) {
        Message.EMPTY.send(sender, MessageUtils.listToArrowSep(track.getGroups(), old, nextGroup.getName(), false));
    }
    ExtendedLogEntry.build().actor(sender).acted(user).action("promote", track.getName(), context).build().submit(plugin, sender);
    StorageAssistant.save(user, sender, plugin);
    plugin.getEventFactory().handleUserPromote(user, track, old, nextGroup.getName(), sender);
    return CommandResult.SUCCESS;
}
Also used : CommandException(me.lucko.luckperms.common.command.abstraction.CommandException) SubCommand(me.lucko.luckperms.common.command.abstraction.SubCommand) LocaleManager(me.lucko.luckperms.common.locale.LocaleManager) MutableContextSet(me.lucko.luckperms.api.context.MutableContextSet) StorageAssistant(me.lucko.luckperms.common.command.utils.StorageAssistant) ArgumentParser(me.lucko.luckperms.common.command.utils.ArgumentParser) CommandSpec(me.lucko.luckperms.common.locale.command.CommandSpec) Message(me.lucko.luckperms.common.locale.message.Message) LuckPermsPlugin(me.lucko.luckperms.common.plugin.LuckPermsPlugin) ExtendedLogEntry(me.lucko.luckperms.common.actionlog.ExtendedLogEntry) MessageUtils(me.lucko.luckperms.common.command.utils.MessageUtils) Set(java.util.Set) NodeFactory(me.lucko.luckperms.common.node.NodeFactory) Sender(me.lucko.luckperms.common.sender.Sender) Collectors(java.util.stream.Collectors) CommandResult(me.lucko.luckperms.common.command.CommandResult) CommandPermission(me.lucko.luckperms.common.command.access.CommandPermission) Track(me.lucko.luckperms.common.model.Track) List(java.util.List) TabCompletions(me.lucko.luckperms.common.command.utils.TabCompletions) Group(me.lucko.luckperms.common.model.Group) Node(me.lucko.luckperms.api.Node) DataConstraints(me.lucko.luckperms.common.storage.DataConstraints) ArgumentPermissions(me.lucko.luckperms.common.command.access.ArgumentPermissions) User(me.lucko.luckperms.common.model.User) Predicates(me.lucko.luckperms.common.utils.Predicates) Group(me.lucko.luckperms.common.model.Group) MutableContextSet(me.lucko.luckperms.api.context.MutableContextSet) Node(me.lucko.luckperms.api.Node) Track(me.lucko.luckperms.common.model.Track)

Aggregations

Node (me.lucko.luckperms.api.Node)56 LocalizedNode (me.lucko.luckperms.api.LocalizedNode)15 Group (me.lucko.luckperms.common.model.Group)15 User (me.lucko.luckperms.common.model.User)13 Map (java.util.Map)12 ImmutableLocalizedNode (me.lucko.luckperms.common.node.ImmutableLocalizedNode)12 List (java.util.List)11 NodeFactory (me.lucko.luckperms.common.node.NodeFactory)10 Message (me.lucko.luckperms.common.locale.message.Message)9 LuckPermsPlugin (me.lucko.luckperms.common.plugin.LuckPermsPlugin)9 Sender (me.lucko.luckperms.common.sender.Sender)9 Collectors (java.util.stream.Collectors)8 MutableContextSet (me.lucko.luckperms.api.context.MutableContextSet)8 CommandResult (me.lucko.luckperms.common.command.CommandResult)8 CommandPermission (me.lucko.luckperms.common.command.access.CommandPermission)8 LocaleManager (me.lucko.luckperms.common.locale.LocaleManager)8 CommandSpec (me.lucko.luckperms.common.locale.command.CommandSpec)8 Predicates (me.lucko.luckperms.common.utils.Predicates)8 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)7