Skip to content

Commit

Permalink
Merge pull request kitodo#6295 from solth/default-client
Browse files Browse the repository at this point in the history
Add option to set default client for each user
  • Loading branch information
solth authored Nov 11, 2024
2 parents 7f01ecf + 4b40539 commit d9b2746
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
--
-- (c) Kitodo. Key to digital objects e. V. <[email protected]>
--
-- 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;
Original file line number Diff line number Diff line change
Expand Up @@ -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();");
}
}

Expand Down Expand Up @@ -162,6 +166,15 @@ public List<Client> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions Kitodo/src/main/resources/messages/messages_de.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions Kitodo/src/main/resources/messages/messages_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,25 @@
onchange="toggleSave()" required="#{empty param['editForm:saveButtonToggler']}"
redisplay="true"/>
</h:panelGroup>
<h:panelGroup layout="block"
id="defaultClientWrapper"
rendered="#{UserForm.userObject.clients.size() gt 1}">
<p:outputLabel for="defaultClient"
value="#{msgs['defaultClient']}"
title="#{msgs['defaultClient']}"/>
<p:selectOneMenu id="defaultClient"
converter="#{clientConverter}"
value="#{UserForm.userObject.defaultClient}"
disabled="#{isViewMode}">
<f:selectItem noSelectionOption="true"
itemLabel="#{msgs['notSelected']}"/>
<f:selectItems value="#{UserForm.userObject.clients}"
var="client"
itemLabel="#{client.name}"/>
<p:ajax event="change"
oncomplete="toggleSave()"/>
</p:selectOneMenu>
</h:panelGroup>
<div>
<!-- table size -->
<p:outputLabel for="table-size" value="#{msgs.tableSize}" />
Expand Down
50 changes: 40 additions & 10 deletions Kitodo/src/test/java/org/kitodo/selenium/LoginST.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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);
}
}

0 comments on commit d9b2746

Please sign in to comment.