Completed
Push — master ( ca0f2f...6aa6d2 )
by Björn
33:52 queued 15:38
created

DeletedShareAPIController::getRoomShareHelper()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 * @Copyright 2018, Roeland Jago Douma <[email protected]>
5
 * @Copyright 2018, John Molakvoæ (skjnldsv) <[email protected]>
6
 *
7
 * @author John Molakvoæ (skjnldsv) <[email protected]>
8
 * @author Roeland Jago Douma <[email protected]>
9
 *
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
namespace OCA\Files_Sharing\Controller;
28
29
use OCP\App\IAppManager;
30
use OCP\AppFramework\Http\DataResponse;
31
use OCP\AppFramework\OCS\OCSException;
32
use OCP\AppFramework\OCS\OCSNotFoundException;
33
use OCP\AppFramework\OCSController;
34
use OCP\AppFramework\QueryException;
35
use OCP\Files\IRootFolder;
36
use OCP\Files\NotFoundException;
37
use OCP\IGroupManager;
38
use OCP\IRequest;
39
use OCP\IServerContainer;
40
use OCP\IUserManager;
41
use OCP\Share\Exceptions\GenericShareException;
42
use OCP\Share\Exceptions\ShareNotFound;
43
use OCP\Share\IManager as ShareManager;
44
use OCP\Share\IShare;
45
46
class DeletedShareAPIController extends OCSController {
47
48
	/** @var ShareManager */
49
	private $shareManager;
50
51
	/** @var string */
52
	private $userId;
53
54
	/** @var IUserManager */
55
	private $userManager;
56
57
	/** @var IGroupManager */
58
	private $groupManager;
59
60
	/** @var IRootFolder */
61
	private $rootFolder;
62
63
	/** @var IAppManager */
64
	private $appManager;
65
66
	/** @var IServerContainer */
67
	private $serverContainer;
68
69 View Code Duplication
	public function __construct(string $appName,
70
								IRequest $request,
71
								ShareManager $shareManager,
72
								string $UserId,
73
								IUserManager $userManager,
74
								IGroupManager $groupManager,
75
								IRootFolder $rootFolder,
76
								IAppManager $appManager,
77
								IServerContainer $serverContainer) {
78
		parent::__construct($appName, $request);
79
80
		$this->shareManager = $shareManager;
81
		$this->userId = $UserId;
82
		$this->userManager = $userManager;
83
		$this->groupManager = $groupManager;
84
		$this->rootFolder = $rootFolder;
85
		$this->appManager = $appManager;
86
		$this->serverContainer = $serverContainer;
87
	}
88
89
	/**
90
	 * @suppress PhanUndeclaredClassMethod
91
	 */
92
	private function formatShare(IShare $share): array {
93
94
		$result = [
95
			'id' => $share->getFullId(),
96
			'share_type' => $share->getShareType(),
97
			'uid_owner' => $share->getSharedBy(),
98
			'displayname_owner' => $this->userManager->get($share->getSharedBy())->getDisplayName(),
99
			'permissions' => 0,
100
			'stime' => $share->getShareTime()->getTimestamp(),
101
			'parent' => null,
102
			'expiration' => null,
103
			'token' => null,
104
			'uid_file_owner' => $share->getShareOwner(),
105
			'displayname_file_owner' => $this->userManager->get($share->getShareOwner())->getDisplayName(),
106
			'path' => $share->getTarget(),
107
		];
108
		$userFolder = $this->rootFolder->getUserFolder($share->getSharedBy());
109
		$nodes = $userFolder->getById($share->getNodeId());
110 View Code Duplication
		if (empty($nodes)) {
111
			// fallback to guessing the path
112
			$node = $userFolder->get($share->getTarget());
113
			if ($node === null || $share->getTarget() === '') {
114
				throw new NotFoundException();
115
			}
116
		} else {
117
			$node = $nodes[0];
118
		}
119
120
		$result['path'] = $userFolder->getRelativePath($node->getPath());
121
		if ($node instanceOf \OCP\Files\Folder) {
122
			$result['item_type'] = 'folder';
123
		} else {
124
			$result['item_type'] = 'file';
125
		}
126
		$result['mimetype'] = $node->getMimetype();
127
		$result['storage_id'] = $node->getStorage()->getId();
128
		$result['storage'] = $node->getStorage()->getCache()->getNumericStorageId();
129
		$result['item_source'] = $node->getId();
130
		$result['file_source'] = $node->getId();
131
		$result['file_parent'] = $node->getParent()->getId();
132
		$result['file_target'] = $share->getTarget();
133
134
		$expiration = $share->getExpirationDate();
135
		if ($expiration !== null) {
136
			$result['expiration'] = $expiration->format('Y-m-d 00:00:00');
137
		}
138
139
		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
140
			$group = $this->groupManager->get($share->getSharedWith());
141
			$result['share_with'] = $share->getSharedWith();
142
			$result['share_with_displayname'] = $group !== null ? $group->getDisplayName() : $share->getSharedWith();
143 View Code Duplication
		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_ROOM) {
144
			$result['share_with'] = $share->getSharedWith();
145
			$result['share_with_displayname'] = '';
146
147
			try {
148
				$result = array_merge($result, $this->getRoomShareHelper()->formatShare($share));
149
			} catch (QueryException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
150
			}
151
		}
152
153
		return $result;
154
155
	}
156
157
	/**
158
	 * @NoAdminRequired
159
	 */
160
	public function index(): DataResponse {
161
		$groupShares = $this->shareManager->getDeletedSharedWith($this->userId, \OCP\Share::SHARE_TYPE_GROUP, null, -1, 0);
162
		$roomShares = $this->shareManager->getDeletedSharedWith($this->userId, \OCP\Share::SHARE_TYPE_ROOM, null, -1, 0);
163
164
		$shares = array_merge($groupShares, $roomShares);
165
166
		$shares = array_map(function (IShare $share) {
167
			return $this->formatShare($share);
168
		}, $shares);
169
170
		return new DataResponse($shares);
171
	}
172
173
	/**
174
	 * @NoAdminRequired
175
	 *
176
	 * @throws OCSException
177
	 */
178
	public function undelete(string $id): DataResponse {
179
		try {
180
			$share = $this->shareManager->getShareById($id, $this->userId);
181
		} catch (ShareNotFound $e) {
182
			throw new OCSNotFoundException('Share not found');
183
		}
184
185
		if ($share->getPermissions() !== 0) {
186
			throw new OCSNotFoundException('No deleted share found');
187
		}
188
189
		try {
190
			$this->shareManager->restoreShare($share, $this->userId);
191
		} catch (GenericShareException $e) {
192
			throw new OCSException('Something went wrong');
193
		}
194
195
		return new DataResponse([]);
196
	}
197
198
	/**
199
	 * Returns the helper of DeletedShareAPIController for room shares.
200
	 *
201
	 * If the Talk application is not enabled or the helper is not available
202
	 * a QueryException is thrown instead.
203
	 *
204
	 * @return \OCA\Spreed\Share\Helper\DeletedShareAPIController
205
	 * @throws QueryException
206
	 */
207
	private function getRoomShareHelper() {
208
		if (!$this->appManager->isEnabledForUser('spreed')) {
209
			throw new QueryException();
210
		}
211
212
		return $this->serverContainer->query('\OCA\Spreed\Share\Helper\DeletedShareAPIController');
213
	}
214
}
215