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.Database</class>
<class>de.hsadmin.mods.db.MySqlDatabase</class> <class>de.hsadmin.mods.db.MySqlDatabase</class>
<class>de.hsadmin.mods.db.PgSqlDatabase</class> <class>de.hsadmin.mods.db.PgSqlDatabase</class>
<properties> </persistence-unit>
<property name="openjpa.ConnectionDriverName" value="org.postgresql.Driver"/> <persistence-unit name="hsar">
<property name="openjpa.Compatibility" value="QuotedNumbersInQueries=true"/> <jta-data-source>HsarDataSource</jta-data-source>
</properties> <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-unit>
</persistence> </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; package de.hsadmin.core.model;
import java.util.Set;
import javax.ejb.LocalBean; import javax.ejb.LocalBean;
import javax.ejb.Stateful; 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; import de.hsadmin.mods.user.UnixUser;
@Stateful(name="LoginSession") @Stateful(name="LoginSession")
@LocalBean @LocalBean
public class LoginSession implements LoginSessionLocal { public class LoginSession implements LoginSessionLocal {
@PersistenceContext(unitName="hsadmin",type=PersistenceContextType.TRANSACTION) private UnixUser loginUser;
private EntityManager entityManager;
private String loginName;
@Override @Override
public UnixUser getLoginUser() { public UnixUser getLoginUser() {
String loginName = getLoginName(); return loginUser;
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;
} }
@Override @Override
public String getLoginName() { public String getLoginName() {
if (loginName != null) { return loginUser.getName();
return loginName;
}
throw new TechnicalException("no login");
} }
@Override @Override
public boolean login(String user, String ticket) throws AuthenticationException { public void setLoginUser(UnixUser user) {
String ticketUser = TicketValidator.getInstance().validateTicket(ticket); loginUser = user;
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);
} }
} }

View File

