From f021dad204afab2977417265f121d660d3d71252 Mon Sep 17 00:00:00 2001
From: Bjoern Schiessle <schiessle@owncloud.com>
Date: Tue, 29 Oct 2013 15:50:33 +0100
Subject: [PATCH] remove user from cache if he was deleted successfully

---
 lib/private/user.php         | 25 ++++++++++++++++---------
 lib/private/user/manager.php | 14 ++++++++++++++
 2 files changed, 30 insertions(+), 9 deletions(-)

diff --git a/lib/private/user.php b/lib/private/user.php
index 6b350d4cf1..710f0fd66d 100644
--- a/lib/private/user.php
+++ b/lib/private/user.php
@@ -187,18 +187,25 @@ class OC_User {
 	public static function deleteUser($uid) {
 		$user = self::getManager()->get($uid);
 		if ($user) {
-			$user->delete();
+			$result = $user->delete();
 
-			// We have to delete the user from all groups
-			foreach (OC_Group::getUserGroups($uid) as $i) {
-				OC_Group::removeFromGroup($uid, $i);
+			// if delete was successful we clean-up the rest
+			if ($result) {
+
+				// We have to delete the user from all groups
+				foreach (OC_Group::getUserGroups($uid) as $i) {
+					OC_Group::removeFromGroup($uid, $i);
+					}
+				// Delete the user's keys in preferences
+					OC_Preferences::deleteUser($uid);
+
+				// Delete user files in /data/
+				OC_Helper::rmdirr(OC_Config::getValue('datadirectory', OC::$SERVERROOT . '/data') . '/' . $uid . '/');
+
+				// Remove it from the Cache
+				self::getManager()->delete($uid);
 			}
-			// Delete the user's keys in preferences
-			OC_Preferences::deleteUser($uid);
 
-			// Delete user files in /data/
-			OC_Helper::rmdirr(OC_Config::getValue('datadirectory', OC::$SERVERROOT . '/data') . '/' . $uid . '/');
-			
 			return true;
 		} else {
 			return false;
diff --git a/lib/private/user/manager.php b/lib/private/user/manager.php
index 13286bc28a..703c8cd741 100644
--- a/lib/private/user/manager.php
+++ b/lib/private/user/manager.php
@@ -118,6 +118,20 @@ class Manager extends PublicEmitter {
 		return ($user !== null);
 	}
 
+	/**
+	 * remove deleted user from cache
+	 *
+	 * @param string $uid
+	 * @return bool
+	 */
+	public function delete($uid) {
+		if (isset($this->cachedUsers[$uid])) {
+			unset($this->cachedUsers[$uid]);
+			return true;
+		}
+		return false;
+	}
+
 	/**
 	 * Check if the password is valid for the user
 	 *
-- 
GitLab