[TASK] Create own response instance in controller actions
[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
22 /**
23 * Implements the AJAX functionality for the various asynchronous calls
24 */
25 class AjaxController
26 {
27 /**
28 * Sets the TYPO3 Backend context to a certain workspace,
29 * called by the Backend toolbar menu
30 *
31 * @param ServerRequestInterface $request
32 * @return ResponseInterface
33 */
34 public function switchWorkspaceAction(ServerRequestInterface $request): ResponseInterface
35 {
36 $parsedBody = $request->getParsedBody();
37 $queryParams = $request->getQueryParams();
38 $workspaceId = (int)($parsedBody['workspaceId'] ?? $queryParams['workspaceId']);
39 $pageId = (int)($parsedBody['pageId'] ?? $queryParams['pageId']);
40 $finalPageUid = 0;
41 $originalPageId = $pageId;
42
43 $this->getBackendUser()->setWorkspace($workspaceId);
44
45 while ($pageId) {
46 $page = BackendUtility::getRecordWSOL(
47 'pages',
48 $pageId,
49 '*',
50 ' AND pages.t3ver_wsid IN (0, ' . $workspaceId . ')'
51 );
52 if ($page) {
53 if ($this->getBackendUser()->doesUserHaveAccess($page, 1)) {
54 break;
55 }
56 } else {
57 $page = BackendUtility::getRecord('pages', $pageId);
58 }
59 $pageId = $page['pid'];
60 }
61
62 if (isset($page['uid'])) {
63 $finalPageUid = (int)$page['uid'];
64 }
65
66 $ajaxResponse = [
67 'title' => \TYPO3\CMS\Workspaces\Service\WorkspaceService::getWorkspaceTitle($workspaceId),
68 'workspaceId' => $workspaceId,
69 'pageId' => ($finalPageUid && $originalPageId == $finalPageUid) ? null : $finalPageUid
70 ];
71 return new JsonResponse($ajaxResponse);
72 }
73
74 /**
75 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
76 */
77 protected function getBackendUser()
78 {
79 return $GLOBALS['BE_USER'];
80 }
81 }