-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSettingsController.php
More file actions
205 lines (183 loc) · 7.4 KB
/
SettingsController.php
File metadata and controls
205 lines (183 loc) · 7.4 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
// SPDX-FileCopyrightText: 2025 Lennart Dohmann <lennart.dohmann@gdata.de>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
namespace OCA\GDataVaas\Controller;
use OCA\GDataVaas\Service\TagService;
use OCA\GDataVaas\Service\VerdictService;
use OCA\GDataVaas\Settings\VaasOperator;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\Attribute\AuthorizedAdminSetting;
use OCP\AppFramework\Http\JSONResponse;
use OCP\DB\Exception;
use OCP\IAppConfig;
use OCP\IRequest;
use OCP\Mail\IMailer;
use VaasSdk\Exceptions\VaasAuthenticationException;
use VaasSdk\Options\VaasOptions;
use VaasSdk\Vaas;
use VaasSdk\Verdict;
class SettingsController extends Controller {
private IAppConfig $config;
private TagService $tagService;
private IMailer $mailer;
private VerdictService $verdictService;
public function __construct(
$appName,
IRequest $request,
IAppConfig $config,
TagService $tagService,
IMailer $mailer,
VerdictService $verdictService,
) {
parent::__construct($appName, $request);
$this->config = $config;
$this->tagService = $tagService;
$this->mailer = $mailer;
$this->verdictService = $verdictService;
}
public function setAdminSettings(
$username,
$password,
$clientId,
$clientSecret,
$authMethod,
$maxScanSize,
$timeout,
bool $cache,
bool $hashlookup,
): JSONResponse {
if ((int)$maxScanSize < 1) {
return new JSONResponse(['status' => 'error', 'message' => 'Invalid max scan size: ' . $maxScanSize]);
}
if ((int)$timeout < 1) {
return new JSONResponse(['status' => 'error', 'message' => 'Invalid timeout: ' . $timeout]);
}
$this->config->setValueString($this->appName, 'username', $username);
$this->config->setValueString($this->appName, 'password', $password);
$this->config->setValueString($this->appName, 'clientId', $clientId);
$this->config->setValueString($this->appName, 'clientSecret', $clientSecret);
$this->config->setValueString($this->appName, 'authMethod', $authMethod);
$this->config->setValueInt($this->appName, 'maxScanSizeInMB', (int)$maxScanSize);
$this->config->setValueInt($this->appName, 'timeout', (int)$timeout);
$this->config->setValueBool($this->appName, 'cache', $cache);
$this->config->setValueBool($this->appName, 'hashlookup', $hashlookup);
return new JSONResponse(['status' => 'success']);
}
public function setAdvancedConfig($tokenEndpoint, $vaasUrl): JSONResponse {
$this->config->setValueString($this->appName, 'tokenEndpoint', $tokenEndpoint);
$this->config->setValueString($this->appName, 'vaasUrl', $vaasUrl);
return new JSONResponse(['status' => 'success']);
}
#[AuthorizedAdminSetting(settings: VaasOperator::class)]
public function setOperatorSettings(
$quarantineFolder,
$scanOnlyThis,
$doNotScanThis,
$notifyMails,
): JSONResponse {
if (!empty($notifyMails)) {
$mails = explode(',', preg_replace('/\s+/', '', $notifyMails));
foreach ($mails as $mail) {
if ($this->mailer->validateMailAddress($mail) === false) {
return new JSONResponse(['status' => 'error', 'message' => 'Invalid email address: ' . $mail]);
}
}
}
$this->config->setValueString($this->appName, 'quarantineFolder', $quarantineFolder);
$this->config->setValueString($this->appName, 'scanOnlyThis', $scanOnlyThis);
$this->config->setValueString($this->appName, 'doNotScanThis', $doNotScanThis);
$this->config->setValueString($this->appName, 'notifyMails', $notifyMails);
return new JSONResponse(['status' => 'success']);
}
#[AuthorizedAdminSetting(settings: VaasOperator::class)]
public function setAutoScan(bool $autoScanFiles): JSONResponse {
$this->config->setValueBool($this->appName, 'autoScanFiles', $autoScanFiles);
return new JSONResponse(['status' => 'success']);
}
#[AuthorizedAdminSetting(settings: VaasOperator::class)]
public function getAutoScan(): JSONResponse {
return new JSONResponse(['status' => $this->config->getValueBool($this->appName, 'autoScanFiles')]);
}
#[AuthorizedAdminSetting(settings: VaasOperator::class)]
public function setPrefixMalicious(bool $prefixMalicious): JSONResponse {
$this->config->setValueBool($this->appName, 'prefixMalicious', $prefixMalicious);
return new JSONResponse(['status' => 'success']);
}
#[AuthorizedAdminSetting(settings: VaasOperator::class)]
public function getPrefixMalicious(): JSONResponse {
return new JSONResponse(['status' => $this->config->getValueBool($this->appName, 'prefixMalicious')]);
}
public function getAuthMethod(): JSONResponse {
return new JSONResponse(['status' => $this->config->getValueString($this->appName, 'authMethod')]);
}
#[AuthorizedAdminSetting(settings: VaasOperator::class)]
public function setDisableUnscannedTag(bool $disableUnscannedTag): JSONResponse {
$this->config->setValueBool($this->appName, 'disableUnscannedTag', $disableUnscannedTag);
return new JSONResponse(['status' => 'success']);
}
#[AuthorizedAdminSetting(settings: VaasOperator::class)]
public function getDisableUnscannedTag(): JSONResponse {
return new JSONResponse(['status' => $this->config->getValueBool($this->appName, 'disableUnscannedTag')]);
}
public function resetAllTags(): JSONResponse {
$this->tagService->resetAllTags();
return new JSONResponse(['status' => 'success']);
}
#[AuthorizedAdminSetting(settings: VaasOperator::class)]
public function getCounters(): JSONResponse {
try {
$filesCount = $this->tagService->getScannedFilesCount();
} catch (Exception $e) {
return new JSONResponse([
'status' => 'error',
'message' => $e->getMessage()
]);
}
return new JSONResponse([
'status' => 'success',
'all' => $filesCount['all'],
'scanned' => $filesCount['scanned']
]);
}
#[AuthorizedAdminSetting(settings: VaasOperator::class)]
public function getSendMailOnVirusUpload(): JSONResponse {
return new JSONResponse(
['status' => $this->config->getValueBool($this->appName, 'sendMailOnVirusUpload')]
);
}
#[AuthorizedAdminSetting(settings: VaasOperator::class)]
public function setSendMailOnVirusUpload(bool $sendMailOnVirusUpload): JSONResponse {
$this->config->setValueBool($this->appName, 'sendMailOnVirusUpload', $sendMailOnVirusUpload);
return new JSONResponse(['status' => 'success']);
}
public function testSettings(string $tokenEndpoint, string $vaasUrl): JSONResponse {
try {
$authenticator = $this->verdictService->getAuthenticator($this->verdictService->authMethod, $tokenEndpoint);
$options = new VaasOptions(true, true, $vaasUrl);
$vaas = Vaas::builder()
->withAuthenticator($authenticator)
->withOptions($options)
->build();
$verdict = $vaas->forUrlAsync('https://www.gdata.de')->await();
if ($verdict->verdict === Verdict::CLEAN) {
return new JSONResponse(['status' => 'success']);
}
return new JSONResponse(['status' => 'error', 'message' => 'Test URL verdict: ' . $verdict->verdict->value]);
} catch (VaasAuthenticationException $e) {
return new JSONResponse([
'status' => 'error',
'message' => 'Authentication failed. Please also check your login details above and save them before
taking the test. ' . $e->getMessage()
]);
} catch (\Exception $e) {
return new JSONResponse(['status' => 'error', 'message' => $e->getMessage()]);
}
}
public function getCache(): JSONResponse {
return new JSONResponse(['status' => $this->config->getValueBool($this->appName, 'cache', true)]);
}
public function getHashlookup(): JSONResponse {
return new JSONResponse(['status' => $this->config->getValueBool($this->appName, 'hashlookup', true)]);
}
}