Commit c25079ee authored by Martin Slind Hagen's avatar Martin Slind Hagen 🏑 Committed by Olaf Rosendahl
Browse files

Add min width and height to ConfirmationBox

Add delete user
parent 4d09623d
......@@ -4,6 +4,7 @@ import Database.Hibernate;
import Roots.LoginRoot;
import Main.ApplicationManager;
import java.util.logging.Level;
import javax.persistence.PersistenceException;
/**
* Authentication class for authenticating username and password for each user
......@@ -68,23 +69,73 @@ public final class Authentication {
} else {
return false;
}
} catch (ExceptionInInitializerError | NoClassDefFoundError e) {
} catch (ExceptionInInitializerError | NoClassDefFoundError | PersistenceException e) {
FileLogger.getLogger().log(Level.FINE, e.getMessage());
FileLogger.closeHandler();
throw e;
} catch (Exception e) {
}
}
/**
* Checks if a user with a username and password is the current user logged in
*
* @param username of user
* @param password of user
* @return if this user is the current user
*/
public static boolean isCurrentUser(String username, String password){
try {
// Getting salt from db using username
String salt = Hibernate.getSalt(username);
// Generating hash using salt
String encrypter = Encrypter.encrypt(password, salt);
String hash = Encrypter.getHash(encrypter);
// Checks if the current user's hash is equal to the hash from the password entered
return UserInfo.getUser().getHash().equals(hash);
} catch (ExceptionInInitializerError | NoClassDefFoundError | PersistenceException e) {
FileLogger.getLogger().log(Level.FINE, e.getMessage());
FileLogger.closeHandler();
return false;
throw e;
}
}
/**
* Deletes the current logged in user
* will compare password from db to the entered password
*
* @param username of user
* @param password of user
* @param confirmation if the user confirmed to delete the user
* @return if deleting the user was successful
*/
public static boolean deleteUser(String username, String password, boolean confirmation){
try {
// Checks if this is the current user and the user has given consent
if (isCurrentUser(username, password) && confirmation) {
Hibernate.deleteUser(username);
return true;
} else {
return false;
}
} catch (ExceptionInInitializerError | NoClassDefFoundError | PersistenceException e) {
FileLogger.getLogger().log(Level.FINE, e.getMessage());
FileLogger.closeHandler();
throw e;
}
}
/**
* Log out the user and redirect to login scene
*
* @return loginRoot the root the user is sent to
*/
public static void logout() {
public static LoginRoot logout() {
UserInfo.logOut();
Hibernate.getEm().clear();
ApplicationManager.setRoot(new LoginRoot());
LoginRoot loginRoot = new LoginRoot();
ApplicationManager.setRoot(loginRoot);
// Returning loginRoot allows adding a message when logging out the user
return loginRoot;
}
}
......@@ -21,12 +21,18 @@ public final class ConfirmationBox {
/**
* Creates the confirmation box and displays it
*
* @param width the width of the confirmation box, if less than 200 it is set to 200
* @param height the height of the confirmation box, if less than 100 it is set to 100
* @param title the title of the conformation box
* @param message the massage that the confirmation box is going to display
* @param message the message that the confirmation box is going to display
* @return boolean answer of the confirmation box
*/
public static boolean display(String title, String message) {
PopUpWindow dialogWindow = new PopUpWindow(ApplicationManager.getStage(), 250, 150);
public static boolean display(int width, int height, String title, String message) {
// Makes sure the confirmation box size is at least 200 x 100
if (width < 200) width = 200;
if (height < 100) height = 100;
PopUpWindow dialogWindow = new PopUpWindow(ApplicationManager.getStage(), width, height);
dialogWindow.getDialogWindow().close();
Button yesButton = new Button("Yes");
......@@ -41,11 +47,11 @@ public final class ConfirmationBox {
dialogWindow.getDialogWindow().close();
});
Css.setButton(150,30,15,yesButton,noButton);
Css.setButton(width - 100, 30, 15, yesButton, noButton);
dialogWindow.getDialogWindow().setTitle(title);
dialogWindow.getDialogText().setText(message);
dialogWindow.getDialogVBox().getChildren().addAll(yesButton,noButton);
dialogWindow.getDialogVBox().getChildren().addAll(yesButton, noButton);
dialogWindow.getDialogWindow().showAndWait();
......
......@@ -88,7 +88,7 @@ public final class ApplicationManager {
* If the user exits the program the connection to the database will be closed
*/
private static void closeProgram() {
boolean close = ConfirmationBox.display("Exit", "Are you sure you want to exit?");
boolean close = ConfirmationBox.display(250, 150, "Exit", "Are you sure you want to exit?");
if (close) {
stage.close();
Hibernate.getEm().clear();
......
......@@ -32,7 +32,7 @@ final class AlbumsRoot extends SceneRoot {
private final ScrollPane SCROLL_PANE = new ScrollPane();
private final VBox SCROLL_PANE_VBOX = new VBox();
private final Button NEW_ALBUM_BUTTON = new Button("New album");
private final Button DELETE_ALBUM_BUTTON = new Button("Delete album");
private final Button DELETE_ALBUM_BUTTON = new Button("Delete selected albums");
private final Text feedbackText = new Text();
private final List<AlbumContainer> ALBUM_CONTAINER_LIST = new ArrayList<>();
......@@ -93,7 +93,7 @@ final class AlbumsRoot extends SceneRoot {
Css.setTextFont(17, feedbackText);
SCROLL_PANE_VBOX.getChildren().add(feedbackText);
SCROLL_PANE_VBOX.setAlignment(Pos.CENTER);
feedbackText.setText("No albums registered: Add an album by pressing the add album button");
feedbackText.setText("No albums registered: Add an album by pressing the \"New album\" button");
feedbackText.setTextAlignment(TextAlignment.RIGHT);
DELETE_ALBUM_BUTTON.setDisable(true);
}
......
package Roots;
import Components.Authentication;
import Components.ConfirmationBox;
import Components.FileLogger;
import Components.UserInfo;
import Css.Css;
import Css.FeedbackType;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javax.persistence.PersistenceException;
import java.util.logging.Level;
/**
* Class for the delete user root
*/
final class DeleteUserRoot extends SceneRoot {
private final Label INSTRUCTIONS_LABEL = new Label("Enter password to delete your user:");
private final PasswordField PASSWORD_FIELD = new PasswordField();
private final Label FEEDBACK_LABEL = new Label();
private final Button DELETE_USER_BUTTON = new Button("Delete user");
/**
* DeleteUserRoot constructor
* Uses SceneRoot constructor to create an object of the DeleteUserRoot class
*/
DeleteUserRoot() {
super();
this.setLayout();
}
/**
* Overrides SceneRoot method.
* Assigns layout components to RootBuilders GridPane
* Sets styling to layout components
* Sets functionality to button nodes
* Used in constructor
*/
@Override
void setLayout() {
super.setLayout();
super.setPageTitle("Delete user");
super.getGridPane().add(INSTRUCTIONS_LABEL, 0, 0);
super.getGridPane().add(PASSWORD_FIELD, 0, 1);
super.getGridPane().add(DELETE_USER_BUTTON, 0, 2);
super.getGridPane().add(FEEDBACK_LABEL, 0, 3);
PASSWORD_FIELD.setPromptText("Password");
//Sets styling for the layout components
Css.setButton(700, 25, 20, DELETE_USER_BUTTON);
Css.setTextField(700, 20, 17, PASSWORD_FIELD);
Css.setLabel(20, INSTRUCTIONS_LABEL);
DELETE_USER_BUTTON.setDefaultButton(true);
DELETE_USER_BUTTON.setOnAction(e -> deleteUser());
}
/**
* Deletes the current user's user
* If the password is correct and the user confirms the choice
*/
private void deleteUser() {
try {
String currentUsername = UserInfo.getUser().getUsername();
if (PASSWORD_FIELD.getText().trim().length() == 0) {
Css.playFeedBackLabelTransition(FeedbackType.ERROR, "Password is missing", 13, FEEDBACK_LABEL);
} else if (Authentication.isCurrentUser(currentUsername, PASSWORD_FIELD.getText())) {
if (Authentication.deleteUser(currentUsername, PASSWORD_FIELD.getText(), ConfirmationBox.display(350, 150, "Delete user", "Are you sure you want to delete your user?"))) {
Css.playFeedBackLabelTransition(FeedbackType.SUCCESSFUL, "User successfully deleted", 13, Authentication.logout().getLOG_IN_LABEL());
}
} else {
Css.playFeedBackLabelTransition(FeedbackType.ERROR, "Password does not match you current password", 13, FEEDBACK_LABEL);
}
} catch (ExceptionInInitializerError | PersistenceException e) {
Css.playFeedBackLabelTransition(FeedbackType.ERROR, "Could not connect to database", 13, FEEDBACK_LABEL);
FileLogger.getLogger().log(Level.FINE, e.getMessage());
FileLogger.closeHandler();
}
}
}
......@@ -9,6 +9,7 @@ import javafx.animation.PauseTransition;
import javafx.scene.control.*;
import javafx.util.Duration;
import java.util.logging.Level;
import javax.persistence.PersistenceException;
/**
* Class for the login root
......@@ -25,14 +26,19 @@ public final class LoginRoot extends SceneRoot {
private final ProgressIndicator LOADING_ANIMATION = new ProgressIndicator();
/**
* Login constructor
* Uses SceneRoot constructor to create an object of the Login class
* LoginRoot constructor
* Uses SceneRoot constructor to create an object of the LoginRoot class
*/
public LoginRoot() {
super();
this.setLayout();
}
Label getLOG_IN_LABEL() {
// Used in DeleteUserRoot to show successful deletion when redirected to the LoginRoot
return LOG_IN_LABEL;
}
/**
* Overrides SceneRoot method.
* Assigns layout components to RootBuilders GridPane
......@@ -77,15 +83,15 @@ public final class LoginRoot extends SceneRoot {
pause.setOnFinished(p -> {
try {
if (USERNAME_FIELD.getText().trim().length() == 0 || PASSWORD_FIELD.getText().trim().length() == 0) {
Css.playFeedBackLabelTransition(FeedbackType.ERROR, "Username or Password is missing", 13, LOG_IN_LABEL);
Css.playFeedBackLabelTransition(FeedbackType.ERROR, "Username or password is missing", 13, LOG_IN_LABEL);
LOADING_ANIMATION.setVisible(false);
} else if (Authentication.logIn(USERNAME_FIELD.getText(), PASSWORD_FIELD.getText())) {
ApplicationManager.setRoot(new MenuRoot());
} else {
Css.playFeedBackLabelTransition(FeedbackType.ERROR, "Log in failed", 13, LOG_IN_LABEL);
Css.playFeedBackLabelTransition(FeedbackType.ERROR, "Username or password is wrong", 13, LOG_IN_LABEL);
LOADING_ANIMATION.setVisible(false);
}
} catch (ExceptionInInitializerError | javax.persistence.PersistenceException e) {
} catch (ExceptionInInitializerError | PersistenceException e) {
Css.playFeedBackLabelTransition(FeedbackType.ERROR, "Could not connect to database", 13, LOG_IN_LABEL);
LOADING_ANIMATION.setVisible(false);
FileLogger.getLogger().log(Level.FINE, e.getMessage());
......
......@@ -15,6 +15,7 @@ public final class MenuRoot extends SceneRoot {
private final Button ALBUMS_BUTTON = new Button("Albums");
private final Button MAP_BUTTON = new Button("Map");
private final Button LOG_OUT_BUTTON = new Button("Log out");
private final Button DELETE_USER_BUTTON = new Button("Delete user");
/**
* Instantiates a new Menu root.
......@@ -34,18 +35,20 @@ public final class MenuRoot extends SceneRoot {
super.setLayout();
super.setPageTitle("Menu");
Css.setButton(340,100,40, UPLOAD_BUTTON, PHOTOS_BUTTON, ALBUMS_BUTTON, MAP_BUTTON, LOG_OUT_BUTTON);
Css.setButton(340,100,40, UPLOAD_BUTTON, PHOTOS_BUTTON, ALBUMS_BUTTON, MAP_BUTTON, LOG_OUT_BUTTON, DELETE_USER_BUTTON);
UPLOAD_BUTTON.setOnAction(e -> ApplicationManager.setRoot(new UploadRoot()));
PHOTOS_BUTTON.setOnAction(e -> ApplicationManager.setRoot(new PhotosRoot()));
ALBUMS_BUTTON.setOnAction(e -> ApplicationManager.setRoot(new AlbumsRoot()));
MAP_BUTTON.setOnAction(e -> ApplicationManager.setRoot(new MapRoot()));
LOG_OUT_BUTTON.setOnAction(e -> Authentication.logout());
DELETE_USER_BUTTON.setOnAction(e -> ApplicationManager.setRoot(new DeleteUserRoot()));
super.getGridPane().add(UPLOAD_BUTTON,0,0);
super.getGridPane().add(PHOTOS_BUTTON, 1,0);
super.getGridPane().add(ALBUMS_BUTTON, 0,1);
super.getGridPane().add(MAP_BUTTON, 1,1);
super.getGridPane().add(LOG_OUT_BUTTON, 0,2);
super.getGridPane().add(DELETE_USER_BUTTON, 1, 2);
}
}
......@@ -111,10 +111,10 @@ final class PhotosRoot extends SceneRoot {
}
/**
* Shows a massage that tells the user that it has no photos
* Shows a message that tells the user that it has no photos
*/
private void showNoPhotos() {
Text noPhotosText = new Text("No photos stored. You can upload photos in \"Upload\"");
Text noPhotosText = new Text("No photos uploaded: To upload press the \"Add photo\" button");
Css.setTextFont(17, noPhotosText);
SCROLL_PANE_VBOX.getChildren().add(noPhotosText);
SCROLL_PANE_VBOX.setAlignment(Pos.CENTER);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment