From d6822d95646cef6fcda6f32e5c99ad61ae5696d4 Mon Sep 17 00:00:00 2001 From: Ralf Becker Date: Sun, 29 Aug 2010 08:16:27 +0000 Subject: [PATCH] - fixed not working accounts::cache_invalidate(): cache is now in regular egw_cache on session level - calling static methods static: accounts::cache_invalidate() or egw::invalidate_session_cache() - fixed wrong number of deleted items in setup_cmd_ldap sub-comand=delete_ldap - only use create, if we have an ldap_admin_pw set - call an add_account hook for each created account, if specified (not by default) --- phpgwapi/inc/class.accounts.inc.php | 3 +- setup/inc/class.setup.inc.php | 2 +- setup/inc/class.setup_cmd.inc.php | 2 +- setup/inc/class.setup_cmd_ldap.inc.php | 63 +++++++++++++++++++++----- 4 files changed, 55 insertions(+), 15 deletions(-) diff --git a/phpgwapi/inc/class.accounts.inc.php b/phpgwapi/inc/class.accounts.inc.php index 27c70270de..8390c5c7b2 100644 --- a/phpgwapi/inc/class.accounts.inc.php +++ b/phpgwapi/inc/class.accounts.inc.php @@ -832,10 +832,11 @@ class accounts { //echo "

accounts::cache_invalidate($account_id)

\n"; if (self::$cache) self::$cache = array(); + egw_cache::unsetSession('accounts_cache','phpgwapi'); if (method_exists($GLOBALS['egw'],'invalidate_session_cache')) // egw object in setup is limited { - $GLOBALS['egw']->invalidate_session_cache(); // invalidates whole egw-enviroment if stored in the session + egw::invalidate_session_cache(); // invalidates whole egw-enviroment if stored in the session } } diff --git a/setup/inc/class.setup.inc.php b/setup/inc/class.setup.inc.php index cc5765fc3c..1c8d475c28 100644 --- a/setup/inc/class.setup.inc.php +++ b/setup/inc/class.setup.inc.php @@ -1003,7 +1003,7 @@ class setup } $this->accounts = new accounts($config); if (!isset($GLOBALS['egw']->accounts)) $GLOBALS['egw']->accounts = $this->accounts; - $this->accounts->cache_invalidate(); // the cache is shared for all instances of the class + accounts::cache_invalidate(); // the cache is shared for all instances of the class if($this->accounts->backend instanceof accounts_ldap && !$this->accounts->backend->ds) { diff --git a/setup/inc/class.setup_cmd.inc.php b/setup/inc/class.setup_cmd.inc.php index dabfe411c0..606df17c9d 100644 --- a/setup/inc/class.setup_cmd.inc.php +++ b/setup/inc/class.setup_cmd.inc.php @@ -168,7 +168,7 @@ abstract class setup_cmd extends admin_cmd if (!is_null(self::$egw_accounts_backup)) { $GLOBALS['egw']->accounts = self::$egw_accounts_backup; - $GLOBALS['egw']->accounts->cache_invalidate(); + accounts::cache_invalidate(); unset(self::$egw_accounts_backup); } } diff --git a/setup/inc/class.setup_cmd_ldap.inc.php b/setup/inc/class.setup_cmd_ldap.inc.php index ed2b50961b..9c00cbfdd0 100644 --- a/setup/inc/class.setup_cmd_ldap.inc.php +++ b/setup/inc/class.setup_cmd_ldap.inc.php @@ -42,6 +42,7 @@ class setup_cmd_ldap extends setup_cmd * @param string $ldap_search_filter=null search-filter for accounts, default "(uid=%user)" * @param string $ldap_group_context=null ou for groups, default "ou=groups,$base" * @param string $sub_command='create_ldap' 'create_ldap', 'test_ldap', 'test_ldap_root' + * @param string $ldap_encryption_type='des' */ function __construct($domain,$ldap_host=null,$ldap_suffix=null,$ldap_admin=null,$ldap_admin_pw=null, $ldap_base=null,$ldap_root_dn=null,$ldap_root_pw=null,$ldap_context=null,$ldap_search_filter=null, @@ -125,7 +126,7 @@ class setup_cmd_ldap extends setup_cmd { $msg = array(); // if migrating to ldap, check ldap and create context if not yet exiting - if ($to_ldap) + if ($to_ldap && !empty($this->ldap_admin_pw)) { $msg[] = $this->create(); } @@ -169,14 +170,7 @@ class setup_cmd_ldap extends setup_cmd } else { - if ($account['account_pwd'][0] != '{') // plain has to be explicitly specified for sql, in ldap it's the default - { - $account['account_passwd'] = '{PLAIN}'.$account['account_pwd']; - } - else - { - $account['account_passwd'] = $account['account_pwd']; - } + $account['account_passwd'] = self::hash_ldap2sql($account['account_pwd']); } unset($account['person_id']); @@ -189,6 +183,29 @@ class setup_cmd_ldap extends setup_cmd $accounts_obj->set_memberships($account['memberships'],$account_id); $msg[] = lang('%1 created in %2.',$what,$target); $accounts_created++; + + // should we run any or some addAccount hooks + if ($this->add_account_hook) + { + try + { + $account['location'] = 'addAccount'; + // running all addAccount hooks (currently NOT working, as not all work in setup) + if ($this->add_account_hook === true) + { + $GLOBALS['egw']->hooks->process($account,array(),true); + } + elseif(is_callable($this->add_account_hook)) + { + call_user_func($this->add_account_hook,$account); + } + } + catch(Exception $e) + { + $msg[] = $e->getMessage(); + $errors++; + } + } } else { @@ -207,9 +224,11 @@ class setup_cmd_ldap extends setup_cmd else { $msg[] = lang('%1 already exists in %2.',$what,$target); - + $errors++; + if ($accounts_obj->id2name($account_id) != $account['account_lid']) { + $msg[] = lang("==> different group '%1' under that gidNumber %2, NOT setting memberships!",$account['account_lid'],$account_id); ++$errors; continue; // different group under that gidnumber! } @@ -218,6 +237,8 @@ class setup_cmd_ldap extends setup_cmd $accounts_obj->set_members($account['members'],$account_id); } } + $this->restore_db(); + return lang('%1 users and %2 groups created, %3 errors',$accounts_created,$groups_created,$errors). ($errors || $this->verbose ? "\n- ".implode("\n- ",$msg) : ''); } @@ -253,6 +274,21 @@ class setup_cmd_ldap extends setup_cmd return $hash; } + /** + * Convert LDAP hash to SQL hash + * + * @param string $hash + * @return string + */ + public static function hash_ldap2sql($hash) + { + if ($hash[0] != '{') // plain has to be explicitly specified for sql, in ldap it's the default + { + $hash = '{PLAIN}'.$hash; + } + return $hash; + } + /** * Read all accounts from sql or ldap * @@ -284,6 +320,8 @@ class setup_cmd_ldap extends setup_cmd $account['memberships'] = $accounts_obj->memberships($account_id,true); } } + accounts::cache_invalidate(); + return $accounts; } @@ -446,16 +484,17 @@ class setup_cmd_ldap extends setup_cmd { throw new egw_exception(lang('Error listing "dn=%1"!',$dn)); } + $deleted = 0; foreach($entries as $n => $entry) { if ($n === 'count') continue; - $this->rdelete($entry['dn']); + $deleted += $this->rdelete($entry['dn']); } if (!ldap_delete($this->test_ldap->ds,$dn)) { throw new egw_exception(lang('Error deleting "dn=%1"!',$dn)); } - return 1 + $entries['count']; + return ++$deleted; } /**