These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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\Entity; |
||
29 | use OCP\IConfig; |
||
30 | |||
31 | |||
32 | class CredentialRevisionService { |
||
33 | |||
34 | private CredentialRevisionMapper $credentialRevisionMapper; |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
35 | private EncryptService $encryptService; |
||
36 | private $server_key; |
||
37 | |||
38 | public function __construct(CredentialRevisionMapper $credentialRevisionMapper, EncryptService $encryptService, IConfig $config) { |
||
39 | $this->credentialRevisionMapper = $credentialRevisionMapper; |
||
40 | $this->encryptService = $encryptService; |
||
41 | $this->server_key = $config->getSystemValue('passwordsalt', ''); |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * Create a new revision for a credential |
||
46 | * |
||
47 | * @param $credential |
||
48 | * @param $userId |
||
49 | * @param $credential_id |
||
50 | * @param $edited_by |
||
51 | * @return CredentialRevision |
||
52 | * @throws \Exception |
||
53 | */ |
||
54 | public function createRevision($credential, $userId, $credential_id, $edited_by) { |
||
55 | $credential = $this->encryptService->encryptCredential($credential); |
||
56 | return $this->credentialRevisionMapper->create($credential, $userId, $credential_id, $edited_by); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Get revisions of a credential |
||
61 | * |
||
62 | * @param int $credential_id |
||
63 | * @param string|null $user_id |
||
64 | * @return CredentialRevision[] |
||
65 | */ |
||
66 | public function getRevisions(int $credential_id, string $user_id = null) { |
||
67 | $result = $this->credentialRevisionMapper->getRevisions($credential_id, $user_id); |
||
68 | foreach ($result as $index => $revision) { |
||
69 | $c = json_decode(base64_decode($revision->getCredentialData()), true); |
||
70 | $result[$index] = $revision->jsonSerialize(); |
||
71 | $result[$index]['credential_data'] = $this->encryptService->decryptCredential($c); |
||
72 | } |
||
73 | return $result; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * @param int $credential_id |
||
78 | * @param string|null $user_id |
||
79 | * @return CredentialRevision |
||
80 | */ |
||
81 | public function getRevision(int $credential_id, string $user_id = null) { |
||
82 | $revision = $this->credentialRevisionMapper->getRevision($credential_id, $user_id); |
||
83 | $c = json_decode(base64_decode($revision->getCredentialData()), true); |
||
84 | $revision->setCredentialData($this->encryptService->decryptCredential($c)); |
||
85 | return $revision; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Delete a revision |
||
90 | * |
||
91 | * @param int $revision_id |
||
92 | * @param string $user_id |
||
93 | * @return CredentialRevision |
||
94 | */ |
||
95 | public function deleteRevision(int $revision_id, string $user_id) { |
||
96 | return $this->credentialRevisionMapper->deleteRevision($revision_id, $user_id); |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Update revision |
||
101 | * |
||
102 | * @param CredentialRevision $credentialRevision |
||
103 | * @return CredentialRevision|Entity |
||
104 | * @throws \Exception |
||
105 | */ |
||
106 | public function updateRevision(CredentialRevision $credentialRevision) { |
||
107 | $credential_data = $credentialRevision->getCredentialData(); |
||
108 | $credential_data = json_decode(base64_decode($credential_data), true); |
||
109 | $credential_data = base64_encode(json_encode($this->encryptService->encryptCredential($credential_data))); |
||
110 | $credentialRevision->setCredentialData($credential_data); |
||
111 | return $this->credentialRevisionMapper->update($credentialRevision); |
||
112 | } |
||
113 | } |
||
114 |