fixed account handling

fixed view CDI integration
This commit is contained in:
2018-11-12 16:13:00 +01:00
parent 00925aa389
commit 7fceccc109
9 changed files with 86 additions and 78 deletions

View File

@ -12,9 +12,10 @@ import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.ejb.EJB; import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped; import javax.faces.view.ViewScoped;
import javax.faces.bean.ManagedBean; import javax.inject.Named;
import javax.faces.bean.ManagedProperty; import javax.inject.Inject;
import org.primefaces.event.SelectEvent;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -22,14 +23,14 @@ import org.slf4j.LoggerFactory;
* *
* @author jomu * @author jomu
*/ */
@ManagedBean(name = "accountView") @ViewScoped
@SessionScoped @Named("accountView")
public class AccountView implements Serializable { public class AccountView implements Serializable {
private static final long serialVersionUID = -8050582392249849438L; private static final long serialVersionUID = -8050582392249849438L;
private static final Logger LOGGER = LoggerFactory.getLogger(AccountView.class); private static final Logger LOGGER = LoggerFactory.getLogger(AccountView.class);
@ManagedProperty(value = "#{applicationView}") @Inject
private ApplicationView applicationView; private ApplicationView applicationView;
@EJB @EJB
private AccountControl accountService; private AccountControl accountService;
@ -50,8 +51,6 @@ public class AccountView implements Serializable {
// account currently on edit // account currently on edit
private AccountEntity currentAccount; private AccountEntity currentAccount;
private List<ApplicationRoleEntity> currentAccountRoles = null; private List<ApplicationRoleEntity> currentAccountRoles = null;
// boolean flag to toggle buttons which require an account to be selected
private boolean accountSelected = false;
public List<AccountEntity> getAccounts() { public List<AccountEntity> getAccounts() {
if (accountList == null) { if (accountList == null) {
@ -68,6 +67,17 @@ public class AccountView implements Serializable {
return applicationRoles; return applicationRoles;
} }
public void selectAccount(SelectEvent event) {
if (currentAccount == null) {
applicationRoles = null;
currentAccountRoles = null;
}
}
public boolean getAccountSelected() {
return currentAccount != null;
}
public void newAccount() { public void newAccount() {
currentAccount = new AccountEntity(); currentAccount = new AccountEntity();
currentAccount.setUsername(null); currentAccount.setUsername(null);
@ -101,8 +111,7 @@ public class AccountView implements Serializable {
currentAccount.setUsername(null); currentAccount.setUsername(null);
FacesUtil.addErrorMessage("editDialogMessaegs", "Create new account failed", "Account with username " + username + " already exists"); FacesUtil.addErrorMessage("editDialogMessaegs", "Create new account failed", "Account with username " + username + " already exists");
} else { } else {
accountService.saveAccount(currentAccount, currentAccountRoles); accountService.saveAccount(applicationView.getCurrentApplication(), currentAccount, currentAccountRoles);
selectAccount();
if (currentAccount.getId() == null) { if (currentAccount.getId() == null) {
// this was a new account // this was a new account
// force accounts to be loaded from database again // force accounts to be loaded from database again
@ -118,7 +127,6 @@ public class AccountView implements Serializable {
FacesUtil.addGlobalInfoMessage("Info", "Account " + currentAccount.getUsername() + " deleted"); FacesUtil.addGlobalInfoMessage("Info", "Account " + currentAccount.getUsername() + " deleted");
currentAccount = null; currentAccount = null;
currentAccountRoles = null; currentAccountRoles = null;
deselectAccount();
} catch (AccountException ex) { } catch (AccountException ex) {
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
LOGGER.debug(ex.toString(), ex); 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() { public void showDisabledAccountsChange() {
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
LOGGER.debug("show diabled accounts changed to {}", showDisabledAccounts); LOGGER.debug("show diabled accounts changed to {}", showDisabledAccounts);
@ -150,9 +150,9 @@ public class AccountView implements Serializable {
} }
/* **** getter / setter **** */ /* **** getter / setter **** */
/** /**
* setter for managed property applicationView * setter for managed property applicationView
*
* @param applicationView the applicaton view to inject * @param applicationView the applicaton view to inject
*/ */
public void setApplicationView(ApplicationView applicationView) { public void setApplicationView(ApplicationView applicationView) {
@ -167,14 +167,6 @@ public class AccountView implements Serializable {
this.currentAccount = currentAccount; this.currentAccount = currentAccount;
} }
public boolean isAccountSelected() {
return accountSelected;
}
public void setAccountSelected(boolean accountSelected) {
this.accountSelected = accountSelected;
}
public boolean isShowDisabledAccounts() { public boolean isShowDisabledAccounts() {
return showDisabledAccounts; return showDisabledAccounts;
} }

View File

@ -6,8 +6,9 @@ import de.muehlencord.shared.jeeutil.FacesUtil;
import java.io.Serializable; import java.io.Serializable;
import java.util.List; import java.util.List;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean; import javax.annotation.PreDestroy;
import javax.faces.bean.SessionScoped; import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject; import javax.inject.Inject;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -16,8 +17,8 @@ import org.slf4j.LoggerFactory;
* *
* @author Joern Muehlencord <joern at muehlencord.de> * @author Joern Muehlencord <joern at muehlencord.de>
*/ */
@ManagedBean (name="applicationView")
@SessionScoped @SessionScoped
@Named("applicationView")
public class ApplicationView implements Serializable { public class ApplicationView implements Serializable {
private static final long serialVersionUID = -5515249316880163539L; private static final long serialVersionUID = -5515249316880163539L;
@ -36,7 +37,18 @@ public class ApplicationView implements Serializable {
getAllApplications(); getAllApplications();
if ((applicationList != null) && (!applicationList.isEmpty())) { if ((applicationList != null) && (!applicationList.isEmpty())) {
currentApplication = applicationList.get(0); 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<ApplicationEntity> getAllApplications() { public List<ApplicationEntity> getAllApplications() {
@ -87,11 +99,11 @@ public class ApplicationView implements Serializable {
FacesUtil.addGlobalErrorMessage("Error", "Cannot delete non persistent data"); FacesUtil.addGlobalErrorMessage("Error", "Cannot delete non persistent data");
} else { } else {
String applicationName = currentApplication.getApplicationName(); String applicationName = currentApplication.getApplicationName();
applicationService.delete (currentApplication); applicationService.delete(currentApplication);
applicationList = null; // force reload to update view applicationList = null; // force reload to update view
currentApplication = null; currentApplication = null;
selectDefaultCurrentApplication(); selectDefaultCurrentApplication();
FacesUtil.addGlobalInfoMessage("Info", "Application "+applicationName+" deleted"); FacesUtil.addGlobalInfoMessage("Info", "Application " + applicationName + " deleted");
} }
} }

View File

@ -23,9 +23,9 @@ import de.muehlencord.shared.jeeutil.FacesUtil;
import java.io.Serializable; import java.io.Serializable;
import java.util.List; import java.util.List;
import javax.ejb.EJB; import javax.ejb.EJB;
import javax.faces.bean.ManagedBean; import javax.inject.Named;
import javax.faces.bean.ManagedProperty; import javax.faces.view.ViewScoped;
import javax.faces.bean.SessionScoped; import javax.inject.Inject;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -33,14 +33,14 @@ import org.slf4j.LoggerFactory;
* *
* @author Joern Muehlencord <joern at muehlencord.de> * @author Joern Muehlencord <joern at muehlencord.de>
*/ */
@ManagedBean(name = "permissionView") @ViewScoped
@SessionScoped @Named("permissionView")
public class PermissionView implements Serializable { public class PermissionView implements Serializable {
private static final long serialVersionUID = -1469453490360990772L; private static final long serialVersionUID = -1469453490360990772L;
private static final Logger LOGGER = LoggerFactory.getLogger(PermissionView.class); private static final Logger LOGGER = LoggerFactory.getLogger(PermissionView.class);
@ManagedProperty(value = "#{applicationView}") @Inject
private ApplicationView applicationView; private ApplicationView applicationView;
@EJB @EJB

View File

@ -25,9 +25,9 @@ import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.ejb.EJB; import javax.ejb.EJB;
import javax.faces.bean.ManagedBean; import javax.faces.view.ViewScoped;
import javax.faces.bean.ManagedProperty; import javax.inject.Named;
import javax.faces.bean.SessionScoped; import javax.inject.Inject;
import org.primefaces.event.SelectEvent; import org.primefaces.event.SelectEvent;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -36,14 +36,14 @@ import org.slf4j.LoggerFactory;
* *
* @author Joern Muehlencord <joern at muehlencord.de> * @author Joern Muehlencord <joern at muehlencord.de>
*/ */
@ManagedBean(name = "roleView") @ViewScoped
@SessionScoped @Named("roleView")
public class RoleView implements Serializable { public class RoleView implements Serializable {
private static final long serialVersionUID = 1669321020398119007L; private static final long serialVersionUID = 1669321020398119007L;
private static final Logger LOGGER = LoggerFactory.getLogger(RoleView.class); private static final Logger LOGGER = LoggerFactory.getLogger(RoleView.class);
@ManagedProperty(value = "#{applicationView}") @Inject
private ApplicationView applicationView; private ApplicationView applicationView;
@EJB @EJB

View File

@ -32,9 +32,6 @@
<span>Accounts</span> <span>Accounts</span>
</p:link> </p:link>
</li> </li>
<li> <li>
<p:commandLink target="/logout.xhtml" actionListener="#{loginView.logout}"> <p:commandLink target="/logout.xhtml" actionListener="#{loginView.logout}">
<i class="fa fa-sign-out"></i> <i class="fa fa-sign-out"></i>

View File

@ -21,7 +21,7 @@ limitations under the License.
xmlns:h="http://xmlns.jcp.org/jsf/html"> xmlns:h="http://xmlns.jcp.org/jsf/html">
<ui:define name="head"> <ui:define name="head">
<title>Product Catalog Designer</title> <title>Account UI</title>
<link rel="shortcut icon" href="#{resource['images/favicon/favicon.ico']}" /> <link rel="shortcut icon" href="#{resource['images/favicon/favicon.ico']}" />
<h:outputStylesheet name="css/admin.css" /> <h:outputStylesheet name="css/admin.css" />
</ui:define> </ui:define>

View File

@ -22,7 +22,7 @@
<p:dataTable id="accountTable" value="#{accountView.accounts}" var="account" rowKey="#{account.username}" selectionMode="single" selection="#{accountView.currentAccount}" <p:dataTable id="accountTable" value="#{accountView.accounts}" var="account" rowKey="#{account.username}" selectionMode="single" selection="#{accountView.currentAccount}"
styleClass="box-primary"> styleClass="box-primary">
<p:ajax event="rowSelect" update="deleteButton,editButton" listener="#{accountView.selectAccount}" /> <p:ajax event="rowSelect" update="deleteButton,editButton" listener="#{accountView.selectAccount}" />
<p:ajax event="rowUnselect" update="deleteButton,editButton" listener="#{accountView.deselectAccount}" /> <p:ajax event="rowUnselect" update="deleteButton,editButton" listener="#{accountView.selectAccount}" />
<p:column headerText="Username"> <p:column headerText="Username">
<h:outputText value="#{account.username}" /> <h:outputText value="#{account.username}" />
</p:column> </p:column>
@ -220,10 +220,10 @@
<p:outputLabel for="roles" value="Roles" /> <p:outputLabel for="roles" value="Roles" />
</div> </div>
<div class="col-sm-12 col-md-6"> <div class="col-sm-12 col-md-6">
<p:selectManyMenu id="roles" var="role" label="#{role.roleName}" value="#{accountView.currentAccountRoles}" converter="omnifaces.SelectItemsConverter" required="true" > <p:selectManyMenu id="roles" var="role" label="#{role.roleName}" value="#{accountView.currentAccountRoles}" converter="omnifaces.SelectItemsConverter" required="false" >
<f:selectItems value="#{accountView.allApplicationRoles}" var="roleItem" itemLabel="#{roleItem.roleName}" itemValue="#{role}" /> <f:selectItems value="#{accountView.allApplicationRoles}" var="roleItem" itemValue="#{roleItem}" />
<p:column> <p:column>
<h:outputText value="#{role.roleName}"/> <h:outputText value="#{role.application.applicationName}-#{role.roleName}"/>
</p:column> </p:column>
</p:selectManyMenu> </p:selectManyMenu>
</div> </div>

View File

@ -3,7 +3,7 @@
CAUTION: Do not modify this file unless you know what you are doing. CAUTION: Do not modify this file unless you know what you are doing.
Unexpected results may occur if the code is changed deliberately. Unexpected results may occur if the code is changed deliberately.
--> -->
<dbmodel pgmodeler-ver="0.9.1" author="Joern Muehlencord" last-position="259,0" last-zoom="0.85" <dbmodel pgmodeler-ver="0.9.1" author="Joern Muehlencord" last-position="0,0" last-zoom="0.85"
default-schema="public" default-owner="postgres"> default-schema="public" default-owner="postgres">
<database name="account_test" encoding="UTF8" lc-collate="German_Germany.1252" lc-ctype="German_Germany.1252" is-template="false" allow-conns="true" sql-disabled="true"> <database name="account_test" encoding="UTF8" lc-collate="German_Germany.1252" lc-ctype="German_Germany.1252" is-template="false" allow-conns="true" sql-disabled="true">
</database> </database>
@ -49,6 +49,9 @@ CAUTION: Do not modify this file unless you know what you are doing.
<constraint name="application_role_pk" type="pk-constr" table="public.application_role"> <constraint name="application_role_pk" type="pk-constr" table="public.application_role">
<columns names="id" ref-type="src-columns"/> <columns names="id" ref-type="src-columns"/>
</constraint> </constraint>
<constraint name="application_role_name_uidx" type="uq-constr" table="public.application_role">
<columns names="application,role_name" ref-type="src-columns"/>
</constraint>
</table> </table>
<table name="account"> <table name="account">

View File

@ -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.mail.boundary.MailService;
import de.muehlencord.shared.account.business.account.entity.AccountEntity; import de.muehlencord.shared.account.business.account.entity.AccountEntity;
import de.muehlencord.shared.account.business.account.entity.ApplicationRoleEntity; 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.business.config.entity.ConfigException;
import de.muehlencord.shared.account.util.SecurityUtil; import de.muehlencord.shared.account.util.SecurityUtil;
import java.io.Serializable; import java.io.Serializable;
@ -78,7 +79,7 @@ public class AccountControl implements Serializable {
StringBuilder queryBuilder = new StringBuilder(); StringBuilder queryBuilder = new StringBuilder();
queryBuilder.append("SELECT a FROM AccountEntity a "); queryBuilder.append("SELECT a FROM AccountEntity a ");
if (loadRoles) { if (loadRoles) {
queryBuilder.append("JOIN FETCH a.applicationRoleList "); queryBuilder.append("LEFT JOIN FETCH a.applicationRoleList ");
} }
queryBuilder.append("WHERE a.username = :username"); queryBuilder.append("WHERE a.username = :username");
Query query = em.createQuery(queryBuilder.toString()); Query query = em.createQuery(queryBuilder.toString());
@ -91,7 +92,7 @@ public class AccountControl implements Serializable {
} }
@Transactional @Transactional
public AccountEntity saveAccount(AccountEntity account, List<ApplicationRoleEntity> applicationRoles) { public AccountEntity saveAccount(ApplicationEntity application, AccountEntity account, List<ApplicationRoleEntity> applicationRoles) {
Date now = new Date(); // Todo now in UTC Date now = new Date(); // Todo now in UTC
Subject currentUser = SecurityUtils.getSubject(); Subject currentUser = SecurityUtils.getSubject();
String currentLoggedInUser = currentUser.getPrincipal().toString(); String currentLoggedInUser = currentUser.getPrincipal().toString();
@ -125,12 +126,15 @@ public class AccountControl implements Serializable {
boolean roleSetupChanged = false; boolean roleSetupChanged = false;
// remove roles which are no longer listed // remove roles which are no longer listed
for (ApplicationRoleEntity currentlyAssignedRole : account.getApplicationRoleList()) { // ensure this is only done for the given application - keep the other applications untouched
if (!applicationRoles.contains (currentlyAssignedRole)) { List<ApplicationRoleEntity> assignedRoles = new ArrayList<>();
assignedRoles.addAll(account.getApplicationRoleList());
for (ApplicationRoleEntity currentlyAssignedRole : assignedRoles) {
if ((currentlyAssignedRole.getApplication().equals(application) && (!applicationRoles.contains(currentlyAssignedRole)))) {
account.getApplicationRoleList().remove(currentlyAssignedRole); account.getApplicationRoleList().remove(currentlyAssignedRole);
roleSetupChanged = true; roleSetupChanged = true;
if (LOGGER.isDebugEnabled()) { 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());
} }
} }
} }
@ -138,10 +142,10 @@ public class AccountControl implements Serializable {
// add newly added roles to role list // add newly added roles to role list
for (ApplicationRoleEntity applicationRole : applicationRoles) { for (ApplicationRoleEntity applicationRole : applicationRoles) {
if (!account.getApplicationRoleList().contains(applicationRole)) { if (!account.getApplicationRoleList().contains(applicationRole)) {
account.addApplicationRole (applicationRole); account.addApplicationRole(applicationRole);
roleSetupChanged = true; roleSetupChanged = true;
if (LOGGER.isDebugEnabled()) { 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());
} }
} }
} }