2010-10-05 21:42:07 +02:00
|
|
|
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;
|
2011-08-12 16:19:21 +02:00
|
|
|
import de.hsadmin.core.model.Transaction;
|
2010-10-05 21:42:07 +02:00
|
|
|
import de.hsadmin.mods.dom.Domain;
|
2011-08-12 16:19:21 +02:00
|
|
|
import de.hsadmin.mods.user.UnixUser;
|
2010-10-05 21:42:07 +02:00
|
|
|
|
|
|
|
public class EMailAddressModuleImpl extends AbstractModuleImpl {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<AbstractEntity> search(Class<? extends AbstractEntity> 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 {
|
2012-01-04 17:02:22 +01:00
|
|
|
Transaction tx = getTransaction();
|
|
|
|
EntityManager em = tx.getEntityManager();
|
2010-10-05 21:42:07 +02:00
|
|
|
EMailAddress adr = (EMailAddress) newEntity;
|
|
|
|
if (adr.getTarget() == null || adr.getTarget().length() == 0) {
|
|
|
|
throw new HSAdminException("target required");
|
|
|
|
}
|
2011-03-22 23:02:06 +01:00
|
|
|
if (adr.getLocalpart() == null) {
|
|
|
|
adr.setLocalpart("");
|
2010-10-05 21:42:07 +02:00
|
|
|
}
|
2013-01-21 13:40:17 +01:00
|
|
|
if (adr.getSubdomain() == null) {
|
|
|
|
adr.setSubdomain("");
|
|
|
|
}
|
2010-10-05 21:42:07 +02:00
|
|
|
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);
|
2012-01-04 17:02:22 +01:00
|
|
|
UnixUser loginUser = tx.getLoginUser();
|
|
|
|
if (dom.isPacDomain() && !loginUser.hasHostmasterRole()) {
|
|
|
|
throw new AuthorisationException(loginUser, "add", adr);
|
|
|
|
}
|
2010-10-05 21:42:07 +02:00
|
|
|
return super.add(newEntity);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public AbstractEntity update(AbstractEntity existingEntity) throws HSAdminException {
|
2011-08-12 16:19:21 +02:00
|
|
|
Transaction transaction = getTransaction();
|
|
|
|
UnixUser loginUser = transaction.getLoginUser();
|
2010-10-05 21:42:07 +02:00
|
|
|
EMailAddress detachedAddr = (EMailAddress) existingEntity;
|
2011-08-12 16:19:21 +02:00
|
|
|
EntityManager em = transaction.getEntityManager();
|
2010-10-05 21:42:07 +02:00
|
|
|
EMailAddress attachedAddr = em.find(EMailAddress.class, detachedAddr.getId());
|
2011-03-24 18:18:30 +01:00
|
|
|
String domain = detachedAddr.getDomain().getName();
|
|
|
|
if (domain != null && !domain.equals(attachedAddr.getDomain().getName())) {
|
2010-10-05 21:42:07 +02:00
|
|
|
detachedAddr.setDomain(attachedAddr.getDomain());
|
2011-08-12 16:19:21 +02:00
|
|
|
throw new AuthorisationException(loginUser, "update", detachedAddr, "domain");
|
2010-10-05 21:42:07 +02:00
|
|
|
}
|
|
|
|
String subdomain = detachedAddr.getSubdomain();
|
|
|
|
if (subdomain != null && !subdomain.equals(attachedAddr.getSubdomain())) {
|
|
|
|
detachedAddr.setSubdomain(attachedAddr.getSubdomain());
|
2011-08-12 16:19:21 +02:00
|
|
|
throw new AuthorisationException(loginUser, "update", detachedAddr, "subdomain");
|
2010-10-05 21:42:07 +02:00
|
|
|
}
|
2011-03-22 23:02:06 +01:00
|
|
|
String localPart = detachedAddr.getLocalpart();
|
|
|
|
if (localPart != null && !localPart.equals(attachedAddr.getLocalpart())) {
|
|
|
|
detachedAddr.setLocalpart(attachedAddr.getLocalpart());
|
2011-08-12 16:19:21 +02:00
|
|
|
throw new AuthorisationException(loginUser, "update", detachedAddr, "localpart");
|
2010-10-05 21:42:07 +02:00
|
|
|
}
|
|
|
|
String target = detachedAddr.getTarget();
|
|
|
|
if (target == null) {
|
|
|
|
throw new HSAdminException("target required");
|
|
|
|
}
|
|
|
|
attachedAddr.setTarget(target);
|
|
|
|
return super.update(attachedAddr);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|