From 0d100bcb0ba7e9e955fb5bab8ff40a5b9765efa3 Mon Sep 17 00:00:00 2001 From: Arved Solth Date: Thu, 7 Nov 2024 08:56:47 +0100 Subject: [PATCH 1/2] Add option to set default client for each user --- .../org/kitodo/data/database/beans/User.java | 23 +++++++++ .../V2_136__Add_default_client_to_user.sql | 13 +++++ .../controller/SessionClientController.java | 17 ++++++- .../org/kitodo/production/forms/UserForm.java | 3 ++ .../security/CustomLoginSuccessHandler.java | 3 +- .../resources/messages/messages_de.properties | 1 + .../resources/messages/messages_en.properties | 1 + .../templates/includes/userEdit/details.xhtml | 17 +++++++ .../java/org/kitodo/selenium/LoginST.java | 50 +++++++++++++++---- 9 files changed, 115 insertions(+), 13 deletions(-) create mode 100644 Kitodo-DataManagement/src/main/resources/db/migration/V2_136__Add_default_client_to_user.sql diff --git a/Kitodo-DataManagement/src/main/java/org/kitodo/data/database/beans/User.java b/Kitodo-DataManagement/src/main/java/org/kitodo/data/database/beans/User.java index 2d3535e8a56..f9b38d731f0 100644 --- a/Kitodo-DataManagement/src/main/java/org/kitodo/data/database/beans/User.java +++ b/Kitodo-DataManagement/src/main/java/org/kitodo/data/database/beans/User.java @@ -117,6 +117,10 @@ public class User extends BaseBean { @Column(name = "show_physical_page_number_below_thumbnail") private boolean showPhysicalPageNumberBelowThumbnail; + @ManyToOne + @JoinColumn(name = "default_client_id", foreignKey = @ForeignKey(name = "FK_user_default_client_id")) + private Client defaultClient; + /** * Constructor for User Entity. */ @@ -160,6 +164,7 @@ public User(User user) { this.projects = Objects.isNull(user.projects) ? new ArrayList<>() : user.projects; this.clients = Objects.isNull(user.clients) ? new ArrayList<>() : user.clients; this.filters = Objects.isNull(user.filters) ? new ArrayList<>() : user.filters; + this.defaultClient = Objects.isNull(user.defaultClient) ? null : user.defaultClient; if (Objects.nonNull(user.tableSize)) { this.tableSize = user.tableSize; @@ -517,6 +522,24 @@ public void setShowPhysicalPageNumberBelowThumbnail(boolean showPhysicalPageNumb this.showPhysicalPageNumberBelowThumbnail = showPhysicalPageNumberBelowThumbnail; } + /** + * Get default client. + * + * @return default client + */ + public Client getDefaultClient() { + return defaultClient; + } + + /** + * Set default client. + * + * @param defaultClient default client + */ + public void setDefaultClient(Client defaultClient) { + this.defaultClient = defaultClient; + } + /** * Removes a user from the environment. Since the * user ID may still be referenced somewhere, the user is not hard deleted from diff --git a/Kitodo-DataManagement/src/main/resources/db/migration/V2_136__Add_default_client_to_user.sql b/Kitodo-DataManagement/src/main/resources/db/migration/V2_136__Add_default_client_to_user.sql new file mode 100644 index 00000000000..1a9e0faf855 --- /dev/null +++ b/Kitodo-DataManagement/src/main/resources/db/migration/V2_136__Add_default_client_to_user.sql @@ -0,0 +1,13 @@ +-- +-- (c) Kitodo. Key to digital objects e. V. +-- +-- This file is part of the Kitodo project. +-- +-- It is licensed under GNU General Public License version 3 or later. +-- +-- For the full copyright and license information, please read the +-- GPL3-License.txt file that was distributed with this source code. +-- + +-- Add column "default_client_id" to "user" table +ALTER TABLE user ADD default_client_id INT; diff --git a/Kitodo/src/main/java/org/kitodo/production/controller/SessionClientController.java b/Kitodo/src/main/java/org/kitodo/production/controller/SessionClientController.java index 9db615e1cc4..6d0fa97d36c 100644 --- a/Kitodo/src/main/java/org/kitodo/production/controller/SessionClientController.java +++ b/Kitodo/src/main/java/org/kitodo/production/controller/SessionClientController.java @@ -80,10 +80,14 @@ public boolean shouldUserChangeSessionClient() { * Display client selection dialog if user is logged in and has multiple clients. */ public void showClientSelectDialog() { - if (Objects.isNull(getCurrentSessionClient()) && !userHasOnlyOneClient()) { - PrimeFaces.current().executeScript("PF('selectClientDialog').show();"); + User currentUser = ServiceManager.getUserService().getCurrentUser(); + Client defaultClient = currentUser.getDefaultClient(); + if (Objects.nonNull(defaultClient)) { + setSessionClient(defaultClient); } else if (userHasOnlyOneClient()) { setSessionClient(getFirstClientOfCurrentUser()); + } else if (Objects.isNull(getCurrentSessionClient()) && !userHasOnlyOneClient()) { + PrimeFaces.current().executeScript("PF('selectClientDialog').show();"); } } @@ -162,6 +166,15 @@ public List getAvailableClientsOfCurrentUser() { return clients; } + /** + * Get default client of current user. + * + * @return default client of current user + */ + public Client getDefaultClientOfCurrentUser() { + return ServiceManager.getUserService().getCurrentUser().getDefaultClient(); + } + /** * Get list of available clients of current user sorted by name. * @return list of available clients of current user sorted by name diff --git a/Kitodo/src/main/java/org/kitodo/production/forms/UserForm.java b/Kitodo/src/main/java/org/kitodo/production/forms/UserForm.java index 570c5fe4c98..55c4c3c2cba 100644 --- a/Kitodo/src/main/java/org/kitodo/production/forms/UserForm.java +++ b/Kitodo/src/main/java/org/kitodo/production/forms/UserForm.java @@ -364,6 +364,9 @@ public String deleteFromClient() { for (Client client : this.userObject.getClients()) { if (client.getId().equals(clientId)) { this.userObject.getClients().remove(client); + if (client.equals(this.userObject.getDefaultClient())) { + this.userObject.setDefaultClient(null); + } break; } } diff --git a/Kitodo/src/main/java/org/kitodo/production/security/CustomLoginSuccessHandler.java b/Kitodo/src/main/java/org/kitodo/production/security/CustomLoginSuccessHandler.java index c8f8dfbf6e3..0ad26640737 100644 --- a/Kitodo/src/main/java/org/kitodo/production/security/CustomLoginSuccessHandler.java +++ b/Kitodo/src/main/java/org/kitodo/production/security/CustomLoginSuccessHandler.java @@ -48,7 +48,8 @@ public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpS try { SessionClientController controller = new SessionClientController(); if (ServiceManager.getIndexingService().isIndexCorrupted() - || controller.getAvailableClientsOfCurrentUser().size() > 1) { + || (controller.getAvailableClientsOfCurrentUser().size() > 1 + && Objects.isNull(controller.getDefaultClientOfCurrentUser()))) { // redirect to empty landing page, where dialogs are displayed depending on both checks! redirectStrategy.sendRedirect(httpServletRequest, httpServletResponse, EMPTY_LANDING_PAGE); } else { diff --git a/Kitodo/src/main/resources/messages/messages_de.properties b/Kitodo/src/main/resources/messages/messages_de.properties index 5cb752b8328..e63c9f3794e 100644 --- a/Kitodo/src/main/resources/messages/messages_de.properties +++ b/Kitodo/src/main/resources/messages/messages_de.properties @@ -363,6 +363,7 @@ databaseStatistic=Datenbankstatistik day=Tag days=Tage deactivatedTemplates=Deaktivierte Produktionsvorlagen +defaultClient=Standardmandant defaults=Vorgaben delete=L\u00F6schen deleteAfterMove=Nach dem Export l\u00F6schen diff --git a/Kitodo/src/main/resources/messages/messages_en.properties b/Kitodo/src/main/resources/messages/messages_en.properties index 426bc1b48ec..9f50b60b7b2 100644 --- a/Kitodo/src/main/resources/messages/messages_en.properties +++ b/Kitodo/src/main/resources/messages/messages_en.properties @@ -363,6 +363,7 @@ databaseStatistic=Database statistics day=day days=days deactivatedTemplates=Deactivated templates +defaultClient=Default client defaults=Defaults delete=Delete deleteAfterMove=Delete after export diff --git a/Kitodo/src/main/webapp/WEB-INF/templates/includes/userEdit/details.xhtml b/Kitodo/src/main/webapp/WEB-INF/templates/includes/userEdit/details.xhtml index 1086018ee82..b1a4d70d307 100644 --- a/Kitodo/src/main/webapp/WEB-INF/templates/includes/userEdit/details.xhtml +++ b/Kitodo/src/main/webapp/WEB-INF/templates/includes/userEdit/details.xhtml @@ -123,6 +123,23 @@ onchange="toggleSave()" required="#{empty param['editForm:saveButtonToggler']}" redisplay="true"/> +
+ + + + + + +
diff --git a/Kitodo/src/test/java/org/kitodo/selenium/LoginST.java b/Kitodo/src/test/java/org/kitodo/selenium/LoginST.java index 641fa5bff81..c34f076a11c 100644 --- a/Kitodo/src/test/java/org/kitodo/selenium/LoginST.java +++ b/Kitodo/src/test/java/org/kitodo/selenium/LoginST.java @@ -11,27 +11,26 @@ package org.kitodo.selenium; -import static org.junit.jupiter.api.Assertions.assertEquals; - -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -import org.kitodo.data.elasticsearch.exceptions.CustomResponseException; -import org.kitodo.data.exceptions.DataException; +import org.kitodo.data.database.beans.Client; +import org.kitodo.data.database.beans.Process; +import org.kitodo.data.database.beans.User; import org.kitodo.production.services.ServiceManager; import org.kitodo.selenium.testframework.BaseTestSelenium; import org.kitodo.selenium.testframework.Browser; import org.kitodo.selenium.testframework.Pages; +import java.util.Collections; + +import static org.junit.jupiter.api.Assertions.*; + public class LoginST extends BaseTestSelenium { - @BeforeAll - public static void manipulateIndex() throws DataException, CustomResponseException { + @Test + public void indexWarningTest() throws Exception { // remove one process from index but not from DB to provoke index warning ServiceManager.getProcessService().removeFromIndex(1, true); - } - @Test - public void indexWarningTest() throws Exception { // log into Kitodo with non-admin user to be redirected to 'checks' page Pages.getLoginPage().goTo().performLogin(ServiceManager.getUserService().getById(2)); assertEquals("http://localhost:8080/kitodo/pages/checks.jsf", Browser.getCurrentUrl()); @@ -41,5 +40,36 @@ public void indexWarningTest() throws Exception { Pages.getLoginPage().goTo().performLoginAsAdmin(); assertEquals("http://localhost:8080/kitodo/pages/system.jsf?tabIndex=2", Browser.getCurrentUrl()); Pages.getTopNavigation().logout(); + + // restore deleted process to index + Process unindexedProcess = ServiceManager.getProcessService().getById(1); + ServiceManager.getProcessService().addAllObjectsToIndex(Collections.singletonList(unindexedProcess)); + } + + @Test + public void defaultClientTest() throws Exception { + User userNowak = ServiceManager.getUserService().getByLogin("nowak"); + assertTrue(userNowak.getClients().size() > 1, "Test user should have more than one client"); + Client defaultClient = userNowak.getDefaultClient(); + assertNull(defaultClient, "Default client should be null."); + + Pages.getLoginPage().goTo().performLogin(userNowak); + assertEquals("http://localhost:8080/kitodo/pages/checks.jsf", Browser.getCurrentUrl(), + "User with multiple clients but no default client should get redirected to 'checks' page."); + Pages.getTopNavigation().cancelClientSelection(); + + // set default client of user + Client firstClient = ServiceManager.getClientService().getById(1); + userNowak.setDefaultClient(firstClient); + ServiceManager.getUserService().saveToDatabase(userNowak); + + Pages.getLoginPage().goTo().performLogin(userNowak); + assertEquals("http://localhost:8080/kitodo/pages/desktop.jsf", Browser.getCurrentUrl(), + "User with default client should get redirected to 'desktop' page."); + Pages.getTopNavigation().logout(); + + // restore users original settings + userNowak.setDefaultClient(null); + ServiceManager.getUserService().saveToDatabase(userNowak); } } From 4b405394c6de481bd71f4d1150a56dc8ec002c67 Mon Sep 17 00:00:00 2001 From: Arved Solth Date: Fri, 8 Nov 2024 15:43:54 +0100 Subject: [PATCH 2/2] Hide default client selection in user settings if user has only one client to choose from --- .../WEB-INF/templates/includes/userEdit/details.xhtml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Kitodo/src/main/webapp/WEB-INF/templates/includes/userEdit/details.xhtml b/Kitodo/src/main/webapp/WEB-INF/templates/includes/userEdit/details.xhtml index b1a4d70d307..d191394e069 100644 --- a/Kitodo/src/main/webapp/WEB-INF/templates/includes/userEdit/details.xhtml +++ b/Kitodo/src/main/webapp/WEB-INF/templates/includes/userEdit/details.xhtml @@ -123,7 +123,9 @@ onchange="toggleSave()" required="#{empty param['editForm:saveButtonToggler']}" redisplay="true"/> -
+ @@ -139,7 +141,7 @@ -
+