fix not always working instantiation for contacts and accounts objects differing from global configured repository

This commit is contained in:
ralf 2022-07-06 21:27:21 +02:00
parent 46b172c88e
commit c151f7786f

View File

@ -57,26 +57,65 @@ class Import
throw new \InvalidArgumentException("Invalid account_import_source='{$GLOBALS['egw_info']['server']['account_import_source']}'!"); throw new \InvalidArgumentException("Invalid account_import_source='{$GLOBALS['egw_info']['server']['account_import_source']}'!");
} }
$class = 'EGroupware\\Api\\Contacts\\'.ucfirst($source); $this->contacts = ($frontend = self::contactsFactory($source))->so_accounts ?: $frontend->somain;
/** @var Api\Contacts\Ldap $contacts */ $this->contacts_sql_frontend = self::contactsFactory('sql');
$this->contacts = new $class($GLOBALS['egw_info']['server']); $this->contacts_sql = $this->contacts_sql_frontend->so_accounts ?: $this->contacts_sql_frontend->somain;
$this->contacts_sql = new Api\Contacts\Sql();
// instantiate contacts object with SQL backend
$this->contacts_sql_frontend = new Api\Contacts();
$this->contacts_sql_frontend->somain = $this->contacts_sql_frontend->so_accounts = $this->contacts_sql;
$class = 'EGroupware\\Api\\Accounts\\'.ucfirst($source); $this->accounts = self::accountsFactory($source)->backend;
/** @var Api\Accounts\Ldap $accounts */ $this->frontend_sql = self::accountsFactory('sql');
$this->accounts = new $class($frontend = new Api\Accounts(['account_repository' => $source]+$GLOBALS['egw_info']['server'])); $this->accounts_sql = $this->frontend_sql->backend;
// instantiate accounts backend and frontend for SQL
$this->accounts_sql = new Api\Accounts\Sql();
$this->frontend_sql = new Api\Accounts(['account_repository' => 'sql']+$GLOBALS['egw_info']['server'], $this->accounts_sql);
$this->accounts_sql->setFrontend($this->frontend_sql);
$this->accounts_sql->setContacts($this->contacts_sql_frontend);
$this->_logger = $logger; $this->_logger = $logger;
} }
/**
* Instantiate accounts object with given accounts-backend
*
* @param string $account_repository backend to use
* @return Api\Accounts
*/
protected static function accountsFactory(string $account_repository)
{
static $cache = [];
if (!isset($cache[$account_repository]))
{
$backup_repo = $GLOBALS['egw_info']['server']['account_repository'];
$GLOBALS['egw_info']['server']['account_repository'] = $account_repository;
$cache[$account_repository] = new Api\Accounts();
$GLOBALS['egw_info']['server']['account_repository'] = $backup_repo;
}
return $cache[$account_repository];
}
/**
* Instantiate contacts object for given accounts backend
*
* @param string $account_repository
* @return Api\Contacts
*/
protected static function contactsFactory(string $account_repository)
{
static $cache = [];
if (!isset($cache[$account_repository]))
{
$backup_repo = $GLOBALS['egw_info']['server']['account_repository'];
$backup_accounts = $GLOBALS['egw']->accounts;
$GLOBALS['egw_info']['server']['account_repository'] = $account_repository;
if ($backup_repo !== $account_repository)
{
$GLOBALS['egw']->accounts = self::accountsFactory($account_repository);
}
$cache[$account_repository] = new Api\Contacts();
$GLOBALS['egw_info']['server']['account_repository'] = $backup_repo;
$GLOBALS['egw']->accounts = $backup_accounts;
}
return $cache[$account_repository];
}
/** /**
* @param string $message * @param string $message
* @param string $level log-level: "debug", "detail", "info", "error" or "fatal" * @param string $level log-level: "debug", "detail", "info", "error" or "fatal"