From d6e6528fffe519595a21134d0c91fc34763a4d91 Mon Sep 17 00:00:00 2001
From: Sergio Cambra <sergio@programatica.es>
Date: Fri, 4 Jul 2014 12:33:31 +0200
Subject: [PATCH] set webdav sys_groupid onAfterInsert, it didn't work
 onBeforeInsert. Change server_id and sys_groupid if parent_domain_id changes

---
 interface/web/sites/webdav_user_edit.php | 30 +++++++++++++++++-------
 1 file changed, 21 insertions(+), 9 deletions(-)

diff --git a/interface/web/sites/webdav_user_edit.php b/interface/web/sites/webdav_user_edit.php
index 97b2bcc852..56b019c000 100644
--- a/interface/web/sites/webdav_user_edit.php
+++ b/interface/web/sites/webdav_user_edit.php
@@ -139,15 +139,6 @@ class page_action extends tform_actions {
 			 */
 			$hash = md5($this->dataRecord["username"] . ':' . $this->dataRecord["dir"] . ':' . $this->dataRecord["password"]);
 			$this->dataRecord["password"] = $hash;
-
-			/*
-			*  Get the data of the domain, owning the webdav user
-			*/
-			$web = $app->db->queryOneRecord("SELECT * FROM web_domain WHERE domain_id = ".$app->functions->intval($this->dataRecord["parent_domain_id"]));
-			/* The server is the server of the domain */
-			$this->dataRecord["server_id"] = $web["server_id"];
-			/* The Webdav user shall be owned by the same group then the website */
-			$this->dataRecord["sys_groupid"] = $web['sys_groupid'];
 		}
 
 		parent::onBeforeInsert();
@@ -155,6 +146,15 @@ class page_action extends tform_actions {
 
 	function onAfterInsert() {
 		global $app, $conf;
+
+		$web = $app->db->queryOneRecord("SELECT * FROM web_domain WHERE domain_id = ".$app->functions->intval($this->dataRecord["parent_domain_id"]));
+		$server_id = $app->functions->intval($web["server_id"]);
+
+		// The webdav user shall be owned by the same group then the website
+		$sys_groupid = $app->functions->intval($web['sys_groupid']);
+
+		$sql = "UPDATE webdav_user SET server_id = $server_id, sys_groupid = '$sys_groupid' WHERE webdav_user_id = ".$this->id;
+		$app->db->query($sql);
 	}
 
 	function onBeforeUpdate() {
@@ -184,6 +184,18 @@ class page_action extends tform_actions {
 
 	function onAfterUpdate() {
 		global $app, $conf;
+
+		//* When the site of the webdav user has been changed
+		if(isset($this->dataRecord['parent_domain_id']) && $this->oldDataRecord['parent_domain_id'] != $this->dataRecord['parent_domain_id']) {
+			$web = $app->db->queryOneRecord("SELECT * FROM web_domain WHERE domain_id = ".$app->functions->intval($this->dataRecord["parent_domain_id"]));
+			$server_id = $app->functions->intval($web["server_id"]);
+
+			// The webdav user shall be owned by the same group then the website
+			$sys_groupid = $app->functions->intval($web['sys_groupid']);
+
+			$sql = "UPDATE webdav_user SET server_id = $server_id, sys_groupid = '$sys_groupid' WHERE webdav_user_id = ".$this->id;
+			$app->db->query($sql);
+		}
 	}
 
 }
-- 
GitLab