Search in sources :

Example 31 with User

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

the class UserSwitchPrimaryGroup method execute.

@Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List<String> args, String label, CommandPermission permission) {
    // cast to user
    // although this command is build as a sharedsubcommand,
    // it is only added to the listings for users.
    User user = ((User) holder);
    if (ArgumentPermissions.checkModifyPerms(plugin, sender, permission, user)) {
        Message.COMMAND_NO_PERMISSION.send(sender);
        return CommandResult.NO_PERMISSION;
    }
    String opt = plugin.getConfiguration().get(ConfigKeys.PRIMARY_GROUP_CALCULATION_METHOD);
    if (!opt.equals("stored")) {
        Message.USER_PRIMARYGROUP_WARN_OPTION.send(sender, opt);
    }
    Group group = plugin.getGroupManager().getIfLoaded(args.get(0).toLowerCase());
    if (group == null) {
        Message.DOES_NOT_EXIST.send(sender, args.get(0).toLowerCase());
        return CommandResult.INVALID_ARGS;
    }
    if (user.getPrimaryGroup().getStoredValue().orElse(NodeFactory.DEFAULT_GROUP_NAME).equalsIgnoreCase(group.getName())) {
        Message.USER_PRIMARYGROUP_ERROR_ALREADYHAS.send(sender, user.getFriendlyName(), group.getFriendlyName());
        return CommandResult.STATE_ERROR;
    }
    if (!user.inheritsGroup(group)) {
        Message.USER_PRIMARYGROUP_ERROR_NOTMEMBER.send(sender, user.getFriendlyName(), group.getName());
        user.setPermission(NodeFactory.buildGroupNode(group.getName()).build());
    }
    user.getPrimaryGroup().setStoredValue(group.getName());
    Message.USER_PRIMARYGROUP_SUCCESS.send(sender, user.getFriendlyName(), group.getFriendlyName());
    ExtendedLogEntry.build().actor(sender).acted(user).action("parent", "switchprimarygroup", group.getName()).build().submit(plugin, sender);
    StorageAssistant.save(user, sender, plugin);
    return CommandResult.SUCCESS;
}
Also used : Group(me.lucko.luckperms.common.model.Group) User(me.lucko.luckperms.common.model.User)

Example 32 with User

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

the class ArgumentPermissions method checkModifyPerms.

/**
 * Checks if the sender has permission to modify 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 modify
 * @return true if the sender should NOT be allowed to modify the target, true if they should
 */
public static boolean checkModifyPerms(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 edit themselves
            Tristate ret = sender.getPermissionValue(base.getPermission() + ".modify.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_MODIFY_SELF);
                return !globalRet.asBoolean();
            }
        } else {
            // they're trying to edit another user
            Tristate ret = sender.getPermissionValue(base.getPermission() + ".modify.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_MODIFY_OTHERS);
                return !globalRet.asBoolean();
            }
        }
    } else if (target instanceof Group) {
        Group targetGroup = ((Group) target);
        Tristate ret = sender.getPermissionValue(base.getPermission() + ".modify." + 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_MODIFY.apply(targetGroup.getName()));
            return !globalRet.asBoolean();
        }
    } else if (target instanceof Track) {
        Track targetTrack = ((Track) target);
        Tristate ret = sender.getPermissionValue(base.getPermission() + ".modify." + 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_MODIFY.apply(targetTrack.getName()));
            return !globalRet.asBoolean();
        }
    } else {
        throw new IllegalStateException();
    }
}
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)

Example 33 with User

use of me.lucko.luckperms.common.model.User 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)

Example 34 with User

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

the class ConfigurateDao method init.

@Override
public void init() {
    try {
        File data = FileUtils.mkdirs(new File(this.plugin.getBootstrap().getDataDirectory(), this.dataFolderName));
        this.usersDirectory = FileUtils.mkdir(new File(data, "users"));
        this.groupsDirectory = FileUtils.mkdir(new File(data, "groups"));
        this.tracksDirectory = FileUtils.mkdir(new File(data, "tracks"));
        this.uuidDataFile = FileUtils.createNewFile(new File(data, "uuidcache.txt"));
        this.actionLogFile = FileUtils.createNewFile(new File(data, "actions.log"));
        // Listen for file changes.
        this.plugin.getFileWatcher().ifPresent(watcher -> {
            watcher.subscribe("user", this.usersDirectory.toPath(), s -> {
                if (!s.endsWith(this.fileExtension)) {
                    return;
                }
                String user = s.substring(0, s.length() - this.fileExtension.length());
                UUID uuid = Uuids.parseNullable(user);
                if (uuid == null) {
                    return;
                }
                User u = this.plugin.getUserManager().getIfLoaded(uuid);
                if (u != null) {
                    this.plugin.getLogger().info("[FileWatcher] Refreshing user " + u.getFriendlyName());
                    this.plugin.getStorage().loadUser(uuid, null);
                }
            });
            watcher.subscribe("group", this.groupsDirectory.toPath(), s -> {
                if (!s.endsWith(this.fileExtension)) {
                    return;
                }
                String groupName = s.substring(0, s.length() - this.fileExtension.length());
                this.plugin.getLogger().info("[FileWatcher] Refreshing group " + groupName);
                this.plugin.getUpdateTaskBuffer().request();
            });
            watcher.subscribe("track", this.tracksDirectory.toPath(), s -> {
                if (!s.endsWith(this.fileExtension)) {
                    return;
                }
                String trackName = s.substring(0, s.length() - this.fileExtension.length());
                this.plugin.getLogger().info("[FileWatcher] Refreshing track " + trackName);
                this.plugin.getStorage().loadAllTracks();
            });
        });
    } catch (IOException e) {
        e.printStackTrace();
        return;
    }
    this.uuidCache.load(this.uuidDataFile);
    this.actionLogger.init(this.actionLogFile);
}
Also used : User(me.lucko.luckperms.common.model.User) IOException(java.io.IOException) UUID(java.util.UUID) File(java.io.File)

