Search in sources :

Example 11 with Tristate

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

the class PermissionCalculator method getPermissionValue.

/**
 * Performs a permission check against this calculator.
 *
 * <p>The result is calculated using the calculators backing 'processors'.</p>
 *
 * @param permission the permission to check
 * @param origin marks where this check originated from
 * @return the result
 */
public Tristate getPermissionValue(String permission, CheckOrigin origin) {
    // convert the permission to lowercase, as all values in the backing map are also lowercase.
    // this allows fast case insensitive lookups
    permission = permission.toLowerCase();
    // get the result
    Tristate result = this.lookupCache.get(permission);
    // log this permission lookup to the verbose handler
    this.plugin.getVerboseHandler().offerCheckData(origin, this.metadata.getObjectName(), this.metadata.getContext(), permission, result);
    // return the result
    return result;
}
Also used : Tristate(me.lucko.luckperms.api.Tristate)

Example 12 with Tristate

use of me.lucko.luckperms.api.Tristate 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 13 with Tristate

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

the class AssignmentExpression method parse.

public boolean parse(PermissionHolder holder, Tristate tristate) throws IllegalArgumentException {
    ScriptEngine engine = Scripting.getScriptEngine();
    if (engine == null) {
        throw new NullPointerException("script engine");
    }
    Predicate<Node> checker = node -> holder.hasPermission(node, StandardNodeEquality.IGNORE_VALUE_OR_IF_TEMPORARY) == tristate;
    String exp = this.expression.stream().map(t -> t.forExpression(checker)).collect(Collectors.joining()).replace("&", "&&").replace("|", "||");
    try {
        String result = engine.eval(exp).toString();
        if (!result.equals("true") && !result.equals("false")) {
            throw new IllegalArgumentException();
        }
        return Boolean.parseBoolean(result);
    } catch (Throwable t) {
        throw new IllegalArgumentException(exp, t);
    }
}
Also used : LegacyNodeFactory(me.lucko.luckperms.common.node.LegacyNodeFactory) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Scripting(me.lucko.luckperms.common.utils.Scripting) Predicate(java.util.function.Predicate) PermissionHolder(me.lucko.luckperms.common.model.PermissionHolder) Tristate(me.lucko.luckperms.api.Tristate) ScriptEngine(javax.script.ScriptEngine) Node(me.lucko.luckperms.api.Node) Collectors(java.util.stream.Collectors) StandardNodeEquality(me.lucko.luckperms.api.StandardNodeEquality) Node(me.lucko.luckperms.api.Node) ScriptEngine(javax.script.ScriptEngine)

Example 14 with Tristate

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

the class WildcardProcessor method hasPermission.

@Override
public Tristate hasPermission(String permission) {
    String node = permission;
    while (true) {
        int endIndex = node.lastIndexOf(ImmutableNode.NODE_SEPARATOR);
        if (endIndex == -1) {
            break;
        }
        node = node.substring(0, endIndex);
        if (!node.isEmpty()) {
            Tristate t = Tristate.fromNullableBoolean(this.sourceMap.get(node + WILDCARD_SUFFIX));
            if (t != Tristate.UNDEFINED) {
                return t;
            }
        }
    }
    Tristate t = Tristate.fromNullableBoolean(this.sourceMap.get(GLOBAL_WILDCARD_1));
    if (t != Tristate.UNDEFINED) {
        return t;
    }
    return Tristate.fromNullableBoolean(this.sourceMap.get(GLOBAL_WILDCARD_2));
}
Also used : Tristate(me.lucko.luckperms.api.Tristate)

Example 15 with Tristate

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

the class VaultPermissionHook method userHasPermission.

@Override
public boolean userHasPermission(String world, UUID uuid, String permission) {
    if (uuid == null) {
        return false;
    }
    Objects.requireNonNull(permission, "permission");
    User user = getUser(uuid);
    if (user == null) {
        return false;
    }
    Contexts contexts = contextForLookup(user, world);
    PermissionCache permissionData = user.getCachedData().getPermissionData(contexts);
    Tristate result = permissionData.getPermissionValue(permission, CheckOrigin.INTERNAL);
    if (log()) {
        logMsg("#userHasPermission: %s - %s - %s - %s", user.getFriendlyName(), contexts.getContexts().toMultimap(), permission, result);
    }
    return result.asBoolean();
}
Also used : User(me.lucko.luckperms.common.model.User) Tristate(me.lucko.luckperms.api.Tristate) PermissionCache(me.lucko.luckperms.common.caching.type.PermissionCache) Contexts(me.lucko.luckperms.api.Contexts)

Aggregations

Tristate (me.lucko.luckperms.api.Tristate)20 User (me.lucko.luckperms.common.model.User)6 Contexts (me.lucko.luckperms.api.Contexts)4 ProxiedPlayer (net.md_5.bungee.api.connection.ProxiedPlayer)4 EventHandler (net.md_5.bungee.event.EventHandler)4 Group (me.lucko.luckperms.common.model.Group)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Nonnull (javax.annotation.Nonnull)2 PermissionCache (me.lucko.luckperms.common.caching.type.PermissionCache)2 Track (me.lucko.luckperms.common.model.Track)2 LPPermissionDescription (me.lucko.luckperms.sponge.service.model.LPPermissionDescription)2 LPSubject (me.lucko.luckperms.sponge.service.model.LPSubject)2 LPSubjectCollection (me.lucko.luckperms.sponge.service.model.LPSubjectCollection)2 ImmutableList (com.google.common.collect.ImmutableList)1 List (java.util.List)1 UUID (java.util.UUID)1 Predicate (java.util.function.Predicate)1 Collectors (java.util.stream.Collectors)1 ScriptEngine (javax.script.ScriptEngine)1