local test are running

This commit is contained in:
Peter Hormanns 2013-06-28 18:55:15 +02:00
parent 1988b1a30c
commit b41827e66a
11 changed files with 199 additions and 165 deletions

View File

@ -24,9 +24,30 @@
<class>de.hsadmin.mods.db.Database</class>
<class>de.hsadmin.mods.db.MySqlDatabase</class>
<class>de.hsadmin.mods.db.PgSqlDatabase</class>
<properties>
<property name="openjpa.ConnectionDriverName" value="org.postgresql.Driver"/>
<property name="openjpa.Compatibility" value="QuotedNumbersInQueries=true"/>
</properties>
</persistence-unit>
<persistence-unit name="hsar">
<jta-data-source>HsarDataSource</jta-data-source>
<class>de.hsadmin.core.qserv.QueueTask</class>
<class>de.hsadmin.mods.cust.Customer</class>
<class>de.hsadmin.mods.cust.Contact</class>
<class>de.hsadmin.mods.cust.BankAccount</class>
<class>de.hsadmin.mods.pac.Pac</class>
<class>de.hsadmin.mods.pac.BasePac</class>
<class>de.hsadmin.mods.pac.BaseComponent</class>
<class>de.hsadmin.mods.pac.Component</class>
<class>de.hsadmin.mods.pac.PacComponent</class>
<class>de.hsadmin.mods.pac.Hive</class>
<class>de.hsadmin.mods.pac.INetAddress</class>
<class>de.hsadmin.mods.user.UnixUser</class>
<class>de.hsadmin.mods.dom.Domain</class>
<class>de.hsadmin.mods.dom.DomainOption</class>
<class>de.hsadmin.mods.email.EMailAddress</class>
<class>de.hsadmin.mods.email.EMailAlias</class>
<class>de.hsadmin.mods.db.DatabaseUser</class>
<class>de.hsadmin.mods.db.MySqlUser</class>
<class>de.hsadmin.mods.db.PgSqlUser</class>
<class>de.hsadmin.mods.db.Database</class>
<class>de.hsadmin.mods.db.MySqlDatabase</class>
<class>de.hsadmin.mods.db.PgSqlDatabase</class>
</persistence-unit>
</persistence>

Binary file not shown.

View File

