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

commented login window

parent 09df61f1
No related branches found
No related tags found
No related merge requests found
......@@ -16,13 +16,20 @@ import static com.application.DB.AccountHandler.*;
import static com.application.DB.HelpingFunctions.isValidInput;
import static com.application.GUI.Panes.CreateLogoBar.getLogin;
/**
* This class handles all login functionality
*
* @author Eilert Tunheim, Karin Pettersen, Mads Arnesen
* @version 1.0.0
*/
public class LoginPopup {
// Defines input fields
private static PasswordField PASSWORD_TEXT_FIELD = new PasswordField();
private static TextField USERNAME_TEXT_FIELD = new TextField();
/**
* Tries to login
* Tries to log in
*
* @return a boolean if an error should be thrown or not. True gives no error, false throws an error.
*/
......@@ -41,6 +48,9 @@ public class LoginPopup {
}
/**
* This function adds all components to the login window
*/
public static void login(){
Stage window = new Stage();
......@@ -54,7 +64,10 @@ public class LoginPopup {
Button loginButton = new Button("Login");
getPasswordTextField().clear();
// Closes the window if the close button is pressed
closeButton.setOnAction(event -> window.close());
// Handles login if enter is pressed on the keyboard in the password field
getPasswordTextField().setOnKeyPressed( event -> {
if (event.getCode() == KeyCode.ENTER) {
if(!isValidInput(getPasswordTextField().getText()) && !isValidInput(getUsernameTextField().getText())) {
......@@ -65,6 +78,7 @@ public class LoginPopup {
}
});
// Handles login if the login button is pressed
loginButton.setOnAction(event -> {
if(!isValidInput(getPasswordTextField().getText()) && !isValidInput(getUsernameTextField().getText())) {
if (loginButtonPressed()) {
......@@ -73,8 +87,7 @@ public class LoginPopup {
}
});
// Adds all components
VBox layout = new VBox(10);
layout.setAlignment(Pos.CENTER);
layout.getChildren().addAll(userNameLabel, getUsernameTextField(), passwordLabel, getPasswordTextField(), loginButton, closeButton);
......@@ -85,22 +98,28 @@ public class LoginPopup {
window.showAndWait();
}
/**
* This function handles admin window functionality
*/
public static void adminPopup(){
Stage window = new Stage();
window.initModality(Modality.APPLICATION_MODAL);
window.setTitle("Admin window");
// Information fields
Label usernameLabel = new Label("Username: ");
TextField usernameTextfield = new TextField();
usernameTextfield.setText(HelpingFunctions.getUserName());
usernameTextfield.setEditable(false);
// Defines all buttons
Button addUser = new Button("Add User");
Button deleteUser = new Button("Delete User");
Button logout = new Button("Logout");
Button close = new Button("Close");
// Handles functionality for each button
addUser.setOnAction(event -> {
adminAddUser();
});
......@@ -113,7 +132,7 @@ public class LoginPopup {
});
close.setOnAction(event -> window.close());
// adds all components to the VBox
VBox layout = new VBox(10);
layout.setAlignment(Pos.CENTER);
layout.getChildren().addAll(usernameLabel, usernameTextfield, addUser, deleteUser, logout, close);
......@@ -124,12 +143,16 @@ public class LoginPopup {
window.showAndWait();
}
/**
* This function adds a user to the database based on the input parameters given by the user
*/
public static void adminAddUser(){
Stage window = new Stage();
window.initModality(Modality.APPLICATION_MODAL);
window.setTitle("Admin window");
// Defines all labels
Label firstNameLabel = new Label("First Name: ");
Label lastNameLabel = new Label("Last Name: ");
Label phoneNoLabel = new Label("Phone No: ");
......@@ -138,6 +161,7 @@ public class LoginPopup {
Label passwordSecondLabel = new Label("Password Repeat: ");
Label isAdminLabel = new Label("Is Admin: ");
// Defines all input fields
TextField firstNameTextField = new TextField();
TextField lastNameTextField = new TextField();
TextField phoneNoTextField = new TextField();
......@@ -147,9 +171,11 @@ public class LoginPopup {
CheckBox isAdminBox = new CheckBox();
isAdminBox.setSelected(false);
// Defines all buttons
Button close = new Button("Close");
Button addUser = new Button("Add User");
// Handles the functionality for each button
close.setOnAction(event -> window.close());
addUser.setOnAction(event -> {
......@@ -160,7 +186,8 @@ public class LoginPopup {
!isValidInput(usernameTextField.getText()) &&
!isValidInput(passwordFirstField.getText()) &&
!isValidInput(passwordSecondField.getText())) {
// If the passwords match each other, add the user, if not display an errormessage
// If the passwords match each other, add the user, if not, display an error message
if(passwordFirstField.getText().contentEquals(passwordSecondField.getText())){
// Hashing the password if they match.
......@@ -184,7 +211,7 @@ public class LoginPopup {
});
// Adds all components to the VBox
VBox layout = new VBox(10);
layout.setAlignment(Pos.CENTER);
layout.getChildren().addAll(firstNameLabel, firstNameTextField, lastNameLabel, lastNameTextField,
......@@ -198,22 +225,30 @@ public class LoginPopup {
window.showAndWait();
}
/**
* This function handles deleting an account
*/
public static void adminDeleteUser(){
Stage window = new Stage();
window.initModality(Modality.APPLICATION_MODAL);
window.setTitle("Admin window");
// Defines the input field
Label usernameLabel = new Label("Username: ");
TextField usernameTextField = new TextField();
// Defines all buttons
Button close = new Button("Close");
Button delete = new Button("Delete User");
// Handle the functionality for each button
close.setOnAction(event -> window.close());
delete.setOnAction(event -> {
// Validate the input parameter
if(!isValidInput(usernameTextField.getText())) {
try {
// Checks if the user is deleted
boolean results = deleteUser(usernameTextField.getText());
if (results) {
NotificationPopUp.displayNotificationWindow(usernameTextField.getText() + " was successfully deleted!");
......@@ -228,6 +263,7 @@ public class LoginPopup {
}
});
// Adds the components to the VBox
VBox layout = new VBox(10);
layout.setAlignment(Pos.CENTER);
layout.getChildren().addAll(usernameLabel, usernameTextField, delete, close);
......@@ -238,6 +274,9 @@ public class LoginPopup {
window.showAndWait();
}
/**
* This function handles logout functionality
*/
public static void logout(){
getLogin().setText("Login");
HelpingFunctions.setFirstName("");
......@@ -247,34 +286,41 @@ public class LoginPopup {
HelpingFunctions.setUserName("");
}
/**
* This function handles popup screen for a normal user
*/
public static void userPopup(){
Stage window = new Stage();
window.initModality(Modality.APPLICATION_MODAL);
window.setTitle("User window");
// Defines labels
Label nameLabel = new Label("Name: ");
Label phoneNoLabel = new Label("Phone no: ");
// Defines information fields
TextField nameTextfield = new TextField();
TextField phoneNoTextField = new TextField();
nameTextfield.setEditable(false);
phoneNoTextField.setEditable(false);
// Sets the values
nameTextfield.setText(HelpingFunctions.getFirstName() + " " + HelpingFunctions.getLastName());
phoneNoTextField.setText(HelpingFunctions.getPhoneNo());
// Define buttons
Button close = new Button("Close");
Button logout = new Button("Logout");
// Handle each button
close.setOnAction(event -> window.close());
logout.setOnAction(event -> {
logout();
window.close();
});
// Adds all components to the VBox
VBox layout = new VBox(10);
layout.setAlignment(Pos.CENTER);
layout.getChildren().addAll(nameLabel, nameTextfield, phoneNoLabel, phoneNoTextField, logout, close);
......@@ -285,6 +331,12 @@ public class LoginPopup {
window.showAndWait();
}
/**
* This function hash the password using the SHA-512 algorithm
*
* @param password input parameter for the password
* @return a 64-bit hashed string
*/
public static String hashPassword(String password){
try {
MessageDigest messageDigest = MessageDigest.getInstance("SHA-512");
......
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