public static function assignModel(OA_Admin_Template $template, $query = '') { $accounts = OA_Permission::getLinkedAccounts(true, true); $remainingCounts = array(); // Prepare recently used accountName $recentlyUsed = array(); global $session; if (empty($query) && !empty($session['recentlyUsedAccounts'])) { $allAcountsNoGroups = array(); foreach ($accounts as $k => $v) { foreach ($accounts[$k] as $accountId => $accountName) { $allAcountsNoGroups[$accountId] = $accountName; } } $recentlyUsedAccountIds = $session['recentlyUsedAccounts']; $added = 0; foreach ($recentlyUsedAccountIds as $k => $recentlyUserAccountId) { if (++$added > self::MAX_ACCOUNTS_IN_GROUP) { break; } $recentlyUsed[$recentlyUserAccountId] = $allAcountsNoGroups[$recentlyUserAccountId]; } } // Prepare admin accounts if (isset($accounts[OA_ACCOUNT_ADMIN])) { $adminAccounts = self::filterByNameAndLimit($accounts[OA_ACCOUNT_ADMIN], $query, $remainingCounts, OA_ACCOUNT_ADMIN); unset($accounts[OA_ACCOUNT_ADMIN]); } else { $adminAccounts = array(); } $showSearchAndRecent = false; foreach ($accounts as $k => $v) { $workingFor = sprintf($GLOBALS['strWorkingFor'], ucfirst(strtolower($k))); $accounts[$workingFor] = self::filterByNameAndLimit($v, $query, $remainingCounts, $workingFor); $count = count($accounts[$workingFor]); if ($count == 0) { unset($accounts[$workingFor]); } $showSearchAndRecent |= isset($remainingCounts[$workingFor]); unset($accounts[$k]); } // Prepend recently used to the results if (!empty($recentlyUsed) && $showSearchAndRecent) { $accounts = array_merge(array($GLOBALS['strRecentlyUsed'] => $recentlyUsed), $accounts); } $template->assign('adminAccounts', $adminAccounts); $template->assign('otherAccounts', $accounts); $template->assign('remainingCounts', $remainingCounts); $template->assign('query', $query); $template->assign('noAccountsMessage', sprintf($GLOBALS['strNoAccountWithXInNameFound'], $query)); $template->assign('currentAccountId', OA_Permission::getAccountId()); $template->assign('showSearchAndRecent', $showSearchAndRecent); }
/** * Resets default user's account to one of the account's ids which is linked to him. * * @param integer $userId * @param integer $accountId */ function resetUserDefaultAccount($userId, $accountId) { $linkedAccounts = OA_Permission::getLinkedAccounts(false, $userId); $doUsers = OA_Dal::staticGetDO('users', $userId); if ($doUsers->default_account_id == $accountId) { $doUsers->default_account_id = array_shift($linkedAccounts); $doUsers->update(); } }
function attemptToSwitchToAccount($accountType) { $oUser = OA_Permission::getCurrentUser(); if (!$oUser) { return false; } $aAccountTypes = is_array($accountType) ? $accountType : func_get_args(); $aAccountIds = OA_Permission::getLinkedAccounts(true); $defaultUserAccountId = $oUser->aUser['default_account_id']; foreach ($aAccountTypes as $accountType) { if (isset($aAccountIds[$accountType])) { if (isset($aAccountIds[$accountType][$defaultUserAccountId])) { $accountId = $defaultUserAccountId; } else { $accountId = array_shift(array_keys($aAccountIds[$accountType])); } OA_Permission::switchAccount($accountId, $hasAccess = true); return true; } } return false; }