From 726244a061d9df921a7a40d7e1f067e24cf6d77d Mon Sep 17 00:00:00 2001 From: Peter Hormanns Date: Tue, 5 Oct 2010 19:42:07 +0000 Subject: [PATCH] Module member, emailaddress, domain --- hsarback/build.xml | 1 + hsarback/conf/META-INF/persistence.xml | 3 + .../de/hsadmin/cliClientConnector/Base64.java | 244 ----------- .../CLIClientConnectorServlet.java | 12 +- .../hsadmin/core/model/GenericModuleImpl.java | 20 +- .../src/de/hsadmin/core/util/DNSCheck.java | 51 +++ .../src/de/hsadmin/core/util/TextUtil.java | 45 ++ .../de/hsadmin/core/util/dns/DNSQuery.java | 395 ++++++++++++++++++ .../core/util/dns/DNSResourceRecord.java | 190 +++++++++ .../de/hsadmin/core/util/dns/DNSService.java | 80 ++++ .../src/de/hsadmin/mods/cust/Customer.java | 12 + .../hsadmin/mods/cust/CustomerModuleImpl.java | 67 +++ hsarback/src/de/hsadmin/mods/dom/Domain.java | 232 ++++++++++ .../de/hsadmin/mods/dom/DomainModuleImpl.java | 198 +++++++++ .../mods/dom/DomainProcessorFactory.java | 207 +++++++++ .../src/de/hsadmin/mods/dom/htaccess.jtpl | 2 + .../src/de/hsadmin/mods/dom/index.html.jtpl | 22 + .../src/de/hsadmin/mods/dom/test.cgi.jtpl | 5 + .../src/de/hsadmin/mods/dom/zonefile.jtpl | 29 ++ .../de/hsadmin/mods/email/EMailAddress.java | 208 +++++++++ .../mods/email/EMailAddressModuleImpl.java | 75 ++++ .../email/EMailAddressProcessorFactory.java | 70 ++++ .../src/de/hsadmin/mods/email/EMailAlias.java | 200 +++++++++ .../mods/email/EMailAliasModuleImpl.java | 20 + .../email/EMailAliasProcessorFactory.java | 46 ++ .../hsadmin/mods/user/UnixUserModuleImpl.java | 14 +- .../src/de/hsadmin/remote/AbstractRemote.java | 6 +- .../src/de/hsadmin/remote/CustomerRemote.java | 4 + .../src/de/hsadmin/remote/DomainRemote.java | 63 +++ .../de/hsadmin/remote/EMailAddressRemote.java | 74 ++++ .../de/hsadmin/remote/EMailAliasRemote.java | 46 ++ .../xmlrpc/webserver/XmlRpcServlet.properties | 3 + .../test/de/hsadmin/remote/CustomerTest.java | 63 ++- .../test/de/hsadmin/remote/DomainTest.java | 140 +++++++ .../de/hsadmin/remote/EMailAddressTest.java | 246 +++++++++++ .../de/hsadmin/remote/EMailAliasTest.java | 61 +++ .../test/de/hsadmin/remote/QueueTaskTest.java | 8 +- .../de/hsadmin/remote/RemoteCASHelper.java | 15 +- .../test/de/hsadmin/remote/RemoteTest.java | 5 +- .../de/hsadmin/remote/RemoteTestHelper.java | 14 +- .../test/de/hsadmin/remote/UnixUserTest.java | 18 +- hsarback/webapp/WEB-INF/web.xml | 26 +- 42 files changed, 2924 insertions(+), 316 deletions(-) delete mode 100644 hsarback/src/de/hsadmin/cliClientConnector/Base64.java create mode 100644 hsarback/src/de/hsadmin/core/util/DNSCheck.java create mode 100644 hsarback/src/de/hsadmin/core/util/TextUtil.java create mode 100644 hsarback/src/de/hsadmin/core/util/dns/DNSQuery.java create mode 100644 hsarback/src/de/hsadmin/core/util/dns/DNSResourceRecord.java create mode 100644 hsarback/src/de/hsadmin/core/util/dns/DNSService.java create mode 100644 hsarback/src/de/hsadmin/mods/dom/Domain.java create mode 100644 hsarback/src/de/hsadmin/mods/dom/DomainModuleImpl.java create mode 100644 hsarback/src/de/hsadmin/mods/dom/DomainProcessorFactory.java create mode 100644 hsarback/src/de/hsadmin/mods/dom/htaccess.jtpl create mode 100644 hsarback/src/de/hsadmin/mods/dom/index.html.jtpl create mode 100644 hsarback/src/de/hsadmin/mods/dom/test.cgi.jtpl create mode 100644 hsarback/src/de/hsadmin/mods/dom/zonefile.jtpl create mode 100644 hsarback/src/de/hsadmin/mods/email/EMailAddress.java create mode 100644 hsarback/src/de/hsadmin/mods/email/EMailAddressModuleImpl.java create mode 100644 hsarback/src/de/hsadmin/mods/email/EMailAddressProcessorFactory.java create mode 100644 hsarback/src/de/hsadmin/mods/email/EMailAlias.java create mode 100644 hsarback/src/de/hsadmin/mods/email/EMailAliasModuleImpl.java create mode 100644 hsarback/src/de/hsadmin/mods/email/EMailAliasProcessorFactory.java create mode 100644 hsarback/src/de/hsadmin/remote/DomainRemote.java create mode 100644 hsarback/src/de/hsadmin/remote/EMailAddressRemote.java create mode 100644 hsarback/src/de/hsadmin/remote/EMailAliasRemote.java create mode 100644 hsarback/test/de/hsadmin/remote/DomainTest.java create mode 100644 hsarback/test/de/hsadmin/remote/EMailAddressTest.java create mode 100644 hsarback/test/de/hsadmin/remote/EMailAliasTest.java diff --git a/hsarback/build.xml b/hsarback/build.xml index 297d0c8..b4dd781 100644 --- a/hsarback/build.xml +++ b/hsarback/build.xml @@ -41,6 +41,7 @@ + diff --git a/hsarback/conf/META-INF/persistence.xml b/hsarback/conf/META-INF/persistence.xml index adb13ff..b8bd39c 100644 --- a/hsarback/conf/META-INF/persistence.xml +++ b/hsarback/conf/META-INF/persistence.xml @@ -16,6 +16,9 @@ de.hsadmin.mods.pac.Hive de.hsadmin.mods.pac.INetAddress de.hsadmin.mods.user.UnixUser + de.hsadmin.mods.dom.Domain + de.hsadmin.mods.email.EMailAddress + de.hsadmin.mods.email.EMailAlias Redirect permanent / http://www.{DOMAIN}/ + \ No newline at end of file diff --git a/hsarback/src/de/hsadmin/mods/dom/index.html.jtpl b/hsarback/src/de/hsadmin/mods/dom/index.html.jtpl new file mode 100644 index 0000000..b55e909 --- /dev/null +++ b/hsarback/src/de/hsadmin/mods/dom/index.html.jtpl @@ -0,0 +1,22 @@ + + + +Willkommen bei {DOMAIN} + + + + +

Willkommen bei {DOMAIN}

+ +

Diese neue Website wurde gerade bei der +Hostsharing eG +für {USER_NAME} eingerichtet.

+ +

Der Inhaber der Domain ist bereits per Email unter +webmaster(at){DOMAIN} +zu erreichen.

