Completed
Push — master ( bec1ac...fc3186 )
by Sander
10s
created

ShareService::deleteShareRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
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
27
use Icewind\SMB\Share;
28
use OCA\Passman\Db\CredentialMapper;
29
use OCA\Passman\Db\CredentialRevision;
30
use OCA\Passman\Db\ShareRequest;
31
use OCA\Passman\Db\ShareRequestMapper;
32
use OCA\Passman\Db\SharingACL;
33
use OCA\Passman\Db\SharingACLMapper;
34
use OCA\Passman\Utility\Utils;
35
use OCP\AppFramework\Db\DoesNotExistException;
36
37
class ShareService {
38
	private $sharingACL;
39
	private $shareRequest;
40
	private $credential;
41
	private $revisions;
42
	private $encryptService;
43
44
45
	public function __construct(
46
		SharingACLMapper $sharingACL,
47
		ShareRequestMapper $shareRequest,
48
		CredentialMapper $credentials,
49
		CredentialRevisionService $revisions,
50
		EncryptService $encryptService
51
	) {
52
		$this->sharingACL = $sharingACL;
53
		$this->shareRequest = $shareRequest;
54
		$this->credential = $credentials;
55
		$this->revisions = $revisions;
56
		$this->encryptService = $encryptService;
57
	}
58
59
	/**
60
	 * Creates requests for all the items on the request array of objects.
61
	 * This array must follow this spec:
62
	 *      user_id:    The target user id
63
	 *      vault_id:   The id of the target vault
64
	 *      guid:       The guid of the target vault
65
	 *      key:        The shared key cyphered with the target vault RSA public key
66
	 *
67
	 * @param $target_item_id   string      The shared item ID
68
	 * @param $target_item_guid string      The shared item GUID
69
	 * @param $request_array    array
70
	 * @param $permissions      integer     Must be created with a bitmask from options on the ShareRequest class
71
	 * @return array                        Array of sharing requests
72
	 */
73
	public function createBulkRequests($target_item_id, $target_item_guid, $request_array, $permissions, $credential_owner) {
74
		$created = Utils::getTime();
75
		$requests = array();
76
		foreach ($request_array as $req) {
77
			$t = new ShareRequest();
78
			$t->setItemId($target_item_id);
79
			$t->setItemGuid($target_item_guid);
80
			$t->setTargetUserId($req['user_id']);
81
			$t->setTargetVaultId($req['vault_id']);
82
			$t->setTargetVaultGuid($req['guid']);
83
			$t->setSharedKey($req['key']);
84
			$t->setPermissions($permissions);
85
			$t->setCreated($created);
86
			$t->setFromUserId($credential_owner);
87
			array_push($requests, $this->shareRequest->createRequest($t));
88
		}
89
		return $requests;
90
	}
91
92
	public function createACLEntry(SharingACL $acl) {
93
		if ($acl->getCreated() === null) $acl->setCreated((new \DateTime())->getTimestamp());
94
		return $this->sharingACL->createACLEntry($acl);
95
	}
96
97
	/**
98
	 * Applies the given share, defaults to no expire
99
	 *
100
	 * @param $item_guid
101
	 * @param $target_vault_guid
102
	 * @param $final_shared_key
103
	 */
104
	public function applyShare($item_guid, $target_vault_guid, $final_shared_key) {
105
		$request = $this->shareRequest->getRequestByItemAndVaultGuid($item_guid, $target_vault_guid);
106
		$permissions = $request->getPermissions();
107
108
		$acl = new SharingACL();
109
		$acl->setItemId($request->getItemId());
110
		$acl->setItemGuid($request->getItemGuid());
111
		$acl->setUserId($request->getTargetUserId());
112
		$acl->setCreated($request->getCreated());
113
		$acl->setExpire(0);
114
		$acl->setPermissions($permissions);
115
		$acl->setVaultId($request->getTargetVaultId());
116
		$acl->setVaultGuid($request->getTargetVaultGuid());
117
		$acl->setSharedKey($final_shared_key);
118
119
		$this->sharingACL->createACLEntry($acl);
120
		$this->shareRequest->cleanItemRequestsForUser($request->getItemId(), $request->getTargetUserId());
121
	}
122
123
	/**
124
	 * Obtains pending requests for the given user ID
125
	 *
126
	 * @param $user_id
127
	 * @return \OCA\Passman\Db\ShareRequest[]
128
	 */
129
	public function getUserPendingRequests($user_id) {
130
		return $this->shareRequest->getUserPendingRequests($user_id);
131
	}
132
133
	/**
134
	 * Get shared credentials from a user
135
	 *
136
	 * @param $user_id
137
	 * @param $vault_guid
138
	 * @return \OCA\Passman\Db\SharingACL[]
139
	 */
140
	public function getSharedItems($user_id, $vault_guid) {
141
		$entries = $this->sharingACL->getVaultEntries($user_id, $vault_guid);
142
143
		$return = [];
144
		foreach ($entries as $entry) {
145
			// Check if the user can read the credential, probably unnecesary, but just to be sure
146
			if (!$entry->hasPermission(SharingACL::READ)) continue;
147
			$tmp = $entry->jsonSerialize();
148
			$credential = $this->credential->getCredentialById($entry->getItemId());
149
			$credential = $this->encryptService->decryptCredential($credential);
150
			$tmp['credential_data'] = $credential->jsonSerialize();
151
152
			if (!$entry->hasPermission(SharingACL::FILES)) unset($tmp['credential_data']['files']);
153
			unset($tmp['credential_data']['shared_key']);
154
			$return[] = $tmp;
155
		}
156
		return $return;
157
	}
158
159
	/**
160
	 * Gets the acl for a given item guid
161
	 *
162
	 * @param $user_id
163
	 * @param $item_guid
164
	 * @return SharingACL
165
	 */
166
	public function getACL($user_id, $item_guid) {
167
		return $this->sharingACL->getItemACL($user_id, $item_guid);
168
	}
169
170
	public function getSharedItem($user_id, $item_guid) {
171
		$acl = $this->sharingACL->getItemACL($user_id, $item_guid);
172
173
		// Check if the user can read the credential, probably unnecesary, but just to be sure
174
		if (!$acl->hasPermission(SharingACL::READ)) throw new DoesNotExistException("Item not found or wrong access level");
175
176
		$tmp = $acl->jsonSerialize();
177
		$credential = $this->credential->getCredentialById($acl->getItemId());
178
		$credential = $this->encryptService->decryptCredential($credential);
179
180
		$tmp['credential_data'] = $credential->jsonSerialize();
181
182
		if (!$acl->hasPermission(SharingACL::FILES)) unset($tmp['credential_data']['files']);
183
		unset($tmp['credential_data']['shared_key']);
184
185
		return $tmp;
186
	}
187
188
	/**
189
	 * Gets history from the given item checking the user's permissions to access it
190
	 *
191
	 * @param $user_id
192
	 * @param $item_guid
193
	 * @return CredentialRevision[]
194
	 */
195
	public function getItemHistory($user_id, $item_guid) {
196
		$acl = $this->sharingACL->getItemACL($user_id, $item_guid);
197
		if (!$acl->hasPermission(SharingACL::READ | SharingACL::HISTORY)) return [];
198
199
		return $this->revisions->getRevisions($acl->getItemId());
200
	}
201
202
203
	/**
204
	 * Deletes a share request by the item ID
205
	 *
206
	 * @param ShareRequest $request
207
	 * @return \PDOStatement
208
	 */
209
	public function cleanItemRequestsForUser(ShareRequest $request) {
210
		return $this->shareRequest->cleanItemRequestsForUser($request->getItemId(), $request->getTargetUserId());
211
	}
212
213
	/**
214
	 * Get an share request by id
215
	 *
216
	 * @param $id
217
	 * @return ShareRequest
218
	 */
219
	public function getShareRequestById($id) {
220
		return $this->shareRequest->getShareRequestById($id);
221
	}
222
223
	/**
224
	 * Get an share request by $item_guid and $target_vault_guid
225
	 *
226
	 * @param $item_guid
227
	 * @param $target_vault_guid
228
	 * @return ShareRequest
229
	 */
230
	public function getRequestByGuid($item_guid, $target_vault_guid) {
231
		return $this->shareRequest->getRequestByItemAndVaultGuid($item_guid, $target_vault_guid);
232
	}
233
234
	/**
235
	 * Get the access control list by item guid
236
	 *
237
	 * @param string $item_guid
238
	 * @return \OCA\Passman\Db\SharingACL[]
239
	 */
240
	public function getCredentialAclList($item_guid) {
241
		return $this->sharingACL->getCredentialAclList($item_guid);
242
	}
243
244
	public function getCredentialPendingAclList($item_guid) {
245
		return $this->shareRequest->getRequestsByItemGuidGroupedByUser($item_guid);
246
	}
247
248
	/**
249
	 * Gets the ACL on the credential for the user
250
	 *
251
	 * @param $user_id
252
	 * @param $item_guid
253
	 * @return SharingACL
254
	 */
255
	public function getCredentialAclForUser($user_id, $item_guid) {
256
		return $this->sharingACL->getItemACL($user_id, $item_guid);
257
	}
258
259
	/**
260
	 * Get pending share requests by guid
261
	 *
262
	 * @param  string $item_guid
263
	 * @return \OCA\Passman\Db\ShareRequest[]
264
	 */
265
	public function getShareRequestsByGuid($item_guid) {
266
		return $this->shareRequest->getShareRequestsByItemGuid($item_guid);
267
	}
268
269
	/**
270
	 * Get pending share requests by guid
271
	 *
272
	 * @param  ShareRequest $request
273
	 * @return ShareRequest
274
	 */
275
	public function deleteShareRequest(ShareRequest $request) {
276
		return $this->shareRequest->deleteShareRequest($request);
277
	}
278
279
	/**
280
	 * Delete ACL
281
	 *
282
	 * @param  ShareRequest $request
0 ignored issues
show
Bug introduced by
There is no parameter named $request. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
283
	 * @return \OCA\Passman\Db\ShareRequest[]
284
	 */
285
	public function deleteShareACL(SharingACL $ACL) {
286
		return $this->sharingACL->deleteShareACL($ACL);
287
	}
288
289
	/**
290
	 * Updates the given ACL entry
291
	 *
292
	 * @param SharingACL $sharingACL
293
	 * @return SharingACL
294
	 */
295
	public function updateCredentialACL(SharingACL $sharingACL) {
296
		return $this->sharingACL->updateCredentialACL($sharingACL);
297
	}
298
299
	public function updateCredentialShareRequest(ShareRequest $shareRequest) {
300
		return $this->shareRequest->updateShareRequest($shareRequest);
301
	}
302
303
304
	/**
305
	 * Get pending share requests by guid and uid
306
	 *
307
	 * @param  ShareRequest $request
0 ignored issues
show
Bug introduced by
There is no parameter named $request. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
308
	 * @return \OCA\Passman\Db\ShareRequest[]
309
	 */
310
	public function getPendingShareRequestsForCredential($item_guid, $user_id) {
311
		return $this->shareRequest->getPendingShareRequests($item_guid, $user_id);
312
	}
313
314
315
	public function updatePendingShareRequestsForCredential($item_guid, $user_id, $permissions) {
316
		return $this->shareRequest->updatePendingRequestPermissions($item_guid, $user_id, $permissions);
317
	}
318
319
	/**
320
	 * Clean up on credential destroyed.
321
	 * This will delete all ACL's and share requests.
322
	 * @param string $item_guid
323
	 */
324
325
	public function unshareCredential($item_guid) {
326
		$acl_list = $this->getCredentialAclList($item_guid);
327
		$request_list = $this->getShareRequestsByGuid($item_guid);
328
		foreach ($acl_list as $ACL) {
329
			$this->deleteShareACL($ACL);
330
		}
331
		foreach ($request_list as $request) {
332
			$this->deleteShareRequest($request);
333
			$manager = \OC::$server->getNotificationManager();
334
			$notification = $manager->createNotification();
335
			$notification->setApp('passman')
336
				->setObject('passman_share_request', $request->getId())
337
				->setUser($request->getTargetUserId());
338
			$manager->markProcessed($notification);
339
		}
340
	}
341
}