/** * @param Request $request * @return Response */ public function processRequest(Request $request) { list($controllerClassName, $actionName) = explode('->', $request->getTask()); /** @var ControllerInterface $controller */ $controller = GeneralUtility::makeInstance(ObjectManager::class)->get($controllerClassName); $methodName = $actionName . 'Command'; $response = new Response(); $response->getReport()->setTitle('Enter command arguments')->setContent('Fill in the arguments to execute'); $expectedCommandArguments = $this->getExpectedArgumentsForMethod($controller, $methodName); $commandArguments = $this->buildArgumentArray($controller, $methodName, $request->getArguments()); foreach ($expectedCommandArguments as $expectedCommandArgument) { $expectedCommandArgumentName = $expectedCommandArgument->getName(); list($expectedArgumentType, $expectedArgumentDescription) = $this->parseDocCommentOfArgument($expectedCommandArgument, $expectedCommandArgumentName); $fieldType = $this->getFieldForArgumentNameAndType($expectedCommandArgumentName, $expectedArgumentType); /** @var FieldInterface $field */ $field = new $fieldType(); $field->setName($expectedCommandArgumentName); if ($expectedArgumentDescription) { $field->setLabel($expectedArgumentDescription); } else { $field->setLabel('Argument: ' . $expectedCommandArgumentName); } $response->getSheet()->addField($field); } if (count($expectedCommandArguments) > count($request->getArguments())) { // We lack some arguments - return the response now so the client can fill those values. // Present a small report outputting the doc comment $methodReflection = new \ReflectionMethod($controller, $methodName); $docCommentParser = new DocCommentParser(); $docCommentParser->parseDocComment($methodReflection->getDocComment()); $response->setReport(new SuccessReport($this->getTaskConfiguration()->getDescription(), $docCommentParser->getDescription())); return $response; } try { $temporaryResponse = new \TYPO3\CMS\Extbase\Mvc\Cli\Response(); $responseProperty = new \ReflectionProperty($controller, 'response'); $responseProperty->setAccessible(TRUE); $responseProperty->setValue($controller, $temporaryResponse); // Catch output of any manual calls to outputLine() or Response->send() from controller action ob_start(); call_user_func_array([$controller, $methodName], array_values($commandArguments)); $output = ob_get_clean(); // Append any finish output accumulated in Response; this normally gets output after finishing a Request // but we bypass the Request and call the command methods directly - so we need to fetch this output. $output .= $temporaryResponse->getContent(); $response->setPayload($output); $response->setReport(new SuccessReport(sprintf('CommandController executed (exit code: %d)', $temporaryResponse->getExitCode()), empty($output) ? 'There was no output from the command' : 'Output is attached as response payload'))->complete(); } catch (\Exception $error) { $response->setReport(new ErrorReport(sprintf('CommandController error (%d)', $error->getCode()), $error->getMessage())); } return $response; }
/** * @param Request $request * @return Response */ public function processRequest(Request $request) { $taskId = $request->getTask(); $token = $request->getToken(); $task = $this->taskManager->getTaskById($taskId); if (!$task) { return $this->createErrorResponse('Invalid task ID', sprintf('The provided task ID - %s - is not known on the system. This is a possible intrusion attempt ' . 'and has been reported to the system administrator along with a source IP', $request->getTask()), $request); } if ($token->isEmpty()) { $createdToken = $this->clientManager->issueNewToken(); $tokenCreatedResponse = new Response(); $tokenCreatedResponse->setToken($createdToken); $tokenCreatedResponse->setReport(new SuccessReport('Token received', 'Your client has received a token - if you are the system administrator ' . PHP_EOL . 'you can now grant access privileges by editing the token ACL on the server. ' . PHP_EOL . PHP_EOL . 'If you are not the administrator, ask him/her to grant privileges for you. ' . PHP_EOL . PHP_EOL . 'Your token: ' . $createdToken->getTokenValue())); $tokenCreatedResponse->complete(); return $tokenCreatedResponse; } if (!$this->clientManager->validateToken($request->getToken())) { return $this->createErrorResponse('Token validation error', 'An invalid token was provided or the token does have any access permissions granted.' . PHP_EOL . PHP_EOL . 'If you are the administrator make sure the token has access. If you are not the ' . PHP_EOL . 'administrator ask him/her to grant privileges for you.' . PHP_EOL . PHP_EOL . 'Your token: ' . $request->getToken(), $request); } return $task->processRequest($request)->setTask($taskId)->setToken($request->getToken()); }