diff options
author | Matthias <ilovemilk@wusa.io> | 2020-09-14 19:15:26 +0300 |
---|---|---|
committer | Matthias <ilovemilk@wusa.io> | 2020-09-14 19:15:26 +0300 |
commit | dfeda8d4bdd88d5ec99a87610b47ade98a8065d7 (patch) | |
tree | 538b60cacac6e2e53f838e3d1d61702cbe47dd42 /lib/Service | |
parent | 8981ac7f42307d3b56e76a3aae9a99d4c6f83200 (diff) |
add recovered file operations
Diffstat (limited to 'lib/Service')
-rw-r--r-- | lib/Service/FileOperationService.php | 22 | ||||
-rw-r--r-- | lib/Service/RecoveredFileOperationService.php | 159 |
2 files changed, 180 insertions, 1 deletions
diff --git a/lib/Service/FileOperationService.php b/lib/Service/FileOperationService.php index 049de93..6412b63 100644 --- a/lib/Service/FileOperationService.php +++ b/lib/Service/FileOperationService.php @@ -22,24 +22,31 @@ namespace OCA\RansomwareDetection\Service; use OCA\RansomwareDetection\Db\FileOperationMapper; +use OCA\RansomwareDetection\Db\RecoveredFileOperationMapper; class FileOperationService { /** @var FileOperationMapper */ protected $mapper; + /** @var RecoveredFileOperationMapper */ + protected $recoveredMapper; + /** @var string */ protected $userId; /** * @param FileOperationMapper $mapper + * @param RecoveredFileOperationMapper $recoveredMapper * @param string $userId */ public function __construct( FileOperationMapper $mapper, + RecoveredFileOperationMapper $recoveredMapper, $userId ) { $this->mapper = $mapper; + $this->recoveredMapper = $recoveredMapper; $this->userId = $userId; } @@ -123,9 +130,14 @@ class FileOperationService * * @param int $id */ - public function deleteById($id) + public function deleteById($id, $addToHistory = false) { + $fileOperation = $this->mapper->find($id, $this->userId); $this->mapper->deleteById($id, $this->userId); + if ($addToHistory) { + $this->recoveredMapper->insert($fileOperation->toRecoveredFileOperation()); + } + } /** @@ -135,7 +147,15 @@ class FileOperationService */ public function deleteSequenceById($sequence) { + $params = []; + array_push($params, $sequence); + array_push($params, $this->userId); + + $fileOperations = $this->mapper->findSequenceById($params, null, null); $this->mapper->deleteSequenceById($sequence, $this->userId); + foreach ($fileOperations as $fileOperation) { + $this->recoveredMapper->insert($fileOperation->toRecoveredFileOperation()); + } } /** diff --git a/lib/Service/RecoveredFileOperationService.php b/lib/Service/RecoveredFileOperationService.php new file mode 100644 index 0000000..c983bf4 --- /dev/null +++ b/lib/Service/RecoveredFileOperationService.php @@ -0,0 +1,159 @@ +<?php + +/** + * @copyright Copyright (c) 2020 Matthias Held <matthias.held@uni-konstanz.de> + * @author Matthias Held <matthias.held@uni-konstanz.de> + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +namespace OCA\RansomwareDetection\Service; + +use OCA\RansomwareDetection\Db\RecoveredFileOperationMapper; +use OCA\RansomwareDetection\Db\FileOperationMapper; + +class RecoveredFileOperationService +{ + /** @var RecoveredFileOperationMapper */ + protected $recoveredMapper; + + /** @var FileOperationMapper */ + protected $mapper; + + /** @var string */ + protected $userId; + + /** + * @param RecoveredFileOperationMapper $recoveredMapper + * @param FileOperationMapper $mapper + * @param string $userId + */ + public function __construct( + RecoveredFileOperationMapper $recoveredMapper, + FileOperationMapper $mapper, + $userId + ) { + $this->recoveredMapper = $recoveredMapper; + $this->mapper = $mapper; + $this->userId = $userId; + } + + /** + * Find one by the id. + * + * @throws \OCP\AppFramework\Db\DoesNotExistException if not found + * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result + * + * @param int $id + * + * @return Entity + */ + public function find($id) + { + return $this->recoveredMapper->find($id, $this->userId); + } + + /** + * Find one by the file name. + * + * @throws \OCP\AppFramework\Db\DoesNotExistException if not found + * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result + * + * @param string $name + * + * @return Entity + */ + public function findOneByFileName($name) + { + return $this->recoveredMapper->findOneByFileName($name, $this->userId); + } + + /** + * Find one with the highest id. + * + * @throws \OCP\AppFramework\Db\DoesNotExistException if not found + * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result + * + * @return Entity + */ + public function findOneWithHighestId() + { + return $this->recoveredMapper->findOneWithHighestId($this->userId); + } + + /** + * Find all. + * + * @param array $params + * @param int $limit + * @param int $offset + * + * @return array + */ + public function findAll(array $params = [], $limit = null, $offset = null) + { + array_push($params, $this->userId); + + return $this->recoveredMapper->findAll($params, $limit, $offset); + } + + /** + * Find sequence by id. + * + * @param array $params + * @param int $limit + * @param int $offset + * + * @return array + */ + public function findSequenceById(array $params = [], $limit = null, $offset = null) + { + array_push($params, $this->userId); + + return $this->recoveredMapper->findSequenceById($params, $limit, $offset); + } + + /** + * Delete one by id. + * + * @param int $id + */ + public function deleteById($id) + { + $fileOperation = $this->recoveredMapper->find($id, $this->userId); + $this->recoveredMapper->deleteById($id, $this->userId); + $this->mapper->insert($fileOperation->toFileOperation()); + } + + /** + * Delete sequence by id. + * + * @param int $sequence + */ + public function deleteSequenceById($sequence) + { + $this->recoveredMapper->deleteSequenceById($sequence, $this->userId); + } + + /** + * Delete all entries before $timestamp. + * + * @param int $timestamp + */ + public function deleteFileOperationsBefore($timestamp) + { + $this->recoveredMapper->deleteFileOperationsBefore($timestamp); + } +} |