Prepared code to switch accounts without reload (not working yet)

This commit is contained in:
djmaze 2021-11-11 17:14:06 +01:00
parent d24e0a3e1b
commit 597ea21b70
7 changed files with 67 additions and 31 deletions

View file

@ -56,6 +56,13 @@ class RemoteUserFetch extends AbstractFetchRemote {
this.defaultRequest(fCallback, 'Login', oData);
}
/*
// TODO SystemDropDownUserView.accountClick
switchAccount(fCallback, email) {
this.defaultRequest(fCallback, 'AccountSwitch', {Email:email});
}
*/
/**
* @param {?Function} fCallback
*/

View file

@ -16,6 +16,8 @@ import { doc, Settings, leftPanelDisabled } from 'Common/Globals';
import { ThemeStore } from 'Stores/Theme';
//import Remote from 'Remote/User/Fetch';
export class SystemDropDownUserView extends AbstractViewRight {
constructor() {
super('SystemDropDown');
@ -44,11 +46,28 @@ export class SystemDropDownUserView extends AbstractViewRight {
}
accountClick(account, event) {
/* TODO
event.preventDefault();
event.stopPropagation();
Remote.switchAccount(
(iError, oData) => {
if (iError) {
console.dir({
iError: iError,
oData: oData
})
} else {
rl.setData(oData.Result);
// rl.route.reload();
}
},
account.email
);
*/
if (account && 0 === event.button) {
AccountUserStore.loading(true);
setTimeout(() => AccountUserStore.loading(false), 1000);
}
return true;
}

View file

@ -200,6 +200,17 @@ trait Accounts
return $this->FalseResponse(__FUNCTION__);
}
/**
* @throws \MailSo\Base\Exceptions\Exception
*/
public function DoAccountSwitch(): array
{
if ($this->switchAccount(\trim($this->GetActionParam('Email', '')))) {
return $this->DefaultResponse(__FUNCTION__, $this->AppData(false));
}
return $this->FalseResponse(__FUNCTION__);
}
/**
* @throws \MailSo\Base\Exceptions\Exception
*/

View file

@ -143,6 +143,26 @@ trait UserAuth
return $oAccount;
}
public function switchAccount(string $sEmail) : bool
{
$this->Http()->ServerNoCache();
$oMainAccount = $this->getMainAccountFromToken(false);
if ($sEmail && $oMainAccount && $this->GetCapa(false, \RainLoop\Enumerations\Capa::ADDITIONAL_ACCOUNTS, $oMainAccount)) {
$oAccountToLogin = null;
$sEmail = \MailSo\Base\Utils::IdnToAscii($sEmail);
$aAccounts = $this->GetAccounts($oMainAccount);
if (isset($aAccounts[$sEmail])) {
$oAccountToLogin = \RainLoop\Model\AdditionalAccount::NewInstanceFromTokenArray(
$this,
$aAccounts[$sEmail]
);
}
$this->SetAdditionalAuthToken($oAccountToLogin);
return true;
}
return false;
}
/**
* Returns RainLoop\Model\AdditionalAccount when it exists,
* else returns RainLoop\Model\Account when it exists,

View file

@ -201,9 +201,9 @@ class Account implements \JsonSerializable
if (!empty($aAccountHash[0]) && 'account' === $aAccountHash[0] && 7 === \count($aAccountHash)) {
$oAccount = static::NewInstanceByLogin(
$oActions,
$aAccountHash[1],
$aAccountHash[2],
$aAccountHash[3],
$aAccountHash[1] ?: '',
$aAccountHash[2] ?: '',
$aAccountHash[3] ?: '',
$aAccountHash[4] ?: '',
$bThrowExceptionOnFalse
);

View file

@ -816,30 +816,9 @@ class ServiceActions
*/
public function ServiceChange() : string
{
$this->oHttp->ServerNoCache();
$oMainAccount = $this->oActions->getMainAccountFromToken(false);
if ($oMainAccount && $this->oActions->GetCapa(false, Enumerations\Capa::ADDITIONAL_ACCOUNTS, $oMainAccount))
{
$oAccountToLogin = null;
$sEmail = empty($this->aPaths[2]) ? '' : \urldecode(\trim($this->aPaths[2]));
if (!empty($sEmail))
{
$sEmail = \MailSo\Base\Utils::IdnToAscii($sEmail);
$aAccounts = $this->oActions->GetAccounts($oMainAccount);
if (isset($aAccounts[$sEmail]))
{
$oAccountToLogin = \RainLoop\Model\AdditionalAccount::NewInstanceFromTokenArray(
$this->oActions,
$aAccounts[$sEmail]
);
}
}
$this->oActions->SetAdditionalAuthToken($oAccountToLogin);
}
$oMainAccount = $this->oActions->switchAccount(
empty($this->aPaths[2]) ? '' : \urldecode(\trim($this->aPaths[2]))
);
$this->oActions->Location('./');
return '';
}
@ -849,7 +828,7 @@ class ServiceActions
*/
public function ErrorTemplates(string $sTitle, string $sDesc, bool $bShowBackLink = true)
{
return strtr(file_get_contents(APP_VERSION_ROOT_PATH.'app/templates/Error.html'), array(
return \strtr(\file_get_contents(APP_VERSION_ROOT_PATH.'app/templates/Error.html'), array(
'{{BaseWebStaticPath}}' => Utils::WebStaticPath(),
'{{ErrorTitle}}' => $sTitle,
'{{ErrorHeader}}' => $sTitle,
@ -862,7 +841,7 @@ class ServiceActions
private function localError(string $sTitle, string $sDesc) : string
{
header('Content-Type: text/html; charset=utf-8');
\header('Content-Type: text/html; charset=utf-8');
return $this->ErrorTemplates($sTitle, \nl2br($sDesc));
}

View file

@ -69,7 +69,7 @@ if (defined('APP_VERSION'))
define('APP_DATA_FOLDER_PATH_UNIX', str_replace('\\', '/', APP_DATA_FOLDER_PATH));
$sSalt = is_file(APP_DATA_FOLDER_PATH.'SALT.php') ? file_get_contents(APP_DATA_FOLDER_PATH.'SALT.php') : '';
$sSalt = is_file(APP_DATA_FOLDER_PATH.'SALT.php') ? trim(file_get_contents(APP_DATA_FOLDER_PATH.'SALT.php')) : '';
$sData = is_file(APP_DATA_FOLDER_PATH.'DATA.php') ? file_get_contents(APP_DATA_FOLDER_PATH.'DATA.php') : '';
$sInstalled = is_file(APP_DATA_FOLDER_PATH.'INSTALLED') ? file_get_contents(APP_DATA_FOLDER_PATH.'INSTALLED') : '';