From d5fa0d17a12c3089b37425a408abd7d8f96d10f3 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Sat, 27 Jun 2020 08:25:37 +0200 Subject: [PATCH 1/3] Validate user name during login and registration --- src/main/java/envoy/client/ui/controller/LoginScene.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/envoy/client/ui/controller/LoginScene.java b/src/main/java/envoy/client/ui/controller/LoginScene.java index b17fa81..5c6fab9 100644 --- a/src/main/java/envoy/client/ui/controller/LoginScene.java +++ b/src/main/java/envoy/client/ui/controller/LoginScene.java @@ -23,6 +23,7 @@ import envoy.event.EventBus; import envoy.event.HandshakeRejection; import envoy.event.MessageStatusChange; import envoy.exception.EnvoyException; +import envoy.util.Bounds; import envoy.util.EnvoyLog; /** @@ -115,8 +116,11 @@ public final class LoginScene { if (registerCheckBox.isSelected() && !passwordField.getText().equals(repeatPasswordField.getText())) { new Alert(AlertType.ERROR, "The entered password is unequal to the repeated one").showAndWait(); repeatPasswordField.clear(); - } - else performHandshake( + } else if (!Bounds.isValidContactName(userTextField.getText())) { + new Alert(AlertType.ERROR, "The entered user name is not valid (" + Bounds.CONTACT_NAME_PATTERN.toString() + ")").showAndWait(); + userTextField.clear(); + } else + performHandshake( new LoginCredentials(userTextField.getText(), passwordField.getText().toCharArray(), registerCheckBox.isSelected(), Startup.VERSION)); } From 3d6a38e3546f721eae8f471f0e74ee0f3fc553a6 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Sat, 27 Jun 2020 09:03:59 +0200 Subject: [PATCH 2/3] Validate group name and size on creation --- .../ui/controller/GroupCreationScene.java | 40 ++++++++++++------- .../client/ui/controller/LoginScene.java | 2 +- .../resources/fxml/GroupCreationScene.fxml | 6 +-- 3 files changed, 30 insertions(+), 18 deletions(-) diff --git a/src/main/java/envoy/client/ui/controller/GroupCreationScene.java b/src/main/java/envoy/client/ui/controller/GroupCreationScene.java index ea4a58e..f4311aa 100644 --- a/src/main/java/envoy/client/ui/controller/GroupCreationScene.java +++ b/src/main/java/envoy/client/ui/controller/GroupCreationScene.java @@ -5,15 +5,16 @@ import java.util.stream.Collectors; import javafx.application.Platform; import javafx.fxml.FXML; import javafx.scene.control.*; +import javafx.scene.control.Alert.AlertType; import envoy.client.data.LocalDB; import envoy.client.event.SendEvent; import envoy.client.ui.ContactListCell; import envoy.client.ui.SceneContext; import envoy.data.Contact; -import envoy.data.User; import envoy.event.EventBus; import envoy.event.GroupCreation; +import envoy.util.Bounds; /** * Project: envoy-client
@@ -25,9 +26,6 @@ import envoy.event.GroupCreation; */ public class GroupCreationScene { - @FXML - private Button backButton; - @FXML private Button createButton; @@ -39,7 +37,7 @@ public class GroupCreationScene { private SceneContext sceneContext; - private static EventBus eventBus = EventBus.getInstance(); + private static final EventBus eventBus = EventBus.getInstance(); @FXML private void initialize() { @@ -56,22 +54,36 @@ public class GroupCreationScene { public void initializeData(SceneContext sceneContext, LocalDB localDB) { this.sceneContext = sceneContext; Platform.runLater(() -> contactList.getItems() - .addAll(localDB.getUsers() - .values() - .stream() - .filter(c -> c instanceof User && c.getID() != localDB.getUser().getID()) - .collect(Collectors.toList()))); + .addAll(localDB.getUsers().values().stream().filter(c -> c.getID() != localDB.getUser().getID()).collect(Collectors.toList()))); } /** - * Sends a {@link GroupCreation} to the server. + * Enables the {@code createButton} if at least one contact is selected. + * + * @since Envoy Client v0.1-beta + */ + @FXML + private void contactListClicked() { createButton.setDisable(contactList.getSelectionModel().isEmpty()); } + + /** + * Sends a {@link GroupCreation} to the server and closes this scene. + *

+ * If the given group name is not valid, an error is displayed instead. * * @since Envoy Client v0.1-beta */ @FXML - private void sendGroupObject() { - eventBus.dispatch(new SendEvent(new GroupCreation(groupNameBar.getText(), - contactList.getSelectionModel().getSelectedItems().stream().map(Contact::getID).collect(Collectors.toSet())))); + private void createButtonClicked() { + final var name = groupNameBar.getText(); + if (!Bounds.isValidContactName(name)) { + new Alert(AlertType.ERROR, "The entered group name is not valid (" + Bounds.CONTACT_NAME_PATTERN + ")").showAndWait(); + groupNameBar.clear(); + } else { + eventBus.dispatch(new SendEvent(new GroupCreation(name, + contactList.getSelectionModel().getSelectedItems().stream().map(Contact::getID).collect(Collectors.toSet())))); + new Alert(AlertType.INFORMATION, String.format("Group '%s' successfully created.", name)).showAndWait(); + sceneContext.pop(); + } } @FXML diff --git a/src/main/java/envoy/client/ui/controller/LoginScene.java b/src/main/java/envoy/client/ui/controller/LoginScene.java index 5c6fab9..01e456d 100644 --- a/src/main/java/envoy/client/ui/controller/LoginScene.java +++ b/src/main/java/envoy/client/ui/controller/LoginScene.java @@ -117,7 +117,7 @@ public final class LoginScene { new Alert(AlertType.ERROR, "The entered password is unequal to the repeated one").showAndWait(); repeatPasswordField.clear(); } else if (!Bounds.isValidContactName(userTextField.getText())) { - new Alert(AlertType.ERROR, "The entered user name is not valid (" + Bounds.CONTACT_NAME_PATTERN.toString() + ")").showAndWait(); + new Alert(AlertType.ERROR, "The entered user name is not valid (" + Bounds.CONTACT_NAME_PATTERN + ")").showAndWait(); userTextField.clear(); } else performHandshake( diff --git a/src/main/resources/fxml/GroupCreationScene.fxml b/src/main/resources/fxml/GroupCreationScene.fxml index 8e36d11..9fbdc92 100644 --- a/src/main/resources/fxml/GroupCreationScene.fxml +++ b/src/main/resources/fxml/GroupCreationScene.fxml @@ -38,21 +38,21 @@ - + - -