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.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<ApplicationRoleEntity> currentAccountRoles = null;
// boolean flag to toggle buttons which require an account to be selected
private boolean accountSelected = false;
public List<AccountEntity> 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);
@ -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
@ -118,7 +127,6 @@ public class AccountView implements Serializable {
FacesUtil.addGlobalInfoMessage("Info", "Account " + currentAccount.getUsername() + " deleted");
currentAccount = null;
currentAccountRoles = null;
deselectAccount();
} 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,9 +150,9 @@ public class AccountView implements Serializable {
}
/* **** getter / setter **** */
/**
* setter for managed property applicationView
*
* @param applicationView the applicaton view to inject
*/
public void setApplicationView(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;
}

View File

@ -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 <joern at muehlencord.de>
*/
@ManagedBean (name="applicationView")
@SessionScoped
@Named("applicationView")
public class ApplicationView implements Serializable {
private static final long serialVersionUID = -5515249316880163539L;
@ -36,7 +37,18 @@ public class ApplicationView implements Serializable {
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<ApplicationEntity> getAllApplications() {
@ -87,11 +99,11 @@ public class ApplicationView implements Serializable {
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");
}
}

View File

@ -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 <joern at muehlencord.de>
*/
@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

View File

@ -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 <joern at muehlencord.de>
*/
@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

View File

@ -32,9 +32,6 @@
<span>Accounts</span>
</p:link>
</li>
<li>
<p:commandLink target="/logout.xhtml" actionListener="#{loginView.logout}">
<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">
<ui:define name="head">
<title>Product Catalog Designer</title>
<title>Account UI</title>
<link rel="shortcut icon" href="#{resource['images/favicon/favicon.ico']}" />
<h:outputStylesheet name="css/admin.css" />
</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}"
styleClass="box-primary">
<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">
<h:outputText value="#{account.username}" />
</p:column>
@ -220,10 +220,10 @@
<p:outputLabel for="roles" value="Roles" />
</div>
<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" >
<f:selectItems value="#{accountView.allApplicationRoles}" var="roleItem" itemLabel="#{roleItem.roleName}" itemValue="#{role}" />
<p:selectManyMenu id="roles" var="role" label="#{role.roleName}" value="#{accountView.currentAccountRoles}" converter="omnifaces.SelectItemsConverter" required="false" >
<f:selectItems value="#{accountView.allApplicationRoles}" var="roleItem" itemValue="#{roleItem}" />
<p:column>
<h:outputText value="#{role.roleName}"/>
<h:outputText value="#{role.application.applicationName}-#{role.roleName}"/>
</p:column>
</p:selectManyMenu>
</div>

View File

@ -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.
-->
<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">
<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>
@ -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">
<columns names="id" ref-type="src-columns"/>
</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 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.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<ApplicationRoleEntity> applicationRoles) {
public AccountEntity saveAccount(ApplicationEntity application, AccountEntity account, List<ApplicationRoleEntity> applicationRoles) {
Date now = new Date(); // Todo now in UTC
Subject currentUser = SecurityUtils.getSubject();
String currentLoggedInUser = currentUser.getPrincipal().toString();
@ -125,12 +126,15 @@ public class AccountControl implements Serializable {
boolean roleSetupChanged = false;
// remove roles which are no longer listed
for (ApplicationRoleEntity currentlyAssignedRole : account.getApplicationRoleList()) {
if (!applicationRoles.contains (currentlyAssignedRole)) {
// ensure this is only done for the given application - keep the other applications untouched
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);
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());
}
}
}
@ -138,10 +142,10 @@ public class AccountControl implements Serializable {
// 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());
}
}
}