Skip to content

Commit

Permalink
Implement restricted users rules with * wildcard support (Enhancement #…
Browse files Browse the repository at this point in the history
  • Loading branch information
HexelDev authored and sgdc3 committed Jul 25, 2017
1 parent 70d7249 commit a973dc3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
19 changes: 7 additions & 12 deletions src/main/java/fr/xephi/authme/service/ValidationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,19 +160,14 @@ public boolean fulfillsNameRestrictions(Player player) {
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;
Expand Down
11 changes: 10 additions & 1 deletion src/test/java/fr/xephi/authme/service/ValidationServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -350,27 +350,36 @@ public void shouldCheckNameRestrictions() {
// 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
boolean isBobbyAdmitted = validationService.fulfillsNameRestrictions(bobby);
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
assertThat(isBobbyAdmitted, equalTo(true));
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));
}

Expand Down

0 comments on commit a973dc3

Please sign in to comment.