Implement removeUserData API (adrieder@sbox.tugraz.at, Request #8452).
authorJan Schneider <jan@horde.org>
Fri, 24 Jul 2009 08:55:31 +0000 (10:55 +0200)
committerJan Schneider <jan@horde.org>
Fri, 24 Jul 2009 08:55:31 +0000 (10:55 +0200)
ingo/docs/CHANGES
ingo/lib/Storage.php
ingo/lib/Storage/Sql.php
ingo/lib/api.php

index 7ee6785..081a062 100644 (file)
@@ -10,6 +10,7 @@ v2.0-git
 v1.2.2-cvs
 ----------
 
+[jan] Implement removeUserData API (adrieder@sbox.tugraz.at, Request #8452).
 [jan] Take default settings for forwards, vacation and spam rules from
       config/prefs.php in the SQL storage driver.
 [jan] No longer try to read spam folder from IMP's preferences.
index 8e908a4..0ffd753 100644 (file)
@@ -364,4 +364,17 @@ class Ingo_Storage
         return $ob;
     }
 
+    /**
+     * Removes the user data from the storage backend.
+     * Stub for child class to override if it can implement.
+     *
+     * @param string $user The user name to delete filters for.
+     *
+     * @return mixed  True | PEAR_Error
+     */
+    function removeUserData($user)
+    {
+       return PEAR::raiseError(_("Removing user data is not supported with the current filter storage backend."));
+    }
+
 }
index 137fa03..ca556bc 100644 (file)
@@ -395,4 +395,40 @@ class Ingo_Storage_Sql extends Ingo_Storage
         return $ret;
     }
 
+    /**
+     * Removes the data of the specified user from the storage backend.
+     *
+     * @param string $user  The user name to delete filters for.
+     *
+     * @return mixed  True | PEAR_Error
+     */
+    function removeUserData($user)
+    {
+        if (!Horde_Auth::isAdmin() && $user != Horde_Auth::getAuth()) {
+            return PEAR::raiseError(_("Permission Denied"));
+        }
+
+        $queries = array(sprintf('DELETE FROM %s WHERE rule_owner = ?',
+                                 $this->_params['table_rules']),
+                         sprintf('DELETE FROM %s WHERE list_owner = ?',
+                                 $this->_params['table_lists']),
+                         sprintf('DELETE FROM %s WHERE vacation_owner = ?',
+                                 $this->_params['table_vacations']),
+                         sprintf('DELETE FROM %s WHERE forward_owner = ?',
+                                 $this->_params['table_forwards']),
+                         sprintf('DELETE FROM %s WHERE spam_owner = ?',
+                                 $this->_params['table_spam']));
+
+        $values = array($user);
+        foreach ($queries as $query) {
+            Horde::logMessage('Ingo_Storage_sql::removeUserData(): ' . $query, __FILE__, __LINE__, PEAR_LOG_DEBUG);
+            $result = $this->_write_db->query($query, $values);
+            if (is_a($result, 'PEAR_Error')) {
+                return $result;
+            }
+        }
+
+        return $true;
+    }
+
 }
index ca29971..43b566a 100644 (file)
@@ -13,6 +13,11 @@ $_services['perms'] = array(
     'args' => array(),
     'type' => '{urn:horde}stringArray');
 
+$_services['removeUserData'] = array(
+    'args' => array('user' => 'string'),
+    'type' => 'boolean'
+);
+
 $_services['blacklistFrom'] = array(
     'args' => array('addresses' => '{urn:horde}stringArray'),
     'type' => 'boolean',
@@ -85,6 +90,68 @@ function _ingo_perms()
 }
 
 /**
+ * Removes user data.
+ *
+ * @param string $user  Name of user to remove data for.
+ *
+ * @return mixed  true on success | PEAR_Error on failure
+ */
+function _ingo_removeUserData($user) {
+
+    if (!Horde_Auth::isAdmin() && $user != Horde_Auth::getAuth()) {
+        return PEAR::raiseError(_("You are not allowed to remove user data."));
+    }
+
+    require_once dirname(__FILE__) . '/../lib/base.php';
+
+    /* Remove all filters/rules owned by the user. */
+    $result = $GLOBALS['ingo_storage']->removeUserData($user);
+    if (is_a($result, 'PEAR_Error')) {
+        Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR);
+        return $result;
+    }
+
+    /* Now remove all shares owned by the user. */
+    if (!empty($GLOBALS['ingo_shares'])) {
+        /* Get the user's default share. */
+        $share = $GLOBALS['ingo_shares']->getShare($user);
+        if (is_a($share, 'PEAR_Error')) {
+            Horde::logMessage($share, __FILE__, __LINE__, PEAR_LOG_ERR);
+            return $share;
+        } else {
+            $result = $GLOBALS['ingo_shares']->removeShare($share);
+            if (is_a($result, 'PEAR_Error')) {
+                Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR);
+                return $result;
+            }
+        }
+
+        /* Get a list of all shares this user has perms to and remove the
+         * perms. */
+        $shares = $GLOBALS['ingo_shares']->listShares($user);
+        if (is_a($shares, 'PEAR_Error')) {
+            Horde::logMessage($shares, __FILE__, __LINE__, PEAR_LOG_ERR);
+        }
+        foreach ($shares as $share) {
+            $share->removeUser($user);
+        }
+
+        /* Get a list of all shares this user owns and has perms to delete and
+         * remove them. */
+        $shares = $GLOBALS['ingo_shares']->listShares($user, PERMS_DELETE, $user);
+        if (is_a($shares, 'PEAR_Error')) {
+            Horde::logMessage($shares, __FILE__, __LINE__, PEAR_LOG_ERR);
+            return $shares;
+        }
+        foreach ($shares as $share) {
+            $GLOBALS['ingo_shares']->removeShare($share);
+        }
+    }
+
+    return true;
+}
+
+/**
  * Add addresses to the blacklist.
  *
  * @param string $addresses  The addresses to add to the blacklist.