@ -7,11 +7,10 @@ import de.hsadmin.mods.user.UnixUser;
@Local @Local
public interface LoginSessionLocal { public interface LoginSessionLocal {
public abstract void setLoginUser(UnixUser user);
public abstract UnixUser getLoginUser(); public abstract UnixUser getLoginUser();
public abstract String getLoginName(); 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 AbstractEntity update(LoginSessionLocal session, AbstractEntity existingEntity) throws HSAdminException;
public void delete(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 java.util.List;
import javax.ejb.EJB; import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless; import javax.ejb.Stateless;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
@ -27,10 +28,11 @@ import de.hsadmin.hostsharing.MultiOption;
import de.hsadmin.mods.pac.Pac; import de.hsadmin.mods.pac.Pac;
import de.hsadmin.mods.user.UnixUser; import de.hsadmin.mods.user.UnixUser;
@Stateless @Stateless(name="EMailAliasModule")
@LocalBean
public class EMailAliasModuleImpl implements Module { public class EMailAliasModuleImpl implements Module {
@PersistenceContext(name="hsadmin") @PersistenceContext(name="hsar")
private EntityManager entityManager; private EntityManager entityManager;
@EJB @EJB

View File

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

View File

@ -10,27 +10,24 @@ import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import javax.ejb.EJB;
import javax.naming.Context; import javax.naming.Context;
import javax.naming.InitialContext; import javax.naming.InitialContext;
import javax.naming.NamingException; import javax.naming.NamingException;
import de.hsadmin.core.model.AbstractEntity; import de.hsadmin.core.model.AbstractEntity;
import de.hsadmin.core.model.AuthenticationException;
import de.hsadmin.core.model.AuthorisationException; import de.hsadmin.core.model.AuthorisationException;
import de.hsadmin.core.model.HSAdminException; import de.hsadmin.core.model.HSAdminException;
import de.hsadmin.core.model.LoginBeanLocal;
import de.hsadmin.core.model.LoginSessionLocal; import de.hsadmin.core.model.LoginSessionLocal;
import de.hsadmin.core.model.Module;
import de.hsadmin.mods.email.EMailAlias; import de.hsadmin.mods.email.EMailAlias;
import de.hsadmin.mods.email.EMailAliasModuleImpl;
import de.hsadmin.mods.user.UnixUser; import de.hsadmin.mods.user.UnixUser;
public class EMailAliasRemote { public class EMailAliasRemote {
@EJB private LoginBeanLocal login;
private LoginSessionLocal session; private LoginSessionLocal session;
private Module module;
@EJB
private EMailAliasModuleImpl module;
public EMailAliasRemote() { public EMailAliasRemote() {
Properties props = new Properties(); Properties props = new Properties();
@ -39,7 +36,8 @@ public class EMailAliasRemote {
try { try {
ctx = new InitialContext(props); ctx = new InitialContext(props);
session = (LoginSessionLocal) ctx.lookup("LoginSessionLocal"); session = (LoginSessionLocal) ctx.lookup("LoginSessionLocal");
module = (EMailAliasModuleImpl) ctx.lookup("EMailAliasModuleLocal"); login = (LoginBeanLocal) ctx.lookup("LoginBeanLocal");
module = (Module) ctx.lookup("EMailAliasModuleLocal");
} catch (NamingException e) { } catch (NamingException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -49,25 +47,22 @@ public class EMailAliasRemote {
Map<String, String> whereParams) throws HSAdminException { Map<String, String> whereParams) throws HSAdminException {
String user = runAsUser; String user = runAsUser;
try { try {
if (session.login(user, ticket)) { session.setLoginUser(login.login(user, ticket));
UnixUser unixUser = session.getLoginUser(); UnixUser unixUser = session.getLoginUser();
List<AbstractEntity> list = module.search(session, getEntityClass(), List<AbstractEntity> list = module.search(session, getEntityClass(),
buildQueryCondition(whereParams), null); buildQueryCondition(whereParams), null);
if (list == null) { if (list == null) {
throw new HSAdminException("result list is null, runtime-error?"); throw new HSAdminException("result list is null, runtime-error?");
}
ArrayList<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
for (AbstractEntity e : list) {
HashMap<String, Object> entry = new HashMap<String, Object>();
entity2map(e, entry);
if (e.isReadAllowedFor(unixUser)) {
result.add(entry);
}
}
return result;
} else {
throw new AuthenticationException("authentication failed");
} }
ArrayList<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
for (AbstractEntity e : list) {
HashMap<String, Object> entry = new HashMap<String, Object>();
entity2map(e, entry);
if (e.isReadAllowedFor(unixUser)) {
result.add(entry);
}
}
return result;
} catch (SecurityException e) { } catch (SecurityException e) {
throw new HSAdminException(e); throw new HSAdminException(e);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
@ -79,19 +74,16 @@ public class EMailAliasRemote {
Map<String, Object> setParams) throws HSAdminException { Map<String, Object> setParams) throws HSAdminException {
String user = runAsUser; String user = runAsUser;
try { try {
if (session.login(user, ticket)) { session.setLoginUser(login.login(user, ticket));
Constructor<? extends AbstractEntity> constructor = Constructor<? extends AbstractEntity> constructor =
getEntityClass().getConstructor(); getEntityClass().getConstructor();
AbstractEntity entity = constructor.newInstance(); AbstractEntity entity = constructor.newInstance();
module.initialize(session, entity); module.initialize(session, entity);
map2entity(setParams, entity); map2entity(setParams, entity);
AbstractEntity insertedEntity = module.add(session, entity); AbstractEntity insertedEntity = module.add(session, entity);
HashMap<String, Object> entry = new HashMap<String, Object>(); HashMap<String, Object> entry = new HashMap<String, Object>();
entity2map(insertedEntity, entry); entity2map(insertedEntity, entry);
return entry; return entry;
} else {
throw new AuthenticationException("authentication failed");
}
} catch (Exception e) { } catch (Exception e) {
throw new HSAdminException(e); throw new HSAdminException(e);
} }
@ -101,24 +93,21 @@ public class EMailAliasRemote {
Map<String, String> whereParams) throws HSAdminException { Map<String, String> whereParams) throws HSAdminException {
String user = runAsUser; String user = runAsUser;
try { try {
if (session.login(user, ticket)) { session.setLoginUser(login.login(user, ticket));
UnixUser unixUser = session.getLoginUser(); UnixUser unixUser = session.getLoginUser();
String queryCondition = buildQueryCondition(whereParams); String queryCondition = buildQueryCondition(whereParams);
if (queryCondition == null || queryCondition.length() == 0) { if (queryCondition == null || queryCondition.length() == 0) {
throw new HSAdminException( throw new HSAdminException(
"better safe than sorry: no where parameter found"); "better safe than sorry: no where parameter found");
}
List<AbstractEntity> list = module.search(session, getEntityClass(),
queryCondition, null);
for (AbstractEntity e : list) {
if (e.isWriteAllowedFor(unixUser)) {
module.delete(session, e);
} else {
throw new AuthorisationException(unixUser, "delete", e);
} }
List<AbstractEntity> list = module.search(session, getEntityClass(),
queryCondition, null);
for (AbstractEntity e : list) {
if (e.isWriteAllowedFor(unixUser)) {
module.delete(session, e);
} else {
throw new AuthorisationException(unixUser, "delete", e);
}
}
} else {
throw new AuthenticationException("authentication failed");
} }
} catch (SecurityException e) { } catch (SecurityException e) {
throw new HSAdminException(e); throw new HSAdminException(e);
@ -132,32 +121,29 @@ public class EMailAliasRemote {
throws HSAdminException { throws HSAdminException {
String user = runAsUser; String user = runAsUser;
try { try {
if (session.login(user, ticket)) { session.setLoginUser(login.login(user, ticket));
UnixUser unixUser = session.getLoginUser(); UnixUser unixUser = session.getLoginUser();
ArrayList<Map<String, Object>> result = new ArrayList<Map<String, Object>>(); ArrayList<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
String queryCondition = buildQueryCondition(whereParams); String queryCondition = buildQueryCondition(whereParams);
if (queryCondition == null || queryCondition.length() == 0) { if (queryCondition == null || queryCondition.length() == 0) {
throw new HSAdminException( throw new HSAdminException(
"better safe than sorry: no where parameter found"); "better safe than sorry: no where parameter found");
}
List<AbstractEntity> list = module.search(session, getEntityClass(),
queryCondition, getOrderBy());
for (AbstractEntity update : list) {
if (update.isWriteAllowedFor(unixUser)) {
module.detach(update);
map2entity(setParams, update);
update = module.update(session, update);
HashMap<String, Object> entry = new HashMap<String, Object>();
entity2map(update, entry);
result.add(entry);
} else {
throw new AuthorisationException(unixUser, "update", update);
}
}
return result;
} else {
throw new AuthenticationException("authentication failed");
} }
List<AbstractEntity> list = module.search(session, getEntityClass(),
queryCondition, getOrderBy());
for (AbstractEntity update : list) {
if (update.isWriteAllowedFor(unixUser)) {
module.detach(update);
map2entity(setParams, update);
update = module.update(session, update);
HashMap<String, Object> entry = new HashMap<String, Object>();
entity2map(update, entry);
result.add(entry);
} else {
throw new AuthorisationException(unixUser, "update", update);
}
}
return result;
} catch (SecurityException e) { } catch (SecurityException e) {
throw new HSAdminException(e); throw new HSAdminException(e);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {