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