From 6bda687298b25d25c962084216711900227a9461 Mon Sep 17 00:00:00 2001
From: Purodha Blissenbach <purodha.blissenbach@hostsharing.net>
Date: Fri, 21 Sep 2012 15:18:30 +0200
Subject: [PATCH] Merge branch 'master' of ssh://hsh04-source@hsh04.hostsharing.net/home/doms/source.hostsharing.net/source/hsadmin.git

---
 hsarback/src/de/hsadmin/mods/user/UnixUser.java              |    2 
 hsarback/src/de/hsadmin/remote/PacRemote.java                |   66 ++++++++++++++-------
 hsarback/src/de/hsadmin/mods/pac/Pac.java                    |    2 
 hsarback/build.xml                                           |    2 
 hsarback/src/de/hsadmin/mods/dom/DomainProcessorFactory.java |   38 +++++++++---
 hsarback/src/de/hsadmin/mods/pac/PacModuleImpl.java          |   26 ++++++++
 6 files changed, 99 insertions(+), 37 deletions(-)

diff --git a/hsarback/build.xml b/hsarback/build.xml
index 80c9b57..a6bb7e4 100644
--- a/hsarback/build.xml
+++ b/hsarback/build.xml
@@ -39,7 +39,7 @@
 	<target name="deploy" depends="war">
 		<delete dir="${hsar.deploy.dir}/hsar" />
 		<copy file="build/hsar.war" todir="${hsar.deploy.dir}" />
-		<sleep minutes="2"/>
+		<!-- <sleep minutes="2"/> -->
 	</target>
 
 	<target name="jar" depends="enhance">
diff --git a/hsarback/src/de/hsadmin/mods/dom/DomainProcessorFactory.java b/hsarback/src/de/hsadmin/mods/dom/DomainProcessorFactory.java
index dc7652f..be6a826 100644
--- a/hsarback/src/de/hsadmin/mods/dom/DomainProcessorFactory.java
+++ b/hsarback/src/de/hsadmin/mods/dom/DomainProcessorFactory.java
@@ -27,8 +27,13 @@
 
 public class DomainProcessorFactory implements EntityProcessorFactory {
 
-	private static final String[] DW_STRUCTURE = new String[] { "htdocs", "htdocs-ssl", "subs", "subs/www", "subs-ssl", "subs-ssl/www", "cgi", "fastcgi", "cgi-ssl", "fastcgi-ssl", "etc", "var" };
-	private static final String[] SW_STRUCTURE = new String[] { "htdocs", "htdocs-ssl", "subs", "subs/www", "subs-ssl", "subs-ssl/www", "etc", "var" };
+	private static final String[] DW_STRUCTURE = new String[] { 
+		"htdocs", "htdocs-ssl", "subs", "subs/www", "subs-ssl", "subs-ssl/www", "cgi", "fastcgi", "cgi-ssl", "fastcgi-ssl", "etc", "var" 
+	};
+	
+	private static final String[] SW_STRUCTURE = new String[] { 
+		"htdocs", "htdocs-ssl", "subs", "subs/www", "subs-ssl", "subs-ssl/www", "etc", "var" 
+	};
 
 	public <T extends AbstractEntity> Processor createCreateProcessor(EntityManager em, T entity) throws ProcessorException {
 		String hiveName = entity.getHiveName();
@@ -370,17 +375,32 @@
 		String domDir = pacDir + "/doms/" + pac.getName() + ".hostsharing.net"; 
 		String[] sourceDirs = new String[] { "web", "web-ssl", "cgi", "cgi-ssl", "fastcgi", "fastcgi-ssl" } ;
 		String[] targetDirs = new String[] { "htdocs", "htdocs-ssl", "cgi", "cgi-ssl", "fastcgi", "fastcgi-ssl" } ;
-		CompoundProcessor processor = new CompoundProcessor(
-			new ShellProcessor("rm -rf " + domDir + "/subs " + domDir + "/htdocs/.htaccess " 
-										 + domDir + "/subs-ssl " + domDir + "/htdocs-ssl/.htaccess "));
+		CompoundProcessor processor = new CompoundProcessor();
+		for (int idx = 0; idx < targetDirs.length; idx++) {
+			processor.appendProcessor(
+					new ShellProcessor("rm -rf " + domDir + targetDirs[idx]));
+		}
 		for (int idx = 0; idx < sourceDirs.length; idx++) {
 			processor.appendProcessor(
 				new ShellProcessor("shopt -s dotglob && ls " + pacDir + "/" + sourceDirs[idx] + " >/dev/null 2>&1" +
-						" && mv " + pacDir + "/" + sourceDirs[idx] + "/* " + domDir + "/" + targetDirs[idx] + "/ " +
-						" && ( rmdir " + pacDir + "/" + sourceDirs[idx] + " || rm " + pacDir + "/" + sourceDirs[idx] + " ) " +
+						" && mv " + pacDir + "/" + sourceDirs[idx] + domDir + "/" + targetDirs[idx] +
 						" || echo 'directory " + pacDir + "/" + sourceDirs[idx] + " not found'"));
-		}
-		return processor;
+		}		
+		processor.appendProcessor(
+				new ShellProcessor("test -L " + domDir + "/htdocs" +
+						" && `stat -c '%N' " + domDir + "/htdocs |sed -e's/^.*`//' -e\"s/'$//\"` = web-ssl " +
+						" && cd " + domDir +
+						" && rm " + domDir + "/htdocs" +
+						" && ln -sf htdocs-ssl htdocs" +
+						" && chown -h " + pac + ":" + pac + " " + domDir + "/htdocs"));
+		processor.appendProcessor(
+				new ShellProcessor("test -L " + domDir + "/htdocs-ssl" +
+						" && `stat -c '%N' " + domDir + "/htdocs-ssl |sed -e's/^.*`//' -e\"s/'$//\"` = web " +
+						" && cd " + domDir +
+						" && rm " + domDir + "/htdocs-ssl" +
+						" && ln -sf htdocs htdocs-ssl" +
+						" && chown -h " + pac + ":" + pac + " " + domDir + "/htdocs-ssl"));
+		return processor;		
 	}
 
 	private String selectVHostTemplate(Domain dom) {
diff --git a/hsarback/src/de/hsadmin/mods/pac/Pac.java b/hsarback/src/de/hsadmin/mods/pac/Pac.java
index 28b8532..746811c 100644
--- a/hsarback/src/de/hsadmin/mods/pac/Pac.java
+++ b/hsarback/src/de/hsadmin/mods/pac/Pac.java
@@ -75,7 +75,7 @@
 	@ManyToOne(fetch = EAGER)
 	private INetAddress oldINetAddr;
 
-	@OneToMany(fetch = LAZY, cascade = ALL, mappedBy="pac")
+	@OneToMany(fetch = EAGER, cascade = ALL, mappedBy="pac")
 	private Set<PacComponent> pacComponents;
 
 	@OneToMany(fetch = LAZY, cascade = ALL, mappedBy="pac")
diff --git a/hsarback/src/de/hsadmin/mods/pac/PacModuleImpl.java b/hsarback/src/de/hsadmin/mods/pac/PacModuleImpl.java
index c3d848c..32ae06f 100644
--- a/hsarback/src/de/hsadmin/mods/pac/PacModuleImpl.java
+++ b/hsarback/src/de/hsadmin/mods/pac/PacModuleImpl.java
@@ -110,9 +110,16 @@
 	}
 
 	@Override
