From 9d6af97ef15c1fc0bb88ecd539329b45f1656b00 Mon Sep 17 00:00:00 2001 From: Sebastian Stenzel Date: Sat, 4 Oct 2014 19:17:39 +0200 Subject: [PATCH] username max length --- .../oce/ui/AdvancedController.java | 17 +++++++------ .../oce/ui/InitializeController.java | 25 +++++++++++++++---- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/oce-main/oce-ui/src/main/java/de/sebastianstenzel/oce/ui/AdvancedController.java b/oce-main/oce-ui/src/main/java/de/sebastianstenzel/oce/ui/AdvancedController.java index a169e8f36..d0dafd8d5 100644 --- a/oce-main/oce-ui/src/main/java/de/sebastianstenzel/oce/ui/AdvancedController.java +++ b/oce-main/oce-ui/src/main/java/de/sebastianstenzel/oce/ui/AdvancedController.java @@ -11,9 +11,6 @@ package de.sebastianstenzel.oce.ui; import java.net.URL; import java.util.ResourceBundle; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.event.EventHandler; @@ -22,10 +19,15 @@ import javafx.fxml.Initializable; import javafx.scene.control.TextField; import javafx.scene.input.KeyEvent; import javafx.scene.layout.GridPane; + +import org.apache.commons.lang3.CharUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import de.sebastianstenzel.oce.ui.settings.Settings; public class AdvancedController implements Initializable { - + private static final Logger LOG = LoggerFactory.getLogger(AdvancedController.class); @FXML @@ -40,22 +42,23 @@ public class AdvancedController implements Initializable { portTextField.addEventFilter(KeyEvent.KEY_TYPED, new NumericKeyTypeEventFilter()); portTextField.focusedProperty().addListener(new PortTextFieldFocusListener()); } - + /** * Consumes key events, if typed key is not 0-9. */ private static final class NumericKeyTypeEventFilter implements EventHandler { + @Override public void handle(KeyEvent t) { if (t.getCharacter() == null || t.getCharacter().length() == 0) { return; } char c = t.getCharacter().charAt(0); - if (!(c >= '0' && c <= '9')) { + if (!CharUtils.isAsciiNumeric(c)) { t.consume(); } } } - + /** * Saves port settings, when textfield loses focus. */ diff --git a/oce-main/oce-ui/src/main/java/de/sebastianstenzel/oce/ui/InitializeController.java b/oce-main/oce-ui/src/main/java/de/sebastianstenzel/oce/ui/InitializeController.java index 4b0f2f3a8..686c77c8d 100644 --- a/oce-main/oce-ui/src/main/java/de/sebastianstenzel/oce/ui/InitializeController.java +++ b/oce-main/oce-ui/src/main/java/de/sebastianstenzel/oce/ui/InitializeController.java @@ -19,20 +19,22 @@ import java.nio.file.InvalidPathException; import java.nio.file.Path; import java.nio.file.StandardOpenOption; import java.util.ResourceBundle; -import java.util.regex.Pattern; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.event.ActionEvent; +import javafx.event.EventHandler; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TextField; +import javafx.scene.input.KeyEvent; import javafx.scene.layout.GridPane; import javafx.stage.DirectoryChooser; import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.CharUtils; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -45,7 +47,7 @@ import de.sebastianstenzel.oce.ui.util.MasterKeyFilter; public class InitializeController implements Initializable { private static final Logger LOG = LoggerFactory.getLogger(InitializeController.class); - private static final Pattern USERNAME_PATTERN = Pattern.compile("[a-z0-9_-]*", Pattern.CASE_INSENSITIVE); + private static final int MAX_USERNAME_LENGTH = 200; private ResourceBundle localization; @FXML @@ -67,6 +69,7 @@ public class InitializeController implements Initializable { public void initialize(URL url, ResourceBundle rb) { this.localization = rb; workDirTextField.textProperty().addListener(new WorkDirChangeListener()); + usernameField.addEventFilter(KeyEvent.KEY_TYPED, new AlphaNumericKeyTypeEventFilter()); usernameField.textProperty().addListener(new UsernameChangeListener()); usernameField.disableProperty().addListener(new ClearOnDisableListener(usernameField)); passwordField.textProperty().addListener(new PasswordChangeListener()); @@ -118,12 +121,24 @@ public class InitializeController implements Initializable { /** * Step 2: Choose a valid username */ + private static final class AlphaNumericKeyTypeEventFilter implements EventHandler { + @Override + public void handle(KeyEvent t) { + if (t.getCharacter() == null || t.getCharacter().length() == 0) { + return; + } + char c = t.getCharacter().charAt(0); + if (!CharUtils.isAsciiAlphanumeric(c)) { + t.consume(); + } + } + } + private final class UsernameChangeListener implements ChangeListener { @Override public void changed(ObservableValue property, String oldValue, String newValue) { - final boolean isValidUsername = USERNAME_PATTERN.matcher(newValue).matches(); - if (!isValidUsername) { - usernameField.setText(oldValue); + if (StringUtils.length(newValue) > MAX_USERNAME_LENGTH) { + usernameField.setText(newValue.substring(0, MAX_USERNAME_LENGTH)); } passwordField.setDisable(StringUtils.isEmpty(usernameField.getText())); }