-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNotifyAdminJob.php
More file actions
106 lines (94 loc) · 2.89 KB
/
NotifyAdminJob.php
File metadata and controls
106 lines (94 loc) · 2.89 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
<?php
// SPDX-FileCopyrightText: 2025 Lennart Dohmann <lennart.dohmann@gdata.de>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
namespace OCA\GDataVaas\BackgroundJobs;
use OC\User\NoUserException;
use OCA\GDataVaas\AppInfo\Application;
use OCA\GDataVaas\Db\DbFileMapper;
use OCA\GDataVaas\Service\FileService;
use OCA\GDataVaas\Service\MailService;
use OCA\GDataVaas\Service\TagService;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\TimedJob;
use OCP\DB\Exception;
use OCP\Files\File;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\IAppConfig;
use OCP\SystemTag\TagNotFoundException;
use Psr\Log\LoggerInterface;
class NotifyAdminJob extends TimedJob {
private IAppConfig $appConfig;
private TagService $tagService;
private DbFileMapper $dbFileMapper;
private MailService $mailService;
private LoggerInterface $logger;
private FileService $fileService;
public function __construct(ITimeFactory $time,
IAppConfig $appConfig,
TagService $tagService,
DbFileMapper $dbFileMapper,
LoggerInterface $logger,
MailService $mailService,
FileService $fileService) {
parent::__construct($time);
$this->appConfig = $appConfig;
$this->tagService = $tagService;
$this->dbFileMapper = $dbFileMapper;
$this->logger = $logger;
$this->mailService = $mailService;
$this->fileService = $fileService;
$this->setInterval(7 * 24 * 3600);
$this->setAllowParallelRuns(false);
$this->setTimeSensitivity(self::TIME_INSENSITIVE);
}
/**
* @param $argument
* @return void
* @throws Exception
* @throws NotFoundException
* @throws NotPermittedException
* @throws \Exception
*/
#[\Override]
protected function run($argument): void {
$notifyAdminEnabled = $this->appConfig->getValueBool(Application::APP_ID, 'notifyAdminEnabled');
if (!$notifyAdminEnabled) {
return;
}
try {
$maliciousTagId = $this->tagService->getTag(TagService::MALICIOUS, false)->getId();
} catch (TagNotFoundException) {
return;
}
$allFiles = $this->dbFileMapper->getFilesCount();
$maliciousFiles = $this->dbFileMapper->getFileIdsWithTags([$maliciousTagId], $allFiles);
$this->logger->info(
'Found ' . count($maliciousFiles) . ' malicious files out of ' . $allFiles . ' total files'
);
if (count($maliciousFiles) > 0) {
$this->logger->debug('Sending notification to admin');
$this->mailService->notifyWeeklySummary($this->getFilesFromFileIds($maliciousFiles));
} else {
$this->logger->info('No malicious files found, no weekly summary sent');
}
}
/**
* @param array $fileIds
* @return array
* @throws NotFoundException
* @throws NotPermittedException
* @throws NoUserException
*/
private function getFilesFromFileIds(array $fileIds): array {
$files = [];
foreach ($fileIds as $fileId) {
$file = $this->fileService->getNodeFromFileId($fileId);
if ($file instanceof File) {
$files[] = $file;
}
}
return $files;
}
}