From 9ebb649458c480d8ce69edb867e1aca03b4da191 Mon Sep 17 00:00:00 2001 From: jomu Date: Sat, 10 Nov 2018 14:03:34 +0100 Subject: [PATCH] added first draft of mapplication support --- .../account/boundary/AccountControl.java | 597 ++++++++------- .../account/entity/AccountEntity.java | 718 +++++++++--------- .../account/entity/AccountStatus.java | 41 +- .../account/entity/ApplicationRoleEntity.java | 317 ++++---- .../boundary/ApplicationService.java | 73 ++ .../application/entity/ApplicationEntity.java | 107 +++ 6 files changed, 1045 insertions(+), 808 deletions(-) create mode 100644 account/src/main/java/de/muehlencord/shared/account/business/application/boundary/ApplicationService.java create mode 100644 account/src/main/java/de/muehlencord/shared/account/business/application/entity/ApplicationEntity.java 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 008e850..ec887ea 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 @@ -1,287 +1,310 @@ -package de.muehlencord.shared.account.business.account.boundary; - -import de.muehlencord.shared.account.business.account.entity.AccountException; -import de.muehlencord.shared.account.business.account.entity.AccountStatus; -import de.muehlencord.shared.account.business.config.boundary.ConfigService; -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.config.entity.ConfigException; -import de.muehlencord.shared.account.util.SecurityUtil; -import java.io.Serializable; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; -import javax.ejb.EJB; -import javax.ejb.Stateless; -import javax.inject.Inject; -import javax.persistence.EntityManager; -import javax.persistence.NoResultException; -import javax.persistence.Query; -import javax.transaction.Transactional; -import org.apache.commons.lang3.RandomStringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.apache.shiro.SecurityUtils; -import org.apache.shiro.subject.Subject; - -/** - * - * @author joern.muehlencord - */ -@Stateless -public class AccountControl implements Serializable { - - private static final Logger LOGGER = LoggerFactory.getLogger(AccountControl.class.getName()); - private static final long serialVersionUID = 3424816272598108101L; - - @EJB - private ConfigService configService; - - @EJB - private MailService mailService; - - @Inject - EntityManager em; - - public List getAccounts() { - Query query = em.createQuery("SELECT a FROM AccountEntity a WHERE a.status <> :status", AccountEntity.class); - query.setParameter("status", AccountStatus.DISABLED.name()); - return query.getResultList(); - } - - public AccountEntity getAccountEntity(String userName, boolean loadRoles) { - StringBuilder queryBuilder = new StringBuilder(); - queryBuilder.append("SELECT a FROM AccountEntity a "); - if (loadRoles) { - queryBuilder.append("JOIN FETCH a.applicationRoleList "); - } - queryBuilder.append("WHERE a.username = :username"); - Query query = em.createQuery(queryBuilder.toString()); - query.setParameter("username", userName); - try { - return (AccountEntity) query.getSingleResult(); - } catch (NoResultException ex) { - return null; - } - } - - @Transactional - // TODO add role names from application because only application can know how its roles are named - public AccountEntity saveAccount(AccountEntity account, boolean isAdmin) { - Date now = new Date(); // Todo now in UTC - Subject currentUser = SecurityUtils.getSubject(); - String currentLoggedInUser = currentUser.getPrincipal().toString(); - - account.setLastUpdatedBy(currentLoggedInUser); - account.setLastUpdatedOn(now); - - boolean newAccount = (account.getCreatedOn() == null); - - // new account - if (newAccount) { - account.setCreatedOn(now); - account.setCreatedBy(currentLoggedInUser); - - // set default random password, user has to get password via lost passwort option afterwards - String randomPassword = RandomStringUtils.random(20, true, true); - String hashedPassword = SecurityUtil.createPassword(randomPassword); - account.setAccountPassword(hashedPassword); - em.persist(account); - } else { - em.merge(account); - - // reload account from db and join roles - account = getAccountEntity(account.getUsername(), true); - } - - // load Admin or User role from database - String roleName = (isAdmin ? "Admin" : "User"); - Query roleQuery = em.createNamedQuery("ApplicationRoleEntity.findByRoleName"); - roleQuery.setParameter("roleName", roleName); - ApplicationRoleEntity role = (ApplicationRoleEntity) roleQuery.getSingleResult(); - - if (role != null) { - // add new user add required role - // do not request based on newUser variable; this way existing users with missing role (for whatever reason) - // will be fixed automatically - if (account.getApplicationRoleList() == null || account.getApplicationRoleList().isEmpty()) { - account.setApplicationRoleList(new ArrayList<>()); - account.getApplicationRoleList().add(role); - em.merge(account); - LOGGER.info("Added role " + roleName + " to user " + account.getUsername()); - - } else if (!account.getApplicationRoleList().get(0).equals(role)) { - // change role from User to Admin and vice versa - // user already exists, has existing role - // check if existing role is different from current role and change it - // be carefull: this only works as long as a user has exactly one role! - // he is either User or Admin - // TODO add "UserRole" to every user, make this default Role configurable - // TODO add AdminRole in addtion if needed - account.getApplicationRoleList().remove(0); - account.getApplicationRoleList().add(role); - em.merge(account); - LOGGER.info("Switched role of user " + account.getUsername() + " to " + roleName); - - } - } - - return account; - } - - public void deleteAccount(AccountEntity account) throws AccountException { - Date now = new Date(); // Todo now in UTC - Subject currentUser = SecurityUtils.getSubject(); - String currentUserName = currentUser.getPrincipal().toString(); - - if (account.getUsername().equals(currentUserName)) { - throw new AccountException("Cannot delete own account"); - } else { - account.setStatus(AccountStatus.DISABLED.name()); - account.setLastUpdatedBy(currentUserName); - account.setLastUpdatedOn(now); - em.merge(account); - } - - } - - public boolean initPasswordReset(String userName) { - try { - AccountEntity account = getAccountEntity(userName, false); - if (account == null) { - LOGGER.warn("Account with name " + userName + " not found"); - return false; - } - - if (account.getStatus().equals(AccountStatus.BLOCKED.name())) { - LOGGER.warn("Account " + userName + " is locked, cannot initialize password reset"); - return false; - } - - String randomString = RandomStringUtils.random(40, true, true); - - Date validTo = new Date(); // TODO now in UTC - validTo = new Date(validTo.getTime() + 1000 * 600); // 10 minutes to react - - account.setPasswordResetHash(randomString); - account.setPasswordResetOngoing(true); - account.setPasswordResetValidTo(validTo); - - mailService.sendPasswortResetStartEmail(account, randomString); - - em.merge(account); - return true; - } catch (MailException ex) { - LOGGER.error("Error while sending password reset mail. " + ex.toString()); - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("Error while sending password reset mail.", ex); - } - return false; - } - } - - public boolean resetPassword(String userName, String newPassword, String resetPasswordToken) { - AccountEntity account = getAccountEntity(userName, false); - - if (account == null) { - LOGGER.warn("Error while resetting password, no account with username " + userName + " found"); - // TODO add extra logging for intrusion protection system like fail2ban - return false; - } - - if (account.getPasswordResetOngoing() && (account.getPasswordResetHash() != null) && (account.getPasswordResetValidTo() != null)) { - Date now = new Date(); // TODO now in UTC - String storedHash = account.getPasswordResetHash().trim(); - if (account.getPasswordResetValidTo().after(now)) { - if (storedHash.equals(resetPasswordToken)) { - // everything ok, reset password - executePasswordReset(account, newPassword); - LOGGER.info("Updated password for user " + userName); - return true; - } else { - // token is not valid, refuse to change password - LOGGER.warn("Trying to reset password for user " + userName + " but wrong token " + resetPasswordToken + " provided"); - addLoginError(account); - return false; - } - } else { - // password reset token no longer valid - LOGGER.warn("Trying to reset password for user " + userName + " but token is no longer valid"); - addLoginError(account); - return false; - } - } else { - // user is not is password reset mode - LOGGER.warn("Trying to reset password for user " + userName + " but password reset was not requested"); - addLoginError(account); - return false; - } - } - - private void executePasswordReset(AccountEntity account, String newPassword) { - Date now = new Date(); // TODO now in UTC - - String hashedPassword = SecurityUtil.createPassword(newPassword); - account.setAccountPassword(hashedPassword); - - account.setPasswordResetOngoing(false); - account.setPasswordResetHash(null); - account.setPasswordResetValidTo(null); - - account.setLastUpdatedBy(account.getUsername()); - account.setLastUpdatedOn(now); - em.merge(account); - - } - - public void updateLogin(AccountEntity account) { - Date now = new Date(); // TODO now in UTC - // a scucessful login ends a password reset procedure - if (account.getPasswordResetOngoing()) { - account.setPasswordResetOngoing(false); - account.setPasswordResetHash(null); - account.setPasswordResetValidTo(null); - account.setLastUpdatedOn(now); - account.setLastUpdatedBy(account.getUsername()); - } - - account.setLastLogin(now); - account.setFailureCount(0); - account.setStatus(AccountStatus.NORMAL.name()); - - em.merge(account); - } - - public void addLoginError(AccountEntity account) { - try { - Date now = new Date(); // TODO now in UTC - account.setLastFailedLogin(now); - account.setFailureCount(account.getFailureCount() + 1); - - int maxFailedLogins = Integer.parseInt(configService.getConfigValue("account.maxFailedLogins")); - if ((account.getFailureCount() >= maxFailedLogins) && (!account.getStatus().equals("LOCKED"))) { // TOD add status enum - // max failed logins reached, disabling user - LOGGER.info("Locking account " + account.getUsername() + " due to " + account.getFailureCount() + " failed logins"); - account.setStatus(AccountStatus.BLOCKED.name()); - } - - // on a failed login request, disable password reset - account.setPasswordResetOngoing(false); - account.setPasswordResetHash(null); - account.setPasswordResetValidTo(null); - - account.setLastUpdatedBy("system"); - account.setLastUpdatedOn(now); - em.merge(account); - } catch (ConfigException ex) { - if (LOGGER.isDebugEnabled()) { - LOGGER.debug(ex.toString(), ex); - } else { - LOGGER.error(ex.toString()); - } - } - } - -} +package de.muehlencord.shared.account.business.account.boundary; + +import de.muehlencord.shared.account.business.account.entity.AccountException; +import de.muehlencord.shared.account.business.account.entity.AccountStatus; +import de.muehlencord.shared.account.business.config.boundary.ConfigService; +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.config.entity.ConfigException; +import de.muehlencord.shared.account.util.SecurityUtil; +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import javax.ejb.EJB; +import javax.ejb.Stateless; +import javax.inject.Inject; +import javax.persistence.EntityManager; +import javax.persistence.NoResultException; +import javax.persistence.Query; +import javax.transaction.Transactional; +import org.apache.commons.lang3.RandomStringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.shiro.SecurityUtils; +import org.apache.shiro.subject.Subject; + +/** + * + * @author joern.muehlencord + */ +@Stateless +public class AccountControl implements Serializable { + + private static final Logger LOGGER = LoggerFactory.getLogger(AccountControl.class.getName()); + private static final long serialVersionUID = 3424816272598108101L; + + @EJB + private ConfigService configService; + + @EJB + private MailService mailService; + + @Inject + EntityManager em; + + /** + * returns a list of active accounts + * + * @return a list of active accounts + */ + public List getActiveAccounts() { + Query query = em.createQuery("SELECT a FROM AccountEntity a WHERE a.status <> :status", AccountEntity.class); + query.setParameter("status", AccountStatus.DISABLED.name()); + return query.getResultList(); + } + + /** + * returns a list of active accounts + * + * @return a list of active accounts + */ + public List getAllAccounts() { + Query query = em.createNamedQuery("AccountEntity.findAll"); + return query.getResultList(); + } + + public List getAccounts(boolean includeDisabled) { + if (includeDisabled) { + return getAllAccounts(); + } else { + return getActiveAccounts(); + } + } + + public AccountEntity getAccountEntity(String userName, boolean loadRoles) { + StringBuilder queryBuilder = new StringBuilder(); + queryBuilder.append("SELECT a FROM AccountEntity a "); + if (loadRoles) { + queryBuilder.append("JOIN FETCH a.applicationRoleList "); + } + queryBuilder.append("WHERE a.username = :username"); + Query query = em.createQuery(queryBuilder.toString()); + query.setParameter("username", userName); + try { + return (AccountEntity) query.getSingleResult(); + } catch (NoResultException ex) { + return null; + } + } + + @Transactional + public AccountEntity saveAccount(AccountEntity account, List applicationRoles) { + Date now = new Date(); // Todo now in UTC + Subject currentUser = SecurityUtils.getSubject(); + String currentLoggedInUser = currentUser.getPrincipal().toString(); + + account.setLastUpdatedBy(currentLoggedInUser); + account.setLastUpdatedOn(now); + + boolean newAccount = (account.getCreatedOn() == null); + + // new account + if (newAccount) { + account.setCreatedOn(now); + account.setCreatedBy(currentLoggedInUser); + + // set default random password, user has to get password via lost passwort option afterwards + String randomPassword = RandomStringUtils.random(20, true, true); + String hashedPassword = SecurityUtil.createPassword(randomPassword); + account.setAccountPassword(hashedPassword); + em.persist(account); + } else { + em.merge(account); + + // 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); + roleSetupChanged = true; + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("Removed role {} from user {}", currentlyAssignedRole.getRoleName(), account.getUsername()); + } + } + } + + // add newly added roles to role list + for (ApplicationRoleEntity applicationRole : applicationRoles) { + if (!account.getApplicationRoleList().contains(applicationRole)) { + account.addApplicationRole (applicationRole); + roleSetupChanged = true; + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("Added role {} to account {}", applicationRole.getRoleName(), account.getUsername()); + } + } + } + + // update account in database if roles changed + if (roleSetupChanged) { + em.merge(account); + } + return account; + } + + @Transactional + public void deleteAccount(AccountEntity account) throws AccountException { + Date now = new Date(); // Todo now in UTC + Subject currentUser = SecurityUtils.getSubject(); + String currentUserName = currentUser.getPrincipal().toString(); + + if (account.getUsername().equals(currentUserName)) { + throw new AccountException("Cannot delete own account"); + } else { + account.setStatus(AccountStatus.DISABLED.name()); + account.setLastUpdatedBy(currentUserName); + account.setLastUpdatedOn(now); + em.merge(account); + } + + } + + public boolean initPasswordReset(String userName) { + try { + AccountEntity account = getAccountEntity(userName, false); + if (account == null) { + LOGGER.warn("Account with name " + userName + " not found"); + return false; + } + + if (account.getStatus().equals(AccountStatus.BLOCKED.name())) { + LOGGER.warn("Account " + userName + " is locked, cannot initialize password reset"); + return false; + } + + String randomString = RandomStringUtils.random(40, true, true); + + Date validTo = new Date(); // TODO now in UTC + validTo = new Date(validTo.getTime() + 1000 * 600); // 10 minutes to react + + account.setPasswordResetHash(randomString); + account.setPasswordResetOngoing(true); + account.setPasswordResetValidTo(validTo); + + mailService.sendPasswortResetStartEmail(account, randomString); + + em.merge(account); + return true; + } catch (MailException ex) { + LOGGER.error("Error while sending password reset mail. " + ex.toString()); + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("Error while sending password reset mail.", ex); + } + return false; + } + } + + public boolean resetPassword(String userName, String newPassword, String resetPasswordToken) { + AccountEntity account = getAccountEntity(userName, false); + + if (account == null) { + LOGGER.warn("Error while resetting password, no account with username " + userName + " found"); + // TODO add extra logging for intrusion protection system like fail2ban + return false; + } + + if (account.getPasswordResetOngoing() && (account.getPasswordResetHash() != null) && (account.getPasswordResetValidTo() != null)) { + Date now = new Date(); // TODO now in UTC + String storedHash = account.getPasswordResetHash().trim(); + if (account.getPasswordResetValidTo().after(now)) { + if (storedHash.equals(resetPasswordToken)) { + // everything ok, reset password + executePasswordReset(account, newPassword); + LOGGER.info("Updated password for user " + userName); + return true; + } else { + // token is not valid, refuse to change password + LOGGER.warn("Trying to reset password for user " + userName + " but wrong token " + resetPasswordToken + " provided"); + addLoginError(account); + return false; + } + } else { + // password reset token no longer valid + LOGGER.warn("Trying to reset password for user " + userName + " but token is no longer valid"); + addLoginError(account); + return false; + } + } else { + // user is not is password reset mode + LOGGER.warn("Trying to reset password for user " + userName + " but password reset was not requested"); + addLoginError(account); + return false; + } + } + + private void executePasswordReset(AccountEntity account, String newPassword) { + Date now = new Date(); // TODO now in UTC + + String hashedPassword = SecurityUtil.createPassword(newPassword); + account.setAccountPassword(hashedPassword); + + account.setPasswordResetOngoing(false); + account.setPasswordResetHash(null); + account.setPasswordResetValidTo(null); + + account.setLastUpdatedBy(account.getUsername()); + account.setLastUpdatedOn(now); + em.merge(account); + + } + + public void updateLogin(AccountEntity account) { + Date now = new Date(); // TODO now in UTC + // a scucessful login ends a password reset procedure + if (account.getPasswordResetOngoing()) { + account.setPasswordResetOngoing(false); + account.setPasswordResetHash(null); + account.setPasswordResetValidTo(null); + account.setLastUpdatedOn(now); + account.setLastUpdatedBy(account.getUsername()); + } + + account.setLastLogin(now); + account.setFailureCount(0); + account.setStatus(AccountStatus.NORMAL.name()); + + em.merge(account); + } + + public void addLoginError(AccountEntity account) { + try { + Date now = new Date(); // TODO now in UTC + account.setLastFailedLogin(now); + account.setFailureCount(account.getFailureCount() + 1); + + int maxFailedLogins = Integer.parseInt(configService.getConfigValue("account.maxFailedLogins")); + if ((account.getFailureCount() >= maxFailedLogins) && (!account.getStatus().equals("LOCKED"))) { // TOD add status enum + // max failed logins reached, disabling user + LOGGER.info("Locking account " + account.getUsername() + " due to " + account.getFailureCount() + " failed logins"); + account.setStatus(AccountStatus.BLOCKED.name()); + } + + // on a failed login request, disable password reset + account.setPasswordResetOngoing(false); + account.setPasswordResetHash(null); + account.setPasswordResetValidTo(null); + + account.setLastUpdatedBy("system"); + account.setLastUpdatedOn(now); + em.merge(account); + } catch (ConfigException ex) { + if (LOGGER.isDebugEnabled()) { + LOGGER.debug(ex.toString(), ex); + } else { + LOGGER.error(ex.toString()); + } + } + } + +} diff --git a/account/src/main/java/de/muehlencord/shared/account/business/account/entity/AccountEntity.java b/account/src/main/java/de/muehlencord/shared/account/business/account/entity/AccountEntity.java index fc5099f..a0ae29e 100644 --- a/account/src/main/java/de/muehlencord/shared/account/business/account/entity/AccountEntity.java +++ b/account/src/main/java/de/muehlencord/shared/account/business/account/entity/AccountEntity.java @@ -1,354 +1,364 @@ -package de.muehlencord.shared.account.business.account.entity; - -import java.io.Serializable; -import java.util.Date; -import java.util.List; -import java.util.UUID; -import javax.persistence.Basic; -import javax.persistence.CascadeType; -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.FetchType; -import javax.persistence.GeneratedValue; -import javax.persistence.Id; -import javax.persistence.JoinColumn; -import javax.persistence.JoinTable; -import javax.persistence.ManyToMany; -import javax.persistence.NamedQueries; -import javax.persistence.NamedQuery; -import javax.persistence.OneToMany; -import javax.persistence.Table; -import javax.persistence.Temporal; -import javax.persistence.TemporalType; -import javax.validation.constraints.NotNull; -import javax.validation.constraints.Size; -import javax.xml.bind.annotation.XmlRootElement; -import javax.xml.bind.annotation.XmlTransient; -import org.hibernate.annotations.GenericGenerator; -import org.hibernate.annotations.Type; - -/** - * - * @author joern.muehlencord - */ -@Entity -@Table(name = "account") -@XmlRootElement -@NamedQueries({ - @NamedQuery(name = "AccountEntity.findAll", query = "SELECT a FROM AccountEntity a"), - @NamedQuery(name = "AccountEntity.findByUsername", query = "SELECT a FROM AccountEntity a WHERE a.username = :username"), - @NamedQuery(name = "AccountEntity.findByEmailaddress", query = "SELECT a FROM AccountEntity a WHERE a.emailaddress = :emailaddress"), - @NamedQuery(name = "AccountEntity.findByFirstname", query = "SELECT a FROM AccountEntity a WHERE a.firstname = :firstname"), - @NamedQuery(name = "AccountEntity.findByLastname", query = "SELECT a FROM AccountEntity a WHERE a.lastname = :lastname"), - @NamedQuery(name = "AccountEntity.findByAccountPassword", query = "SELECT a FROM AccountEntity a WHERE a.accountPassword = :accountPassword"), - @NamedQuery(name = "AccountEntity.findByLastLogin", query = "SELECT a FROM AccountEntity a WHERE a.lastLogin = :lastLogin"), - @NamedQuery(name = "AccountEntity.findByLastFailedLogin", query = "SELECT a FROM AccountEntity a WHERE a.lastFailedLogin = :lastFailedLogin"), - @NamedQuery(name = "AccountEntity.findByFailureCount", query = "SELECT a FROM AccountEntity a WHERE a.failureCount = :failureCount"), - @NamedQuery(name = "AccountEntity.findByStatus", query = "SELECT a FROM AccountEntity a WHERE a.status = :status"), - @NamedQuery(name = "AccountEntity.findByPasswordResetOngoing", query = "SELECT a FROM AccountEntity a WHERE a.passwordResetOngoing = :passwordResetOngoing"), - @NamedQuery(name = "AccountEntity.findByPasswordResetValidTo", query = "SELECT a FROM AccountEntity a WHERE a.passwordResetValidTo = :passwordResetValidTo"), - @NamedQuery(name = "AccountEntity.findByPasswordResetHash", query = "SELECT a FROM AccountEntity a WHERE a.passwordResetHash = :passwordResetHash"), - @NamedQuery(name = "AccountEntity.findByCreatedOn", query = "SELECT a FROM AccountEntity a WHERE a.createdOn = :createdOn"), - @NamedQuery(name = "AccountEntity.findByCreatedBy", query = "SELECT a FROM AccountEntity a WHERE a.createdBy = :createdBy"), - @NamedQuery(name = "AccountEntity.findByLastUpdatedOn", query = "SELECT a FROM AccountEntity a WHERE a.lastUpdatedOn = :lastUpdatedOn"), - @NamedQuery(name = "AccountEntity.findByLastUpdatedBy", query = "SELECT a FROM AccountEntity a WHERE a.lastUpdatedBy = :lastUpdatedBy")}) -public class AccountEntity implements Serializable, Account { - - private static final long serialVersionUID = 6216991757526150935L; - - @Id - @Basic(optional = false) - @NotNull - @Column(name = "id") - @GeneratedValue(generator = "uuid2") - @GenericGenerator(name = "uuid2", strategy = "uuid2") - @Type(type = "pg-uuid") - private UUID id; - @Basic(optional = false) - @NotNull - @Size(min = 1, max = 32) - @Column(name = "username") - private String username; - @Basic(optional = false) - @NotNull - @Size(min = 1, max = 200) - @Column(name = "emailaddress") - private String emailaddress; - @Basic(optional = false) - @NotNull - @Size(min = 1, max = 100) - @Column(name = "firstname") - private String firstname; - @Basic(optional = false) - @NotNull - @Size(min = 1, max = 100) - @Column(name = "lastname") - private String lastname; - @Basic(optional = false) - @NotNull - @Size(min = 1, max = 200) - @Column(name = "account_password", columnDefinition = "bpchar(200)") - private String accountPassword; - @Column(name = "last_login") - @Temporal(TemporalType.TIMESTAMP) - private Date lastLogin; - @Column(name = "last_failed_login") - @Temporal(TemporalType.TIMESTAMP) - private Date lastFailedLogin; - @Basic(optional = false) - @NotNull - @Column(name = "failure_count") - private int failureCount; - @Basic(optional = false) - @NotNull - @Size(min = 1, max = 10) - @Column(name = "status") - private String status; - @Basic(optional = false) - @NotNull - @Column(name = "password_reset_ongoing") - private boolean passwordResetOngoing; - @Column(name = "password_reset_valid_to") - @Temporal(TemporalType.TIMESTAMP) - private Date passwordResetValidTo; - @Size(max = 200) - @Column(name = "password_reset_hash", columnDefinition = "bpchar(200)") - private String passwordResetHash; - @Basic(optional = false) - @NotNull - @Column(name = "created_on") - @Temporal(TemporalType.TIMESTAMP) - private Date createdOn; - @Basic(optional = false) - @NotNull - @Size(min = 1, max = 32) - @Column(name = "created_by") - private String createdBy; - @Basic(optional = false) - @NotNull - @Column(name = "last_updated_on") - @Temporal(TemporalType.TIMESTAMP) - private Date lastUpdatedOn; - @Basic(optional = false) - @NotNull - @Size(min = 1, max = 32) - @Column(name = "last_updated_by") - private String lastUpdatedBy; - @JoinTable(name = "account_role", joinColumns = { - @JoinColumn(name = "account", referencedColumnName = "id")}, inverseJoinColumns = { - @JoinColumn(name = "account_role", referencedColumnName = "id")}) - @ManyToMany(fetch = FetchType.LAZY) - private List applicationRoleList; - @OneToMany(cascade = CascadeType.ALL, mappedBy = "accountId", fetch = FetchType.LAZY) - private List accountHistoryList; - - public AccountEntity() { - } - - public AccountEntity(UUID id) { - this.id = id; - } - - public AccountEntity (String name) { - this.username = name; - } - - public AccountEntity(UUID id, String username, String emailaddress, String firstname, String lastname, String accountPassword, int failureCount, String status, boolean passwordResetOngoing, Date createdOn, String createdBy, Date lastUpdatedOn, String lastUpdatedBy) { - this.id = id; - this.username = username; - this.emailaddress = emailaddress; - this.firstname = firstname; - this.lastname = lastname; - this.accountPassword = accountPassword; - this.failureCount = failureCount; - this.status = status; - this.passwordResetOngoing = passwordResetOngoing; - this.createdOn = createdOn; - this.createdBy = createdBy; - this.lastUpdatedOn = lastUpdatedOn; - this.lastUpdatedBy = lastUpdatedBy; - } - - public UUID getId() { - return id; - } - - public void setId(UUID id) { - this.id = id; - } - - @Override - public String getUsername() { - return username; - } - - public void setUsername(String username) { - this.username = username; - } - - public String getEmailaddress() { - return emailaddress; - } - - public void setEmailaddress(String emailaddress) { - this.emailaddress = emailaddress; - } - - @Override - public String getFirstname() { - return firstname; - } - - public void setFirstname(String firstname) { - this.firstname = firstname; - } - - @Override - public String getLastname() { - return lastname; - } - - public void setLastname(String lastname) { - this.lastname = lastname; - } - - public String getAccountPassword() { - return accountPassword; - } - - public void setAccountPassword(String accountPassword) { - this.accountPassword = accountPassword; - } - - public Date getLastLogin() { - return lastLogin; - } - - public void setLastLogin(Date lastLogin) { - this.lastLogin = lastLogin; - } - - public Date getLastFailedLogin() { - return lastFailedLogin; - } - - public void setLastFailedLogin(Date lastFailedLogin) { - this.lastFailedLogin = lastFailedLogin; - } - - public int getFailureCount() { - return failureCount; - } - - public void setFailureCount(int failureCount) { - this.failureCount = failureCount; - } - - public String getStatus() { - return status; - } - - public void setStatus(String status) { - this.status = status; - } - - public boolean getPasswordResetOngoing() { - return passwordResetOngoing; - } - - public void setPasswordResetOngoing(boolean passwordResetOngoing) { - this.passwordResetOngoing = passwordResetOngoing; - } - - public Date getPasswordResetValidTo() { - return passwordResetValidTo; - } - - public void setPasswordResetValidTo(Date passwordResetValidTo) { - this.passwordResetValidTo = passwordResetValidTo; - } - - public String getPasswordResetHash() { - return passwordResetHash; - } - - public void setPasswordResetHash(String passwordResetHash) { - this.passwordResetHash = passwordResetHash; - } - - public Date getCreatedOn() { - return createdOn; - } - - public void setCreatedOn(Date createdOn) { - this.createdOn = createdOn; - } - - public String getCreatedBy() { - return createdBy; - } - - public void setCreatedBy(String createdBy) { - this.createdBy = createdBy; - } - - public Date getLastUpdatedOn() { - return lastUpdatedOn; - } - - public void setLastUpdatedOn(Date lastUpdatedOn) { - this.lastUpdatedOn = lastUpdatedOn; - } - - public String getLastUpdatedBy() { - return lastUpdatedBy; - } - - public void setLastUpdatedBy(String lastUpdatedBy) { - this.lastUpdatedBy = lastUpdatedBy; - } - - @XmlTransient - public List getApplicationRoleList() { - return applicationRoleList; - } - - public void setApplicationRoleList(List applicationRoleList) { - this.applicationRoleList = applicationRoleList; - } - - @XmlTransient - public List getAccountHistoryList() { - return accountHistoryList; - } - - public void setAccountHistoryList(List accountHistoryList) { - this.accountHistoryList = accountHistoryList; - } - - @Override - public int hashCode() { - int hash = 0; - hash += (id != null ? id.hashCode() : 0); - return hash; - } - - @Override - public boolean equals(Object object) { - // TODO: Warning - this method won't work in the case the id fields are not set - if (!(object instanceof AccountEntity)) { - return false; - } - AccountEntity other = (AccountEntity) object; - if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) { - return false; - } - return true; - } - - @Override - public String toString() { - return "de.muehlencord.shared.account.entity.Account[ id=" + id + " ]"; - } - -} +package de.muehlencord.shared.account.business.account.entity; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.UUID; +import javax.persistence.Basic; +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.JoinTable; +import javax.persistence.ManyToMany; +import javax.persistence.NamedQueries; +import javax.persistence.NamedQuery; +import javax.persistence.OneToMany; +import javax.persistence.Table; +import javax.persistence.Temporal; +import javax.persistence.TemporalType; +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Size; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.XmlTransient; +import org.hibernate.annotations.GenericGenerator; +import org.hibernate.annotations.Type; + +/** + * + * @author joern.muehlencord + */ +@Entity +@Table(name = "account") +@XmlRootElement +@NamedQueries({ + @NamedQuery(name = "AccountEntity.findAll", query = "SELECT a FROM AccountEntity a ORDER by a.username"), + @NamedQuery(name = "AccountEntity.findByUsername", query = "SELECT a FROM AccountEntity a WHERE a.username = :username"), + @NamedQuery(name = "AccountEntity.findByEmailaddress", query = "SELECT a FROM AccountEntity a WHERE a.emailaddress = :emailaddress ORDER BY a.username"), + @NamedQuery(name = "AccountEntity.findByFirstname", query = "SELECT a FROM AccountEntity a WHERE a.firstname = :firstname ORDER BY a.username"), + @NamedQuery(name = "AccountEntity.findByLastname", query = "SELECT a FROM AccountEntity a WHERE a.lastname = :lastname ORDER BY a.username"), + @NamedQuery(name = "AccountEntity.findByAccountPassword", query = "SELECT a FROM AccountEntity a WHERE a.accountPassword = :accountPassword"), + @NamedQuery(name = "AccountEntity.findByLastLogin", query = "SELECT a FROM AccountEntity a WHERE a.lastLogin = :lastLogin"), + @NamedQuery(name = "AccountEntity.findByLastFailedLogin", query = "SELECT a FROM AccountEntity a WHERE a.lastFailedLogin = :lastFailedLogin"), + @NamedQuery(name = "AccountEntity.findByFailureCount", query = "SELECT a FROM AccountEntity a WHERE a.failureCount = :failureCount"), + @NamedQuery(name = "AccountEntity.findByStatus", query = "SELECT a FROM AccountEntity a WHERE a.status = :status"), + @NamedQuery(name = "AccountEntity.findByPasswordResetOngoing", query = "SELECT a FROM AccountEntity a WHERE a.passwordResetOngoing = :passwordResetOngoing"), + @NamedQuery(name = "AccountEntity.findByPasswordResetValidTo", query = "SELECT a FROM AccountEntity a WHERE a.passwordResetValidTo = :passwordResetValidTo"), + @NamedQuery(name = "AccountEntity.findByPasswordResetHash", query = "SELECT a FROM AccountEntity a WHERE a.passwordResetHash = :passwordResetHash"), + @NamedQuery(name = "AccountEntity.findByCreatedOn", query = "SELECT a FROM AccountEntity a WHERE a.createdOn = :createdOn"), + @NamedQuery(name = "AccountEntity.findByCreatedBy", query = "SELECT a FROM AccountEntity a WHERE a.createdBy = :createdBy"), + @NamedQuery(name = "AccountEntity.findByLastUpdatedOn", query = "SELECT a FROM AccountEntity a WHERE a.lastUpdatedOn = :lastUpdatedOn"), + @NamedQuery(name = "AccountEntity.findByLastUpdatedBy", query = "SELECT a FROM AccountEntity a WHERE a.lastUpdatedBy = :lastUpdatedBy")}) +public class AccountEntity implements Serializable, Account { + + private static final long serialVersionUID = 6216991757526150935L; + + @Id + @Basic(optional = false) + @NotNull + @Column(name = "id") + @GeneratedValue(generator = "uuid2") + @GenericGenerator(name = "uuid2", strategy = "uuid2") + @Type(type = "pg-uuid") + private UUID id; + @Basic(optional = false) + @NotNull + @Size(min = 1, max = 32) + @Column(name = "username") + private String username; + @Basic(optional = false) + @NotNull + @Size(min = 1, max = 200) + @Column(name = "emailaddress") + private String emailaddress; + @Basic(optional = false) + @NotNull + @Size(min = 1, max = 100) + @Column(name = "firstname") + private String firstname; + @Basic(optional = false) + @NotNull + @Size(min = 1, max = 100) + @Column(name = "lastname") + private String lastname; + @Basic(optional = false) + @NotNull + @Size(min = 1, max = 200) + @Column(name = "account_password", columnDefinition = "bpchar(200)") + private String accountPassword; + @Column(name = "last_login") + @Temporal(TemporalType.TIMESTAMP) + private Date lastLogin; + @Column(name = "last_failed_login") + @Temporal(TemporalType.TIMESTAMP) + private Date lastFailedLogin; + @Basic(optional = false) + @NotNull + @Column(name = "failure_count") + private int failureCount; + @Basic(optional = false) + @NotNull + @Size(min = 1, max = 10) + @Column(name = "status") + private String status; + @Basic(optional = false) + @NotNull + @Column(name = "password_reset_ongoing") + private boolean passwordResetOngoing; + @Column(name = "password_reset_valid_to") + @Temporal(TemporalType.TIMESTAMP) + private Date passwordResetValidTo; + @Size(max = 200) + @Column(name = "password_reset_hash", columnDefinition = "bpchar(200)") + private String passwordResetHash; + @Basic(optional = false) + @NotNull + @Column(name = "created_on") + @Temporal(TemporalType.TIMESTAMP) + private Date createdOn; + @Basic(optional = false) + @NotNull + @Size(min = 1, max = 32) + @Column(name = "created_by") + private String createdBy; + @Basic(optional = false) + @NotNull + @Column(name = "last_updated_on") + @Temporal(TemporalType.TIMESTAMP) + private Date lastUpdatedOn; + @Basic(optional = false) + @NotNull + @Size(min = 1, max = 32) + @Column(name = "last_updated_by") + private String lastUpdatedBy; + @JoinTable(name = "account_role", joinColumns = { + @JoinColumn(name = "account", referencedColumnName = "id")}, inverseJoinColumns = { + @JoinColumn(name = "account_role", referencedColumnName = "id")}) + @ManyToMany(fetch = FetchType.LAZY) + private List applicationRoleList; + @OneToMany(cascade = CascadeType.ALL, mappedBy = "accountId", fetch = FetchType.LAZY) + private List accountHistoryList; + + public AccountEntity() { + } + + public AccountEntity(UUID id) { + this.id = id; + } + + public AccountEntity (String name) { + this.username = name; + } + + public AccountEntity(UUID id, String username, String emailaddress, String firstname, String lastname, String accountPassword, int failureCount, String status, boolean passwordResetOngoing, Date createdOn, String createdBy, Date lastUpdatedOn, String lastUpdatedBy) { + this.id = id; + this.username = username; + this.emailaddress = emailaddress; + this.firstname = firstname; + this.lastname = lastname; + this.accountPassword = accountPassword; + this.failureCount = failureCount; + this.status = status; + this.passwordResetOngoing = passwordResetOngoing; + this.createdOn = createdOn; + this.createdBy = createdBy; + this.lastUpdatedOn = lastUpdatedOn; + this.lastUpdatedBy = lastUpdatedBy; + } + + public void addApplicationRole(ApplicationRoleEntity applicationRole) { + if (applicationRoleList == null) { + applicationRoleList = new ArrayList<>(); + } + applicationRoleList.add (applicationRole); + } + + /* **** getter / setter **** */ + + public UUID getId() { + return id; + } + + public void setId(UUID id) { + this.id = id; + } + + @Override + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getEmailaddress() { + return emailaddress; + } + + public void setEmailaddress(String emailaddress) { + this.emailaddress = emailaddress; + } + + @Override + public String getFirstname() { + return firstname; + } + + public void setFirstname(String firstname) { + this.firstname = firstname; + } + + @Override + public String getLastname() { + return lastname; + } + + public void setLastname(String lastname) { + this.lastname = lastname; + } + + public String getAccountPassword() { + return accountPassword; + } + + public void setAccountPassword(String accountPassword) { + this.accountPassword = accountPassword; + } + + public Date getLastLogin() { + return lastLogin; + } + + public void setLastLogin(Date lastLogin) { + this.lastLogin = lastLogin; + } + + public Date getLastFailedLogin() { + return lastFailedLogin; + } + + public void setLastFailedLogin(Date lastFailedLogin) { + this.lastFailedLogin = lastFailedLogin; + } + + public int getFailureCount() { + return failureCount; + } + + public void setFailureCount(int failureCount) { + this.failureCount = failureCount; + } + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } + + public boolean getPasswordResetOngoing() { + return passwordResetOngoing; + } + + public void setPasswordResetOngoing(boolean passwordResetOngoing) { + this.passwordResetOngoing = passwordResetOngoing; + } + + public Date getPasswordResetValidTo() { + return passwordResetValidTo; + } + + public void setPasswordResetValidTo(Date passwordResetValidTo) { + this.passwordResetValidTo = passwordResetValidTo; + } + + public String getPasswordResetHash() { + return passwordResetHash; + } + + public void setPasswordResetHash(String passwordResetHash) { + this.passwordResetHash = passwordResetHash; + } + + public Date getCreatedOn() { + return createdOn; + } + + public void setCreatedOn(Date createdOn) { + this.createdOn = createdOn; + } + + public String getCreatedBy() { + return createdBy; + } + + public void setCreatedBy(String createdBy) { + this.createdBy = createdBy; + } + + public Date getLastUpdatedOn() { + return lastUpdatedOn; + } + + public void setLastUpdatedOn(Date lastUpdatedOn) { + this.lastUpdatedOn = lastUpdatedOn; + } + + public String getLastUpdatedBy() { + return lastUpdatedBy; + } + + public void setLastUpdatedBy(String lastUpdatedBy) { + this.lastUpdatedBy = lastUpdatedBy; + } + + @XmlTransient + public List getApplicationRoleList() { + return applicationRoleList; + } + + public void setApplicationRoleList(List applicationRoleList) { + this.applicationRoleList = applicationRoleList; + } + + @XmlTransient + public List getAccountHistoryList() { + return accountHistoryList; + } + + public void setAccountHistoryList(List accountHistoryList) { + this.accountHistoryList = accountHistoryList; + } + + @Override + public int hashCode() { + int hash = 0; + hash += (id != null ? id.hashCode() : 0); + return hash; + } + + @Override + public boolean equals(Object object) { + // TODO: Warning - this method won't work in the case the id fields are not set + if (!(object instanceof AccountEntity)) { + return false; + } + AccountEntity other = (AccountEntity) object; + if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) { + return false; + } + return true; + } + + @Override + public String toString() { + return "de.muehlencord.shared.account.entity.Account[ id=" + id + " ]"; + } + +} diff --git a/account/src/main/java/de/muehlencord/shared/account/business/account/entity/AccountStatus.java b/account/src/main/java/de/muehlencord/shared/account/business/account/entity/AccountStatus.java index d7a3ed8..c1b0f8b 100644 --- a/account/src/main/java/de/muehlencord/shared/account/business/account/entity/AccountStatus.java +++ b/account/src/main/java/de/muehlencord/shared/account/business/account/entity/AccountStatus.java @@ -1,15 +1,26 @@ -package de.muehlencord.shared.account.business.account.entity; - -/** - * - * @author joern.muehlencord - */ -public enum AccountStatus { - - NEW, // account is created but never used - NORMAL, // normal account, at least on login, neither blocked or disabled - BLOCKED, // account is blocked after too many login failures or other security related events - DISABLED; // account is disabled and cannot be used anymore - - -} +package de.muehlencord.shared.account.business.account.entity; + +import java.util.ArrayList; +import java.util.List; + +/** + * + * @author joern.muehlencord + */ +public enum AccountStatus { + + NEW, // account is created but never used + NORMAL, // normal account, at least on login, neither blocked or disabled + BLOCKED, // account is blocked after too many login failures or other security related events + DISABLED; // account is disabled and cannot be used anymore + + public static List getAllStatusNames() { + List statusNames = new ArrayList<>(); + for (AccountStatus currentStatus : AccountStatus.values()) { + statusNames.add (currentStatus.name()); + } + return statusNames; + } + + +} diff --git a/account/src/main/java/de/muehlencord/shared/account/business/account/entity/ApplicationRoleEntity.java b/account/src/main/java/de/muehlencord/shared/account/business/account/entity/ApplicationRoleEntity.java index 2c3e71c..d49f149 100644 --- a/account/src/main/java/de/muehlencord/shared/account/business/account/entity/ApplicationRoleEntity.java +++ b/account/src/main/java/de/muehlencord/shared/account/business/account/entity/ApplicationRoleEntity.java @@ -1,152 +1,165 @@ -package de.muehlencord.shared.account.business.account.entity; - -import java.io.Serializable; -import java.util.List; -import java.util.UUID; -import javax.persistence.Basic; -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.Id; -import javax.persistence.JoinColumn; -import javax.persistence.JoinTable; -import javax.persistence.ManyToMany; -import javax.persistence.NamedQueries; -import javax.persistence.NamedQuery; -import javax.persistence.Table; -import javax.validation.constraints.NotNull; -import javax.validation.constraints.Size; -import javax.xml.bind.annotation.XmlRootElement; -import javax.xml.bind.annotation.XmlTransient; -import org.hibernate.annotations.GenericGenerator; -import org.hibernate.annotations.Type; - -/** - * - * @author joern.muehlencord - */ -@Entity -@Table(name = "application_role") -@XmlRootElement -@NamedQueries({ - @NamedQuery(name = "ApplicationRoleEntity.findAll", query = "SELECT a FROM ApplicationRoleEntity a ORDER BY a.roleName") - , @NamedQuery(name = "ApplicationRoleEntity.findByRoleName", query = "SELECT a FROM ApplicationRoleEntity a WHERE a.roleName = :roleName") - , @NamedQuery(name = "ApplicationRoleEntity.findByRoleDescription", query = "SELECT a FROM ApplicationRoleEntity a WHERE a.roleDescription = :roleDescription")}) - -public class ApplicationRoleEntity implements Serializable { - - private static final long serialVersionUID = -8324054525780893823L; - - @Id - @Basic(optional = false) - @NotNull - @Column(name = "id") - @GeneratedValue(generator = "uuid2") - @GenericGenerator(name = "uuid2", strategy = "uuid2") - @Type(type = "pg-uuid") - private UUID id; - @Basic(optional = false) - @NotNull - @Size(min = 1, max = 80) - @Column(name = "role_name") - private String roleName; - @Basic(optional = false) - @NotNull - @Size(min = 1, max = 200) - @Column(name = "role_description") - private String roleDescription; - @ManyToMany(mappedBy = "applicationRoleList") - private List accountList; - @JoinTable(name = "role_permission", joinColumns = { - @JoinColumn(name = "application_role", referencedColumnName = "id")}, inverseJoinColumns = { - @JoinColumn(name = "role_permission", referencedColumnName = "id")}) - @ManyToMany - private List applicationPermissionList; - - public ApplicationRoleEntity() { - } - - public ApplicationRoleEntity(UUID id) { - this.id = id; - } - - public ApplicationRoleEntity(String roleName, String roleDescription) { - this.id = null; - this.roleName = roleName; - this.roleDescription = roleDescription; - } - - public ApplicationRoleEntity(UUID id, String roleName, String roleDescription) { - this.id = id; - this.roleName = roleName; - this.roleDescription = roleDescription; - } - - public UUID getId() { - return id; - } - - public void setId(UUID id) { - this.id = id; - } - - public String getRoleName() { - return roleName; - } - - public void setRoleName(String roleName) { - this.roleName = roleName; - } - - public String getRoleDescription() { - return roleDescription; - } - - public void setRoleDescription(String roleDescription) { - this.roleDescription = roleDescription; - } - - @XmlTransient - public List getAccountList() { - return accountList; - } - - public void setAccountList(List accountList) { - this.accountList = accountList; - } - - @XmlTransient - public List getApplicationPermissionList() { - return applicationPermissionList; - } - - public void setApplicationPermissionList(List applicationPermissionList) { - this.applicationPermissionList = applicationPermissionList; - } - - @Override - public int hashCode() { - int hash = 0; - hash += (id != null ? id.hashCode() : 0); - return hash; - } - - @Override - public boolean equals(Object object) { - // TODO: Warning - this method won't work in the case the id fields are not set - if (!(object instanceof ApplicationRoleEntity)) { - return false; - } - ApplicationRoleEntity other = (ApplicationRoleEntity) object; - if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) { - return false; - } - return true; - } - - @Override - public String toString() { - return "de.muehlencord.shared.account.entity.ApplicationRole[ id=" + id + " ]"; - } - -} +package de.muehlencord.shared.account.business.account.entity; + +import de.muehlencord.shared.account.business.application.entity.ApplicationEntity; +import java.io.Serializable; +import java.util.List; +import java.util.UUID; +import javax.persistence.Basic; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.JoinTable; +import javax.persistence.ManyToMany; +import javax.persistence.ManyToOne; +import javax.persistence.NamedQueries; +import javax.persistence.NamedQuery; +import javax.persistence.Table; +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Size; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.XmlTransient; +import org.hibernate.annotations.GenericGenerator; +import org.hibernate.annotations.Type; + +/** + * + * @author joern.muehlencord + */ +@Entity +@Table(name = "application_role") +@XmlRootElement +@NamedQueries({ + @NamedQuery(name = "ApplicationRoleEntity.findAll", query = "SELECT a FROM ApplicationRoleEntity a ORDER BY a.roleName") + , @NamedQuery(name = "ApplicationRoleEntity.findByRoleName", query = "SELECT a FROM ApplicationRoleEntity a WHERE a.roleName = :roleName") + , @NamedQuery(name = "ApplicationRoleEntity.findByRoleDescription", query = "SELECT a FROM ApplicationRoleEntity a WHERE a.roleDescription = :roleDescription")}) + +public class ApplicationRoleEntity implements Serializable { + + private static final long serialVersionUID = -8324054525780893823L; + + @Id + @Basic(optional = false) + @NotNull + @Column(name = "id") + @GeneratedValue(generator = "uuid2") + @GenericGenerator(name = "uuid2", strategy = "uuid2") + @Type(type = "pg-uuid") + private UUID id; + @Basic(optional = false) + @NotNull + @Size(min = 1, max = 80) + @Column(name = "role_name") + private String roleName; + @Basic(optional = false) + @NotNull + @Size(min = 1, max = 200) + @Column(name = "role_description") + private String roleDescription; + @ManyToMany(mappedBy = "applicationRoleList") + private List accountList; + @JoinTable(name = "role_permission", joinColumns = { + @JoinColumn(name = "application_role", referencedColumnName = "id")}, inverseJoinColumns = { + @JoinColumn(name = "role_permission", referencedColumnName = "id")}) + @ManyToMany + private List applicationPermissionList; + @JoinColumn(name = "application", referencedColumnName = "id") + @ManyToOne(optional = false) + private ApplicationEntity application; + + public ApplicationRoleEntity() { + } + + public ApplicationRoleEntity(UUID id) { + this.id = id; + } + + public ApplicationRoleEntity(String roleName, String roleDescription) { + this.id = null; + this.roleName = roleName; + this.roleDescription = roleDescription; + } + + public ApplicationRoleEntity(UUID id, String roleName, String roleDescription) { + this.id = id; + this.roleName = roleName; + this.roleDescription = roleDescription; + } + + public UUID getId() { + return id; + } + + public void setId(UUID id) { + this.id = id; + } + + public String getRoleName() { + return roleName; + } + + public void setRoleName(String roleName) { + this.roleName = roleName; + } + + public String getRoleDescription() { + return roleDescription; + } + + public void setRoleDescription(String roleDescription) { + this.roleDescription = roleDescription; + } + + @XmlTransient + public List getAccountList() { + return accountList; + } + + public void setAccountList(List accountList) { + this.accountList = accountList; + } + + @XmlTransient + public List getApplicationPermissionList() { + return applicationPermissionList; + } + + public void setApplicationPermissionList(List applicationPermissionList) { + this.applicationPermissionList = applicationPermissionList; + } + + public ApplicationEntity getApplication() { + return application; + } + + public void setApplication(ApplicationEntity application) { + this.application = application; + } + + @Override + public int hashCode() { + int hash = 0; + hash += (id != null ? id.hashCode() : 0); + return hash; + } + + @Override + public boolean equals(Object object) { + // TODO: Warning - this method won't work in the case the id fields are not set + if (!(object instanceof ApplicationRoleEntity)) { + return false; + } + ApplicationRoleEntity other = (ApplicationRoleEntity) object; + if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) { + return false; + } + return true; + } + + @Override + public String toString() { + return "de.muehlencord.shared.account.entity.ApplicationRole[ id=" + id + " ]"; + } + +} diff --git a/account/src/main/java/de/muehlencord/shared/account/business/application/boundary/ApplicationService.java b/account/src/main/java/de/muehlencord/shared/account/business/application/boundary/ApplicationService.java new file mode 100644 index 0000000..167f7dd --- /dev/null +++ b/account/src/main/java/de/muehlencord/shared/account/business/application/boundary/ApplicationService.java @@ -0,0 +1,73 @@ +package de.muehlencord.shared.account.business.application.boundary; + +import de.muehlencord.shared.account.business.application.entity.ApplicationEntity; +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; +import javax.ejb.Stateless; +import javax.inject.Inject; +import javax.persistence.EntityManager; +import javax.persistence.Query; +import javax.transaction.Transactional; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * + * @author Joern Muehlencord + */ +@Stateless +public class ApplicationService implements Serializable { + + private static final long serialVersionUID = 4262608935325326191L; + private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationService.class); + + @Inject + EntityManager em; + + public List getAllApplications() { + Query query = em.createNamedQuery("ApplicationEntity.findAll"); + List resultList = query.getResultList(); + if (resultList == null) { + return new ArrayList<>(); + } else { + return resultList; + } + } + + @Transactional + public ApplicationEntity createOrUpdate(ApplicationEntity app) { + if (app == null) { + // TODO add error handling + return null; + } else { + if (app.getId() == null) { + em.persist(app); + ApplicationEntity returnValue = findByApplicationName(app.getApplicationName()); + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("Application {} created", app.getApplicationName()); + } + + return returnValue; + } else { + ApplicationEntity returnValue = em.merge(app); + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("Applicateion {} updated", app.getApplicationName()); + } + return returnValue; + } + } + } + + public ApplicationEntity findByApplicationName(String applicationName) { + Query query = em.createNamedQuery("ApplicationEntity.findByApplicationName"); + query.setParameter("applicationName", applicationName); + List resultList = query.getResultList(); + if ((resultList == null) || (resultList.isEmpty())) { + return null; + } else { + return resultList.get(0); + } + } + +} diff --git a/account/src/main/java/de/muehlencord/shared/account/business/application/entity/ApplicationEntity.java b/account/src/main/java/de/muehlencord/shared/account/business/application/entity/ApplicationEntity.java new file mode 100644 index 0000000..425bafb --- /dev/null +++ b/account/src/main/java/de/muehlencord/shared/account/business/application/entity/ApplicationEntity.java @@ -0,0 +1,107 @@ +package de.muehlencord.shared.account.business.application.entity; + +import de.muehlencord.shared.account.business.account.entity.ApplicationRoleEntity; +import java.io.Serializable; +import java.util.List; +import java.util.UUID; +import javax.persistence.Basic; +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.NamedQueries; +import javax.persistence.NamedQuery; +import javax.persistence.OneToMany; +import javax.persistence.Table; +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Size; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.XmlTransient; +import org.hibernate.annotations.GenericGenerator; +import org.hibernate.annotations.Type; + +/** + * + * @author Joern Muehlencord + */ +@Entity +@Table(name = "application") +@XmlRootElement +@NamedQueries({ + @NamedQuery(name = "ApplicationEntity.findAll", query = "SELECT a FROM ApplicationEntity a"), + @NamedQuery(name = "ApplicationEntity.findByApplicationName", query = "SELECT a FROM ApplicationEntity a WHERE a.applicationName = :applicationName")}) +public class ApplicationEntity implements Serializable { + + private static final long serialVersionUID = -6407525020014743727L; + + @Id + @Basic(optional = false) + @NotNull + @Column(name = "id") + @GeneratedValue(generator = "uuid2") + @GenericGenerator(name = "uuid2", strategy = "uuid2") + @Type(type = "pg-uuid") + private UUID id; + @Basic(optional = false) + @NotNull + @Size(min = 1, max = 200) + @Column(name = "application_name", unique = true) + private String applicationName; + @OneToMany(cascade = CascadeType.ALL, mappedBy = "application") + private List applicationRoleEntityList; + + public ApplicationEntity() { + } + + public UUID getId() { + return id; + } + + public void setId(UUID id) { + this.id = id; + } + + public String getApplicationName() { + return applicationName; + } + + public void setApplicationName(String applicationName) { + this.applicationName = applicationName; + } + + @XmlTransient + public List getApplicationRoleEntityList() { + return applicationRoleEntityList; + } + + public void setApplicationRoleEntityList(List applicationRoleEntityList) { + this.applicationRoleEntityList = applicationRoleEntityList; + } + + @Override + public int hashCode() { + int hash = 0; + hash += (id != null ? id.hashCode() : 0); + return hash; + } + + @Override + public boolean equals(Object object) { + // TODO: Warning - this method won't work in the case the id fields are not set + if (!(object instanceof ApplicationEntity)) { + return false; + } + ApplicationEntity other = (ApplicationEntity) object; + if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) { + return false; + } + return true; + } + + @Override + public String toString() { + return "de.muehlencord.shared.account.business.application.entity.ApplicationEntity[ id=" + id + " ]"; + } + +}