+ + + + + \ No newline at end of file diff --git a/hsarback/src/de/hsadmin/mods/dom/test.cgi.jtpl b/hsarback/src/de/hsadmin/mods/dom/test.cgi.jtpl new file mode 100644 index 0000000..cd3dc1b --- /dev/null +++ b/hsarback/src/de/hsadmin/mods/dom/test.cgi.jtpl @@ -0,0 +1,5 @@ +#!/bin/sh +echo Content-type: text/plain +echo +echo Hello, world + \ No newline at end of file diff --git a/hsarback/src/de/hsadmin/mods/dom/zonefile.jtpl b/hsarback/src/de/hsadmin/mods/dom/zonefile.jtpl new file mode 100644 index 0000000..5bc754c --- /dev/null +++ b/hsarback/src/de/hsadmin/mods/dom/zonefile.jtpl @@ -0,0 +1,29 @@ +; default - generated by hsadmin +$TTL 4H +{DOM_HOSTNAME}. IN SOA dns1.hostsharing.net. hostmaster.hostsharing.net. ( + {SIO} ; serial secs since Jan 1 1970 + 6H ; refresh (>=10000) + 1H ; retry (>=1800) + 1W ; expire + 1H ; minimum + ) + +; please read http://www.hostsharing.net/tech/descriptions/domains-zonefile.htm + + IN NS dns1.hostsharing.net. + IN NS dns2.hostsharing.net. + IN NS dns3.hostsharing.net. + + IN MX 30 {PAC_HOSTNAME}. + IN MX 80 backupmx.hostsharing.net. + + IN A {DOM_IPNUMBER} + +www IN A {DOM_IPNUMBER} +ftp IN A {DOM_IPNUMBER} +pop3 IN A {DOM_IPNUMBER} +mail IN A {DOM_IPNUMBER} +mysql IN A {DOM_IPNUMBER} +pgsql IN A {DOM_IPNUMBER} +*.{DOM_HOSTNAME}. IN A {DOM_IPNUMBER} + \ No newline at end of file diff --git a/hsarback/src/de/hsadmin/mods/email/EMailAddress.java b/hsarback/src/de/hsadmin/mods/email/EMailAddress.java new file mode 100644 index 0000000..31da21f --- /dev/null +++ b/hsarback/src/de/hsadmin/mods/email/EMailAddress.java @@ -0,0 +1,208 @@ +package de.hsadmin.mods.email; + +import static javax.persistence.FetchType.EAGER; +import static javax.persistence.GenerationType.SEQUENCE; + +import java.io.Serializable; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.EntityManager; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.SequenceGenerator; +import javax.persistence.Table; +import javax.persistence.Transient; + +import de.hsadmin.core.model.AbstractEntity; +import de.hsadmin.core.model.EntityInfo; +import de.hsadmin.core.model.HSAdminException; +import de.hsadmin.core.model.SearchFilter; +import de.hsadmin.mods.dom.Domain; +import de.hsadmin.mods.user.UnixUser; + +/** + * Entity class for email addresses. + */ +@Entity(name = "EMailAddresses") +@Table(name = "emailaddr") +@SequenceGenerator(name = "EMailAddressesSeqGen", sequenceName = "emailaddr_emailaddr_id_seq") +@EntityInfo(name = "E-Mail-Adresse") +@SearchFilter("domain.user = :loginUser OR " + + "domain.user.pac = :loginUserPac OR " + + "domain.user.pac.customer.name = :loginUserName") +public class EMailAddress extends AbstractEntity implements Serializable { + + private static final long serialVersionUID = -2265500181746604429L; + + @Id + @GeneratedValue(strategy = SEQUENCE, generator = "EMailAddressesSeqGen") + @Column(name = "emailaddr_id", columnDefinition = "integer") + private long id; + + @Column(name = "localpart", updatable = false, nullable= false) + private String localpart = ""; + + @Column(name = "subdomain") + private String subdomain; + + @ManyToOne(fetch = EAGER) + @JoinColumn(name = "domain_id", columnDefinition = "integer", updatable = false) + private Domain domain; + + @Column(name = "target", nullable= false) + private String target; + + public EMailAddress() { + } + + public EMailAddress(String localPart, String subdomain, Domain domain, + String target) { + this.localpart = localPart; + this.subdomain = subdomain; + this.domain = domain; + this.target = target; + } + + public static String createQueryFromStringKey(String humanKey) throws HSAdminException { + String[] parts = humanKey.split("@", 2); + if (parts.length != 2) { + throw new HSAdminException("error in oid: " + humanKey); + } + String[] doms = parts[1].split("\\."); + StringBuilder query = new StringBuilder("localpart='" + parts[0] + + "' AND ( ( subdomain IS NULL AND domain.name='" + parts[1] + + "' )"); + for (int subdomLevel = 1; subdomLevel < doms.length - 1; ++subdomLevel) { + String subdom = ""; + for (int n = 0; n < subdomLevel; ++n) + subdom += "." + doms[n]; + String domain = ""; + for (int n = subdomLevel; n < doms.length; ++n) + domain += "." + doms[n]; + query.append(" OR ( subdomain='" + subdom.substring(1) + + "' AND domain.name='" + domain.substring(1) + "' )"); + } + query.append(" )"); + String queryString = query.toString(); + return queryString; + } + + @Override + public String createStringKey() { + String key = getDomain() != null ? (getLocalPart() + "@" + getFullDomain()) + : "?@?"; + return key; + } + + @Override + public long id() { + return id; + } + + public long getId() { + return id; + } + + protected void setId(long id) { + this.id = id; + } + + public String getLocalPart() { + return localpart == null ? "" : localpart; + } + + public void setLocalPart(String localPart) { + this.localpart = trimToEmpty(localPart); + } + + public String getSubdomain() { + return subdomain == null || subdomain.length() == 0 ? null : subdomain; + } + + public void setSubdomain(String subdomain) { + this.subdomain = trimToNull(subdomain); + } + + public Domain getDomain() { + return domain; + } + + public void setDomain(Domain domain) { + this.domain = domain; + } + + public String getTarget() { + return target; + } + + public void setTarget(String target) { + this.target = trim(target); + } + + @Transient + public String getEMailAddress() { + return createStringKey(); + } + + /** + * returns the full domain (subdomain + domain) + */ + @Transient + public String getFullDomain() { + return (getSubdomain() == null ? "" : (getSubdomain() + ".")) + + getDomain().getName(); + } + + public String toString() { + if (localpart != null && target != null) + return super.toString() + "{ id=" + id + "; address=" + localpart + + subdomain + "; target=" + target + " }"; + else + return super.toString(); + } + + @Override + public boolean isNew() { + return id == 0; + } + + @Override + public String getHiveName() { + if (isNew()) + return null; + else + return getDomain().getUser().getHiveName(); + } + + @Override + public UnixUser owningUser(EntityManager em) { + return domain.owningUser(em); + } + + @Override + public boolean isReadAllowedFor(UnixUser loginUser) { + return getDomain().isReadAllowedFor(loginUser); + } + + @Override + public boolean isWriteAllowedFor(UnixUser loginUser) { + return getDomain().isWriteAllowedFor(loginUser); + } + + /** + * query restriction for this Entity + */ + public static String restriction() { + return "( " + + // domain-owner? + "obj.domain.user = :loginUser OR " + + // pac-admin? (TODO: Hostsharing name convention) + "obj.domain.user.pac.name = :loginUserName OR " + + // customer + "obj.domain.user.pac.customer.name = :loginUserName )"; + } + +} diff --git a/hsarback/src/de/hsadmin/mods/email/EMailAddressModuleImpl.java b/hsarback/src/de/hsadmin/mods/email/EMailAddressModuleImpl.java new file mode 100644 index 0000000..c986e39 --- /dev/null +++ b/hsarback/src/de/hsadmin/mods/email/EMailAddressModuleImpl.java @@ -0,0 +1,75 @@ +package de.hsadmin.mods.email; + +import java.util.List; + +import javax.persistence.EntityManager; +import javax.persistence.Query; + +import de.hsadmin.core.model.AbstractModuleImpl; +import de.hsadmin.core.model.AuthorisationException; +import de.hsadmin.core.model.AbstractEntity; +import de.hsadmin.core.model.HSAdminException; +import de.hsadmin.mods.dom.Domain; + +public class EMailAddressModuleImpl extends AbstractModuleImpl { + + @Override + public List search(Class entityClass, + String condition, String orderBy) throws HSAdminException { + if (orderBy == null || orderBy.length() == 0) { + orderBy = "ORDER BY obj.domain.name ASC, obj.subdomain ASC, obj.localpart ASC"; + } + return super.search(entityClass, condition, orderBy); + } + + @Override + public AbstractEntity add(AbstractEntity newEntity) throws HSAdminException { + EntityManager em = getTransaction().getEntityManager(); + EMailAddress adr = (EMailAddress) newEntity; + if (adr.getTarget() == null || adr.getTarget().length() == 0) { + throw new HSAdminException("target required"); + } + if (adr.getLocalPart() == null) { + adr.setLocalPart(""); + } + if (adr.getDomain() == null + || adr.getDomain().getName() == null + || adr.getDomain().getName().length() == 0) { + throw new HSAdminException("domain required"); + } + Query qDomain = em.createQuery("SELECT d FROM Domains d WHERE d.name = :domName"); + qDomain.setParameter("domName", adr.getDomain().getName()); + Domain dom = (Domain) qDomain.getSingleResult(); + adr.setDomain(dom); + return super.add(newEntity); + } + + @Override + public AbstractEntity update(AbstractEntity existingEntity) throws HSAdminException { + EMailAddress detachedAddr = (EMailAddress) existingEntity; + EntityManager em = getTransaction().getEntityManager(); + EMailAddress attachedAddr = em.find(EMailAddress.class, detachedAddr.getId()); + Domain domain = detachedAddr.getDomain(); + if (domain != null && domain.getId() != attachedAddr.getDomain().getId()) { + detachedAddr.setDomain(attachedAddr.getDomain()); + throw new AuthorisationException(getLoginUser(), "update", detachedAddr, "domain"); + } + String subdomain = detachedAddr.getSubdomain(); + if (subdomain != null && !subdomain.equals(attachedAddr.getSubdomain())) { + detachedAddr.setSubdomain(attachedAddr.getSubdomain()); + throw new AuthorisationException(getLoginUser(), "update", detachedAddr, "subdomain"); + } + String localPart = detachedAddr.getLocalPart(); + if (localPart != null && !localPart.equals(attachedAddr.getLocalPart())) { + detachedAddr.setLocalPart(attachedAddr.getLocalPart()); + throw new AuthorisationException(getLoginUser(), "update", detachedAddr, "localpart"); + } + String target = detachedAddr.getTarget(); + if (target == null) { + throw new HSAdminException("target required"); + } + attachedAddr.setTarget(target); + return super.update(attachedAddr); + } + +} diff --git a/hsarback/src/de/hsadmin/mods/email/EMailAddressProcessorFactory.java b/hsarback/src/de/hsadmin/mods/email/EMailAddressProcessorFactory.java new file mode 100644 index 0000000..26b54f3 --- /dev/null +++ b/hsarback/src/de/hsadmin/mods/email/EMailAddressProcessorFactory.java @@ -0,0 +1,70 @@ +package de.hsadmin.mods.email; + +import java.util.List; + +import javax.persistence.EntityManager; +import javax.persistence.Query; + +import de.hsadmin.core.model.AbstractEntity; +import de.hsadmin.core.qserv.CompoundProcessor; +import de.hsadmin.core.qserv.EntityProcessorFactory; +import de.hsadmin.core.qserv.Processor; +import de.hsadmin.core.qserv.ShellProcessor; + +/** + * System level implementation for EMailAdress module. + */ +public class EMailAddressProcessorFactory implements EntityProcessorFactory { + + /** + * @return a Processor which creates an email address + */ + public Processor createCreateProcessor(EntityManager em, T entity) { + // TODO: combine both keys in a single call (optimization) + EMailAddress email = (EMailAddress) entity; + CompoundProcessor cp = new CompoundProcessor(); + cp.appendProcessor(new ShellProcessor( "postmap -r -i /etc/postfix/virtual", + email.getFullDomain() + " -" ) ); + cp.appendProcessor(new ShellProcessor( "postmap -r -i /etc/postfix/virtual", + email.getEMailAddress() + " " + email.getTarget() ) ); + return cp; + } + + /** + * @return a Processor which updates an email address + */ + public Processor createUpdateProcessor(EntityManager em, T entity) { + // TODO: if update is specified by primary-key or DB query instead of OID, + // a postmap -d might be neccessary + return createCreateProcessor(em, entity); + } + + /** + * @return a Processor which deletes an email address + */ + public Processor createDeleteProcessor(EntityManager em, T entity) { + // TODO: combine both keys in a single call (optimization) + // remove the entry itself + CompoundProcessor cp = new CompoundProcessor(); + EMailAddress email = (EMailAddress) entity; + cp.appendProcessor( + new ShellProcessor( "postmap -d '" + email.getEMailAddress() + "' /etc/postfix/virtual" ) ); + // any other email addresses for this domain? + Query query; + if ( email.getSubdomain() != null ) { + query = em.createQuery("SELECT e FROM EMailAddresses e WHERE e.subdomain=:subdomain AND e.domain=:domain"); + query.setParameter("subdomain", email.getSubdomain()); + } + else { + query = em.createQuery("SELECT e FROM EMailAddresses e WHERE e.subdomain IS NULL AND e.domain=:domain"); + } + query.setParameter("domain", email.getDomain()); + List result = query.getResultList(); + if ( result == null || result.size() == 0 ) { + // remove the domain from virtual.db + cp.appendProcessor( + new ShellProcessor( "postmap -d '" + email.getFullDomain() + "' /etc/postfix/virtual" ) ); + } + return cp; + } +} diff --git a/hsarback/src/de/hsadmin/mods/email/EMailAlias.java b/hsarback/src/de/hsadmin/mods/email/EMailAlias.java new file mode 100644 index 0000000..92fa609 --- /dev/null +++ b/hsarback/src/de/hsadmin/mods/email/EMailAlias.java @@ -0,0 +1,200 @@ +package de.hsadmin.mods.email; + +import static javax.persistence.GenerationType.SEQUENCE; + +import java.io.Serializable; + +import javax.persistence.Column; +import javax.persistence.EntityManager; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.NoResultException; +import javax.persistence.Query; +import javax.persistence.SequenceGenerator; +import javax.persistence.Table; + +import de.hsadmin.core.model.AbstractModuleImpl; +import de.hsadmin.core.model.AbstractEntity; +import de.hsadmin.core.model.EntityInfo; +import de.hsadmin.core.model.SearchFilter; +import de.hsadmin.mods.pac.Pac; +import de.hsadmin.mods.user.UnixUser; + +/** + * Entity class for email aliases. + * + */ +@javax.persistence.Entity(name = "EMailAliases") +@Table(name = "emailalias") +@SequenceGenerator(name = "EMailAliasesSeqGen", sequenceName = "emailalias_emailalias_id_seq") +@EntityInfo(name = "E-Mail-Alias") +@SearchFilter("pac = :loginUserPac OR " + + "pac.customer.name = :loginUserName") +public class EMailAlias extends AbstractEntity implements Serializable { + + private static final long serialVersionUID = -4711415079723587161L; + + @Id + @GeneratedValue(strategy = SEQUENCE, generator = "EMailAliasesSeqGen") + @Column(name = "emailalias_id", columnDefinition = "integer", insertable=false, updatable=false) + private long id; + + @ManyToOne() + @JoinColumn(name = "pac_id", columnDefinition = "integer") + private Pac pac; + + @Column(updatable=false) + private String name; + + @Column + private String target; + + public EMailAlias() { + } + + public EMailAlias(Pac pac, String name, String target) { + this.pac = pac; + this.name = name; + this.target = target; + } + + @Override + public void initialize(EntityManager em, UnixUser loginUser) { + pac = loginUser.getPac(); + name = pac.getName() + "-"; + target = ""; + } + + @Override + public void complete(EntityManager em, UnixUser loginUser) { + if (pac == null && name != null && name.length() > 0) { + // TODO: it's ugly having this code here, needs refactoring + String pacName = name.substring(0, 5); + try { + // get the entities name (query part from FROM to WHERE) + javax.persistence.Entity entityAnnot = Pac.class + .getAnnotation(javax.persistence.Entity.class); + String queryString = "FROM " + entityAnnot.name() + " WHERE " + + Pac.createQueryFromStringKey(pacName); + + // set parameters + Query query = em.createQuery(queryString); + AbstractModuleImpl.setQueryParameter(query, queryString, + "loginUser", loginUser); + AbstractModuleImpl.setQueryParameter(query, queryString, + "loginUserName", loginUser.getName()); + AbstractModuleImpl.setQueryParameter(query, queryString, + "loginUserPac", loginUser.getPac()); + pac = (Pac) query.getSingleResult(); + } catch (NoResultException exc) { + throw new SecurityException("packet '" + pacName + + "' not found or access denied"); + } + } + } + + public static String createQueryFromStringKey(String humanKey) { + return "name='" + humanKey + "'"; + } + + @Override + public String createStringKey() { + return name; + } + + @Override + public long id() { + return id; + } + + public long getId() { + return id; + } + + protected void setId(long id) { + this.id = id; + } + + public Pac getPac() { + return pac; + } + + public void setPac(Pac pac) { + this.pac = pac; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getTarget() { + return target; + } + + public void setTarget(String target) { + this.target = target; + } + + @Override + public boolean isNew() { + return id == 0; + } + + @Override + public String getHiveName() { + if (isNew()) + return null; + else + return getPac().getHiveName(); + } + + @Override + public UnixUser owningUser(EntityManager em) { + return pac.owningUser(em); + } + + public AbstractEntity merge(EntityManager em, UnixUser loginUser) { + EMailAlias attachedEntity = em.find(getClass(), id()); + Pac attachedPacket = attachedEntity.getPac(); + if (pac.getId() != attachedPacket.getId()) + throw new SecurityException( + "changing the Packet of an EMailAlias is not allowed"); + if (!name.equals(attachedPacket.getName()) + && !name.startsWith(attachedPacket.getName() + "-")) + throw new SecurityException( + "changing the Packet of an EMailAlias is not allowed"); + + attachedEntity.setName(name); + attachedEntity.setTarget(target); + return attachedEntity; + } + + @Override + public boolean isReadAllowedFor(UnixUser loginUser) { + return loginUser.hasPacAdminRoleFor(getPac()); + } + + @Override + public boolean isWriteAllowedFor(UnixUser loginUser) { + String pacName = pac.getName(); + if (!name.equals(pacName) && !name.startsWith(pacName + "-")) + return false; + + return loginUser.hasPacAdminRoleFor(getPac()); + } + + public static String restriction() { + return + // all aliases of all pacs of customer + "pac.customer.name=:loginUserName OR " + + // all aliases of packet admin + "pac.name=:loginUserName"; + } + +} diff --git a/hsarback/src/de/hsadmin/mods/email/EMailAliasModuleImpl.java b/hsarback/src/de/hsadmin/mods/email/EMailAliasModuleImpl.java new file mode 100644 index 0000000..c4a436c --- /dev/null +++ b/hsarback/src/de/hsadmin/mods/email/EMailAliasModuleImpl.java @@ -0,0 +1,20 @@ +package de.hsadmin.mods.email; + +import java.util.List; + +import de.hsadmin.core.model.AbstractModuleImpl; +import de.hsadmin.core.model.AbstractEntity; +import de.hsadmin.core.model.HSAdminException; + +public class EMailAliasModuleImpl extends AbstractModuleImpl { + + @Override + public List search(Class entityClass, + String condition, String orderBy) throws HSAdminException { + if (orderBy == null || orderBy.length() == 0) { + orderBy = "ORDER BY name ASC"; + } + return super.search(entityClass, condition, orderBy); + } + +} diff --git a/hsarback/src/de/hsadmin/mods/email/EMailAliasProcessorFactory.java b/hsarback/src/de/hsadmin/mods/email/EMailAliasProcessorFactory.java new file mode 100644 index 0000000..6a2f47e --- /dev/null +++ b/hsarback/src/de/hsadmin/mods/email/EMailAliasProcessorFactory.java @@ -0,0 +1,46 @@ +package de.hsadmin.mods.email; + +import javax.persistence.EntityManager; + +import de.hsadmin.core.model.AbstractEntity; +import de.hsadmin.core.qserv.EntityProcessorFactory; +import de.hsadmin.core.qserv.Processor; +import de.hsadmin.core.qserv.ShellProcessor; + +/** + * System level implementation for EMailAlias module. + */ +public class EMailAliasProcessorFactory implements EntityProcessorFactory { + + // TODO: execution of any of these processors needs to be synchronized + + /** + * @return a Processor which creates an email alias + */ + public Processor createCreateProcessor(EntityManager em, + T entity) { + EMailAlias alias = (EMailAlias) entity; + return new ShellProcessor("postalias -r -i /etc/postfix/aliases", + alias.getName() + ": " + alias.getTarget()); + } + + /** + * @return a Processor which updates an email alias + */ + public Processor createUpdateProcessor(EntityManager em, + T entity) { + EMailAlias alias = (EMailAlias) entity; + return new ShellProcessor("postalias -r -i /etc/postfix/aliases", + alias.getName() + ": " + alias.getTarget()); + } + + /** + * @return a Processor which deletes an email alias + */ + public Processor createDeleteProcessor(EntityManager em, + T entity) { + EMailAlias alias = (EMailAlias) entity; + return new ShellProcessor( + "postalias -d '" + alias.getName() + "' /etc/postfix/aliases"); + } +} diff --git a/hsarback/src/de/hsadmin/mods/user/UnixUserModuleImpl.java b/hsarback/src/de/hsadmin/mods/user/UnixUserModuleImpl.java index 834ad69..2fe09a4 100644 --- a/hsarback/src/de/hsadmin/mods/user/UnixUserModuleImpl.java +++ b/hsarback/src/de/hsadmin/mods/user/UnixUserModuleImpl.java @@ -99,10 +99,14 @@ public class UnixUserModuleImpl extends AbstractModuleImpl { } if (passWord.indexOf(':') >= 0) { throw new AuthorisationException(getLoginUser(), "add", newUnixUser, "userId"); + } + if (newUnixUser.getPac() == null || newUnixUser.getPac().getNew()) { + } Query qPac = em.createQuery("SELECT obj FROM Pacs obj WHERE obj.name = :pacName"); qPac.setParameter("pacName", userName.substring(0, 5)); - Pac pac = (Pac) qPac.getSingleResult(); + Object singleResult = qPac.getSingleResult(); + Pac pac = (Pac) singleResult; newUnixUser.setName(userName); newUnixUser.setHomedir("/home/pacs/" + userName.substring(0, 5) + "/users/" + userName.substring(6)); @@ -138,10 +142,6 @@ public class UnixUserModuleImpl extends AbstractModuleImpl { return super.add(newEntity); } - private EntityManager getEntityManager() { - return getTransaction().getEntityManager(); - } - @Override public AbstractEntity update(AbstractEntity existingEntity) throws HSAdminException { // get the entity from the database @@ -218,6 +218,10 @@ public class UnixUserModuleImpl extends AbstractModuleImpl { super.delete(attachedUnixUser); } + private EntityManager getEntityManager() { + return getTransaction().getEntityManager(); + } + // throws an AuthorisationException if the login user has no write acess // on the pac of the given UnixUser private boolean hasFullAccessOnPacOf(UnixUser user) { diff --git a/hsarback/src/de/hsadmin/remote/AbstractRemote.java b/hsarback/src/de/hsadmin/remote/AbstractRemote.java index 1eadc6c..721ee08 100644 --- a/hsarback/src/de/hsadmin/remote/AbstractRemote.java +++ b/hsarback/src/de/hsadmin/remote/AbstractRemote.java @@ -150,7 +150,7 @@ public abstract class AbstractRemote implements IRemote { "better safe than sorry: no where parameter found"); } List list = module.search(getEntityClass(), - queryCondition, "ORDER BY obj.name ASC"); + queryCondition, getOrderBy()); transaction.beginTransaction(); for (AbstractEntity update : list) { if (update.isWriteAllowedFor(unixUser)) { @@ -178,6 +178,10 @@ public abstract class AbstractRemote implements IRemote { } } + public String getOrderBy() { + return "ORDER BY obj.name ASC"; + } + protected boolean assertNotNull(String string) { return string != null && string.length() > 0; } diff --git a/hsarback/src/de/hsadmin/remote/CustomerRemote.java b/hsarback/src/de/hsadmin/remote/CustomerRemote.java index e04900a..bce4ad8 100644 --- a/hsarback/src/de/hsadmin/remote/CustomerRemote.java +++ b/hsarback/src/de/hsadmin/remote/CustomerRemote.java @@ -94,6 +94,10 @@ public class CustomerRemote extends AbstractRemote { if (assertNotNull(memberCode)) { cust.setName(memberCode); } + String password = setParams.get("password"); + if (assertNotNull(password)) { + cust.setPassword(password); + } String memberNo = setParams.get("memberno"); if (assertNotNull(memberNo)) { cust.setMemberNo(Integer.parseInt(memberNo)); diff --git a/hsarback/src/de/hsadmin/remote/DomainRemote.java b/hsarback/src/de/hsadmin/remote/DomainRemote.java new file mode 100644 index 0000000..6acbc7d --- /dev/null +++ b/hsarback/src/de/hsadmin/remote/DomainRemote.java @@ -0,0 +1,63 @@ +package de.hsadmin.remote; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Map; + +import de.hsadmin.core.model.AbstractEntity; +import de.hsadmin.mods.dom.Domain; +import de.hsadmin.mods.user.UnixUser; + +public class DomainRemote extends AbstractRemote { + + private static final DateFormat df = SimpleDateFormat.getDateInstance(DateFormat.SHORT); + + @Override + protected Class getEntityClass() { + return Domain.class; + } + + @Override + protected void entity2map(AbstractEntity entity, Map resultMap) { + Domain dom = (Domain) entity; + String id = Long.toString(dom.getId()); + resultMap.put("id", id); + String name = dom.getName(); + resultMap.put("name", name); + String user = dom.getUser().getName(); + resultMap.put("user", user); + String hive = dom.getHiveName(); + resultMap.put("hive", hive); + String pac = dom.getUser().getPac().getName(); + resultMap.put("pac", pac); + Date sDate = dom.getSince(); + if (assertNotNull(sDate)) { + String since = df.format(sDate); + resultMap.put("since", since); + } + } + + @Override + protected void map2entity(Map setParams, AbstractEntity entity) { + Domain dom = (Domain) entity; + String name = setParams.get("name"); + String user = setParams.get("user"); + if (assertNotNull(name)) { + dom.setName(name); + } + if (assertNotNull(user)) { + UnixUser u = new UnixUser(); + u.setName(user); + dom.setUser(u); + } + } + + @Override + protected void regularizeKeys(Map whereParams) { + replaceKey(whereParams, "user", "user.name"); + replaceKey(whereParams, "pac", "user.pac.name"); + replaceKey(whereParams, "hive", "hiveName"); + } + +} diff --git a/hsarback/src/de/hsadmin/remote/EMailAddressRemote.java b/hsarback/src/de/hsadmin/remote/EMailAddressRemote.java new file mode 100644 index 0000000..ce984fc --- /dev/null +++ b/hsarback/src/de/hsadmin/remote/EMailAddressRemote.java @@ -0,0 +1,74 @@ +package de.hsadmin.remote; + +import java.util.Map; + +import de.hsadmin.core.model.AbstractEntity; +import de.hsadmin.mods.dom.Domain; +import de.hsadmin.mods.email.EMailAddress; + +public class EMailAddressRemote extends AbstractRemote { + + @Override + protected void entity2map(AbstractEntity entity, Map map) { + EMailAddress adr = (EMailAddress) entity; + long id = adr.getId(); + String domain = adr.getDomain().getName(); + String admin = adr.getDomain().getUser().getName(); + String pac = adr.getDomain().getUser().getPac().getName(); + String target = adr.getTarget(); + String localpart = adr.getLocalPart(); + String subdomain = adr.getSubdomain(); + String emailaddress = adr.getEMailAddress(); + String fulldomain = adr.getFullDomain(); + map.put("id", Long.toString(id)); + map.put("domain", domain); + map.put("admin", admin); + map.put("pac", pac); + map.put("target", target); + map.put("localpart", localpart); + map.put("subdomain", subdomain); + map.put("emailaddress", emailaddress); + map.put("fulldomain", fulldomain); + } + + @Override + protected void map2entity(Map map, AbstractEntity entity) { + EMailAddress adr = (EMailAddress) entity; + String localPart = map.get("localpart"); + if (assertNotNull(localPart)) { + adr.setLocalPart(localPart); + } + String subdomain = map.get("subdomain"); + if (assertNotNull(subdomain)) { + adr.setSubdomain(subdomain); + } + String target = map.get("target"); + if (assertNotNull(target)) { + adr.setTarget(target); + } + String domain = map.get("domain"); + if (assertNotNull(domain)) { + Domain dom = new Domain(); + dom.setName(domain); + adr.setDomain(dom); + } + } + + @Override + protected Class getEntityClass() { + return EMailAddress.class; + } + + @Override + protected void regularizeKeys(Map whereParams) { + replaceKey(whereParams, "domain", "domain.name"); + replaceKey(whereParams, "pac", "domain.user.pac.name"); + replaceKey(whereParams, "admin", "domain.user.name"); + } + + @Override + public String getOrderBy() { + return "ORDER BY obj.domain.name ASC, obj.subdomain ASC, obj.localpart ASC"; + } + +} diff --git a/hsarback/src/de/hsadmin/remote/EMailAliasRemote.java b/hsarback/src/de/hsadmin/remote/EMailAliasRemote.java new file mode 100644 index 0000000..c07ef4c --- /dev/null +++ b/hsarback/src/de/hsadmin/remote/EMailAliasRemote.java @@ -0,0 +1,46 @@ +package de.hsadmin.remote; + +import java.util.Map; + +import de.hsadmin.core.model.AbstractEntity; +import de.hsadmin.mods.email.EMailAlias; + +public class EMailAliasRemote extends AbstractRemote { + + @Override + protected void entity2map(AbstractEntity entity, Map map) { + EMailAlias alias = (EMailAlias) entity; + String id = Long.toString(alias.getId()); + String name = alias.getName(); + String pac = alias.getPac().getName(); + String target = alias.getTarget(); + map.put("id", id); + map.put("name", name); + map.put("pac", pac); + map.put("target", target); + } + + @Override + protected Class getEntityClass() { + return EMailAlias.class; + } + + @Override + protected void map2entity(Map map, AbstractEntity entity) { + EMailAlias alias = (EMailAlias) entity; + String name = map.get("name"); + String target = map.get("target"); + if (assertNotNull(name)) { + alias.setName(name); + } + if (assertNotNull(target)) { + alias.setTarget(target); + } + } + + @Override + protected void regularizeKeys(Map whereParams) { + replaceKey(whereParams, "pac", "pac.name"); + } + +} diff --git a/hsarback/src/org/apache/xmlrpc/webserver/XmlRpcServlet.properties b/hsarback/src/org/apache/xmlrpc/webserver/XmlRpcServlet.properties index 413368f..aeaa200 100644 --- a/hsarback/src/org/apache/xmlrpc/webserver/XmlRpcServlet.properties +++ b/hsarback/src/org/apache/xmlrpc/webserver/XmlRpcServlet.properties @@ -1,3 +1,6 @@ member=de.hsadmin.remote.CustomerRemote user=de.hsadmin.remote.UnixUserRemote +domain=de.hsadmin.remote.DomainRemote +emailalias=de.hsadmin.remote.EMailAliasRemote +emailaddress=de.hsadmin.remote.EMailAddressRemote q=de.hsadmin.remote.QueueTaskRemote diff --git a/hsarback/test/de/hsadmin/remote/CustomerTest.java b/hsarback/test/de/hsadmin/remote/CustomerTest.java index 8481679..02269e2 100644 --- a/hsarback/test/de/hsadmin/remote/CustomerTest.java +++ b/hsarback/test/de/hsadmin/remote/CustomerTest.java @@ -44,12 +44,13 @@ public class CustomerTest { String grantingTicketURL = cas.getGrantingTicketURL(user); Map setParams = new HashMap(); setParams.put("membercode", "hsh00-aaa"); + setParams.put("password", "geheimnIs"); setParams.put("memberno", "20001"); setParams.put("membersince", "01.10.2010"); setParams.put("contact_salut", "Herr"); setParams.put("contact_title", "Dr."); - setParams.put("contact_firstname", "Rainer"); - setParams.put("contact_lastname", "Mustermann"); + setParams.put("contact_firstname", "Ömer Günther"); + setParams.put("contact_lastname", "Janßen-Müller"); setParams.put("contact_salut", "Herr"); setParams.put("contact_street", "Hauptstr. 1"); setParams.put("contact_zipcode", "99998"); @@ -58,7 +59,7 @@ public class CustomerTest { setParams.put("contact_phone_private", "+49 9999 123456"); setParams.put("contact_email", "rainer.mustermann@example.org"); Object[] params = new Object[] { user, - cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.BACKEND_URL), + cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), setParams }; try { client.execute(MODULE + ".add", params); @@ -77,7 +78,7 @@ public class CustomerTest { String grantingTicketURL = cas.getGrantingTicketURL(user); Map whereParams = new HashMap(); Object[] params = new Object[] { user, - cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.BACKEND_URL), + cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), whereParams }; try { Object execute = client.execute(MODULE + ".search", params); @@ -94,7 +95,7 @@ public class CustomerTest { String grantingTicketURL = cas.getGrantingTicketURL(user); Map whereParams = new HashMap(); Object[] params = new Object[] { user, - cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.BACKEND_URL), + cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), whereParams }; try { Object execute = client.execute(MODULE + ".search", params); @@ -111,7 +112,7 @@ public class CustomerTest { String grantingTicketURL = cas.getGrantingTicketURL(user); Map whereParams = new HashMap(); Object[] params = new Object[] { user, - cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.BACKEND_URL), + cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), whereParams }; try { Object execute = client.execute(MODULE + ".search", params); @@ -128,7 +129,7 @@ public class CustomerTest { String grantingTicketURL = cas.getGrantingTicketURL(user); Map whereParams = new HashMap(); Object[] params = new Object[] { user, - cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.BACKEND_URL), + cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), whereParams }; try { Object execute = client.execute(MODULE + ".search", params); @@ -140,25 +141,57 @@ public class CustomerTest { } @Test - public void testDeleteAsMember() { + public void testUpdateAsMember() { String user = "hsh00-aaa"; - int membersCount = -9999; + String grantingTicketURL = cas.getGrantingTicketURL(user); + Map whereParams = new HashMap(); + Map setParams = new HashMap(); + whereParams.put("membercode", "hsh00-aaa"); + setParams.put("contact_firstname", "Hugo"); + Object[] params = new Object[] { user, + cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), + setParams, whereParams }; try { - membersCount = getMembersCount(user); + client.execute(MODULE + ".update", params); + fail("exception erwartet"); + } catch (XmlRpcException e) { + // Exception erwartet + } + } + + @Test + public void testUpdateAsHostmaster() { + String user = "pe"; + String grantingTicketURL = cas.getGrantingTicketURL(user); + Map whereParams = new HashMap(); + Map setParams = new HashMap(); + whereParams.put("membercode", "hsh00-aaa"); + setParams.put("contact_title", "Prof."); + setParams.put("membersince", "Hugo"); + Object[] params = new Object[] { user, + cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), + setParams, whereParams }; + try { + client.execute(MODULE + ".update", params); } catch (XmlRpcException e) { fail(e.getMessage()); } + } + + @Test + public void testDeleteAsMember() { + String user = "hsh00-aaa"; String grantingTicketURL = cas.getGrantingTicketURL(user); Map whereParams = new HashMap(); whereParams.put("membercode", "hsh00-aaa"); Object[] params = new Object[] { user, - cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.BACKEND_URL), + cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), whereParams }; try { client.execute(MODULE + ".delete", params); - assertEquals(membersCount - 1, getMembersCount(user)); + fail("exception erwartet"); } catch (XmlRpcException e) { - fail(e.getMessage()); + // Exception erwartet } } @@ -175,7 +208,7 @@ public class CustomerTest { Map whereParams = new HashMap(); whereParams.put("membercode", "hsh00-aaa"); Object[] params = new Object[] { user, - cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.BACKEND_URL), + cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), whereParams }; try { client.execute(MODULE + ".delete", params); @@ -189,7 +222,7 @@ public class CustomerTest { String grantingTicketURL = cas.getGrantingTicketURL(user); Map whereParams = new HashMap(); Object[] params = new Object[] { user, - cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.BACKEND_URL), + cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), whereParams }; Object execute = client.execute(MODULE + ".search", params); Object[] result = (Object[]) execute; diff --git a/hsarback/test/de/hsadmin/remote/DomainTest.java b/hsarback/test/de/hsadmin/remote/DomainTest.java new file mode 100644 index 0000000..bd3d184 --- /dev/null +++ b/hsarback/test/de/hsadmin/remote/DomainTest.java @@ -0,0 +1,140 @@ +package de.hsadmin.remote; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.xmlrpc.XmlRpcException; +import org.apache.xmlrpc.client.XmlRpcClient; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class DomainTest { + + private static final String MODULE = "domain"; + + private XmlRpcClient client; + private RemoteCASHelper cas; + + @Before + public void setUp() throws Exception { + client = RemoteTestHelper.getClient(); + cas = new RemoteCASHelper(); + } + + @After + public void tearDown() throws Exception { + client = null; + cas = null; + } + + @Test + public void testSearchAllAsPacAdmin() { + String user = "peh00"; + String grantingTicketURL = cas.getGrantingTicketURL(user); + Map whereParams = new HashMap(); + Object[] params = new Object[] { user, + cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), + whereParams }; + try { + Object execute = client.execute(MODULE + ".search", params); + Object[] result = (Object[]) execute; + assertEquals(36, result.length); + for (Object o : result) { + if (o instanceof Map) { + Map row = (Map) o; + assertEquals("peh00", row.get("user")); + } else { + fail("map expected"); + } + } + } catch (XmlRpcException e) { + fail(e.getMessage()); + } + } + + @Test + public void testUpdate() { + String user = "peh00"; + String grantingTicketURL = cas.getGrantingTicketURL(user); + Map setParams = new HashMap(); + Map whereParams = new HashMap(); + setParams.put("user", "peh00-phor"); + whereParams.put("name", "i2null.de"); + Object[] params = new Object[] { user, + cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), + setParams, whereParams }; + try { + Object execute = client.execute(MODULE + ".update", params); + assertNotNull(execute); + fail("exception expected"); + } catch (XmlRpcException e) { +// System.out.println(e.getMessage()); + } + } + + + @Test + public void testCreate() { + int count = getDomsCount(); + String user = "peh00"; + String grantingTicketURL = cas.getGrantingTicketURL(user); + Map setParams = new HashMap(); + setParams.put("name", "f6n.de"); + setParams.put("user", "peh00"); + Object[] params = new Object[] { user, + cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), + setParams }; + try { + Object execute = client.execute(MODULE + ".add", params); + assertTrue(execute instanceof Map); + } catch (XmlRpcException e) { + fail(e.getMessage()); + } + assertEquals(count + 1, getDomsCount()); + } + + @Test + public void testDelete() { + int count = getDomsCount(); + String user = "peh00"; + String grantingTicketURL = cas.getGrantingTicketURL(user); + Map whereParams = new HashMap(); + whereParams.put("name", "f6n.de"); + Object[] params = new Object[] { user, + cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), + whereParams }; + try { + Object execute = client.execute(MODULE + ".delete", params); + assertNull(execute); + } catch (XmlRpcException e) { + fail(e.getMessage()); + } + assertEquals(count - 1, getDomsCount()); + } + + private int getDomsCount() { + int count = 0; + String user = "peh00"; + String grantingTicketURL = cas.getGrantingTicketURL(user); + Map whereParams = new HashMap(); + Object[] params = new Object[] { user, + cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), + whereParams }; + try { + Object execute = client.execute(MODULE + ".search", params); + Object[] result = (Object[]) execute; + count = result.length; + } catch (XmlRpcException e) { + fail(e.getMessage()); + } + return count; + } + +} diff --git a/hsarback/test/de/hsadmin/remote/EMailAddressTest.java b/hsarback/test/de/hsadmin/remote/EMailAddressTest.java new file mode 100644 index 0000000..7eb1d32 --- /dev/null +++ b/hsarback/test/de/hsadmin/remote/EMailAddressTest.java @@ -0,0 +1,246 @@ +package de.hsadmin.remote; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.xmlrpc.XmlRpcException; +import org.apache.xmlrpc.client.XmlRpcClient; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class EMailAddressTest { + + private static final String MODULE = "emailaddress"; + + private XmlRpcClient client; + private RemoteCASHelper cas; + + @Before + public void setUp() throws Exception { + client = RemoteTestHelper.getClient(); + cas = new RemoteCASHelper(); + } + + @After + public void tearDown() throws Exception { + client = null; + cas = null; + } + + @Test + public void testSearchAllAsPacAdmin() { + String user = "peh00"; + String grantingTicketURL = cas.getGrantingTicketURL(user); + Map whereParams = new HashMap(); + Object[] params = new Object[] { user, + cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), + whereParams }; + try { + Object execute = client.execute(MODULE + ".search", params); + Object[] result = (Object[]) execute; + assertEquals(262, result.length); + for (Object o : result) { + if (o instanceof Map) { + Map row = (Map) o; + assertEquals("peh00", row.get("pac")); + } else { + fail("map expected"); + } + } + } catch (XmlRpcException e) { + fail(e.getMessage()); + } + } + + @Test + public void testSearchAsPacAdmin() { + String user = "peh00"; + String grantingTicketURL = cas.getGrantingTicketURL(user); + Map whereParams = new HashMap(); + whereParams.put("domain", "herzensklaenge.de"); + Object[] params = new Object[] { user, + cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), + whereParams }; + try { + Object execute = client.execute(MODULE + ".search", params); + Object[] result = (Object[]) execute; + assertEquals(2, result.length); + for (Object o : result) { + if (o instanceof Map) { + Map row = (Map) o; + assertEquals("peh00", row.get("pac")); + } else { + fail("map expected"); + } + } + } catch (XmlRpcException e) { + fail(e.getMessage()); + } + } + + @Test + public void testUpdate() { + int count = getTargetCount(); + String user = "peh00"; + String grantingTicketURL = cas.getGrantingTicketURL(user); + Map setParams = new HashMap(); + Map whereParams = new HashMap(); + setParams.put("target", "peh00-phor"); + whereParams.put("domain", "jalin.de"); + whereParams.put("localpart", "fax"); + Object[] params = new Object[] { user, + cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), + setParams, whereParams }; + try { + Object execute = client.execute(MODULE + ".update", params); + Object[] result = (Object[]) execute; + assertEquals(1, result.length); + } catch (XmlRpcException e) { + fail(e.getMessage()); + } + assertEquals(count + 1, getTargetCount()); + setParams = new HashMap(); + whereParams = new HashMap(); + setParams.put("target", "peh00-hotline"); + whereParams.put("domain", "jalin.de"); + whereParams.put("localpart", "fax"); + params = new Object[] { user, + cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), + setParams, whereParams }; + try { + Object execute = client.execute(MODULE + ".update", params); + Object[] result = (Object[]) execute; + assertEquals(1, result.length); + } catch (XmlRpcException e) { + fail(e.getMessage()); + } + assertEquals(count, getTargetCount()); + } + + @Test + public void testSearchAsDomAdmin() { + String user = "peh00"; + String grantingTicketURL = cas.getGrantingTicketURL(user); + Map whereParams = new HashMap(); + whereParams.put("domain", "herzensklaenge.de"); + whereParams.put("admin", "peh00"); + Object[] params = new Object[] { user, + cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), + whereParams }; + try { + Object execute = client.execute(MODULE + ".search", params); + Object[] result = (Object[]) execute; + assertEquals(2, result.length); + for (Object o : result) { + if (o instanceof Map) { + Map row = (Map) o; + assertEquals("peh00", row.get("admin")); + } else { + fail("map expected"); + } + } + } catch (XmlRpcException e) { + fail(e.getMessage()); + } + } + + @Test + public void testCreateAsPacAdminFails() { + int count = getObjectCount(); + String user = "peh00"; + String grantingTicketURL = cas.getGrantingTicketURL(user); + Map setParams = new HashMap(); + setParams.put("localpart", "f6n"); + setParams.put("domain", "f6n.de"); + setParams.put("target", "peh00-phor"); + Object[] params = new Object[] { user, + cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), + setParams }; + try { + Object execute = client.execute(MODULE + ".add", params); + assertTrue(execute instanceof Map); + fail("exception expected"); + } catch (XmlRpcException e) { + } + assertEquals(count, getObjectCount()); + } + + @Test + public void testCreateAndDelete() { + int count = getObjectCount(); + String user = "peh00"; + String grantingTicketURL = cas.getGrantingTicketURL(user); + Map setParams = new HashMap(); + setParams.put("localpart", "f6n"); + setParams.put("domain", "jalin.de"); + setParams.put("target", "peh00-phor"); + Object[] params = new Object[] { user, + cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), + setParams }; + try { + Object execute = client.execute(MODULE + ".add", params); + assertTrue(execute instanceof Map); + } catch (XmlRpcException e) { + fail(e.getMessage()); + } + assertEquals(count + 1, getObjectCount()); + count = getObjectCount(); + Map whereParams = new HashMap(); + whereParams.put("localpart", "f6n"); + whereParams.put("domain", "jalin.de"); + params = new Object[] { user, + cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), + whereParams }; + try { + Object execute = client.execute(MODULE + ".delete", params); + assertNull(execute); + } catch (XmlRpcException e) { + fail(e.getMessage()); + } + assertEquals(count - 1, getObjectCount()); + } + + private int getObjectCount() { + int count = -1; + try { + String user = "peh00"; + String grantingTicketURL = cas.getGrantingTicketURL(user); + Map whereParams = new HashMap(); + Object[] params = new Object[] { user, + cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), + whereParams }; + Object execute = client.execute(MODULE + ".search", params); + Object[] result = (Object[]) execute; + count = result.length; + } catch (XmlRpcException e) { + fail(e.getMessage()); + } + return count; + } + + private int getTargetCount() { + int count = -1; + try { + String user = "peh00"; + String grantingTicketURL = cas.getGrantingTicketURL(user); + Map whereParams = new HashMap(); + whereParams.put("target", "peh00-phor"); + Object[] params = new Object[] { user, + cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), + whereParams }; + Object execute = client.execute(MODULE + ".search", params); + Object[] result = (Object[]) execute; + count = result.length; + } catch (XmlRpcException e) { + fail(e.getMessage()); + } + return count; + } + +} diff --git a/hsarback/test/de/hsadmin/remote/EMailAliasTest.java b/hsarback/test/de/hsadmin/remote/EMailAliasTest.java new file mode 100644 index 0000000..9fc610f --- /dev/null +++ b/hsarback/test/de/hsadmin/remote/EMailAliasTest.java @@ -0,0 +1,61 @@ +package de.hsadmin.remote; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.xmlrpc.XmlRpcException; +import org.apache.xmlrpc.client.XmlRpcClient; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class EMailAliasTest { + + private static final String MODULE = "emailalias"; + + private XmlRpcClient client; + private RemoteCASHelper cas; + + @Before + public void setUp() throws Exception { + client = RemoteTestHelper.getClient(); + cas = new RemoteCASHelper(); + } + + @After + public void tearDown() throws Exception { + client = null; + cas = null; + } + + @Test + public void testSearchAllAsPacAdmin() { + String user = "peh00"; + String grantingTicketURL = cas.getGrantingTicketURL(user); + Map whereParams = new HashMap(); + Object[] params = new Object[] { user, + cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), + whereParams }; + try { + Object execute = client.execute(MODULE + ".search", params); + Object[] result = (Object[]) execute; + assertEquals(262, result.length); + for (Object o : result) { + if (o instanceof Map) { + Map row = (Map) o; + assertEquals("peh00", row.get("pac")); + } else { + fail("map expected"); + } + } + } catch (XmlRpcException e) { + fail(e.getMessage()); + } + } + +} diff --git a/hsarback/test/de/hsadmin/remote/QueueTaskTest.java b/hsarback/test/de/hsadmin/remote/QueueTaskTest.java index 7238fc1..f914f83 100644 --- a/hsarback/test/de/hsadmin/remote/QueueTaskTest.java +++ b/hsarback/test/de/hsadmin/remote/QueueTaskTest.java @@ -39,7 +39,7 @@ public class QueueTaskTest { String grantingTicketURL = cas.getGrantingTicketURL(user); Map whereParams = new HashMap(); Object[] params = new Object[] { user, - cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.BACKEND_URL), + cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), whereParams }; try { Object execute = client.execute(MODULE + ".search", params); @@ -67,7 +67,7 @@ public class QueueTaskTest { setParams.put("details", "Test"); whereParams.put("user", "peh00"); Object[] params = new Object[] { user, - cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.BACKEND_URL), + cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), setParams, whereParams }; try { Object execute = client.execute(MODULE + ".update", params); @@ -93,7 +93,7 @@ public class QueueTaskTest { setParams.put("details", "Blupp"); setParams.put("exception", "f6n"); Object[] params = new Object[] { user, - cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.BACKEND_URL), + cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), setParams }; try { Object execute = client.execute(MODULE + ".add", params); @@ -111,7 +111,7 @@ public class QueueTaskTest { Map whereParams = new HashMap(); whereParams.put("user", "peh00"); Object[] params = new Object[] { user, - cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.BACKEND_URL), + cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), whereParams }; try { Object execute = client.execute(MODULE + ".delete", params); diff --git a/hsarback/test/de/hsadmin/remote/RemoteCASHelper.java b/hsarback/test/de/hsadmin/remote/RemoteCASHelper.java index 9658d77..1a3eaf5 100644 --- a/hsarback/test/de/hsadmin/remote/RemoteCASHelper.java +++ b/hsarback/test/de/hsadmin/remote/RemoteCASHelper.java @@ -2,40 +2,33 @@ package de.hsadmin.remote; import java.io.BufferedReader; import java.io.BufferedWriter; -import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.net.URL; import java.net.URLEncoder; -import java.util.Properties; import javax.net.ssl.HttpsURLConnection; +import de.hsadmin.core.util.Config; + public class RemoteCASHelper { private static String LOGIN_URL = "https://login.hostsharing.net:443/cas/v1/tickets"; private String loginURL; - private Properties config; public RemoteCASHelper() { initConfig(); } private void initConfig() { - config = new Properties(); - try { - config.load(new FileInputStream( new File(System.getProperty("user.home"), ".hsadmin.conf"))); - } catch (IOException e) { - } - loginURL = config.getProperty("loginURL", LOGIN_URL); + loginURL = Config.getInstance().getProperty("loginURL", LOGIN_URL); } public String getGrantingTicketURL(String user) { - String pw = config.getProperty(user + ".passWord", "-"); + String pw = Config.getInstance().getProperty(user + ".passWord", "-"); try { String encodedParams = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(user, "UTF-8") diff --git a/hsarback/test/de/hsadmin/remote/RemoteTest.java b/hsarback/test/de/hsadmin/remote/RemoteTest.java index 55c8bba..bfeb293 100644 --- a/hsarback/test/de/hsadmin/remote/RemoteTest.java +++ b/hsarback/test/de/hsadmin/remote/RemoteTest.java @@ -10,8 +10,9 @@ import org.junit.runners.Suite; // MySqlDatabaseTest.class, // MySqlUserTest.class, // PgSqlUserTest.class, -// EMailAddressTest.class, -// DomainTest.class, + EMailAliasTest.class, + EMailAddressTest.class, + DomainTest.class, // HostmasterTest.class, QueueTaskTest.class }) diff --git a/hsarback/test/de/hsadmin/remote/RemoteTestHelper.java b/hsarback/test/de/hsadmin/remote/RemoteTestHelper.java index 2b3a18a..764f73c 100644 --- a/hsarback/test/de/hsadmin/remote/RemoteTestHelper.java +++ b/hsarback/test/de/hsadmin/remote/RemoteTestHelper.java @@ -5,16 +5,19 @@ import java.net.URL; import org.apache.xmlrpc.client.XmlRpcClient; import org.apache.xmlrpc.client.XmlRpcClientConfigImpl; +import de.hsadmin.core.util.Config; + public class RemoteTestHelper { - public static String BACKEND_URL = "https://agnes.ostwall195.de:9443/hsar/backend"; + private static final String XMLRPC_URL = "https://admin.hostsharing.net:443/hsar/xmlrpc/hsadmin"; + private static final String BACKEND_URL = "https://admin.hostsharing.net:443/hsar/backend"; private static XmlRpcClient client; public static XmlRpcClient getClient() throws Exception { if (client == null) { XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl(); - config.setServerURL(new URL("https://agnes.ostwall195.de:9443/hsar/xmlrpc/hsadmin")); + config.setServerURL(new URL(getXmlrpcURL())); config.setEnabledForExtensions(true); client = new XmlRpcClient(); client.setConfig(config); @@ -22,4 +25,11 @@ public class RemoteTestHelper { return client; } + public static String getBackendURL() { + return Config.getInstance().getProperty("backendURL", BACKEND_URL); + } + + public static String getXmlrpcURL() { + return Config.getInstance().getProperty("xmlrpcURL", XMLRPC_URL); + } } diff --git a/hsarback/test/de/hsadmin/remote/UnixUserTest.java b/hsarback/test/de/hsadmin/remote/UnixUserTest.java index 8650db2..c9ffcee 100644 --- a/hsarback/test/de/hsadmin/remote/UnixUserTest.java +++ b/hsarback/test/de/hsadmin/remote/UnixUserTest.java @@ -38,7 +38,7 @@ public class UnixUserTest { String grantingTicketURL = cas.getGrantingTicketURL(user); Map whereParams = new HashMap(); Object[] params = new Object[] { user, - cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.BACKEND_URL), + cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), whereParams }; try { Object execute = client.execute(MODULE + ".search", params); @@ -69,7 +69,7 @@ public class UnixUserTest { setParams.put("quota", "128"); setParams.put("quotalimit", "192"); Object[] params = new Object[] { user, - cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.BACKEND_URL), + cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), setParams }; Object execute = client.execute(MODULE + ".add", params); if (execute instanceof Map) { @@ -96,7 +96,7 @@ public class UnixUserTest { setParams.put("name", "peh01-testfail"); setParams.put("password", "test123"); Object[] params = new Object[] { user, - cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.BACKEND_URL), + cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), setParams }; Object execute = client.execute(MODULE + ".add", params); assertNull(execute); @@ -117,7 +117,7 @@ public class UnixUserTest { setParams.put("name", "peh00-langer-name"); setParams.put("password", "test123"); Object[] params = new Object[] { user, - cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.BACKEND_URL), + cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), setParams }; Object execute = client.execute(MODULE + ".add", params); assertNull(execute); @@ -139,7 +139,7 @@ public class UnixUserTest { whereParams.put("name", "peh00-test2"); setParams.put("password", "test1234"); Object[] params = new Object[] { user, - cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.BACKEND_URL), + cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), setParams, whereParams }; Object execute = client.execute(MODULE + ".update", params); if (execute instanceof Object[]) { @@ -171,7 +171,7 @@ public class UnixUserTest { Map whereParams = new HashMap(); whereParams.put("name", "peh00-langer-name"); Object[] params = new Object[] { user, - cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.BACKEND_URL), + cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), whereParams }; Object execute = client.execute(MODULE + ".delete", params); assertNull(execute); @@ -190,7 +190,7 @@ public class UnixUserTest { Map whereParams = new HashMap(); whereParams.put("name", "peh00-test2"); Object[] params = new Object[] { user, - cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.BACKEND_URL), + cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), whereParams }; Object execute = client.execute(MODULE + ".delete", params); assertNull(execute); @@ -209,7 +209,7 @@ public class UnixUserTest { Map whereParams = new HashMap(); whereParams.put("name", "peh00-test2"); Object[] params = new Object[] { user, - cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.BACKEND_URL), + cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), whereParams }; Object execute = client.execute(MODULE + ".delete", params); assertNull(execute); @@ -226,7 +226,7 @@ public class UnixUserTest { String grantingTicketURL = cas.getGrantingTicketURL(user); Map whereParams = new HashMap(); Object[] params = new Object[] { user, - cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.BACKEND_URL), + cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), whereParams }; Object execute = client.execute(MODULE + ".search", params); Object[] result = (Object[]) execute; diff --git a/hsarback/webapp/WEB-INF/web.xml b/hsarback/webapp/WEB-INF/web.xml index 1eccbbb..521fa45 100644 --- a/hsarback/webapp/WEB-INF/web.xml +++ b/hsarback/webapp/WEB-INF/web.xml @@ -27,7 +27,7 @@ Components - user,member,contact,bankaccount,pac,paccomponent,hive,ipaddr,basepac,basecomponent,component,q + user,domain,member,contact,bankaccount,emailaddress,emailalias,pac,paccomponent,hive,ipaddr,basepac,basecomponent,component,q ComponentClass_user @@ -37,6 +37,14 @@ ComponentDescription_user Unix User + + ComponentClass_domain + de.hsadmin.mods.dom.Domain + + + ComponentDescription_domain + Domains + ComponentClass_member de.hsadmin.mods.cust.Customer @@ -61,6 +69,22 @@ ComponentDescription_bankaccount Bankverbindungen + + ComponentClass_emailaddress + de.hsadmin.mods.email.EMailAddress + + + ComponentDescription_emailaddress + e-Mail Adressen + + + ComponentClass_emailalias + de.hsadmin.mods.email.EMailAlias + + + ComponentDescription_emailalias + e-Mail Aliases + ComponentClass_pac de.hsadmin.mods.pac.Pac