Completed
Pull Request — master (#32303)
by Victor
09:50
created

OcmMiddleware::getValidShare()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 2
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Viktar Dubiniuk <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2018, ownCloud GmbH
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OCA\FederatedFileSharing\Middleware;
23
24
use OCA\FederatedFileSharing\Address;
25
use OCA\FederatedFileSharing\AddressHandler;
26
use OCA\FederatedFileSharing\FederatedShareProvider;
27
use OCA\FederatedFileSharing\Ocm\Exception\BadRequestException;
28
use OCA\FederatedFileSharing\Ocm\Exception\ForbiddenException;
29
use OCA\FederatedFileSharing\Ocm\Exception\NotImplementedException;
30
use OCP\Constants;
31
use OCP\App\IAppManager;
32
use OCP\ILogger;
33
use OCP\IUserManager;
34
use OCP\Share;
35
use OCP\Share\IShare;
36
37
/**
38
 * Class OcmMiddleware
39
 *
40
 * @package OCA\FederatedFileSharing\Controller\Middleware
41
 */
42
class OcmMiddleware {
43
	/**
44
	 * @var FederatedShareProvider
45
	 */
46
	protected $federatedShareProvider;
47
48
	/**
49
	 * @var IAppManager
50
	 */
51
	protected $appManager;
52
53
	/**
54
	 * @var IUserManager
55
	 */
56
	protected $userManager;
57
58
	/**
59
	 * @var AddressHandler
60
	 */
61
	protected $addressHandler;
62
63
	/**
64
	 * @var ILogger
65
	 */
66
	protected $logger;
67
68
	/**
69
	 * constructor.
70
	 *
71
	 * @param FederatedShareProvider $federatedShareProvider
72
	 * @param IAppManager $appManager
73
	 * @param IUserManager $userManager
74
	 * @param AddressHandler $addressHandler
75
	 * @param ILogger $logger
76
	 */
77
	public function __construct(
78
								FederatedShareProvider $federatedShareProvider,
79
								IAppManager $appManager,
80
								IUserManager $userManager,
81
								AddressHandler $addressHandler,
82
								ILogger $logger
83
	) {
84
		$this->federatedShareProvider = $federatedShareProvider;
85
		$this->appManager = $appManager;
86
		$this->userManager = $userManager;
87
		$this->addressHandler = $addressHandler;
88
		$this->logger = $logger;
89
	}
90
91
	/**
92
	 * Check if value an array has any null item
93
	 *
94
	 * @param string[] $params
95
	 *
96
	 * @return bool
97
	 *
98
	 * @throws BadRequestException
99
	 */
100
	public function assertNotNull($params) {
101
		if (\is_array($params)) {
102
			$nullKeys = \array_keys(
103
				\array_filter(
104
					$params,
105
					function ($b) {
106
						return $b === null;
107
					}
108
				)
109
			);
110
			if (\count($nullKeys) > 0) {
111
				$nullKeysAsString = \implode(',', $nullKeys);
112
				throw new BadRequestException(
113
					"Required parameters are missing: $nullKeysAsString"
114
				);
115
			}
116
		}
117
	}
118
119
	/**
120
	 * Get share by id, validate its type and token
121
	 *
122
	 * @param int $id
123
	 * @param string $sharedSecret
124
	 *
125
	 * @return IShare
126
	 *
127
	 * @throws BadRequestException
128
	 * @throws ForbiddenException
129
	 */
130
	public function getValidShare($id, $sharedSecret) {
131
		try {
132
			$share = $this->federatedShareProvider->getShareById($id);
133
		} catch (Share\Exceptions\ShareNotFound $e) {
134
			throw new BadRequestException("Share with id {$id} does not exist");
135
		}
136
		if ($share->getShareType() !== FederatedShareProvider::SHARE_TYPE_REMOTE) {
137
			throw new BadRequestException("Share with id {$id} does not exist");
138
		}
139
140
		if ($share->getToken() !== $sharedSecret) {
141
			throw new ForbiddenException("The secret does not match");
142
		}
143
		return $share;
144
	}
145
146
	/**
147
	 * @param IShare $share
148
	 *
149
	 * @return void
150
	 *
151
	 * @throws BadRequestException
152
	 */
153
	public function assertSharingPermissionSet(IShare $share) {
154
		$reSharingAllowed = $share->getPermissions() & Constants::PERMISSION_SHARE;
155
		if (!$reSharingAllowed) {
156
			throw new BadRequestException("Owner restricted sharing for this resource");
157
		}
158
	}
159
160
	/**
161
	 * @param Address $user1
162
	 * @param Address $user2
163
	 *
164
	 * @return void
165
	 *
166
	 * @throws ForbiddenException
167
	 */
168
	public function assertNotSameUser(Address $user1, Address $user2) {
169
		if ($user1->equalTo($user2)) {
170
			throw new ForbiddenException('Sharing back to the owner is not allowed');
171
		}
172
	}
173
174
	/**
175
	 * Make sure that incoming shares are enabled
176
	 *
177
	 * @return void
178
	 *
179
	 * @throws NotImplementedException
180
	 */
181
	public function assertIncomingSharingEnabled() {
182
		if (!$this->appManager->isEnabledForUser('files_sharing')
183
			|| !$this->federatedShareProvider->isIncomingServer2serverShareEnabled()
184
		) {
185
			throw new NotImplementedException();
186
		}
187
	}
188
189
	/**
190
	 * Make sure that outgoing shares are enabled
191
	 *
192
	 * @return void
193
	 *
194
	 * @throws NotImplementedException
195
	 */
196
	public function assertOutgoingSharingEnabled() {
197
		if (!$this->appManager->isEnabledForUser('files_sharing')
198
			|| !$this->federatedShareProvider->isOutgoingServer2serverShareEnabled()
199
		) {
200
			throw new NotImplementedException();
201
		}
202
	}
203
}
204