e62406117b70d4db1ba2779cef5cb8fcdbb03a94
[Packages/TYPO3.CMS.git] / typo3 / sysext / workspaces / Classes / Controller / AjaxController.php
1 <?php
2 namespace TYPO3\CMS\Workspaces\Controller;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use Psr\Http\Message\ResponseInterface;
18 use Psr\Http\Message\ServerRequestInterface;
19 use TYPO3\CMS\Backend\Utility\BackendUtility;
20 use TYPO3\CMS\Core\Http\JsonResponse;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22
23 /**
24 * Implements the AJAX functionality for the various asynchronous calls
25 */
26 class AjaxController
27 {
28 /**
29 * Sets the TYPO3 Backend context to a certain workspace,
30 * called by the Backend toolbar menu
31 *
32 * @param ServerRequestInterface $request
33 * @return ResponseInterface
34 */
35 public function switchWorkspaceAction(ServerRequestInterface $request): ResponseInterface
36 {
37 $parsedBody = $request->getParsedBody();
38 $queryParams = $request->getQueryParams();
39 $workspaceId = (int)(isset($parsedBody['workspaceId']) ? $parsedBody['workspaceId'] : $queryParams['workspaceId']);
40 $pageId = (int)(isset($parsedBody['pageId']) ? $parsedBody['pageId'] : $queryParams['pageId']);
41 $finalPageUid = 0;
42 $originalPageId = $pageId;
43
44 $this->getBackendUser()->setWorkspace($workspaceId);
45
46 while ($pageId) {
47 $page = BackendUtility::getRecordWSOL(
48 'pages',
49 $pageId,
50 '*',
51 ' AND pages.t3ver_wsid IN (0, ' . $workspaceId . ')'
52 );
53 if ($page) {
54 if ($this->getBackendUser()->doesUserHaveAccess($page, 1)) {
55 break;
56 }
57 } else {
58 $page = BackendUtility::getRecord('pages', $pageId);
59 }
60 $pageId = $page['pid'];
61 }
62
63 if (isset($page['uid'])) {
64 $finalPageUid = (int)$page['uid'];
65 }
66
67 $ajaxResponse = [
68 'title' => \TYPO3\CMS\Workspaces\Service\WorkspaceService::getWorkspaceTitle($workspaceId),
69 'workspaceId' => $workspaceId,
70 'pageId' => ($finalPageUid && $originalPageId == $finalPageUid) ? null : $finalPageUid
71 ];
72 return GeneralUtility::makeInstance(JsonResponse::class, $ajaxResponse);
73 }
74
75 /**
76 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
77 */
78 protected function getBackendUser()
79 {
80 return $GLOBALS['BE_USER'];
81 }
82 }