Example 35 with User

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

the class SqlDao method loadUser.

@Override
public User loadUser(UUID uuid, String username) throws SQLException {
    User user = this.plugin.getUserManager().getOrMake(UserIdentifier.of(uuid, username));
    user.getIoLock().lock();
    try {
        List<NodeModel> data = new ArrayList<>();
        String primaryGroup = null;
        String userName = null;
        // Collect user permissions
        try (Connection c = this.provider.getConnection()) {
            try (PreparedStatement ps = c.prepareStatement(this.prefix.apply(USER_PERMISSIONS_SELECT))) {
                ps.setString(1, user.getUuid().toString());
                try (ResultSet rs = ps.executeQuery()) {
                    while (rs.next()) {
                        String permission = rs.getString("permission");
                        boolean value = rs.getBoolean("value");
                        String server = rs.getString("server");
                        String world = rs.getString("world");
                        long expiry = rs.getLong("expiry");
                        String contexts = rs.getString("contexts");
                        data.add(deserializeNode(permission, value, server, world, expiry, contexts));
                    }
                }
            }
        }
        // Collect user meta (username & primary group)
        try (Connection c = this.provider.getConnection()) {
            try (PreparedStatement ps = c.prepareStatement(this.prefix.apply(PLAYER_SELECT_BY_UUID))) {
                ps.setString(1, user.getUuid().toString());
                try (ResultSet rs = ps.executeQuery()) {
                    if (rs.next()) {
                        userName = rs.getString("username");
                        primaryGroup = rs.getString("primary_group");
                    }
                }
            }
        }
        // update username & primary group
        if (primaryGroup == null) {
            primaryGroup = NodeFactory.DEFAULT_GROUP_NAME;
        }
        user.getPrimaryGroup().setStoredValue(primaryGroup);
        // Update their username to what was in the storage if the one in the local instance is null
        user.setName(userName, true);
        // If the user has any data in storage
        if (!data.isEmpty()) {
            Set<Node> nodes = data.stream().map(NodeModel::toNode).collect(Collectors.toSet());
            user.setEnduringNodes(nodes);
            // Save back to the store if data they were given any defaults or had permissions expire
            if (this.plugin.getUserManager().giveDefaultIfNeeded(user, false) | user.auditTemporaryPermissions()) {
                // This should be fine, as the lock will be acquired by the same thread.
                saveUser(user);
            }
        } else {
            // User has no data in storage.
            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 : NodeModel(me.lucko.luckperms.common.node.NodeModel) User(me.lucko.luckperms.common.model.User) Node(me.lucko.luckperms.api.Node) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Aggregations

User (me.lucko.luckperms.common.model.User)67 Group (me.lucko.luckperms.common.model.Group)20 UUID (java.util.UUID)16 Node (me.lucko.luckperms.api.Node)14 Contexts (me.lucko.luckperms.api.Contexts)10 List (java.util.List)9 NodeFactory (me.lucko.luckperms.common.node.NodeFactory)9 Tristate (me.lucko.luckperms.api.Tristate)8 Track (me.lucko.luckperms.common.model.Track)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 CommandPermission (me.lucko.luckperms.common.command.access.CommandPermission)7 ProgressLogger (me.lucko.luckperms.common.logging.ProgressLogger)7 Sender (me.lucko.luckperms.common.sender.Sender)7 Map (java.util.Map)6 Set (java.util.Set)6 Collectors (java.util.stream.Collectors)6 LuckPermsPlugin (me.lucko.luckperms.common.plugin.LuckPermsPlugin)6 MutableContextSet (me.lucko.luckperms.api.context.MutableContextSet)5 CommandResult (me.lucko.luckperms.common.command.CommandResult)5 SubCommand (me.lucko.luckperms.common.command.abstraction.SubCommand)5