@ -0,0 +1,82 @@
package de.hsadmin.core.model;
import java.util.Set;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import de.hsadmin.core.util.Config;
import de.hsadmin.mods.cust.Customer;
import de.hsadmin.mods.pac.Pac;
import de.hsadmin.mods.user.UnixUser;
@Stateless(name="LoginBean")
@LocalBean
public class LoginBean implements LoginBeanLocal {
@PersistenceContext(unitName="hsar")
private EntityManager entityManager;
@Override
public UnixUser login(String user, String ticket) throws AuthenticationException {
String ticketUser = TicketValidator.getInstance().validateTicket(ticket);
if (user != null && user.equals(ticketUser)) {
return readLoginUser(ticketUser); // user himself
}
if (ticketUser != null && ticketUser.length() == 2) {
return readLoginUser(ticketUser); // 2-letter hostmaster
}
String hostmasterAccountPrefix = Config.getInstance().getProperty("accountprefix.hostmaster", "hsh01") + "-";
if (ticketUser != null && ticketUser.startsWith(hostmasterAccountPrefix) && ticketUser.length() == 8) {
return readLoginUser(ticketUser); // hsh01 hostmaster
}
if (ticketUser != null && ticketUser.length() == 5) {
Query userQuery = entityManager.createQuery("SELECT u FROM UnixUsers u WHERE u.name = :username");
userQuery.setParameter("username", user);
UnixUser unixUser = (UnixUser) userQuery.getSingleResult();
String pacName = unixUser.getPac().getName();
boolean loginOk = ticketUser.equals(pacName);
if (loginOk) {
return readLoginUser(ticketUser);
}
throw new AuthenticationException("User " + ticketUser + " is not allowed to run as " + user);
}
String memberAccountPrefix = Config.getInstance().getProperty("accountprefix.customer", "hsh00") + "-";
if (ticketUser != null && (ticketUser.length() == 3 || (ticketUser.length() >= 9 && ticketUser.startsWith(memberAccountPrefix)))) {
Query memberQuery = entityManager.createQuery("SELECT c FROM Customers c WHERE c.name = :membername");
memberQuery.setParameter("membername", ticketUser.length() == 3 ? (memberAccountPrefix + ticketUser) : ticketUser);
Customer member = (Customer) memberQuery.getSingleResult();
Set<Pac> pacs = member.getPacs();
for (Pac p : pacs) {
if (p.getName().equals(user)) {
return readLoginUser(ticketUser); // member as pac-admin
}
Set<UnixUser> users = p.getUnixUser();
for (UnixUser u : users) {
if (u.getName().equals(user)) {
return readLoginUser(ticketUser); // member as pac-user
}
}
}
}
throw new AuthenticationException("User " + ticketUser + " is not allowed to run as " + user);
}
private UnixUser readLoginUser(String loginName) {
if (loginName != null && loginName.length() == 2) {
loginName = Config.getInstance().getProperty("accountprefix.hostmaster", "hsh01") + "-" + loginName;
}
if (loginName != null && loginName.length() == 3) {
loginName = Config.getInstance().getProperty("accountprefix.customer", "hsh00") + "-" + loginName;
}
Query userQuery = entityManager.createQuery("SELECT u FROM UnixUsers u WHERE u.name = :username");
userQuery.setParameter("username", loginName);
UnixUser user = (UnixUser) userQuery.getSingleResult();
user.getPac().getCustomer();
return user;
}
}

View File

@ -0,0 +1,12 @@
package de.hsadmin.core.model;
import javax.ejb.Local;
import de.hsadmin.mods.user.UnixUser;
@Local
public interface LoginBeanLocal {
public abstract UnixUser login(String user, String ticket) throws AuthenticationException;
}

View File

