Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ShareService.php « Service « lib - github.com/nextcloud/passman.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 922cb5c752b16c76a0c140788f6ed6ebc8c7c32d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
<?php
/**
 * Nextcloud - passman
 *
 * @copyright Copyright (c) 2016, Sander Brand (brantje@gmail.com)
 * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel (wolfi@wolfi.es)
 * @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 <http://www.gnu.org/licenses/>.
 *
 */

namespace OCA\Passman\Service;


use OCA\Passman\Db\CredentialMapper;
use OCA\Passman\Db\ShareRequest;
use OCA\Passman\Db\ShareRequestMapper;
use OCA\Passman\Db\SharingACL;
use OCA\Passman\Db\SharingACLMapper;
use OCA\Passman\Utility\Utils;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\Entity;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\DB\Exception;
use OCP\DB\IResult;
use OCP\Notification\IManager;

class ShareService {
	private SharingACLMapper $sharingACL;
	private ShareRequestMapper $shareRequest;
	private CredentialMapper $credential;
	private CredentialRevisionService $revisions;
	private EncryptService $encryptService;
	private IManager $IManager;


	public function __construct(
		SharingACLMapper $sharingACL,
		ShareRequestMapper $shareRequest,
		CredentialMapper $credentials,
		CredentialRevisionService $revisions,
		EncryptService $encryptService,
		IManager $IManager
	) {
		$this->sharingACL = $sharingACL;
		$this->shareRequest = $shareRequest;
		$this->credential = $credentials;
		$this->revisions = $revisions;
		$this->encryptService = $encryptService;
		$this->IManager = $IManager;
	}

	/**
	 * Creates requests for all the items on the request array of objects.
	 * This array must follow this spec:
	 *      user_id:    The target user id
	 *      vault_id:   The id of the target vault
	 *      guid:       The guid of the target vault
	 *      key:        The shared key cyphered with the target vault RSA public key
	 *
	 * @param $target_item_id   string      The shared item ID
	 * @param $target_item_guid string      The shared item GUID
	 * @param $request_array    array
	 * @param $permissions      integer     Must be created with a bitmask from options on the ShareRequest class
	 * @return array                        Array of sharing requests
	 */
	public function createBulkRequests($target_item_id, $target_item_guid, $request_array, $permissions, $credential_owner) {
		$created = Utils::getTime();
		$requests = array();
		foreach ($request_array as $req) {
			$t = new ShareRequest();
			$t->setItemId($target_item_id);
			$t->setItemGuid($target_item_guid);
			$t->setTargetUserId($req['user_id']);
			$t->setTargetVaultId($req['vault_id']);
			$t->setTargetVaultGuid($req['guid']);
			$t->setSharedKey($req['key']);
			$t->setPermissions($permissions);
			$t->setCreated($created);
			$t->setFromUserId($credential_owner);
			array_push($requests, $this->shareRequest->createRequest($t));
		}
		return $requests;
	}

	/**
	 * @param SharingACL $acl
	 * @return Entity
	 */
	public function createACLEntry(SharingACL $acl) {
		if ($acl->getCreated() === null) $acl->setCreated((new \DateTime())->getTimestamp());
		return $this->sharingACL->createACLEntry($acl);
	}

	/**
	 * Applies the given share, defaults to no expire
	 *
	 * @param string $item_guid
	 * @param string $target_vault_guid
	 * @param string $final_shared_key
	 * @throws DoesNotExistException
	 * @throws Exception
	 * @throws MultipleObjectsReturnedException
	 */
	public function applyShare(string $item_guid, string $target_vault_guid, string $final_shared_key) {
		$request = $this->shareRequest->getRequestByItemAndVaultGuid($item_guid, $target_vault_guid);
		$permissions = $request->getPermissions();

		$acl = new SharingACL();
		$acl->setItemId($request->getItemId());
		$acl->setItemGuid($request->getItemGuid());
		$acl->setUserId($request->getTargetUserId());
		$acl->setCreated($request->getCreated());
		$acl->setExpire(0);
		$acl->setPermissions($permissions);
		$acl->setVaultId($request->getTargetVaultId());
		$acl->setVaultGuid($request->getTargetVaultGuid());
		$acl->setSharedKey($final_shared_key);

		$this->sharingACL->createACLEntry($acl);
		$this->shareRequest->cleanItemRequestsForUser($request->getItemId(), $request->getTargetUserId());
	}

