From 6fe5dd9da9109b5df293180bb438c09999ccd8b9 Mon Sep 17 00:00:00 2001 From: Thomas Bruederli Date: Wed, 23 Apr 2014 19:58:16 +0200 Subject: Add utility script to remove a user record from the local database --- bin/deluser.sh | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100755 bin/deluser.sh (limited to 'bin') diff --git a/bin/deluser.sh b/bin/deluser.sh new file mode 100755 index 000000000..84c8446ec --- /dev/null +++ b/bin/deluser.sh @@ -0,0 +1,97 @@ +#!/usr/bin/env php + | + +-----------------------------------------------------------------------+ +*/ + +define('INSTALL_PATH', realpath(dirname(__FILE__) . '/..') . '/' ); + +require_once INSTALL_PATH . 'program/include/clisetup.php'; + +function print_usage() +{ + print "Usage: deluser.sh [--host=mail_host] username\n"; + print "--host=HOST The IMAP hostname or IP the given user is related to\n"; +} + +function _die($msg, $usage=false) +{ + fputs(STDERR, $msg . "\n"); + if ($usage) print_usage(); + exit(1); +} + +$rcmail = rcmail::get_instance(); + +// get arguments +$args = rcube_utils::get_opt(array('h' => 'host')); +$username = trim($args[0]); + +if (empty($username)) { + _die("Missing required parameters", true); +} + +if (empty($args['host'])) { + $hosts = $rcmail->config->get('default_host', ''); + if (is_string($hosts)) { + $args['host'] = $hosts; + } + else if (is_array($hosts) && count($hosts) == 1) { + $args['host'] = reset($hosts); + } + else { + _die("Specify a host name", true); + } +} + +// connect to DB +$db = $rcmail->get_dbh(); +$db->db_connect('w'); + +if (!$db->is_connected() || $db->is_error()) { + _die("No DB connection\n" . $db->is_error()); +} + +// find user in loca database +$user = rcube_user::query($username, $args['host']); + +if (!$user) { + die("User not found.\n"); +} + +// let plugins cleanup their own user-related data +$plugin = $rcmail->plugins->exec_hook('user_delete', array('user' => $user, 'username' => $username, 'host' => $args['host'])); + +if ($plugin['abort']) { + _die("User deletion aborted by plugin"); +} + +// deleting the user record should be sufficient due to ON DELETE CASCADE foreign key references +// but not all database backends actually support this so let's do it by hand +foreach (array('identities','contacts','contactgroups','dictionaries','cache','cache_index','cache_messages','cache_thread','searches','users') as $table) { + $db->query('DELETE FROM ' . $db->table_name($table) . ' WHERE user_id=?', $user->ID); +} + +if ($db->is_error()) { + _die("DB error occurred: " . $db->is_error()); +} +else { + echo "Successfully deleted user $user->ID\n"; +} + -- cgit v1.2.3