@ -1,99 +1,29 @@
package de.hsadmin.core.model;
import java.util.Set;
import javax.ejb.LocalBean;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.persistence.Query;
import de.hsadmin.core.util.Config;
import de.hsadmin.mods.cust.Customer;
import de.hsadmin.mods.pac.Pac;
import de.hsadmin.mods.user.UnixUser;
@Stateful(name="LoginSession")
@LocalBean
public class LoginSession implements LoginSessionLocal {
@PersistenceContext(unitName="hsadmin",type=PersistenceContextType.TRANSACTION)
private EntityManager entityManager;
private String loginName;
private UnixUser loginUser;
@Override
public UnixUser getLoginUser() {
String loginName = getLoginName();
if (loginName != null && loginName.length() == 2) {
loginName = Config.getInstance().getProperty("accountprefix.hostmaster", "hsh01") + "-" + loginName;
}
if (loginName != null && loginName.length() == 3) {
loginName = Config.getInstance().getProperty("accountprefix.customer", "hsh00") + "-" + loginName;
}
Query userQuery = entityManager.createQuery("SELECT u FROM UnixUsers u WHERE u.name = :username");
userQuery.setParameter("username", loginName);
UnixUser unixUser = (UnixUser) userQuery.getSingleResult();
return unixUser;
return loginUser;
}
@Override
public String getLoginName() {
if (loginName != null) {
return loginName;
}
throw new TechnicalException("no login");
return loginUser.getName();
}
@Override
public boolean login(String user, String ticket) throws AuthenticationException {
String ticketUser = TicketValidator.getInstance().validateTicket(ticket);
if (user != null && user.equals(ticketUser)) {
loginName = ticketUser;
return true; // user himself
}
if (ticketUser != null && ticketUser.length() == 2) {
loginName = ticketUser;
return true; // 2-letter hostmaster
}
String hostmasterAccountPrefix = Config.getInstance().getProperty("accountprefix.hostmaster", "hsh01") + "-";
if (ticketUser != null && ticketUser.startsWith(hostmasterAccountPrefix) && ticketUser.length() == 8) {
loginName = ticketUser;
return true; // hsh01 hostmaster
}
if (ticketUser != null && ticketUser.length() == 5) {
Query userQuery = entityManager.createQuery("SELECT u FROM UnixUsers u WHERE u.name = :username");
userQuery.setParameter("username", user);
UnixUser unixUser = (UnixUser) userQuery.getSingleResult();
String pacName = unixUser.getPac().getName();
boolean loginOk = ticketUser.equals(pacName);
if (loginOk) {
loginName = ticketUser;
}
return loginOk; // pac-admin
}
String memberAccountPrefix = Config.getInstance().getProperty("accountprefix.customer", "hsh00") + "-";
if (ticketUser != null && (ticketUser.length() == 3 || (ticketUser.length() >= 9 && ticketUser.startsWith(memberAccountPrefix)))) {
Query memberQuery = entityManager.createQuery("SELECT c FROM Customers c WHERE c.name = :membername");
memberQuery.setParameter("membername", ticketUser.length() == 3 ? (memberAccountPrefix + ticketUser) : ticketUser);
Customer member = (Customer) memberQuery.getSingleResult();
Set<Pac> pacs = member.getPacs();
for (Pac p : pacs) {
if (p.getName().equals(user)) {
loginName = ticketUser;
return true; // member as pac-admin
}
Set<UnixUser> users = p.getUnixUser();
for (UnixUser u : users) {
if (u.getName().equals(user)) {
loginName = ticketUser;
return true; // member as pac-user
}
}
}
}
throw new AuthenticationException("User " + ticketUser + " is not allowed to run as " + user);
public void setLoginUser(UnixUser user) {
loginUser = user;
}
}

View File

@ -7,11 +7,10 @@ import de.hsadmin.mods.user.UnixUser;
@Local
public interface LoginSessionLocal {
public abstract void setLoginUser(UnixUser user);
public abstract UnixUser getLoginUser();
public abstract String getLoginName();
public abstract boolean login(String user, String ticket)
throws AuthenticationException;
}

View File

@ -20,4 +20,6 @@ public interface Module {
public AbstractEntity update(LoginSessionLocal session, AbstractEntity existingEntity) throws HSAdminException;
public void delete(LoginSessionLocal session, AbstractEntity existingEntity) throws HSAdminException;
public void detach(AbstractEntity update);
}

View File

@ -5,6 +5,7 @@ import java.util.LinkedList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
@ -27,10 +28,11 @@ import de.hsadmin.hostsharing.MultiOption;
import de.hsadmin.mods.pac.Pac;
import de.hsadmin.mods.user.UnixUser;
@Stateless
@Stateless(name="EMailAliasModule")
@LocalBean
public class EMailAliasModuleImpl implements Module {
@PersistenceContext(name="hsadmin")
@PersistenceContext(name="hsar")
private EntityManager entityManager;
@EJB

View File

@ -26,7 +26,7 @@ import de.hsadmin.mods.pac.Pac;
@Entity(name = "UnixUsers")
@Table(name = "unixuser")
@SequenceGenerator(name = "UnixUsersSeqGen", sequenceName = "unixuser_unixuser_id_seq")
@AnnModuleImpl(de.hsadmin.mods.user.UnixUserModuleImpl.class)
@AnnModuleImpl(UnixUserModuleImpl.class)
public class UnixUser extends AbstractEntity implements Serializable {
private static final long serialVersionUID = 7823071611805642906L;

View File

@ -10,27 +10,24 @@ import java.util.Map;
import java.util.Properties;
import java.util.StringTokenizer;
import javax.ejb.EJB;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import de.hsadmin.core.model.AbstractEntity;
import de.hsadmin.core.model.AuthenticationException;
import de.hsadmin.core.model.AuthorisationException;
import de.hsadmin.core.model.HSAdminException;
import de.hsadmin.core.model.LoginBeanLocal;
import de.hsadmin.core.model.LoginSessionLocal;
import de.hsadmin.core.model.Module;
import de.hsadmin.mods.email.EMailAlias;
import de.hsadmin.mods.email.EMailAliasModuleImpl;
import de.hsadmin.mods.user.UnixUser;
public class EMailAliasRemote {
@EJB
private LoginBeanLocal login;
private LoginSessionLocal session;
@EJB
private EMailAliasModuleImpl module;
private Module module;
public EMailAliasRemote() {
Properties props = new Properties();
@ -39,7 +36,8 @@ public class EMailAliasRemote {
try {
ctx = new InitialContext(props);
session = (LoginSessionLocal) ctx.lookup("LoginSessionLocal");
module = (EMailAliasModuleImpl) ctx.lookup("EMailAliasModuleLocal");
login = (LoginBeanLocal) ctx.lookup("LoginBeanLocal");
module = (Module) ctx.lookup("EMailAliasModuleLocal");
} catch (NamingException e) {
e.printStackTrace();
}
@ -49,7 +47,7 @@ public class EMailAliasRemote {
Map<String, String> whereParams) throws HSAdminException {
String user = runAsUser;
try {
if (session.login(user, ticket)) {
session.setLoginUser(login.login(user, ticket));
UnixUser unixUser = session.getLoginUser();
List<AbstractEntity> list = module.search(session, getEntityClass(),
buildQueryCondition(whereParams), null);
@ -65,9 +63,6 @@ public class EMailAliasRemote {
}
}
return result;
} else {
throw new AuthenticationException("authentication failed");
}
} catch (SecurityException e) {
throw new HSAdminException(e);
} catch (IllegalArgumentException e) {
@ -79,7 +74,7 @@ public class EMailAliasRemote {
Map<String, Object> setParams) throws HSAdminException {
String user = runAsUser;
try {
if (session.login(user, ticket)) {
session.setLoginUser(login.login(user, ticket));
Constructor<? extends AbstractEntity> constructor =
getEntityClass().getConstructor();
AbstractEntity entity = constructor.newInstance();
@ -89,9 +84,6 @@ public class EMailAliasRemote {
HashMap<String, Object> entry = new HashMap<String, Object>();
entity2map(insertedEntity, entry);
return entry;
} else {
throw new AuthenticationException("authentication failed");
}
} catch (Exception e) {
throw new HSAdminException(e);
}
@ -101,7 +93,7 @@ public class EMailAliasRemote {
Map<String, String> whereParams) throws HSAdminException {
String user = runAsUser;
try {
if (session.login(user, ticket)) {
session.setLoginUser(login.login(user, ticket));
UnixUser unixUser = session.getLoginUser();
String queryCondition = buildQueryCondition(whereParams);
if (queryCondition == null || queryCondition.length() == 0) {
@ -117,9 +109,6 @@ public class EMailAliasRemote {
throw new AuthorisationException(unixUser, "delete", e);
}
}
} else {
throw new AuthenticationException("authentication failed");
}
} catch (SecurityException e) {
throw new HSAdminException(e);
} catch (IllegalArgumentException e) {
@ -132,7 +121,7 @@ public class EMailAliasRemote {
throws HSAdminException {
String user = runAsUser;
try {
if (session.login(user, ticket)) {
session.setLoginUser(login.login(user, ticket));
UnixUser unixUser = session.getLoginUser();
ArrayList<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
String queryCondition = buildQueryCondition(whereParams);
@ -155,9 +144,6 @@ public class EMailAliasRemote {
}
}
return result;
} else {
throw new AuthenticationException("authentication failed");
}
} catch (SecurityException e) {
throw new HSAdminException(e);
} catch (IllegalArgumentException e) {