1 | <?php |
||
33 | |||
34 | class CredentialRevisionService { |
||
35 | |||
36 | private CredentialRevisionMapper $credentialRevisionMapper; |
||
|
|||
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); |
||
120 |