-	public AbstractEntity update(AbstractEntity existingEntity)
+	public AbstractEntity update(AbstractEntity entity)
 			throws HSAdminException {
-		throw new AuthorisationException(getTransaction().getLoginUser(), "update", existingEntity);
+		UnixUser loginUser = getTransaction().getLoginUser();
+		if (entity instanceof Pac) {
+			Pac pac = (Pac) entity;
+		} else {
+			throw new AuthorisationException(loginUser, "update", entity);
+		}
+		needsWriteAccessOn(entity, "update");
+		return super.update(entity);
 	}
 
 	@Override
@@ -154,4 +161,19 @@
 		super.delete(existingEntity);
 	}
 	
+	private void needsWriteAccessOn(AbstractEntity entity, String method) throws AuthorisationException {
+		UnixUser loginUser = getTransaction().getLoginUser();
+		if (entity instanceof Pac) {
+			Pac pac = (Pac) entity;
+			String aLoginUserName = loginUser.getName();
+			boolean isPacAdmin = loginUser.hasPacAdminRoleFor(pac);
+			boolean isCustomer = aLoginUserName.equals(pac.getCustomer().getName());
+			boolean isHostmaster = loginUser.hasHostmasterRole();
+			if (!isPacAdmin && !isCustomer && !isHostmaster) {
+				throw new AuthorisationException(loginUser, method, pac);
+			}
+		} else {
+			throw new AuthorisationException(loginUser, method, entity);
+		}
+	}
 }
