#8: Implement Login Logic
All checks were successful
Quality Check / Linting Check (push) Successful in 12s
Quality Check / Javadoc Check (push) Successful in 21s

Signed-off-by: Dominik Säume <Dominik.Saeume@hmmh.de>
This commit is contained in:
Dominik Säume 2024-05-21 15:51:48 +02:00
parent b9eae8088f
commit b617e8c242
Signed by: SZUT-Dominik
GPG key ID: 67D15BB250B41E7C
4 changed files with 52 additions and 11 deletions

View file

@ -36,7 +36,6 @@ public class Main extends Application {
public void start(Stage primaryStage) { public void start(Stage primaryStage) {
this.primaryStage = primaryStage; this.primaryStage = primaryStage;
executePassword(); executePassword();
//executeMainApplication();
} }
private void executePassword() { private void executePassword() {
@ -55,7 +54,7 @@ public class Main extends Application {
loginStage.showAndWait(); loginStage.showAndWait();
if(controller.loginSucessfull){ if(controller.user == null){
executeMainApplication(); executeMainApplication();
} }
} catch (IOException exception) { } catch (IOException exception) {

View file

@ -2,11 +2,14 @@ package de.hitec.nhplus.login;
import de.hitec.nhplus.datastorage.DaoFactory; import de.hitec.nhplus.datastorage.DaoFactory;
import de.hitec.nhplus.login.database.UserDao; import de.hitec.nhplus.login.database.UserDao;
import javafx.event.ActionEvent; import javafx.animation.PauseTransition;
import javafx.animation.TranslateTransition;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField; import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField; import javafx.scene.control.TextField;
import javafx.stage.Stage; import javafx.stage.Stage;
import javafx.util.Duration;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.security.MessageDigest; import java.security.MessageDigest;
@ -16,27 +19,54 @@ import java.util.Arrays;
public class LoginController { public class LoginController {
public User user;
@FXML @FXML
public TextField textFieldUsername; public TextField textFieldUsername;
@FXML @FXML
public PasswordField passwordField; public PasswordField passwordField;
@FXML
public Button buttonSubmit;
private Stage stage; private Stage stage;
public boolean loginSucessfull = false;
private int loginTries = 0; private int loginTries = 0;
public void initialize(Stage stage) { public void initialize(Stage stage) {
this.stage = stage; this.stage = stage;
} }
private void handleWrongPasswordOrUsername() { private void handleWrongPasswordOrUsername() {
loginTries++; loginTries++;
// Shake
TranslateTransition ttUsername = new TranslateTransition(Duration.millis(50), textFieldUsername);
ttUsername.setByX(10);
ttUsername.setAutoReverse(true);
ttUsername.setCycleCount(6);
TranslateTransition ttPassword = new TranslateTransition(Duration.millis(50), passwordField);
ttPassword.setByX(10);
ttPassword.setAutoReverse(true);
ttPassword.setCycleCount(6);
ttUsername.play();
ttPassword.play();
// Timout
PauseTransition pause = new PauseTransition(Duration.seconds(3));
pause.setOnFinished(event -> {
if (loginTries == 3) { if (loginTries == 3) {
stage.close(); stage.close();
} }
buttonSubmit.setDisable(false);
});
pause.play();
} }
@FXML @FXML
public void handleSubmit() { public void handleSubmit() {
buttonSubmit.setDisable(true);
UserDao dao = DaoFactory.getInstance().createUserDAO(); UserDao dao = DaoFactory.getInstance().createUserDAO();
try { try {
int id = dao.readUserId(textFieldUsername.getText()); int id = dao.readUserId(textFieldUsername.getText());
@ -53,7 +83,7 @@ public class LoginController {
handleWrongPasswordOrUsername(); handleWrongPasswordOrUsername();
return; return;
} }
loginSucessfull = true; user = dao.read(id);
stage.close(); stage.close();
} catch (SQLException | NoSuchAlgorithmException exception) { } catch (SQLException | NoSuchAlgorithmException exception) {
exception.printStackTrace(); exception.printStackTrace();

View file

@ -43,7 +43,12 @@ public class UserDao implements Dao<User> {
@Override @Override
public User read(int id) throws SQLException { public User read(int id) throws SQLException {
final String SQL = """ final String SQL = """
SELECT user.username, user.passwordSalt, user.passwordHash, user__permissions.permissions, user__nurse.nurseId SELECT
user.username,
user.passwordSalt,
user.passwordHash,
user__permissions.permissions,
user__nurse.nurseId
FROM user FROM user
LEFT JOIN user__permissions ON user.id = user__permissions.userId LEFT JOIN user__permissions ON user.id = user__permissions.userId
LEFT JOIN user__nurse ON user.id = user__nurse.userId LEFT JOIN user__nurse ON user.id = user__nurse.userId
@ -174,7 +179,13 @@ public class UserDao implements Dao<User> {
@Override @Override
public List<User> readAll() throws SQLException { public List<User> readAll() throws SQLException {
final String SQL = """ final String SQL = """
SELECT user.id, user.username, user.passwordSalt, user.passwordHash, user__permissions.permissions, user__nurse.nurseId SELECT
user.id,
user.username,
user.passwordSalt,
user.passwordHash,
user__permissions.permissions,
user__nurse.nurseId
FROM user FROM user
LEFT JOIN user__permissions ON user.id = user__permissions.userId LEFT JOIN user__permissions ON user.id = user__permissions.userId
LEFT JOIN user__nurse ON user.id = user__nurse.userId LEFT JOIN user__nurse ON user.id = user__nurse.userId

View file

@ -38,6 +38,7 @@
</center> </center>
<bottom> <bottom>
<Button <Button
fx:id="buttonSubmit"
text="Bestätigen" text="Bestätigen"
BorderPane.alignment="CENTER" BorderPane.alignment="CENTER"
onAction="#handleSubmit" onAction="#handleSubmit"