forked from Sebobo/Shel.Neos.Terminal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTerminalCommandController.php
More file actions
165 lines (136 loc) · 5.7 KB
/
Copy pathTerminalCommandController.php
File metadata and controls
165 lines (136 loc) · 5.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
declare(strict_types=1);
namespace Shel\Neos\Terminal\Controller;
/**
* This file is part of the Shel.Neos.Terminal package.
*
* (c) 2021 Sebastian Helzle
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Neos\ContentRepository\Core\Projection\ContentGraph\Node;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\I18n\Exception\IndexOutOfBoundsException;
use Neos\Flow\I18n\Exception\InvalidFormatPlaceholderException;
use Neos\Flow\I18n\Translator;
use Neos\Flow\Mvc\ActionRequest;
use Neos\Flow\Mvc\ActionResponse;
use Neos\Flow\Mvc\Controller\ActionController;
use Neos\Flow\Mvc\View\JsonView;
use Neos\Flow\Security\Authorization\PrivilegeManagerInterface;
use Neos\Flow\Security\Context;
use Neos\Flow\Security\Exception\AccessDeniedException;
use Neos\Neos\Ui\Domain\Model\FeedbackCollection;
use Shel\Neos\Terminal\Domain\CommandContext;
use Shel\Neos\Terminal\Domain\CommandInvocationResult;
use Shel\Neos\Terminal\Exception as TerminalException;
use Shel\Neos\Terminal\Security\TerminalCommandPrivilege;
use Shel\Neos\Terminal\Security\TerminalCommandPrivilegeSubject;
use Shel\Neos\Terminal\Service\SerializationService;
use Shel\Neos\Terminal\Service\TerminalCommandService;
#[Flow\Scope('singleton')]
class TerminalCommandController extends ActionController
{
protected $defaultViewObjectName = JsonView::class;
#[Flow\Inject]
protected Translator $translator;
#[Flow\Inject]
protected FeedbackCollection $feedbackCollection;
#[Flow\InjectConfiguration('frontendConfiguration', 'Neos.Neos.Ui')]
protected array $frontendConfiguration;
#[Flow\Inject]
protected TerminalCommandService $terminalCommandService;
#[Flow\Inject]
protected PrivilegeManagerInterface $privilegeManager;
#[Flow\Inject]
protected Context $securityContext;
public function getCommandsAction(): void
{
if (!$this->privilegeManager->isPrivilegeTargetGranted('Neos.Neos:Backend.GeneralAccess')) {
$this->view->assign('value', ['success' => false, 'result' => []]);
return;
}
$commandNames = $this->terminalCommandService->getCommandNames();
$availableCommandNames = array_filter($commandNames, function ($commandName) {
return $this->privilegeManager->isGranted(TerminalCommandPrivilege::class,
new TerminalCommandPrivilegeSubject($commandName));
});
$commandDefinitions = array_reduce($availableCommandNames, function (array $carry, string $commandName) {
$command = $this->terminalCommandService->getCommand($commandName);
$carry[$commandName] = [
'name' => $commandName,
'description' => $command::getCommandDescription(),
'usage' => $command::getCommandUsage(),
];
return $carry;
}, []);
$this->view->assign('value', ['success' => true, 'result' => $commandDefinitions]);
}
/**
* @throws \JsonException
*/
public function invokeCommandAction(
string $commandName,
?string $argument = null,
?Node $siteNode = null,
?Node $documentNode = null,
?Node $focusedNode = null
): void
{
$this->response->setContentType('application/json');
$command = $this->terminalCommandService->getCommand($commandName);
$commandContext = (new CommandContext($this->getControllerContext()))
->withSiteNode($siteNode)
->withDocumentNode($documentNode)
->withFocusedNode($focusedNode)
->withFocusedNode($focusedNode);
$this->getControllerContext()->getRequest()->getMainRequest()->setFormat('html');
try {
$result = $command->invokeCommand($argument, $commandContext);
} /** @noinspection PhpRedundantCatchClauseInspection */ catch (AccessDeniedException $e) {
$result = new CommandInvocationResult(false,
$this->translateById('commandNotGranted', ['command' => $commandName]));
}
// TODO: Move the feedback related logic into a separate service
if ($result->getUiFeedback()) {
// Change format to prevent url generation errors when serialising url based feedback
foreach ($result->getUiFeedback() as $feedback) {
$this->feedbackCollection->add($feedback);
}
}
$this->view->assign('value', [
'success' => $result->isSuccess(),
'result' => SerializationService::serialize($result->getResult()),
'uiFeedback' => $this->feedbackCollection,
]);
}
protected function initializeController(ActionRequest $request, ActionResponse $response): void
{
parent::initializeController($request, $response);
$this->feedbackCollection->setControllerContext($this->getControllerContext());
}
/**
* Throws an exception when terminal is disabled
*
* @throws TerminalException
*/
protected function initializeAction(): void
{
$terminalConfiguration = $this->frontendConfiguration['Shel.Neos.Terminal:Terminal'];
$terminalEnabled = $terminalConfiguration['enabled'] ?? false;
if (!$terminalEnabled) {
throw new TerminalException($this->translateById('disabled'));
}
parent::initializeAction();
}
protected function translateById(string $id, array $arguments = []): string
{
try {
return $this->translator->translateById('disabled', $arguments);
} catch (InvalidFormatPlaceholderException|IndexOutOfBoundsException) {
}
return $id;
}
}