diff --git a/application/Espo/Core/Utils/Client/ActionRenderer.php b/application/Espo/Core/Utils/Client/ActionRenderer.php index deb50293f8..e31ed09a49 100644 --- a/application/Espo/Core/Utils/Client/ActionRenderer.php +++ b/application/Espo/Core/Utils/Client/ActionRenderer.php @@ -51,7 +51,12 @@ class ActionRenderer */ public function write(Response $response, Params $params): void { - $body = $this->render($params->getController(), $params->getAction(), $params->getData()); + $body = $this->render( + $params->getController(), + $params->getAction(), + $params->getData(), + $params->initAuth() + ); $this->clientManager->writeHeaders($response); $response->writeBody($body); @@ -61,12 +66,15 @@ class ActionRenderer * @deprecated Use`write`. * @param ?array $data */ - public function render(string $controller, string $action, ?array $data = null): string + public function render(string $controller, string $action, ?array $data = null, bool $initAuth = false): string { $encodedData = Json::encode($data); + $initAuthPart = $initAuth ? "app.initAuth();" : ''; + $script = " + {$initAuthPart} app.doAction({ controllerClassName: '{$controller}', action: '{$action}', diff --git a/application/Espo/Core/Utils/Client/ActionRenderer/Params.php b/application/Espo/Core/Utils/Client/ActionRenderer/Params.php index c7e75774fe..45b87329b5 100644 --- a/application/Espo/Core/Utils/Client/ActionRenderer/Params.php +++ b/application/Espo/Core/Utils/Client/ActionRenderer/Params.php @@ -38,6 +38,7 @@ class Params private string $action; /** @var ?array */ private ?array $data; + private bool $initAuth = false; /** * @param ?array $data @@ -57,6 +58,25 @@ class Params return new self($controller, $action, $data); } + /** + * @param array $data + */ + public function withData(array $data): self + { + $obj = clone $this; + $obj->data = $data; + + return $obj; + } + + public function withInitAuth(bool $initAuth = true): self + { + $obj = clone $this; + $obj->initAuth = $initAuth; + + return $obj; + } + public function getController(): string { return $this->controller; @@ -74,4 +94,9 @@ class Params { return $this->data; } + + public function initAuth(): bool + { + return $this->initAuth; + } } diff --git a/application/Espo/Core/Utils/ClientManager.php b/application/Espo/Core/Utils/ClientManager.php index 997b4f5844..aa568ff023 100644 --- a/application/Espo/Core/Utils/ClientManager.php +++ b/application/Espo/Core/Utils/ClientManager.php @@ -29,14 +29,10 @@ namespace Espo\Core\Utils; -use Espo\Core\{ - Api\Response, - Api\ResponseWrapper, - Utils\File\Manager as FileManager, - Utils\Client\DevModeJsFileListProvider, - Utils\Module, - Utils\Json, -}; +use Espo\Core\Api\Response; +use Espo\Core\Api\ResponseWrapper; +use Espo\Core\Utils\Client\DevModeJsFileListProvider; +use Espo\Core\Utils\File\Manager as FileManager; use Slim\Psr7\Response as Psr7Response; use Slim\ResponseEmitter; @@ -47,29 +43,21 @@ use Slim\ResponseEmitter; class ClientManager { protected string $mainHtmlFilePath = 'html/main.html'; - protected string $runScript = "app.start();"; - private string $basePath = ''; - private string $libsConfigPath = 'client/cfg/libs.json'; - private Config $config; - - private ThemeManager $themeManager; - - private Metadata $metadata; - - private FileManager $fileManager; - - private DevModeJsFileListProvider $devModeJsFileListProvider; - - private Module $module; - private string $nonce; private const APP_DESCRIPTION = "EspoCRM - Open Source CRM application."; + private Config $config; + private ThemeManager $themeManager; + private Metadata $metadata; + private FileManager $fileManager; + private DevModeJsFileListProvider $devModeJsFileListProvider; + private Module $module; + public function __construct( Config $config, ThemeManager $themeManager, diff --git a/application/Espo/EntryPoints/LoginAs.php b/application/Espo/EntryPoints/LoginAs.php index 41bd7c8e29..b16fa059b4 100644 --- a/application/Espo/EntryPoints/LoginAs.php +++ b/application/Espo/EntryPoints/LoginAs.php @@ -34,14 +34,13 @@ use Espo\Core\EntryPoint\EntryPoint; use Espo\Core\EntryPoint\Traits\NoAuth; use Espo\Core\Api\Request; use Espo\Core\Api\Response; -use Espo\Core\Utils\ClientManager; -use Espo\Core\Utils\Json; +use Espo\Core\Utils\Client\ActionRenderer; class LoginAs implements EntryPoint { use NoAuth; - public function __construct(private ClientManager $clientManager) {} + public function __construct(private ActionRenderer $actionRenderer) {} /** * @throws BadRequest @@ -54,24 +53,14 @@ class LoginAs implements EntryPoint throw new BadRequest("No anotherUser."); } - $data = [ - 'anotherUser' => $anotherUser, - 'username' => $request->getQueryParam('username'), - ]; - - $encodedData = Json::encode($data); - - $script = - " - app.initAuth(); - app.doAction({ - controllerClassName: 'controllers/login-as', - action: 'login', - options: {$encodedData}, - }); - "; - - $this->clientManager->writeHeaders($response); - $response->writeBody($this->clientManager->render($script)); + $this->actionRenderer->write( + $response, + ActionRenderer\Params::create('controllers/login-as', 'login') + ->withData([ + 'anotherUser' => $anotherUser, + 'username' => $request->getQueryParam('username'), + ]) + ->withInitAuth() + ); } }