[BLIND] Switch rule definition to a list of strings. (+)

(+) Extend/alter slightly, e.g. added a regex example.

Replacement characters are not needed, and default values won't keep
reappearing.

Tests pending, e.g. dump with the upcoming registry log.
This commit is contained in:
asofold 2018-02-02 23:39:08 +01:00
parent 08aaa5fb1c
commit c45a31b979
3 changed files with 62 additions and 23 deletions

View File

@ -79,24 +79,24 @@ public class DefaultConfig extends ConfigFile {
// Permission settings.
set(ConfPaths.PERMISSIONS_POLICY_DEFAULT, "ALWAYS", 1140);
/*
* TODO: Setting the rules paths forces people to keep those entries -
* note: put to the end, if in the way, order should be preserved.
* Consider a preventupdateconfig strategy (where to, where not to), if
* a parent path already is set.
*/
set(ConfPaths.PERMISSIONS_POLICY_RULES + ".nocheatplus#admin#debug", "INTERVAL:5", 1140);
set(ConfPaths.PERMISSIONS_POLICY_RULES + ".nocheatplus#admin*", "ALWAYS", 1140);
// TODO: Command permissions are always checked anyway :p. Will be changed...
set(ConfPaths.PERMISSIONS_POLICY_RULES + ".nocheatplus#command*", "ALWAYS", 1140);
set(ConfPaths.PERMISSIONS_POLICY_RULES + ".nocheatplus#bypass*", "ALWAYS", 1140);
set(ConfPaths.PERMISSIONS_POLICY_RULES + ".nocheatplus#checks#net#*", "INTERVAL:2", 1140);
set(ConfPaths.PERMISSIONS_POLICY_RULES + ".nocheatplus#checks#chat#commands", "INTERVAL:2", 1140);
set(ConfPaths.PERMISSIONS_POLICY_RULES + ".nocheatplus#checks#chat#text", "INTERVAL:2", 1140);
// Relog, logins: Note: aims at login denial, would invalidate once offline/world change. +- not sure.
set(ConfPaths.PERMISSIONS_POLICY_RULES + ".nocheatplus#checks#chat#relog", "INTERVAL:5", 1140);
set(ConfPaths.PERMISSIONS_POLICY_RULES + ".nocheatplus#checks#chat#logins", "INTERVAL:5", 1140);
set(ConfPaths.PERMISSIONS_POLICY_RULES + ".nocheatplus#checks#moving#survivalfly#*", "INTERVAL:5", 1140);
set(ConfPaths.PERMISSIONS_POLICY_RULES, Arrays.asList(
"nocheatplus.admin.debug :: INTERVAL:5",
"nocheatplus.admin* :: ALWAYS",
// TODO: NOTIFY (not command).
// TODO: Command permissions are always checked anyway :p. Will be changed...
"nocheatplus.command* :: ALWAYS",
"nocheatplus.bypass* :: ALWAYS",
"regex:^nocheatplus\\.checks\\..*\\.silent$ :: FALSE",
/*
* Relog, logins: Note: aims at login denial, would invalidate
* once offline/world change. +- not sure.
*/
"nocheatplus.checks.chat.relog :: INTERVAL:10",
"nocheatplus.checks.chat.logins :: INTERVAL:10",
"nocheatplus.checks.chat.* :: INTERVAL:2",
"nocheatplus.checks.net.* :: INTERVAL:2",
"nocheatplus.checks.moving.survivalfly.* :: INTERVAL:5" // (Excludes the sf base permission.)
), 1140);
// Protection features.
// Hide plugins.

View File

@ -170,13 +170,18 @@ public class PermissionSettings {
}
final Map<String, PermissionPolicy> explicitPolicy = new LinkedHashMap<String, PermissionPolicy>();
final List<PermissionRule> implicitRules = new LinkedList<PermissionSettings.PermissionRule>();
final ConfigurationSection section = config.getConfigurationSection(pathRules);
for (String ruleDef : section.getKeys(false)) {
String policyDef = null;
// TODO: Change to List ! +- separators.
final List<String> defs = config.getStringList(pathRules);
for (String def : defs) {
String[] split = def.split(": ", 2);
if (split.length != 2) {
throw new IllegalArgumentException("Must the separate matching rule from the policy definition by ' :: '.");
}
final String ruleDef = RegisteredPermission.toLowerCaseStringRepresentation(split[0].trim());
final String policyDef = split[1].trim();
try {
policyDef = section.getString(ruleDef);
final PermissionPolicy policy = new PermissionPolicy().setPolicyFromConfigLine(policyDef);
ruleDef = RegisteredPermission.toLowerCaseStringRepresentation(ruleDef.trim()).replace('#', '.');
final PermissionRule rule = getMatchingRule(ruleDef, policy);
if (rule == null) {
explicitPolicy.put(ruleDef, policy);

View File

@ -0,0 +1,34 @@
package fr.neatmonster.nocheatplus.test;
import static org.junit.Assert.fail;
import org.junit.Test;
import fr.neatmonster.nocheatplus.permissions.PermissionPolicy;
import fr.neatmonster.nocheatplus.permissions.PermissionSettings;
import fr.neatmonster.nocheatplus.permissions.PermissionSettings.PermissionRule;
public class TestPermissionSettings {
@Test
public void testRegex() {
PermissionPolicy dummy = new PermissionPolicy();
String regex = "^nocheatplus\\.checks\\..*\\.silent$";
String permissionName = "nocheatplus.checks.moving.survivalfly.silent";
// Also/rather a config test.
if (!permissionName.matches(regex)) {
fail("Expect regex to match.");
}
PermissionRule rule = PermissionSettings.getMatchingRule("regex:" + regex, dummy);
if (rule == null) {
fail("Expect factory to return a regex rule.");
}
if (!rule.matches(permissionName)) {
fail("Expect rule to match permissions name.");
}
if (rule.matches("xy" + permissionName) || rule.matches(permissionName + "yx")) {
fail("Rule matches wrong start/end.");
}
}
}