From 7fceccc109bc567d4fdcd2e7cfe421c42bc1369c Mon Sep 17 00:00:00 2001 From: jomu Date: Mon, 12 Nov 2018 16:13:00 +0100 Subject: [PATCH] fixed account handling fixed view CDI integration --- .../account/web/presentation/AccountView.java | 54 ++++++++----------- .../web/presentation/ApplicationView.java | 28 +++++++--- .../web/presentation/PermissionView.java | 12 ++--- .../account/web/presentation/RoleView.java | 12 ++--- .../webapp/resources/template/leftmenu.xhtml | 3 -- .../webapp/resources/template/template.xhtml | 2 +- account-ui/src/main/webapp/web/account.xhtml | 8 +-- account/sql/account.dbm | 5 +- .../account/boundary/AccountControl.java | 40 +++++++------- 9 files changed, 86 insertions(+), 78 deletions(-) diff --git a/account-ui/src/main/java/de/muehlencord/shared/account/web/presentation/AccountView.java b/account-ui/src/main/java/de/muehlencord/shared/account/web/presentation/AccountView.java index fba220b..beff1b7 100644 --- a/account-ui/src/main/java/de/muehlencord/shared/account/web/presentation/AccountView.java +++ b/account-ui/src/main/java/de/muehlencord/shared/account/web/presentation/AccountView.java @@ -12,9 +12,10 @@ import java.io.Serializable; import java.util.ArrayList; import java.util.List; import javax.ejb.EJB; -import javax.enterprise.context.SessionScoped; -import javax.faces.bean.ManagedBean; -import javax.faces.bean.ManagedProperty; +import javax.faces.view.ViewScoped; +import javax.inject.Named; +import javax.inject.Inject; +import org.primefaces.event.SelectEvent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -22,14 +23,14 @@ import org.slf4j.LoggerFactory; * * @author jomu */ -@ManagedBean(name = "accountView") -@SessionScoped +@ViewScoped +@Named("accountView") public class AccountView implements Serializable { private static final long serialVersionUID = -8050582392249849438L; private static final Logger LOGGER = LoggerFactory.getLogger(AccountView.class); - @ManagedProperty(value = "#{applicationView}") + @Inject private ApplicationView applicationView; @EJB private AccountControl accountService; @@ -50,8 +51,6 @@ public class AccountView implements Serializable { // account currently on edit private AccountEntity currentAccount; private List currentAccountRoles = null; - // boolean flag to toggle buttons which require an account to be selected - private boolean accountSelected = false; public List getAccounts() { if (accountList == null) { @@ -68,6 +67,17 @@ public class AccountView implements Serializable { return applicationRoles; } + public void selectAccount(SelectEvent event) { + if (currentAccount == null) { + applicationRoles = null; + currentAccountRoles = null; + } + } + + public boolean getAccountSelected() { + return currentAccount != null; + } + public void newAccount() { currentAccount = new AccountEntity(); currentAccount.setUsername(null); @@ -84,7 +94,7 @@ public class AccountView implements Serializable { this.currentAccountRoles = new ArrayList<>(); if (currentAccount.getApplicationRoleList() != null) { currentAccountRoles.addAll(currentAccount.getApplicationRoleList()); - } + } } } @@ -101,8 +111,7 @@ public class AccountView implements Serializable { currentAccount.setUsername(null); FacesUtil.addErrorMessage("editDialogMessaegs", "Create new account failed", "Account with username " + username + " already exists"); } else { - accountService.saveAccount(currentAccount, currentAccountRoles); - selectAccount(); + accountService.saveAccount(applicationView.getCurrentApplication(), currentAccount, currentAccountRoles); if (currentAccount.getId() == null) { // this was a new account // force accounts to be loaded from database again @@ -117,8 +126,7 @@ public class AccountView implements Serializable { accountList.remove(currentAccount); FacesUtil.addGlobalInfoMessage("Info", "Account " + currentAccount.getUsername() + " deleted"); currentAccount = null; - currentAccountRoles = null; - deselectAccount(); + currentAccountRoles = null; } catch (AccountException ex) { if (LOGGER.isDebugEnabled()) { LOGGER.debug(ex.toString(), ex); @@ -130,14 +138,6 @@ public class AccountView implements Serializable { } } - public void selectAccount() { - this.accountSelected = true; - } - - public void deselectAccount() { - this.accountSelected = false; - } - public void showDisabledAccountsChange() { if (LOGGER.isDebugEnabled()) { LOGGER.debug("show diabled accounts changed to {}", showDisabledAccounts); @@ -150,11 +150,11 @@ public class AccountView implements Serializable { } /* **** getter / setter **** */ - /** * setter for managed property applicationView + * * @param applicationView the applicaton view to inject - */ + */ public void setApplicationView(ApplicationView applicationView) { this.applicationView = applicationView; } @@ -167,14 +167,6 @@ public class AccountView implements Serializable { this.currentAccount = currentAccount; } - public boolean isAccountSelected() { - return accountSelected; - } - - public void setAccountSelected(boolean accountSelected) { - this.accountSelected = accountSelected; - } - public boolean isShowDisabledAccounts() { return showDisabledAccounts; } diff --git a/account-ui/src/main/java/de/muehlencord/shared/account/web/presentation/ApplicationView.java b/account-ui/src/main/java/de/muehlencord/shared/account/web/presentation/ApplicationView.java index 4fcaf4a..8d07b57 100644 --- a/account-ui/src/main/java/de/muehlencord/shared/account/web/presentation/ApplicationView.java +++ b/account-ui/src/main/java/de/muehlencord/shared/account/web/presentation/ApplicationView.java @@ -6,8 +6,9 @@ import de.muehlencord.shared.jeeutil.FacesUtil; import java.io.Serializable; import java.util.List; import javax.annotation.PostConstruct; -import javax.faces.bean.ManagedBean; -import javax.faces.bean.SessionScoped; +import javax.annotation.PreDestroy; +import javax.inject.Named; +import javax.enterprise.context.SessionScoped; import javax.inject.Inject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -16,8 +17,8 @@ import org.slf4j.LoggerFactory; * * @author Joern Muehlencord */ -@ManagedBean (name="applicationView") @SessionScoped +@Named("applicationView") public class ApplicationView implements Serializable { private static final long serialVersionUID = -5515249316880163539L; @@ -29,14 +30,25 @@ public class ApplicationView implements Serializable { private ApplicationEntity currentApplication = null; private ApplicationEntity editApplication = null; private List applicationList = null; - + @PostConstruct public void selectDefaultCurrentApplication() { // force applications to be loaded from database getAllApplications(); if ((applicationList != null) && (!applicationList.isEmpty())) { currentApplication = applicationList.get(0); + } + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("post construct executed"); + } } + + @PreDestroy + public void predestroy() { + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("Predestroy executed"); + } + } public List getAllApplications() { @@ -79,19 +91,19 @@ public class ApplicationView implements Serializable { FacesUtil.addGlobalInfoMessage("Info", "Application saved"); } } - + public void deleteApplication() { if (currentApplication == null) { FacesUtil.addGlobalErrorMessage("Error", "Need to provide data"); } else if (currentApplication.getId() == null) { - FacesUtil.addGlobalErrorMessage("Error", "Cannot delete non persistent data"); + FacesUtil.addGlobalErrorMessage("Error", "Cannot delete non persistent data"); } else { String applicationName = currentApplication.getApplicationName(); - applicationService.delete (currentApplication); + applicationService.delete(currentApplication); applicationList = null; // force reload to update view currentApplication = null; selectDefaultCurrentApplication(); - FacesUtil.addGlobalInfoMessage("Info", "Application "+applicationName+" deleted"); + FacesUtil.addGlobalInfoMessage("Info", "Application " + applicationName + " deleted"); } } diff --git a/account-ui/src/main/java/de/muehlencord/shared/account/web/presentation/PermissionView.java b/account-ui/src/main/java/de/muehlencord/shared/account/web/presentation/PermissionView.java index 4921a5d..6299d72 100644 --- a/account-ui/src/main/java/de/muehlencord/shared/account/web/presentation/PermissionView.java +++ b/account-ui/src/main/java/de/muehlencord/shared/account/web/presentation/PermissionView.java @@ -23,9 +23,9 @@ import de.muehlencord.shared.jeeutil.FacesUtil; import java.io.Serializable; import java.util.List; import javax.ejb.EJB; -import javax.faces.bean.ManagedBean; -import javax.faces.bean.ManagedProperty; -import javax.faces.bean.SessionScoped; +import javax.inject.Named; +import javax.faces.view.ViewScoped; +import javax.inject.Inject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -33,14 +33,14 @@ import org.slf4j.LoggerFactory; * * @author Joern Muehlencord */ -@ManagedBean(name = "permissionView") -@SessionScoped +@ViewScoped +@Named("permissionView") public class PermissionView implements Serializable { private static final long serialVersionUID = -1469453490360990772L; private static final Logger LOGGER = LoggerFactory.getLogger(PermissionView.class); - @ManagedProperty(value = "#{applicationView}") + @Inject private ApplicationView applicationView; @EJB diff --git a/account-ui/src/main/java/de/muehlencord/shared/account/web/presentation/RoleView.java b/account-ui/src/main/java/de/muehlencord/shared/account/web/presentation/RoleView.java index 46ceaaa..a5c15b9 100644 --- a/account-ui/src/main/java/de/muehlencord/shared/account/web/presentation/RoleView.java +++ b/account-ui/src/main/java/de/muehlencord/shared/account/web/presentation/RoleView.java @@ -25,9 +25,9 @@ import java.io.Serializable; import java.util.ArrayList; import java.util.List; import javax.ejb.EJB; -import javax.faces.bean.ManagedBean; -import javax.faces.bean.ManagedProperty; -import javax.faces.bean.SessionScoped; +import javax.faces.view.ViewScoped; +import javax.inject.Named; +import javax.inject.Inject; import org.primefaces.event.SelectEvent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -36,14 +36,14 @@ import org.slf4j.LoggerFactory; * * @author Joern Muehlencord */ -@ManagedBean(name = "roleView") -@SessionScoped +@ViewScoped +@Named("roleView") public class RoleView implements Serializable { private static final long serialVersionUID = 1669321020398119007L; private static final Logger LOGGER = LoggerFactory.getLogger(RoleView.class); - @ManagedProperty(value = "#{applicationView}") + @Inject private ApplicationView applicationView; @EJB diff --git a/account-ui/src/main/webapp/resources/template/leftmenu.xhtml b/account-ui/src/main/webapp/resources/template/leftmenu.xhtml index 99bfdc8..e9d2625 100644 --- a/account-ui/src/main/webapp/resources/template/leftmenu.xhtml +++ b/account-ui/src/main/webapp/resources/template/leftmenu.xhtml @@ -32,9 +32,6 @@ Accounts - - -
  • diff --git a/account-ui/src/main/webapp/resources/template/template.xhtml b/account-ui/src/main/webapp/resources/template/template.xhtml index d8e2034..52892e3 100644 --- a/account-ui/src/main/webapp/resources/template/template.xhtml +++ b/account-ui/src/main/webapp/resources/template/template.xhtml @@ -21,7 +21,7 @@ limitations under the License. xmlns:h="http://xmlns.jcp.org/jsf/html"> - Product Catalog Designer + Account UI diff --git a/account-ui/src/main/webapp/web/account.xhtml b/account-ui/src/main/webapp/web/account.xhtml index 5c6967b..cfb965a 100644 --- a/account-ui/src/main/webapp/web/account.xhtml +++ b/account-ui/src/main/webapp/web/account.xhtml @@ -22,7 +22,7 @@ - + @@ -220,10 +220,10 @@
    - - + + - +
    diff --git a/account/sql/account.dbm b/account/sql/account.dbm index 12d9154..5d78fbf 100644 --- a/account/sql/account.dbm +++ b/account/sql/account.dbm @@ -3,7 +3,7 @@ CAUTION: Do not modify this file unless you know what you are doing. Unexpected results may occur if the code is changed deliberately. --> - @@ -49,6 +49,9 @@ CAUTION: Do not modify this file unless you know what you are doing. + + + diff --git a/account/src/main/java/de/muehlencord/shared/account/business/account/boundary/AccountControl.java b/account/src/main/java/de/muehlencord/shared/account/business/account/boundary/AccountControl.java index ec887ea..4b620de 100644 --- a/account/src/main/java/de/muehlencord/shared/account/business/account/boundary/AccountControl.java +++ b/account/src/main/java/de/muehlencord/shared/account/business/account/boundary/AccountControl.java @@ -7,6 +7,7 @@ import de.muehlencord.shared.account.business.mail.entity.MailException; import de.muehlencord.shared.account.business.mail.boundary.MailService; import de.muehlencord.shared.account.business.account.entity.AccountEntity; import de.muehlencord.shared.account.business.account.entity.ApplicationRoleEntity; +import de.muehlencord.shared.account.business.application.entity.ApplicationEntity; import de.muehlencord.shared.account.business.config.entity.ConfigException; import de.muehlencord.shared.account.util.SecurityUtil; import java.io.Serializable; @@ -78,7 +79,7 @@ public class AccountControl implements Serializable { StringBuilder queryBuilder = new StringBuilder(); queryBuilder.append("SELECT a FROM AccountEntity a "); if (loadRoles) { - queryBuilder.append("JOIN FETCH a.applicationRoleList "); + queryBuilder.append("LEFT JOIN FETCH a.applicationRoleList "); } queryBuilder.append("WHERE a.username = :username"); Query query = em.createQuery(queryBuilder.toString()); @@ -91,7 +92,7 @@ public class AccountControl implements Serializable { } @Transactional - public AccountEntity saveAccount(AccountEntity account, List applicationRoles) { + public AccountEntity saveAccount(ApplicationEntity application, AccountEntity account, List applicationRoles) { Date now = new Date(); // Todo now in UTC Subject currentUser = SecurityUtils.getSubject(); String currentLoggedInUser = currentUser.getPrincipal().toString(); @@ -117,39 +118,42 @@ public class AccountControl implements Serializable { // reload account from db and join roles account = getAccountEntity(account.getUsername(), true); } - + // assign roles to account if (account.getApplicationRoleList() == null) { account.setApplicationRoleList(new ArrayList<>()); } - + boolean roleSetupChanged = false; // remove roles which are no longer listed - for (ApplicationRoleEntity currentlyAssignedRole : account.getApplicationRoleList()) { - if (!applicationRoles.contains (currentlyAssignedRole)) { - account.getApplicationRoleList().remove(currentlyAssignedRole); + // ensure this is only done for the given application - keep the other applications untouched + List assignedRoles = new ArrayList<>(); + assignedRoles.addAll(account.getApplicationRoleList()); + for (ApplicationRoleEntity currentlyAssignedRole : assignedRoles) { + if ((currentlyAssignedRole.getApplication().equals(application) && (!applicationRoles.contains(currentlyAssignedRole)))) { + account.getApplicationRoleList().remove(currentlyAssignedRole); roleSetupChanged = true; if (LOGGER.isDebugEnabled()) { - LOGGER.debug("Removed role {} from user {}", currentlyAssignedRole.getRoleName(), account.getUsername()); - } - } - } - + LOGGER.debug("Removed role {} ({}) from user {}", currentlyAssignedRole.getRoleName(), application.getApplicationName(), account.getUsername()); + } + } + } + // add newly added roles to role list for (ApplicationRoleEntity applicationRole : applicationRoles) { if (!account.getApplicationRoleList().contains(applicationRole)) { - account.addApplicationRole (applicationRole); + account.addApplicationRole(applicationRole); roleSetupChanged = true; if (LOGGER.isDebugEnabled()) { - LOGGER.debug("Added role {} to account {}", applicationRole.getRoleName(), account.getUsername()); - } + LOGGER.debug("Added role {} ({}) to account {}", applicationRole.getRoleName(), application.getApplicationName(), account.getUsername()); + } } } - + // update account in database if roles changed if (roleSetupChanged) { - em.merge(account); - } + em.merge(account); + } return account; }