	/**
	 * Obtains pending requests for the given user ID
	 *
	 * @param string $user_id
	 * @return Entity[]
	 */
	public function getUserPendingRequests(string $user_id) {
		return $this->shareRequest->getUserPendingRequests($user_id);
	}

	/**
	 * Get shared credentials from a user
	 *
	 * @param string $user_id
	 * @param string $vault_guid
	 * @return array
	 * @throws DoesNotExistException
	 * @throws MultipleObjectsReturnedException
	 */
	public function getSharedItems(string $user_id, string $vault_guid) {
		$entries = $this->sharingACL->getVaultEntries($user_id, $vault_guid);

		$return = [];
		foreach ($entries as $entry) {
			// Check if the user can read the credential, probably unnecesary, but just to be sure
			if (!$entry->hasPermission(SharingACL::READ)) continue;
			$tmp = $entry->jsonSerialize();
			$credential = $this->credential->getCredentialById($entry->getItemId());
			$credential = $this->encryptService->decryptCredential($credential);
			$tmp['credential_data'] = $credential->jsonSerialize();

			if (!$entry->hasPermission(SharingACL::FILES)) unset($tmp['credential_data']['files']);
			unset($tmp['credential_data']['shared_key']);
			$return[] = $tmp;
		}
		return $return;
	}

	/**
	 * Gets the acl for a given item guid
	 *
	 * @param string $user_id
	 * @param string $item_guid
	 * @return Entity
	 * @throws DoesNotExistException
	 * @throws MultipleObjectsReturnedException
	 */
	public function getACL(string $user_id, string $item_guid) {
		return $this->sharingACL->getItemACL($user_id, $item_guid);
	}

	/**
	 * @param string $user_id
	 * @param string $item_guid
	 * @return array|mixed
	 * @throws DoesNotExistException
	 * @throws MultipleObjectsReturnedException
	 */
	public function getSharedItem(string $user_id, string $item_guid) {
		$acl = $this->sharingACL->getItemACL($user_id, $item_guid);

		// Check if the user can read the credential, probably unnecesary, but just to be sure
		if (!$acl->hasPermission(SharingACL::READ)) throw new DoesNotExistException("Item not found or wrong access level");

		$tmp = $acl->jsonSerialize();
		$credential = $this->credential->getCredentialById($acl->getItemId());
		$credential = $this->encryptService->decryptCredential($credential);

		$tmp['credential_data'] = $credential->jsonSerialize();

		if (!$acl->hasPermission(SharingACL::FILES)) unset($tmp['credential_data']['files']);
		unset($tmp['credential_data']['shared_key']);

		return $tmp;
	}

	/**
	 * Gets history from the given item checking the user's permissions to access it
	 *
	 * @param string $user_id
	 * @param string $item_guid
	 * @return array|Entity[]
	 * @throws DoesNotExistException
	 * @throws MultipleObjectsReturnedException
	 * @throws \Exception
	 */
	public function getItemHistory(string $user_id, string $item_guid) {
		$acl = $this->sharingACL->getItemACL($user_id, $item_guid);
		if (!$acl->hasPermission(SharingACL::READ | SharingACL::HISTORY)) return [];

		return $this->revisions->getRevisions($acl->getItemId());
	}


	/**
	 * Deletes a share request by the item ID
	 *
	 * @param ShareRequest $request
	 * @return int|IResult
	 * @throws Exception
	 */
	public function cleanItemRequestsForUser(ShareRequest $request) {
		return $this->shareRequest->cleanItemRequestsForUser($request->getItemId(), $request->getTargetUserId());
	}

	/**
	 * Get an share request by id
	 *
	 * @param int $id
	 * @return Entity
	 * @throws DoesNotExistException
	 * @throws MultipleObjectsReturnedException
	 */
	public function getShareRequestById(int $id) {
		return $this->shareRequest->getShareRequestById($id);
	}

