From a50332ef341fa29c7acda0ed3e449a207ef0584d Mon Sep 17 00:00:00 2001 From: Jan Schneider Date: Fri, 24 Jul 2009 10:55:31 +0200 Subject: [PATCH] Implement removeUserData API (adrieder@sbox.tugraz.at, Request #8452). --- ingo/docs/CHANGES | 1 + ingo/lib/Storage.php | 13 ++++++++++ ingo/lib/Storage/Sql.php | 36 ++++++++++++++++++++++++++ ingo/lib/api.php | 67 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 117 insertions(+) diff --git a/ingo/docs/CHANGES b/ingo/docs/CHANGES index 7ee678518..081a06292 100644 --- a/ingo/docs/CHANGES +++ b/ingo/docs/CHANGES @@ -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. diff --git a/ingo/lib/Storage.php b/ingo/lib/Storage.php index 8e908a454..0ffd7534c 100644 --- a/ingo/lib/Storage.php +++ b/ingo/lib/Storage.php @@ -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.")); + } + } diff --git a/ingo/lib/Storage/Sql.php b/ingo/lib/Storage/Sql.php index 137fa037b..ca556bc9d 100644 --- a/ingo/lib/Storage/Sql.php +++ b/ingo/lib/Storage/Sql.php @@ -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; + } + } diff --git a/ingo/lib/api.php b/ingo/lib/api.php index ca2997143..43b566a60 100644 --- a/ingo/lib/api.php +++ b/ingo/lib/api.php @@ -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. -- 2.11.0