Completed
Pull Request — master (#32303)
by Victor
13:39
created

OcmMiddleware   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 145
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 1
A assertNotNull() 0 16 2
A getValidShare() 0 11 3
A assertIncomingSharingEnabled() 0 7 3
A assertOutgoingSharingEnabled() 0 7 3
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\App\IAppManager;
31
use OCA\FederatedFileSharing\FedShareManager;
32
use OCP\ILogger;
33
use OCP\IURLGenerator;
34
use OCP\IUserManager;
35
use OCP\Share;
36
use OCP\Share\IShare;
37
38
/**
39
 * Class OcmMiddleware
40
 *
41
 * @package OCA\FederatedFileSharing\Controller\Middleware
42
 */
43
class OcmMiddleware {
44
	/**
45
	 * @var FederatedShareProvider
46
	 */
47
	protected $federatedShareProvider;
48
49
	/**
50
	 * @var IURLGenerator
51
	 */
52
	protected $urlGenerator;
53
54
	/**
55
	 * @var IAppManager
56
	 */
57
	protected $appManager;
58
59
	/**
60
	 * @var IUserManager
61
	 */
62
	protected $userManager;
63
64
	/**
65
	 * @var AddressHandler
66
	 */
67
	protected $addressHandler;
68
69
	/**
70
	 * @var FedShareManager
71
	 */
72
	protected $fedShareManager;
73
74
	/**
75
	 * @var ILogger
76
	 */
77
	protected $logger;
78
79
	/**
80
	 * constructor.
81
	 *
82
	 * @param FederatedShareProvider $federatedShareProvider
83
	 * @param IURLGenerator $urlGenerator
84
	 * @param IAppManager $appManager
85
	 * @param IUserManager $userManager
86
	 * @param AddressHandler $addressHandler
87
	 * @param FedShareManager $fedShareManager
88
	 * @param ILogger $logger
89
	 */
90
	public function __construct(
91
								FederatedShareProvider $federatedShareProvider,
92
								IURLGenerator $urlGenerator,
93
								IAppManager $appManager,
94
								IUserManager $userManager,
95
								AddressHandler $addressHandler,
96
								FedShareManager $fedShareManager,
97
								ILogger $logger
98
	) {
99
		$this->federatedShareProvider = $federatedShareProvider;
100
		$this->urlGenerator = $urlGenerator;
101
		$this->appManager = $appManager;
102
		$this->userManager = $userManager;
103
		$this->addressHandler = $addressHandler;
104
		$this->fedShareManager = $fedShareManager;
105
		$this->logger = $logger;
106
	}
107
108
	/**
109
	 * Check if value an array has any null item
110
	 *
111
	 * @param string[] $params
112
	 *
113
	 * @return bool
114
	 *
115
	 * @throws BadRequestException
116
	 */
117
	public function assertNotNull($params) {
118
		if (\is_array($params)) {
119
			$nullKeys = \array_keys(
120
				\array_filter(
121
					$params,
122
					function ($b) {
123
						return $b === null;
124
					}
125
				)
126
			);
127
			$nullKeysAsString = \implode(',', $nullKeys);
128
			throw new BadRequestException(
129
				"Required parameters are missing: $nullKeysAsString"
130
			);
131
		}
132
	}
133
134
	/**
135
	 * Get share by id, validate its type and token
136
	 *
137
	 * @param int $id
138
	 * @param string $sharedSecret
139
	 *
140
	 * @return IShare
141
	 *
142
	 * @throws BadRequestException
143
	 * @throws ForbiddenException
144
	 * @throws Share\Exceptions\ShareNotFound
145
	 */
146
	public function getValidShare($id, $sharedSecret) {
147
		$share = $this->federatedShareProvider->getShareById($id);
148
		if ($share->getShareType() !== FederatedShareProvider::SHARE_TYPE_REMOTE) {
149
			throw new BadRequestException("Share with id {$id} does not exist");
150
		}
151
152
		if ($share->getToken() !== $sharedSecret) {
153
			throw new ForbiddenException("The secret does not match");
154
		}
155
		return $share;
156
	}
157
158
	/**
159
	 * Make sure that incoming shares are enabled
160
	 *
161
	 * @return void
162
	 *
163
	 * @throws NotImplementedException
164
	 */
165
	public function assertIncomingSharingEnabled() {
166
		if (!$this->appManager->isEnabledForUser('files_sharing')
167
			|| !$this->federatedShareProvider->isIncomingServer2serverShareEnabled()
168
		) {
169
			throw new NotImplementedException();
170
		}
171
	}
172
173
	/**
174
	 * Make sure that outgoing shares are enabled
175
	 *
176
	 * @return void
177
	 *
178
	 * @throws NotImplementedException
179
	 */
180
	public function assertOutgoingSharingEnabled() {
181
		if (!$this->appManager->isEnabledForUser('files_sharing')
182
			|| !$this->federatedShareProvider->isOutgoingServer2serverShareEnabled()
183
		) {
184
			throw new NotImplementedException();
185
		}
186
	}
187
}
188