Skip to content

Commit

Permalink
Merge pull request #517 from owncloud/feat/background-scanning-setting
Browse files Browse the repository at this point in the history
feat: add setting to enable/disable background scanning
  • Loading branch information
DeepDiver1975 authored Nov 18, 2022
2 parents 871ad29 + 9935d6e commit 7e19823
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 33 deletions.
43 changes: 16 additions & 27 deletions lib/Controller/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,18 @@
namespace OCA\Files_Antivirus\Controller;

use OCA\Files_Antivirus\ScannerFactory;
use \OCP\AppFramework\Controller;
use \OCP\IRequest;
use \OCP\IL10N;
use \OCA\Files_Antivirus\AppConfig;
use OCP\AppFramework\Controller;
use OCP\IRequest;
use OCP\IL10N;
use OCA\Files_Antivirus\AppConfig;

use \OCP\AppFramework\Http\TemplateResponse;
use \OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Http\JSONResponse;

class SettingsController extends Controller {

/**
* @var AppConfig
*/
private $settings;

/**
* @var ScannerFactory
*/
private $scannerFactory;

/**
* @var IL10N
*/
private $l10n;
private AppConfig $settings;
private ScannerFactory $scannerFactory;
private IL10N $l10n;

/**
* SettingsController constructor.
Expand All @@ -61,10 +49,8 @@ public function __construct(

/**
* Print config section
*
* @return TemplateResponse
*/
public function index() {
public function index(): TemplateResponse {
$data = $this->settings->getAllValues();
return new TemplateResponse('files_antivirus', 'settings', $data, 'blank');
}
Expand All @@ -75,12 +61,13 @@ public function index() {
* @param string $avMode - antivirus mode
* @param string $avSocket - path to socket (Socket mode)
* @param string $avHost - antivirus url
* @param int $avPort - port
* @param ?int $avPort - port
* @param string $avInfectedAction - action performed on infected files
* @param int $avStreamMaxLength - reopen socket after bytes
* @param int $avMaxFileSize - file size limit
* @param string $avRequestService
* @param string $avResponseHeader
* @param string $avScanBackground
*
* @return JSONResponse
*/
Expand All @@ -93,8 +80,9 @@ public function save(
$avStreamMaxLength,
$avMaxFileSize,
$avRequestService,
$avResponseHeader
) {
$avResponseHeader,
$avScanBackground
): JSONResponse {
try {
if ($avMode === 'daemon') {
$this->settings->setAvPort($avPort);
Expand Down Expand Up @@ -122,6 +110,7 @@ public function save(
$this->settings->setAvStreamMaxLength($avStreamMaxLength);
$this->settings->setAvMaxFileSize($avMaxFileSize);
$this->settings->setAvMode($avMode);
$this->settings->setAvScanBackground($avScanBackground);

$connectionStatus = (int)$this->scannerFactory->testConnection($this->settings);
$response = [
Expand Down
4 changes: 4 additions & 0 deletions templates/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@
/>
<label for="av_max_file_size" class="a-left"><?php p($l->t('bytes'))?></label>
</p>
<p class="av_scan_background">
<label for="av_scan_background"><?php p($l->t('Background scanning'));?></label>
<select id="av_scan_background" name="avScanBackground"><?php print_unescaped(html_select_options(['true' => $l->t('Enabled'), 'false' => $l->t('Disabled')], $_['avScanBackground'])) ?></select>
</p>
<p class="infected_action">
<label for="av_infected_action"><?php p($l->t('When infected files were found during a background scan'));?></label>
<select id="av_infected_action" name="avInfectedAction"><?php print_unescaped(html_select_options(['only_log' => $l->t('Only log'), 'delete' => $l->t('Delete file')], $_['avInfectedAction'])) ?></select>
Expand Down
17 changes: 11 additions & 6 deletions tests/unit/Controller/SettingsControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function setUp(): void {

$this->config = $this->getMockBuilder(AppConfig::class)
->disableOriginalConstructor()
->setMethods(['setter', 'getAppValue'])
->onlyMethods(['setter', 'getAppValue'])
->getMock();

$this->scannerFactory = $this->getMockBuilder(ScannerFactory::class)
Expand All @@ -59,7 +59,8 @@ public function testSaveExecutable(): void {
['av_infected_action', ['delete']],
['av_stream_max_length', [100]],
['av_max_file_size', [800]],
['av_mode', ['executable']]
['av_mode', ['executable']],
['av_scan_background', [true]],
);

$settings = new SettingsController($this->request, $this->config, $this->scannerFactory, $this->l10n);
Expand All @@ -72,7 +73,8 @@ public function testSaveExecutable(): void {
100,
800,
'',
''
'',
true
);
}

Expand All @@ -84,7 +86,8 @@ public function testSaveSocket(): void {
['av_infected_action', ['delete']],
['av_stream_max_length', [100]],
['av_max_file_size', [800]],
['av_mode', ['socket']]
['av_mode', ['socket']],
['av_scan_background', [false]],
);

$settings = new SettingsController($this->request, $this->config, $this->scannerFactory, $this->l10n);
Expand All @@ -98,7 +101,8 @@ public function testSaveSocket(): void {
100,
800,
'',
''
'',
false
);
}

Expand Down Expand Up @@ -128,7 +132,8 @@ public function testSaveDaemon(): void {
100,
800,
'',
''
'',
true
);
}
}

0 comments on commit 7e19823

Please sign in to comment.