diff --git a/src/main/java/com/application/DB/Account_handler.java b/src/main/java/com/application/DB/Account_handler.java index 2c05fa0b49d607f0a4a593f904c51553f0150a71..ff6383212d8ff517de9929a1231edd59c3b95957 100644 --- a/src/main/java/com/application/DB/Account_handler.java +++ b/src/main/java/com/application/DB/Account_handler.java @@ -51,4 +51,15 @@ public class Account_handler { } } + public static void addUser(String firstName, String lastName, String phoneNo, String username, String password, boolean isAdmin) throws Exception { + // Sqlstatement + final String sqlStatement = + "INSERT INTO " + PROJECT_ID + "." + LOCATION_ID + "." + USERS_TABLE_NAME + "(First_name, Last_name, Phone_no, Username, Password, Admin) " + + "VALUES("+'"'+firstName+'"'+","+'"'+lastName+'"'+","+'"'+phoneNo+'"'+","+'"'+username+'"'+","+'"'+password+'"'+","+ isAdmin+") "; + + System.out.println(sqlStatement); + + HelpingFunctions.createQueryJob(sqlStatement); + } + } diff --git a/src/main/java/com/application/GUI/PopUpWindows/LoginPopup.java b/src/main/java/com/application/GUI/PopUpWindows/LoginPopup.java index 2e28bb1277500b785182163a48089f4b30da4edb..d256ef91eeb4e24e4d22d81e7424b9b0c577f292 100644 --- a/src/main/java/com/application/GUI/PopUpWindows/LoginPopup.java +++ b/src/main/java/com/application/GUI/PopUpWindows/LoginPopup.java @@ -1,22 +1,17 @@ package com.application.GUI.PopUpWindows; -import com.application.GUI.Panes.LogoBar; +import com.application.DB.Account_handler; import javafx.geometry.Pos; import javafx.scene.Scene; -import javafx.scene.control.Button; -import javafx.scene.control.Label; -import javafx.scene.control.PasswordField; -import javafx.scene.control.TextField; +import javafx.scene.control.*; import javafx.scene.layout.VBox; import javafx.stage.Modality; import javafx.stage.Stage; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; -import java.security.SecureRandom; -import java.util.Arrays; +import static com.application.DB.Account_handler.addUser; import static com.application.DB.Account_handler.getAccount; import static com.application.DB.Constants.*; import static com.application.GUI.Panes.LogoBar.getLogin; @@ -44,31 +39,14 @@ public class LoginPopup { loginButton.setOnAction(event -> { try { - MessageDigest messageDigest = MessageDigest.getInstance("SHA-512"); - - assert messageDigest != null; - messageDigest.update(getPasswordTextField().getText().getBytes(StandardCharsets.UTF_8)); - - byte[] hashedPassword = messageDigest.digest(); - - StringBuilder hashedPasswordString = new StringBuilder(); - - for (byte b: hashedPassword) { - hashedPasswordString.append(String.format("%02x",b)); - } - - getAccount(userNameTextField.getText(),hashedPasswordString.toString()); - - if(!getLogin().getText().equals("Login")){ - window.close(); - } - + getAccount(userNameTextField.getText(), hashPassword(getPasswordTextField().getText())); } catch (Exception e) { e.printStackTrace(); } - - + if(!getLogin().getText().equals("Login")){ + window.close(); + } }); @@ -100,16 +78,14 @@ public class LoginPopup { Button logout = new Button("Logout"); addUser.setOnAction(event -> { - window.close(); adminAddUser(); }); deleteUser.setOnAction(event -> { - window.close(); adminDeleteUser(); }); logout.setOnAction(event -> { - window.close(); logout(); + window.close(); }); @@ -132,18 +108,58 @@ public class LoginPopup { Label firstNameLabel = new Label("First Name: "); Label lastNameLabel = new Label("Last Name: "); Label phoneNoLabel = new Label("Phone No: "); - Label username = new Label("Username: "); - Label passwordFirst = new Label("Password"); + Label usernameLabel = new Label("Username: "); + Label passwordFirstLabel = new Label("Password : "); + Label passwordSecondLabel = new Label("Password Repeat: "); + Label isAdminLabel = new Label("Is Admin: "); + TextField firstNameTextField = new TextField(); + TextField lastNameTextField = new TextField(); + TextField phoneNoTextField = new TextField(); + TextField usernameTextField = new TextField(); + PasswordField passwordFirstField = new PasswordField(); + PasswordField passwordSecondField = new PasswordField(); + CheckBox isAdminBox = new CheckBox(); + isAdminBox.setSelected(false); + Button close = new Button("Close"); + Button addUser = new Button("Add User"); + close.setOnAction(event -> window.close()); + addUser.setOnAction(event -> { + + // If the passwords match each other, add the user, if not display an errormessage + if(passwordFirstField.getText().contentEquals(passwordSecondField.getText())){ + + // Hashing the password if they match. + String hashedPassword = hashPassword(passwordFirstField.getText()); + + // Tries to add the user + try { + addUser(firstNameTextField.getText(), lastNameTextField.getText(), phoneNoTextField.getText(), + usernameTextField.getText(), hashedPassword, isAdminBox.isSelected()); + NotificationPopUp.displayNotificationWindow("Successfully added user!"); + window.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } else { + NotificationPopUp.displayNotificationWindow("Passwords does not match!"); + passwordFirstField.clear(); + passwordSecondField.clear(); + } + + }); VBox layout = new VBox(10); layout.setAlignment(Pos.CENTER); - layout.getChildren().addAll(); + layout.getChildren().addAll(firstNameLabel, firstNameTextField, lastNameLabel, lastNameTextField, + phoneNoLabel, phoneNoTextField, usernameLabel, usernameTextField, + passwordFirstLabel, passwordFirstField, passwordSecondLabel, passwordSecondField, + isAdminLabel, isAdminBox, addUser, close); - Scene scene = new Scene(layout, 500, 300); + Scene scene = new Scene(layout, 500, 600); scene.getStylesheets().add(InputPopup.class.getResource("/com.application/CSS/styleSheet.css").toExternalForm()); window.setScene(scene); window.showAndWait(); @@ -155,14 +171,8 @@ public class LoginPopup { window.initModality(Modality.APPLICATION_MODAL); window.setTitle("Admin window"); - Label firstNameLabel = new Label("First Name: "); - Label lastNameLabel = new Label("Last Name: "); - Label phoneNoLabel = new Label("Phone No: "); - Label username = new Label("Username: "); - Label passwordFirst = new Label("Password"); - - - + Label usernameLabel = new Label("Username: "); + TextField usernameTextField = new TextField(); VBox layout = new VBox(10); @@ -197,14 +207,24 @@ public class LoginPopup { TextField nameTextfield = new TextField(); TextField phoneNoTextField = new TextField(); + nameTextfield.setEditable(false); + phoneNoTextField.setEditable(false); + nameTextfield.setText(getFirstName() + " " + getLastName()); phoneNoTextField.setText(getPhoneNo()); + + Button close = new Button("Close"); Button logout = new Button("Logout"); + close.setOnAction(event -> window.close()); + logout.setOnAction(event -> { + logout(); + window.close(); + }); VBox layout = new VBox(10); layout.setAlignment(Pos.CENTER); - layout.getChildren().addAll(nameLabel, nameTextfield, phoneNoLabel, phoneNoTextField, logout); + layout.getChildren().addAll(nameLabel, nameTextfield, phoneNoLabel, phoneNoTextField, logout, close); Scene scene = new Scene(layout, 500, 300); scene.getStylesheets().add(InputPopup.class.getResource("/com.application/CSS/styleSheet.css").toExternalForm()); @@ -212,6 +232,29 @@ public class LoginPopup { window.showAndWait(); } + public static String hashPassword(String password){ + try { + MessageDigest messageDigest = MessageDigest.getInstance("SHA-512"); + + assert messageDigest != null; + messageDigest.update(password.getBytes(StandardCharsets.UTF_8)); + + byte[] hashedPassword = messageDigest.digest(); + + StringBuilder hashedPasswordString = new StringBuilder(); + + for (byte b: hashedPassword) { + hashedPasswordString.append(String.format("%02x",b)); + } + + return hashedPasswordString.toString(); + + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + public static PasswordField getPasswordTextField() { return PASSWORD_TEXT_FIELD; } diff --git a/target/classes/com/application/DB/Account_handler.class b/target/classes/com/application/DB/Account_handler.class index 8d513275eda4d202e46c120ea5da851ea1fba4e1..6e0dd40dcbbfaca02fad27009793c8cbbaf28c39 100644 Binary files a/target/classes/com/application/DB/Account_handler.class and b/target/classes/com/application/DB/Account_handler.class differ diff --git a/target/classes/com/application/GUI/PopUpWindows/LoginPopup.class b/target/classes/com/application/GUI/PopUpWindows/LoginPopup.class index 3f8a75275aab99173bb5eb93f608c03a1989df38..d4a94a6fe190f467c0b42a87ea149c36ce524ac6 100644 Binary files a/target/classes/com/application/GUI/PopUpWindows/LoginPopup.class and b/target/classes/com/application/GUI/PopUpWindows/LoginPopup.class differ