Failed Conditions
Push — master ( e8410d...a45585 )
by Marcos
09:36 queued 11s
created

CredentialRevisionService   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 81
rs 10
wmc 7
lcom 1
cbo 3
1
<?php
2
/**
3
 * Nextcloud - passman
4
 *
5
 * @copyright Copyright (c) 2016, Sander Brand ([email protected])
6
 * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected])
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Passman\Service;
25
26
use OCA\Passman\Db\CredentialRevision;
27
use OCA\Passman\Db\CredentialRevisionMapper;
28
use OCP\AppFramework\Db\DoesNotExistException;
29
use OCP\AppFramework\Db\Entity;
30
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
31
use OCP\IConfig;
32
33
34
class CredentialRevisionService {
35
36
	private CredentialRevisionMapper $credentialRevisionMapper;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
37
	private EncryptService $encryptService;
38
	private $server_key;
39
40
	public function __construct(CredentialRevisionMapper $credentialRevisionMapper, EncryptService $encryptService, IConfig $config) {
41
		$this->credentialRevisionMapper = $credentialRevisionMapper;
42
		$this->encryptService = $encryptService;
43
		$this->server_key = $config->getSystemValue('passwordsalt', '');
44
	}
45
46
	/**
47
	 * Create a new revision for a credential
48
	 *
49
	 * @param $credential
50
	 * @param $userId
51
	 * @param $credential_id
52
	 * @param $edited_by
53
	 * @return CredentialRevision
54
	 * @throws \Exception
55
	 */
56
	public function createRevision($credential, $userId, $credential_id, $edited_by) {
57
		$credential = $this->encryptService->encryptCredential($credential);
58
		return $this->credentialRevisionMapper->create($credential, $userId, $credential_id, $edited_by);
59
	}
60
61
	/**
62
	 * Get revisions of a credential
63
	 *
64
	 * @param int $credential_id
65
	 * @param string|null $user_id
66
	 * @return Entity[]
67
	 * @throws \Exception
68
	 */
69
	public function getRevisions(int $credential_id, string $user_id = null) {
70
		$result = $this->credentialRevisionMapper->getRevisions($credential_id, $user_id);
71
		foreach ($result as $index => $revision) {
72
			$c = json_decode(base64_decode($revision->getCredentialData()), true);
73
			$result[$index] = $revision->jsonSerialize();
74
			$result[$index]['credential_data'] = $this->encryptService->decryptCredential($c);
75
		}
76
		return $result;
77
	}
78
79
	/**
80
	 * @param int $credential_id
81
	 * @param string|null $user_id
82
	 * @return Entity
83
	 * @throws DoesNotExistException
84
	 * @throws MultipleObjectsReturnedException
85
	 * @throws \Exception
86
	 */
87
	public function getRevision(int $credential_id, string $user_id = null) {
88
		$revision = $this->credentialRevisionMapper->getRevision($credential_id, $user_id);
89
		$c = json_decode(base64_decode($revision->getCredentialData()), true);
90
		$revision->setCredentialData($this->encryptService->decryptCredential($c));
91
		return $revision;
92
	}
93
94
	/**
95
	 * Delete a revision
96
	 *
97
	 * @param int $revision_id
98
	 * @param string $user_id
99
	 * @return CredentialRevision
100
	 */
101
	public function deleteRevision(int $revision_id, string $user_id) {
102
		return $this->credentialRevisionMapper->deleteRevision($revision_id, $user_id);
103
	}
104
105
	/**
106
	 * Update revision
107
	 *
108
	 * @param CredentialRevision $credentialRevision
109
	 * @return CredentialRevision|Entity
110
	 * @throws \Exception
111
	 */
112
	public function updateRevision(CredentialRevision $credentialRevision) {
113
		$credential_data = $credentialRevision->getCredentialData();
114
		$credential_data = json_decode(base64_decode($credential_data), true);
115
		$credential_data = base64_encode(json_encode($this->encryptService->encryptCredential($credential_data)));
116
		$credentialRevision->setCredentialData($credential_data);
117
		return $this->credentialRevisionMapper->update($credentialRevision);
118
	}
119
}
120