Implement restricted users rules with * wildcard support (Enhancement #1278 and #1227) (#1297)

This commit is contained in:
HexelDev 2017-07-25 21:39:31 +02:00 committed by Gabriele C
parent 70d7249e12
commit a973dc3f6d
2 changed files with 17 additions and 13 deletions

View File

@ -160,19 +160,14 @@ public class ValidationService implements Reloadable {
for(String restriction : restrictions) {
if(restriction.startsWith("regex:")) {
restriction = restriction.replace("regex:", "");
if(ip.matches(restriction)) {
return true;
}
if(domain.matches(restriction)) {
return true;
}
} else {
if(ip.equals(restriction)) {
return true;
}
if(domain.equalsIgnoreCase(restriction)) {
return true;
}
restriction = restriction.replaceAll("\\*","(.*)");
}
if(ip.matches(restriction)) {
return true;
}
if(domain.matches(restriction)) {
return true;
}
}
return false;

View File

@ -350,13 +350,16 @@ public class ValidationServiceTest {
// given
given(settings.getProperty(RestrictionSettings.ENABLE_RESTRICTED_USERS)).willReturn(true);
given(settings.getProperty(RestrictionSettings.RESTRICTED_USERS))
.willReturn(Arrays.asList("Bobby;127.0.0.4", "Tamara;32.24.16.8", "Gabriel;regex:93\\.23\\.44\\..*"));
.willReturn(Arrays.asList("Bobby;127.0.0.4", "Tamara;32.24.16.8", "Gabriel;regex:93\\.23\\.44\\..*", "emanuel;94.65.24.*", "imyourisp;*.yourisp.net"));
validationService.reload();
Player bobby = mockPlayer("bobby", "127.0.0.4");
Player tamara = mockPlayer("taMARA", "8.8.8.8");
Player gabriel = mockPlayer("Gabriel", "93.23.44.65");
Player gabriel2 = mockPlayer("Gabriel", "93.23.33.34");
Player emanuel = mockPlayer("emanuel", "94.65.24.10");
Player emanuel2 = mockPlayer("emanuel", "94.65.60.10");
Player imyourisp = mockPlayer("imyourisp", "bazinga.yourisp.net");
Player notRestricted = mockPlayer("notRestricted", "0.0.0.0");
// when
@ -364,6 +367,9 @@ public class ValidationServiceTest {
boolean isTamaraAdmitted = validationService.fulfillsNameRestrictions(tamara);
boolean isGabrielAdmitted = validationService.fulfillsNameRestrictions(gabriel);
boolean isGabriel2Admitted = validationService.fulfillsNameRestrictions(gabriel2);
boolean isEmanuelAdmitted = validationService.fulfillsNameRestrictions(emanuel);
boolean isEmanuel2Admitted = validationService.fulfillsNameRestrictions(emanuel2);
boolean isImyourispAdmitted = validationService.fulfillsNameRestrictions(imyourisp);
boolean isNotRestrictedAdmitted = validationService.fulfillsNameRestrictions(notRestricted);
// then
@ -371,6 +377,9 @@ public class ValidationServiceTest {
assertThat(isTamaraAdmitted, equalTo(false));
assertThat(isGabrielAdmitted, equalTo(true));
assertThat(isGabriel2Admitted, equalTo(false));
assertThat(isEmanuelAdmitted, equalTo(true));
assertThat(isEmanuel2Admitted, equalTo(false));
assertThat(isImyourispAdmitted, equalTo(true));
assertThat(isNotRestrictedAdmitted, equalTo(true));
}