egroupware/admin/inc/class.admin_cmd_change_account_id.inc.php

292 lines
8.8 KiB
PHP
Raw Normal View History

<?php
/**
* EGgroupware admin - admin command: change an account_id
*
* @link http://www.egroupware.org
* @author Ralf Becker <RalfBecker-AT-outdoor-training.de>
* @package admin
* @copyright (c) 2007-16 by Ralf Becker <RalfBecker-AT-outdoor-training.de>
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @version $Id$
*/
use EGroupware\Api;
/**
* admin command: change an account_id
*/
class admin_cmd_change_account_id extends admin_cmd
{
/**
* Constructor
*
* @param array $change array with old => new id pairs
*/
function __construct(array $change)
{
2008-10-30 12:35:13 +01:00
if (!isset($change['change']))
{
$change = array(
'change' => $change,
);
}
admin_cmd::__construct($change);
}
/**
* Query changes from all apps
*
* Apps mark columns containing account-ids in "meta" attribute as (account|user|group)[-(abs|commasep|serialized)]
*
* @return array appname => array( table => array(column(s)))
*/
private function get_changes()
{
// happens if one used "root_admin" and config-password
if (empty($GLOBALS['egw_info']['apps']))
{
$apps = new Api\Egw\Applications();
$apps->read_installed_apps();
}
$changes = $setup_info = array();
foreach(array_keys($GLOBALS['egw_info']['apps']) as $app)
{
if (!file_exists($path=EGW_SERVER_ROOT.'/'.$app.'/setup/setup.inc.php') || !include($path)) continue;
foreach((array)$setup_info[$app]['tables'] as $table)
{
if (!($definition = $GLOBALS['egw']->db->get_table_definitions($app, $table))) continue;
$cf = array();
foreach($definition['fd'] as $col => $data)
{
if (!empty($data['meta']))
{
foreach((array)$data['meta'] as $key => $val)
{
list($type, $subtype) = explode('-', $val.'-');
if (in_array($type, array('account', 'user', 'group')))
{
if (!is_numeric($key) || !empty($subtype))
{
$col = array($col);
if (!is_numeric($key)) $col[] = $key;
if (!empty($subtype)) $col['.type'] = $subtype;
}
$changes[$app][$table][] = $col;
}
if (in_array($type, array('cfname', 'cfvalue')))
{
$cf[$type] = $col;
}
}
}
}
// we have a custom field table and cfs containing accounts
if ($cf && !empty($cf['cfname']) && !empty($cf['cfvalue']) &&
($account_cfs = Api\Storage\Customfields::get_account_cfs($app == 'phpgwapi' ? 'addressbook' : $app)))
{
foreach($account_cfs as $type => $names)
{
unset($subtype);
list($type, $subtype) = explode('-', $type);
$col = array($cf['cfvalue']);
if (!empty($subtype)) $col['.type'] = $subtype;
$col[$cf['cfname']] = $names;
$changes[$app][$table][] = $col;
}
}
}
if (isset($changes[$app])) ksort($changes[$app]);
}
ksort($changes);
//print_r($changes);
return $changes;
}
/**
* give or remove run rights from a given account and application
*
* @param boolean $check_only =false only run the checks (and throw the exceptions), but not the command itself
* @return string success message
* @throws Api\Exception\Permission\NoAdmin
* @throws Api\Exception\WrongUserinput(lang("Unknown account: %1 !!!",$this->account),15);
* @throws Api\Exception\WrongUserinput(lang("Application '%1' not found (maybe not installed or misspelled)!",$name),8);
*/
protected function exec($check_only=false)
{
foreach($this->change as $from => $to)
{
if (!(int)$from || !(int)$to)
{
throw new Api\Exception\WrongUserinput(lang("Account-id's have to be integers!"),16);
}
if (($from < 0) != ($to < 0))
{
throw new Api\Exception\WrongUserinput(lang("Can NOT change users into groups, same sign required!"),17);
}
if (!($from_exists = $GLOBALS['egw']->accounts->exists($from)))
{
throw new Api\Exception\WrongUserinput(lang("Source account #%1 does NOT exist!", $from),18);
}
if ($from_exists !== ($from > 0 ? 1 : 2))
{
throw new Api\Exception\WrongUserinput(lang("Group #%1 must have negative sign!", $from),19);
}
if ($GLOBALS['egw']->accounts->exists($to) && !isset($this->change[$to]))
{
throw new Api\Exception\WrongUserinput(lang("Destination account #%1 does exist and is NOT renamed itself! Can not merge Api\Accounts, it will violate unique contains. Delete with transfer of data instead.", $to),20);
}
}
$columns2change = $this->get_changes();
$total = 0;
foreach($columns2change as $app => $data)
{
2010-04-12 08:45:33 +02:00
if (!isset($GLOBALS['egw_info']['apps'][$app])) continue; // $app is not installed
$db = clone($GLOBALS['egw']->db);
$db->set_app($app);
if ($check_only) $db->log_updates = true;
foreach($data as $table => $columns)
{
$db->column_definitions = $db->get_table_definitions($app,$table);
$db->column_definitions = $db->column_definitions['fd'];
if (!$columns)
{
echo "$app: $table no columns with account-id's\n";
continue; // noting to do for this table
}
if (!is_array($columns)) $columns = array($columns);
foreach($columns as $column)
{
$type = $where = null;
if (is_array($column))
{
$type = $column['.type'];
unset($column['.type']);
$where = $column;
$column = array_shift($where);
}
$total += ($changed = self::_update_account_id($this->change,$db,$table,$column,$where,$type));
if (!$check_only && $changed) echo "$app:\t$table.$column $changed id's changed\n";
}
}
}
if (!$check_only)
{
foreach($GLOBALS['egw_info']['apps'] as $app => $data)
{
$total += ($changed = Api\Storage\Customfields::change_account_ids($app, $this->change));
if ($changed) echo "$app:\t$changed id's in definition of private custom fields changed\n";
}
}
if ($total) Api\Cache::flush(Api\Cache::INSTANCE);
return lang("Total of %1 id's changed.",$total)."\n";
}
/**
* Update DB with changed account ids
*
* @param array $ids2change from-id => to-id pairs
* @param Api\Db $db
* @param string $table
* @param string $column
* @param array $where
* @param string $type
* @return int number of changed ids
*/
private static function _update_account_id(array $ids2change,Api\Db $db,$table,$column,array $where=null,$type=null)
{
$update_sql = '';
foreach($ids2change as $from => $to)
{
$update_sql .= "WHEN ".$db->quote($from,$db->column_definitions[$column]['type'])." THEN ".$db->quote($to,$db->column_definitions[$column]['type'])." ";
}
$update_sql .= 'END';
switch($type)
{
case 'commasep':
case 'serialized':
if (!$where) $where = array();
$select = $where;
$select[] = "$column IS NOT NULL";
$select[] = "$column != ''";
$change = array();
foreach($db->select($table,'DISTINCT '.$column,$select,__LINE__,__FILE__) as $row)
{
$ids = $type != 'serialized' ? explode(',',$old_ids=$row[$column]) : json_php_unserialize($old_ids=$row[$column]);
foreach($ids as $key => $id)
{
if (isset($ids2change[$id])) $ids[$key] = $ids2change[$id];
}
$ids2 = $type != 'serialized' ? implode(',',$ids) : serialize($ids);
if ($ids2 != $old_ids)
{
$change[$old_ids] = $ids2;
}
}
$changed = 0;
foreach($change as $from => $to)
{
$db->update($table,array($column=>$to),$where+array($column=>$from),__LINE__,__FILE__);
$changed += $db->affected_rows();
}
break;
case 'abs':
if (!$where) $where = array();
$where[$column] = array();
foreach($ids2change as $from => $to)
{
$where[$column][] = abs($from);
}
$db->update($table,$column.'= CASE '.$column.' '.preg_replace('/-([0-9]+)/','\1',$update_sql),$where,__LINE__,__FILE__);
$changed = $db->affected_rows();
break;
case 'prefs': // prefs groups are shifted down by 2 as -1 and -2 are for default and forced prefs
if (!$where) $where = array();
$where[$column] = array();
$update_sql = '';
foreach($ids2change as $from => $to)
{
if ($from < 0) $from -= 2;
if ($to < 0) $to -= 2;
$where[$column][] = $from;
$update_sql .= 'WHEN '.$db->quote($from,$db->column_definitions[$column]['type']).' THEN '.$db->quote($to,$db->column_definitions[$column]['type']).' ';
}
$db->update($table,$column.'= CASE '.$column.' '.$update_sql.'END',$where,__LINE__,__FILE__);
$changed = $db->affected_rows();
break;
default:
if (!$where) $where = array();
$where[$column] = array_keys($ids2change);
$db->update($table,$column.'= CASE '.$column.' '.$update_sql,$where,__LINE__,__FILE__);
$changed = $db->affected_rows();
break;
}
return $changed;
}
/**
* Return a title / string representation for a given command, eg. to display it
*
* @return string
*/
function __tostring()
{
$change = array();
foreach($this->change as $from => $to)
{
$change[] = $from.'->'.$to;
}
return lang('Change account_id').': '.implode(', ',$change);
}
}