From fabbb318d6a38b4ae799f87f9d5a27eff34d9bd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20S=C3=A4ume?= Date: Wed, 22 May 2024 09:36:07 +0200 Subject: [PATCH] #8: Use Permissions in Nurse Controller MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Dominik Säume --- .../nhplus/main/MainWindowController.java | 17 +++++- .../nhplus/nurse/AllNurseController.java | 56 +++++++++++++------ 2 files changed, 55 insertions(+), 18 deletions(-) diff --git a/src/main/java/de/hitec/nhplus/main/MainWindowController.java b/src/main/java/de/hitec/nhplus/main/MainWindowController.java index aa00e93..5f23f03 100644 --- a/src/main/java/de/hitec/nhplus/main/MainWindowController.java +++ b/src/main/java/de/hitec/nhplus/main/MainWindowController.java @@ -20,12 +20,24 @@ import java.util.List; * @author Dorian Nemec */ public class MainWindowController { + private static MainWindowController instace; @FXML public TabPane mainTabPane; - private User user; private TabManager tabManager; + public MainWindowController() { + instace = this; + } + + public static MainWindowController getInstance() { + return instace; + } + + public User getUser() { + return user; + } + /** * JavaFX Initialization method that is called after the binding of all the fields. * @@ -33,6 +45,7 @@ public class MainWindowController { */ @FXML public void initialize(User user) { + instace = this; this.user = user; this.tabManager = new TabManager(user); setupTabs(); @@ -63,7 +76,7 @@ public class MainWindowController { new TabStruct( "Pfleger", "/de/hitec/nhplus/nurse/AllNurseView.fxml", - Permissions.NURSE | Permissions.MANAGEMENT + Permissions.NURSE | Permissions.MANAGEMENT | Permissions.OWNER ), new TabStruct( "Gesperrte Pfleger", diff --git a/src/main/java/de/hitec/nhplus/nurse/AllNurseController.java b/src/main/java/de/hitec/nhplus/nurse/AllNurseController.java index 6f72582..cf9dfa2 100644 --- a/src/main/java/de/hitec/nhplus/nurse/AllNurseController.java +++ b/src/main/java/de/hitec/nhplus/nurse/AllNurseController.java @@ -1,8 +1,8 @@ package de.hitec.nhplus.nurse; -import static de.hitec.nhplus.utils.Validator.*; - import de.hitec.nhplus.datastorage.DaoFactory; +import de.hitec.nhplus.login.Permissions; +import de.hitec.nhplus.main.MainWindowController; import de.hitec.nhplus.nurse.database.NurseDao; import javafx.beans.value.ChangeListener; import javafx.collections.FXCollections; @@ -17,6 +17,8 @@ import javafx.scene.control.cell.TextFieldTableCell; import java.sql.SQLException; +import static de.hitec.nhplus.utils.Validator.*; + /** * The controller for viewing all {@link Nurse}s. * @@ -49,12 +51,17 @@ public class AllNurseController { private final ObservableList nurses = FXCollections.observableArrayList(); private NurseDao dao; + private boolean hasEditPermissions; /** * Initialization method that is called after the binding of all the fields. */ @FXML public void initialize() { + int editPermissions = Permissions.MANAGEMENT | Permissions.OWNER; + int userPermissions = MainWindowController.getInstance().getUser().getPermissions(); + hasEditPermissions = (userPermissions & editPermissions) != 0; + this.readAllAndShowInTableView(); this.columnId.setCellValueFactory(new PropertyValueFactory<>("id")); @@ -71,12 +78,18 @@ public class AllNurseController { this.tableView.setItems(this.nurses); + this.buttonAdd.setDisable(true); - ChangeListener inputNewNurseValidationListener = (observableValue, oldText, newText)-> + if (!hasEditPermissions) { + this.buttonLock.setDisable(true); + return; + } + + ChangeListener inputNewNurseValidationListener = (observableValue, oldText, newText) -> { boolean isValid = isValidFirstName(this.textFieldFirstName.getText()) - && isValidSurName(this.textFieldSurName.getText()) - && isValidPhoneNumber(this.textFieldPhoneNumber.getText()); + && isValidSurName(this.textFieldSurName.getText()) + && isValidPhoneNumber(this.textFieldPhoneNumber.getText()); AllNurseController.this.buttonAdd.setDisable(!isValid); }; @@ -89,12 +102,12 @@ public class AllNurseController { /** * Internal method to read all data and set it to the table view. */ - private void readAllAndShowInTableView(){ + private void readAllAndShowInTableView() { this.nurses.clear(); this.dao = DaoFactory.getInstance().createNurseDAO(); try { this.nurses.setAll(this.dao.readAllActive()); - }catch (SQLException exception){ + } catch (SQLException exception) { exception.printStackTrace(); } } @@ -120,14 +133,13 @@ public class AllNurseController { } @FXML - public void handleAdd(){ - String surname=this.textFieldSurName.getText(); - String firstName=this.textFieldFirstName.getText(); - String phoneNumber=this.textFieldPhoneNumber.getText(); + public void handleAdd() { + String surname = this.textFieldSurName.getText(); + String firstName = this.textFieldFirstName.getText(); + String phoneNumber = this.textFieldPhoneNumber.getText(); try { this.dao.create(new Nurse(firstName, surname, phoneNumber)); - } - catch (SQLException exception){ + } catch (SQLException exception) { exception.printStackTrace(); } readAllAndShowInTableView(); @@ -135,16 +147,16 @@ public class AllNurseController { } @FXML - public void handleLock(){ + public void handleLock() { Nurse selectedItem = this.tableView.getSelectionModel().getSelectedItem(); - if (selectedItem == null){ + if (selectedItem == null) { return; } try { selectedItem.setLocked(true); this.dao.update(selectedItem); - }catch (SQLException exception){ + } catch (SQLException exception) { exception.printStackTrace(); } readAllAndShowInTableView(); @@ -152,6 +164,10 @@ public class AllNurseController { @FXML public void handleOnEditSurname(TableColumn.CellEditEvent event) { + if(!hasEditPermissions){ + event.getTableView().refresh(); + return; + } String newSurName = event.getNewValue(); if (!isValidSurName(newSurName)) { showValidationError("Nachname"); @@ -164,6 +180,10 @@ public class AllNurseController { @FXML public void handleOnEditFirstname(TableColumn.CellEditEvent event) { + if(!hasEditPermissions){ + event.getTableView().refresh(); + return; + } String newFirstName = event.getNewValue(); if (!isValidFirstName(newFirstName)) { showValidationError("Vorname"); @@ -176,6 +196,10 @@ public class AllNurseController { @FXML public void handleOnEditPhoneNumber(TableColumn.CellEditEvent event) { + if(!hasEditPermissions){ + event.getTableView().refresh(); + return; + } String newPhoneNumber = event.getNewValue(); if (!isValidPhoneNumber(newPhoneNumber)) { showValidationError("Telefonnummer");