Skip to content
Snippets Groups Projects
Commit e3b3ab56 authored by Eilert Tunheim's avatar Eilert Tunheim
Browse files

Added full functionality for adding an user

parent 564ed8ba
No related branches found
No related tags found
No related merge requests found
......@@ -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);
}
}
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;
}
......
No preview for this file type
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment