2015-06-19 15:14:46 +02:00
|
|
|
package com.earth2me.essentials.perm.impl;
|
2011-07-18 00:30:39 +02:00
|
|
|
|
2022-03-27 15:07:36 +02:00
|
|
|
import com.earth2me.essentials.Essentials;
|
|
|
|
import com.earth2me.essentials.User;
|
2015-06-19 15:14:46 +02:00
|
|
|
import com.earth2me.essentials.perm.IPermissionsHandler;
|
2021-05-28 19:35:33 +02:00
|
|
|
import com.earth2me.essentials.utils.TriState;
|
2015-06-19 15:14:46 +02:00
|
|
|
import org.bukkit.Bukkit;
|
2011-07-18 00:30:39 +02:00
|
|
|
import org.bukkit.entity.Player;
|
2020-07-06 20:40:47 +02:00
|
|
|
import org.bukkit.permissions.Permission;
|
2021-05-28 19:35:33 +02:00
|
|
|
import org.bukkit.permissions.PermissionAttachmentInfo;
|
2015-06-19 15:14:46 +02:00
|
|
|
import org.bukkit.plugin.Plugin;
|
2011-07-18 00:30:39 +02:00
|
|
|
|
2015-06-19 15:14:46 +02:00
|
|
|
import java.util.Arrays;
|
2015-04-15 06:06:16 +02:00
|
|
|
import java.util.List;
|
2020-06-03 22:36:27 +02:00
|
|
|
import java.util.function.Function;
|
|
|
|
import java.util.function.Supplier;
|
2011-07-18 00:30:39 +02:00
|
|
|
|
2015-04-15 06:06:16 +02:00
|
|
|
public class SuperpermsHandler implements IPermissionsHandler {
|
2019-12-26 16:08:07 +01:00
|
|
|
protected boolean emulateWildcards() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-15 06:06:16 +02:00
|
|
|
@Override
|
|
|
|
public String getGroup(final Player base) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<String> getGroups(final Player base) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canBuild(final Player base, final String group) {
|
2019-12-26 16:08:07 +01:00
|
|
|
return hasPermission(base, "essentials.build");
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean inGroup(final Player base, final String group) {
|
|
|
|
return hasPermission(base, "group." + group);
|
|
|
|
}
|
|
|
|
|
2020-07-06 20:40:47 +02:00
|
|
|
/**
|
|
|
|
* Return whether a permission is registered and denied to ops.
|
|
|
|
*
|
|
|
|
* <p>The default permission value is {@link org.bukkit.permissions.PermissionDefault#OP}, so for a permission
|
|
|
|
* to be denied to ops it has to be both explicitly registered and have its default evaluate to false for an operator</p>
|
|
|
|
*
|
|
|
|
* @param node node to check
|
|
|
|
* @return whether an op would, in absense of other permissions being set, have
|
|
|
|
*/
|
|
|
|
private boolean isDeniedToOps(final String node) {
|
|
|
|
final Permission perm = Bukkit.getServer().getPluginManager().getPermission(node);
|
|
|
|
return perm != null && !perm.getDefault().getValue(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform a permissions check on {@code base}.
|
|
|
|
*
|
|
|
|
* <p>Unless {@link #emulateWildcards()} is overridden to disable wildcard emulation,
|
|
|
|
* wildcard assignments will be checked for permissions. This has a few subtleties in order
|
|
|
|
* to respect default-false assignments. {@link org.bukkit.permissions.Permissible#isPermissionSet(String)}
|
|
|
|
* will only return true for permissions that are set on an attachment, or that are a default that evaluates to true.
|
|
|
|
* When resolving wildcards, we also want to detect permissions that are not in an attachment, but also won't evaluate
|
2020-11-25 21:24:24 +01:00
|
|
|
* to true for operators — since these are ones we've explicitly set to {@code false} in the {@code plugin.yml}</p>
|
2020-07-06 20:40:47 +02:00
|
|
|
*
|
|
|
|
* <p>For the resolution itself, we check whether the permission is either set on the permissible or explicitly not
|
|
|
|
* granted to ops (i.e. deviating from the default). If so, the permission's value is returned. Otherwise, the portion
|
|
|
|
* of the permission from the beginning to the last occurrence of {@code .} followed by a {@code *} is taken and the process is repeated.</p>
|
|
|
|
*
|
|
|
|
* <p>Once a string without dots has been checked, if no result has been found the literal permission {@code *} is
|
|
|
|
* checked and the result of that check is returned.</p>
|
|
|
|
*
|
|
|
|
* @param base Player to check permissions on
|
|
|
|
* @param node permission to check
|
|
|
|
* @return calculated value
|
|
|
|
*/
|
2015-04-15 06:06:16 +02:00
|
|
|
@Override
|
|
|
|
public boolean hasPermission(final Player base, String node) {
|
2019-12-26 16:08:07 +01:00
|
|
|
if (!emulateWildcards()) {
|
|
|
|
return base.hasPermission(node);
|
|
|
|
}
|
|
|
|
|
2015-04-15 06:06:16 +02:00
|
|
|
String permCheck = node;
|
|
|
|
int index;
|
|
|
|
while (true) {
|
2020-07-06 20:40:47 +02:00
|
|
|
// Either explicitly set on the subject, or globally set to not be automatically granted
|
|
|
|
// by declaring in `plugin.yml` with a `default: false` value (as opposed to the default default value of OP)
|
|
|
|
if (base.isPermissionSet(permCheck) || isDeniedToOps(node)) {
|
2015-04-15 06:06:16 +02:00
|
|
|
return base.hasPermission(permCheck);
|
|
|
|
}
|
|
|
|
|
|
|
|
index = node.lastIndexOf('.');
|
|
|
|
if (index < 1) {
|
|
|
|
return base.hasPermission("*");
|
|
|
|
}
|
|
|
|
|
|
|
|
node = node.substring(0, index);
|
|
|
|
permCheck = node + ".*";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-09 16:56:58 +02:00
|
|
|
@Override
|
|
|
|
public boolean isPermissionSet(final Player base, final String node) {
|
|
|
|
return base.isPermissionSet(node);
|
|
|
|
}
|
|
|
|
|
2021-05-28 19:35:33 +02:00
|
|
|
@Override
|
|
|
|
public TriState isPermissionSetExact(Player base, String node) {
|
|
|
|
for (final PermissionAttachmentInfo perm : base.getEffectivePermissions()) {
|
|
|
|
if (perm.getPermission().equalsIgnoreCase(node)) {
|
|
|
|
return perm.getValue() ? TriState.TRUE : TriState.FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TriState.UNSET;
|
|
|
|
}
|
|
|
|
|
2015-04-15 06:06:16 +02:00
|
|
|
@Override
|
|
|
|
public String getPrefix(final Player base) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getSuffix(final Player base) {
|
|
|
|
return null;
|
|
|
|
}
|
2015-06-19 15:14:46 +02:00
|
|
|
|
2020-06-03 22:36:27 +02:00
|
|
|
@Override
|
2022-03-27 15:07:36 +02:00
|
|
|
public void registerContext(final String context, final Function<User, Iterable<String>> calculator, final Supplier<Iterable<String>> suggestions) {
|
2020-06-03 22:36:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void unregisterContexts() {
|
|
|
|
}
|
|
|
|
|
2020-07-05 20:30:01 +02:00
|
|
|
@Override
|
|
|
|
public String getBackendName() {
|
|
|
|
return getEnabledPermsPlugin();
|
|
|
|
}
|
|
|
|
|
2015-06-19 15:14:46 +02:00
|
|
|
@Override
|
2022-03-27 15:07:36 +02:00
|
|
|
public boolean tryProvider(Essentials ess) {
|
2015-06-19 15:14:46 +02:00
|
|
|
return getEnabledPermsPlugin() != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getEnabledPermsPlugin() {
|
|
|
|
String enabledPermsPlugin = null;
|
2020-10-03 19:46:05 +02:00
|
|
|
final List<String> specialCasePlugins = Arrays.asList("PermissionsEx", "GroupManager",
|
|
|
|
"SimplyPerms", "Privileges", "bPermissions", "zPermissions", "PermissionsBukkit",
|
|
|
|
"DroxPerms", "xPerms", "LuckPerms");
|
|
|
|
for (final Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
|
2015-06-19 15:14:46 +02:00
|
|
|
if (specialCasePlugins.contains(plugin.getName())) {
|
|
|
|
enabledPermsPlugin = plugin.getName();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return enabledPermsPlugin;
|
|
|
|
}
|
2011-07-18 00:30:39 +02:00
|
|
|
}
|