1 | <?php |
||
36 | class CredentialService { |
||
37 | |||
38 | private CredentialMapper $credentialMapper; |
||
|
|||
39 | private SharingACLMapper $sharingACL; |
||
40 | private ShareService $shareService; |
||
41 | private EncryptService $encryptService; |
||
42 | private $server_key; |
||
43 | |||
44 | public function __construct(CredentialMapper $credentialMapper, SharingACLMapper $sharingACL, ShareService $shareService, EncryptService $encryptService, IConfig $config) { |
||
45 | $this->credentialMapper = $credentialMapper; |
||
46 | $this->sharingACL = $sharingACL; |
||
47 | $this->shareService = $shareService; |
||
48 | $this->encryptService = $encryptService; |
||
49 | $this->server_key = $config->getSystemValue('passwordsalt', ''); |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Create a new credential |
||
54 | * |
||
55 | * @param array $credential |
||
56 | * @return Credential |
||
57 | * @throws \Exception |
||
58 | */ |
||
59 | public function createCredential(array $credential) { |
||
60 | $credential = $this->encryptService->encryptCredential($credential); |
||
61 | return $this->credentialMapper->create($credential); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Update credential |
||
66 | * |
||
67 | * @param array $credential |
||
68 | * @param false $useRawUser |
||
69 | * @return Credential|Entity |
||
70 | * @throws DoesNotExistException |
||
71 | * @throws MultipleObjectsReturnedException |
||
72 | */ |
||
73 | public function updateCredential(array $credential, $useRawUser = false) { |
||
74 | $credential = $this->encryptService->encryptCredential($credential); |
||
75 | return $this->credentialMapper->updateCredential($credential, $useRawUser); |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Update credential |
||
80 | * |
||
81 | * @param Credential $credential |
||
82 | * @return Credential|Entity |
||
83 | * @throws DoesNotExistException |
||
84 | * @throws MultipleObjectsReturnedException |
||
85 | */ |
||
86 | public function upd(Credential $credential) { |
||
87 | $credential = $this->encryptService->encryptCredential($credential); |
||
88 | return $this->credentialMapper->updateCredential($credential->jsonSerialize(), false); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Delete credential |
||
93 | * |
||
94 | * @param Credential $credential |
||
95 | * @return Entity |
||
96 | */ |
||
97 | public function deleteCredential(Credential $credential) { |
||
98 | $this->shareService->unshareCredential($credential->getGuid()); |
||
99 | return $this->credentialMapper->deleteCredential($credential); |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * Get credentials by vault id |
||
104 | * |
||
105 | * @param int $vault_id |
||
106 | * @param string $user_id |
||
107 | * @return Entity[] |
||
108 | * @throws \Exception |
||
109 | */ |
||
110 | public function getCredentialsByVaultId(int $vault_id, string $user_id) { |
||
111 | $credentials = $this->credentialMapper->getCredentialsByVaultId($vault_id, $user_id); |
||
112 | foreach ($credentials as $index => $credential) { |
||
113 | $credentials[$index] = $this->encryptService->decryptCredential($credential); |
||
114 | } |
||
115 | return $credentials; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Get a random credential from given vault |
||
120 | * |
||
121 | * @param int $vault_id |
||
122 | * @param string $user_id |
||
123 | * @return mixed |
||
124 | */ |
||
125 | public function getRandomCredentialByVaultId(int $vault_id, string $user_id) { |
||
126 | $credentials = $this->credentialMapper->getRandomCredentialByVaultId($vault_id, $user_id); |
||
127 | foreach ($credentials as $index => $credential) { |
||
128 | $credentials[$index] = $this->encryptService->decryptCredential($credential); |
||
129 | } |
||
130 | return array_pop($credentials); |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Get expired credentials. |
||
135 | * |
||
136 | * @param int $timestamp |
||
137 | * @return Entity[] |
||
138 | * @throws \Exception |
||
139 | */ |
||
140 | public function getExpiredCredentials(int $timestamp) { |
||
141 | $credentials = $this->credentialMapper->getExpiredCredentials($timestamp); |
||
142 | foreach ($credentials as $index => $credential) { |
||
143 | $credentials[$index] = $this->encryptService->decryptCredential($credential); |
||
144 | } |
||
145 | return $credentials; |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * Get a single credential. |
||
150 | * |
||
151 | * @param int $credential_id |
||
152 | * @param string $user_id |
||
153 | * @return array|Credential |
||
154 | * @throws DoesNotExistException |
||
155 | * @throws MultipleObjectsReturnedException |
||
156 | */ |
||
157 | public function getCredentialById(int $credential_id, string $user_id) { |
||
158 | $credential = $this->credentialMapper->getCredentialById($credential_id); |
||
159 | if ($credential->getUserId() === $user_id) { |
||
160 | return $this->encryptService->decryptCredential($credential); |
||
161 | } else { |
||
162 | $acl = $this->sharingACL->getItemACL($user_id, $credential->getGuid()); |
||
163 | if ($acl->hasPermission(SharingACL::READ)) { |
||
164 | return $this->encryptService->decryptCredential($credential); |
||
165 | } else { |
||
166 | throw new DoesNotExistException("Did expect one result but found none when executing"); |
||
167 | } |
||
168 | } |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * Get credential label by credential id. |
||
173 | * |
||
174 | * @param int $credential_id |
||
175 | * @return array|Credential |
||
176 | * @throws DoesNotExistException |
||
177 | * @throws MultipleObjectsReturnedException |
||
178 | */ |
||
179 | public function getCredentialLabelById(int $credential_id) { |
||
180 | $credential = $this->credentialMapper->getCredentialLabelById($credential_id); |
||
181 | return $this->encryptService->decryptCredential($credential); |
||
182 | } |
||
183 | |||
198 |