diff --git a/hsarback/src/de/hsadmin/mods/user/UnixUser.java b/hsarback/src/de/hsadmin/mods/user/UnixUser.java
index a23b060..3a60bad 100644
--- a/hsarback/src/de/hsadmin/mods/user/UnixUser.java
+++ b/hsarback/src/de/hsadmin/mods/user/UnixUser.java
@@ -275,7 +275,7 @@
 		return getName().equals(cust.getName()) || hasHostmasterRole();
 	}
 
-	public boolean hasPacAdminRoleFor(de.hsadmin.mods.pac.Pac pac) {
+	public boolean hasPacAdminRoleFor(Pac pac) {
 		return pac != null && 
 			(pac.getName().equals(getName())
 				|| hasCustomerRoleFor(pac.getCustomer()) );
diff --git a/hsarback/src/de/hsadmin/remote/PacRemote.java b/hsarback/src/de/hsadmin/remote/PacRemote.java
index cb6ca52..9008a76 100644
--- a/hsarback/src/de/hsadmin/remote/PacRemote.java
+++ b/hsarback/src/de/hsadmin/remote/PacRemote.java
@@ -1,10 +1,9 @@
 package de.hsadmin.remote;
 
-import java.util.Comparator;
 import java.util.Date;
+import java.util.HashMap;
 import java.util.Map;
-import java.util.SortedSet;
-import java.util.TreeSet;
+import java.util.Set;
 
 import de.hsadmin.core.model.AbstractEntity;
 import de.hsadmin.core.util.TextUtil;
@@ -27,27 +26,38 @@
 		resultMap.put("curinetaddr", pac.getCurINetAddr().getInetAddr());
 		resultMap.put("created", TextUtil.format(pac.getCreated()));
 		resultMap.put("basepac", pac.getBasepac().getName());
-		SortedSet<PacComponent> sortedComponents = new TreeSet<PacComponent>(new Comparator<PacComponent>() {
-			@Override
-			public int compare(PacComponent o1, PacComponent o2) {
-				return o2.getBaseComponent().getSorting() - o1.getBaseComponent().getSorting();
-			}
-		});
-		sortedComponents.addAll(pac.getPacComponents());
-		StringBuffer bf = new StringBuffer();
-		boolean empty = true;
-		for (PacComponent comp : sortedComponents) {
-			if (comp.getQuantity() > 0) {
-				if (!empty) {
-					bf.append("; ");
-				}
-				bf.append(comp.getBaseComponent().getFeature());
-				bf.append("=");
-				bf.append(comp.getQuantity());
-				empty = false;
+		
+		Map<String, Object> components = new HashMap<String, Object>();
+		Set<PacComponent> pacComponents = pac.getPacComponents();
+		for (PacComponent comp : pacComponents) {
+			int quantity = comp.getQuantity();
+			if (quantity > 0) {
+				components.put(comp.getBaseComponent().getFeature(), Integer.toString(quantity));
 			}
 		}
-		resultMap.put("components", bf.toString());
+		resultMap.put("components", components);
+		
+//		SortedSet<PacComponent> sortedComponents = new TreeSet<PacComponent>(new Comparator<PacComponent>() {
+//			@Override
+//			public int compare(PacComponent o1, PacComponent o2) {
+//				return o2.getBaseComponent().getSorting() - o1.getBaseComponent().getSorting();
+//			}
+//		});
+//		sortedComponents.addAll(pac.getPacComponents());
+//		StringBuffer bf = new StringBuffer();
+//		boolean empty = true;
+//		for (PacComponent comp : sortedComponents) {
+//			if (comp.getQuantity() > 0) {
+//				if (!empty) {
+//					bf.append("; ");
+//				}
+//				bf.append(comp.getBaseComponent().getFeature());
+//				bf.append("=");
+//				bf.append(comp.getQuantity());
+//				empty = false;
+//			}
+//		}
+//		resultMap.put("components", bf.toString());
 	}
 
 	@Override
@@ -86,7 +96,17 @@
 			hive.setName(hiveName);
 			pac.setHive(hive);
 		}
-		pac.setName((String) setParams.get("name"));
+		Object componentsObj = setParams.get("components");
+		if (componentsObj != null && componentsObj instanceof Map) {
+			Map<?, ?> componentsMap = (Map<?, ?>) componentsObj;
+			for (Object key : componentsMap.keySet()) {
+				pac.getPacComponent(key.toString()).setQuantity(Integer.parseInt(componentsMap.get(key).toString()));
+			}
+		}
+		Object nameParamObj = setParams.get("name");
+		if (nameParamObj != null && nameParamObj instanceof String) {
+			pac.setName((String) nameParamObj);
+		}
 	}
 
 	@Override

--
Gitblit v1.9.0-SNAPSHOT