hs.hsadmin/hsarback/src/de/hsadmin/mods/email/EMailAlias.java

196 lines
5.1 KiB
Java
Raw Normal View History

2010-10-05 21:42:07 +02:00
package de.hsadmin.mods.email;
import static javax.persistence.GenerationType.SEQUENCE;
import java.io.Serializable;
import javax.persistence.Column;
2011-03-24 18:18:30 +01:00
import javax.persistence.Entity;
2010-10-05 21:42:07 +02:00
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.AbstractEntity;
2011-08-12 17:58:20 +02:00
import de.hsadmin.core.model.AbstractModuleImpl;
2012-07-27 12:54:11 +02:00
import de.hsadmin.core.model.AnnFieldIO;
2012-07-27 11:18:08 +02:00
import de.hsadmin.core.model.AnnModuleImpl;
2012-07-27 12:54:11 +02:00
import de.hsadmin.core.model.ReadWriteAccess;
2010-10-05 21:42:07 +02:00
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")
2010-12-16 18:24:21 +01:00
@SearchFilter("obj.pac = :loginUserPac OR obj.pac.customer.name = :loginUserName")
2012-07-27 11:18:08 +02:00
@AnnModuleImpl(de.hsadmin.mods.email.EMailAliasModuleImpl.class)
2010-10-05 21:42:07 +02:00
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")
@AnnFieldIO(validation="[a-z0-9]*", rw=ReadWriteAccess.READONLY)
2010-10-05 21:42:07 +02:00
private Pac pac;
2012-07-27 12:54:11 +02:00
@AnnFieldIO(validation="[a-z0-9\\_\\-\\.\\+]*", rw=ReadWriteAccess.WRITEONCE)
2010-10-05 21:42:07 +02:00
@Column(updatable=false)
private String name;
2012-07-27 12:54:11 +02:00
@AnnFieldIO(validation="[\\x20-\\x7E]*", rw=ReadWriteAccess.READWRITE)
2010-10-05 21:42:07 +02:00
@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) {
String pacName = name.substring(0, 5);
try {
2011-03-24 18:18:30 +01:00
Entity entityAnnot = Pac.class.getAnnotation(Entity.class);
String queryString = "SELECT obj FROM " + entityAnnot.name() + " obj WHERE " + Pac.createQueryFromStringKey(pacName);
2010-10-05 21:42:07 +02:00
Query query = em.createQuery(queryString);
2011-08-12 17:58:20 +02:00
AbstractModuleImpl.setQueryParameter(query, queryString, "loginUser", loginUser);
AbstractModuleImpl.setQueryParameter(query, queryString, "loginUserName", loginUser.getName());
AbstractModuleImpl.setQueryParameter(query, queryString, "loginUserPac", loginUser.getPac());
2010-10-05 21:42:07 +02:00
pac = (Pac) query.getSingleResult();
} catch (NoResultException exc) {
2011-03-24 18:18:30 +01:00
throw new SecurityException("packet '" + pacName + "' not found or access denied");
2010-10-05 21:42:07 +02:00
}
}
}
public static String createQueryFromStringKey(String humanKey) {
2011-08-29 16:09:20 +02:00
return "obj.name='" + humanKey + "'";
2010-10-05 21:42:07 +02:00
}
@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
2010-12-16 18:24:21 +01:00
"obj.pac.customer.name=:loginUserName OR " +
2010-10-05 21:42:07 +02:00
// all aliases of packet admin
2010-12-16 18:24:21 +01:00
"obj.pac.name=:loginUserName";
2010-10-05 21:42:07 +02:00
}
}