	/**
	 * Get an share request by $item_guid and $target_vault_guid
	 *
	 * @param string $item_guid
	 * @param string $target_vault_guid
	 * @return Entity
	 * @throws DoesNotExistException
	 * @throws MultipleObjectsReturnedException
	 */
	public function getRequestByGuid(string $item_guid, string $target_vault_guid) {
		return $this->shareRequest->getRequestByItemAndVaultGuid($item_guid, $target_vault_guid);
	}

	/**
	 * Get the access control list by item guid
	 *
	 * @param string $item_guid
	 * @return Entity[]
	 */
	public function getCredentialAclList(string $item_guid) {
		return $this->sharingACL->getCredentialAclList($item_guid);
	}

	/**
	 * @param string $item_guid
	 * @return Entity[]
	 * @throws Exception
	 */
	public function getCredentialPendingAclList(string $item_guid) {
		return $this->shareRequest->getRequestsByItemGuidGroupedByUser($item_guid);
	}

	/**
	 * Gets the ACL on the credential for the user
	 *
	 * @param string $user_id
	 * @param string $item_guid
	 * @return Entity
	 * @throws DoesNotExistException
	 * @throws MultipleObjectsReturnedException
	 */
	public function getCredentialAclForUser(string $user_id, string $item_guid) {
		return $this->sharingACL->getItemACL($user_id, $item_guid);
	}

	/**
	 * Get pending share requests by guid
	 *
	 * @param string $item_guid
	 * @return Entity[]
	 */
	public function getShareRequestsByGuid(string $item_guid) {
		return $this->shareRequest->getShareRequestsByItemGuid($item_guid);
	}

	/**
	 * Get pending share requests by guid
	 *
	 * @param ShareRequest $request
	 * @return ShareRequest
	 */
	public function deleteShareRequest(ShareRequest $request) {
		return $this->shareRequest->deleteShareRequest($request);
	}

	/**
	 * Delete ACL
	 *
	 * @param SharingACL|Entity $ACL
	 * @return SharingACL|Entity
	 */
	public function deleteShareACL(SharingACL $ACL) {
		return $this->sharingACL->deleteShareACL($ACL);
	}

	/**
	 * Updates the given ACL entry
	 *
	 * @param SharingACL $sharingACL
	 * @return SharingACL|Entity
	 */
	public function updateCredentialACL(SharingACL $sharingACL) {
		return $this->sharingACL->updateCredentialACL($sharingACL);
	}

	/**
	 * @param ShareRequest $shareRequest
	 * @return ShareRequest
	 */
	public function updateCredentialShareRequest(ShareRequest $shareRequest) {
		return $this->shareRequest->updateShareRequest($shareRequest);
	}


	/**
	 * Get pending share requests by guid and uid
	 *
	 * @param string $item_guid
	 * @param string $user_id
	 * @return Entity[]
	 */
	public function getPendingShareRequestsForCredential(string $item_guid, string $user_id) {
		return $this->shareRequest->getPendingShareRequests($item_guid, $user_id);
	}

	/**
	 * @param string $item_guid
	 * @param string $user_id
	 * @param int $permissions
	 * @return int|IResult
	 * @throws Exception
	 */
	public function updatePendingShareRequestsForCredential(string $item_guid, string $user_id, int $permissions) {
		return $this->shareRequest->updatePendingRequestPermissions($item_guid, $user_id, $permissions);
	}

	/**
	 * Clean up on credential destroyed.
	 * This will delete all ACL's and share requests.
	 * @param string $item_guid
	 */
	public function unshareCredential(string $item_guid) {
		$acl_list = $this->getCredentialAclList($item_guid);
		$request_list = $this->getShareRequestsByGuid($item_guid);
		foreach ($acl_list as $ACL) {
			$this->deleteShareACL($ACL);
		}
		foreach ($request_list as $request) {
			$this->deleteShareRequest($request);
			$notification = $this->IManager->createNotification();
			$notification->setApp('passman')
				->setObject('passman_share_request', $request->getId())
				->setUser($request->getTargetUserId());
			$this->IManager->markProcessed($notification);
		}
	}
}