Passed
Push — master ( b01c72...9b3625 )
by Roeland
13:09 queued 11s
created
apps/federatedfilesharing/lib/Controller/MountPublicLinkController.php 1 patch
Indentation   +184 added lines, -184 removed lines patch added patch discarded remove patch
@@ -57,188 +57,188 @@
 block discarded – undo
57 57
  */
58 58
 class MountPublicLinkController extends Controller {
59 59
 
60
-	/** @var FederatedShareProvider */
61
-	private $federatedShareProvider;
62
-
63
-	/** @var AddressHandler */
64
-	private $addressHandler;
65
-
66
-	/** @var IManager  */
67
-	private $shareManager;
68
-
69
-	/** @var  ISession */
70
-	private $session;
71
-
72
-	/** @var IL10N */
73
-	private $l;
74
-
75
-	/** @var IUserSession */
76
-	private $userSession;
77
-
78
-	/** @var IClientService */
79
-	private $clientService;
80
-
81
-	/** @var ICloudIdManager  */
82
-	private $cloudIdManager;
83
-
84
-	/**
85
-	 * MountPublicLinkController constructor.
86
-	 *
87
-	 * @param string $appName
88
-	 * @param IRequest $request
89
-	 * @param FederatedShareProvider $federatedShareProvider
90
-	 * @param IManager $shareManager
91
-	 * @param AddressHandler $addressHandler
92
-	 * @param ISession $session
93
-	 * @param IL10N $l
94
-	 * @param IUserSession $userSession
95
-	 * @param IClientService $clientService
96
-	 * @param ICloudIdManager $cloudIdManager
97
-	 */
98
-	public function __construct($appName,
99
-								IRequest $request,
100
-								FederatedShareProvider $federatedShareProvider,
101
-								IManager $shareManager,
102
-								AddressHandler $addressHandler,
103
-								ISession $session,
104
-								IL10N $l,
105
-								IUserSession $userSession,
106
-								IClientService $clientService,
107
-								ICloudIdManager $cloudIdManager
108
-	) {
109
-		parent::__construct($appName, $request);
110
-
111
-		$this->federatedShareProvider = $federatedShareProvider;
112
-		$this->shareManager = $shareManager;
113
-		$this->addressHandler = $addressHandler;
114
-		$this->session = $session;
115
-		$this->l = $l;
116
-		$this->userSession = $userSession;
117
-		$this->clientService = $clientService;
118
-		$this->cloudIdManager = $cloudIdManager;
119
-	}
120
-
121
-	/**
122
-	 * send federated share to a user of a public link
123
-	 *
124
-	 * @NoCSRFRequired
125
-	 * @PublicPage
126
-	 * @BruteForceProtection(action=publicLink2FederatedShare)
127
-	 *
128
-	 * @param string $shareWith
129
-	 * @param string $token
130
-	 * @param string $password
131
-	 * @return JSONResponse
132
-	 */
133
-	public function createFederatedShare($shareWith, $token, $password = '') {
134
-		if (!$this->federatedShareProvider->isOutgoingServer2serverShareEnabled()) {
135
-			return new JSONResponse(
136
-				['message' => 'This server doesn\'t support outgoing federated shares'],
137
-				Http::STATUS_BAD_REQUEST
138
-			);
139
-		}
140
-
141
-		try {
142
-			[, $server] = $this->addressHandler->splitUserRemote($shareWith);
143
-			$share = $this->shareManager->getShareByToken($token);
144
-		} catch (HintException $e) {
145
-			$response = new JSONResponse(['message' => $e->getHint()], Http::STATUS_BAD_REQUEST);
146
-			$response->throttle();
147
-			return $response;
148
-		}
149
-
150
-		// make sure that user is authenticated in case of a password protected link
151
-		$storedPassword = $share->getPassword();
152
-		$authenticated = $this->session->get('public_link_authenticated') === $share->getId() ||
153
-			$this->shareManager->checkPassword($share, $password);
154
-		if (!empty($storedPassword) && !$authenticated) {
155
-			$response = new JSONResponse(
156
-				['message' => 'No permission to access the share'],
157
-				Http::STATUS_BAD_REQUEST
158
-			);
159
-			$response->throttle();
160
-			return $response;
161
-		}
162
-
163
-		if (($share->getPermissions() & Constants::PERMISSION_READ) === 0) {
164
-			$response = new JSONResponse(
165
-				['message' => 'Mounting file drop not supported'],
166
-				Http::STATUS_BAD_REQUEST
167
-			);
168
-			$response->throttle();
169
-			return $response;
170
-		}
171
-
172
-		$share->setSharedWith($shareWith);
173
-		$share->setShareType(IShare::TYPE_REMOTE);
174
-
175
-		try {
176
-			$this->federatedShareProvider->create($share);
177
-		} catch (\Exception $e) {
178
-			\OC::$server->getLogger()->logException($e, [
179
-				'level' => ILogger::WARN,
180
-				'app' => 'federatedfilesharing',
181
-			]);
182
-			return new JSONResponse(['message' => $e->getMessage()], Http::STATUS_BAD_REQUEST);
183
-		}
184
-
185
-		return new JSONResponse(['remoteUrl' => $server]);
186
-	}
187
-
188
-	/**
189
-	 * ask other server to get a federated share
190
-	 *
191
-	 * @NoAdminRequired
192
-	 *
193
-	 * @param string $token
194
-	 * @param string $remote
195
-	 * @param string $password
196
-	 * @param string $owner (only for legacy reasons, can be removed with legacyMountPublicLink())
197
-	 * @param string $ownerDisplayName (only for legacy reasons, can be removed with legacyMountPublicLink())
198
-	 * @param string $name (only for legacy reasons, can be removed with legacyMountPublicLink())
199
-	 * @return JSONResponse
200
-	 */
201
-	public function askForFederatedShare($token, $remote, $password = '', $owner = '', $ownerDisplayName = '', $name = '') {
202
-		// check if server admin allows to mount public links from other servers
203
-		if ($this->federatedShareProvider->isIncomingServer2serverShareEnabled() === false) {
204
-			return new JSONResponse(['message' => $this->l->t('Server to server sharing is not enabled on this server')], Http::STATUS_BAD_REQUEST);
205
-		}
206
-
207
-		$cloudId = $this->cloudIdManager->getCloudId($this->userSession->getUser()->getUID(), $this->addressHandler->generateRemoteURL());
208
-
209
-		$httpClient = $this->clientService->newClient();
210
-
211
-		try {
212
-			$response = $httpClient->post($remote . '/index.php/apps/federatedfilesharing/createFederatedShare',
213
-				[
214
-					'body' =>
215
-						[
216
-							'token' => $token,
217
-							'shareWith' => rtrim($cloudId->getId(), '/'),
218
-							'password' => $password
219
-						],
220
-					'connect_timeout' => 10,
221
-				]
222
-			);
223
-		} catch (\Exception $e) {
224
-			if (empty($password)) {
225
-				$message = $this->l->t("Couldn't establish a federated share.");
226
-			} else {
227
-				$message = $this->l->t("Couldn't establish a federated share, maybe the password was wrong.");
228
-			}
229
-			return new JSONResponse(['message' => $message], Http::STATUS_BAD_REQUEST);
230
-		}
231
-
232
-		$body = $response->getBody();
233
-		$result = json_decode($body, true);
234
-
235
-		if (is_array($result) && isset($result['remoteUrl'])) {
236
-			return new JSONResponse(['message' => $this->l->t('Federated Share request sent, you will receive an invitation. Check your notifications.')]);
237
-		}
238
-
239
-		// if we doesn't get the expected response we assume that we try to add
240
-		// a federated share from a Nextcloud <= 9 server
241
-		$message = $this->l->t("Couldn't establish a federated share, it looks like the server to federate with is too old (Nextcloud <= 9).");
242
-		return new JSONResponse(['message' => $message], Http::STATUS_BAD_REQUEST);
243
-	}
60
+    /** @var FederatedShareProvider */
61
+    private $federatedShareProvider;
62
+
63
+    /** @var AddressHandler */
64
+    private $addressHandler;
65
+
66
+    /** @var IManager  */
67
+    private $shareManager;
68
+
69
+    /** @var  ISession */
70
+    private $session;
71
+
72
+    /** @var IL10N */
73
+    private $l;
74
+
75
+    /** @var IUserSession */
76
+    private $userSession;
77
+
78
+    /** @var IClientService */
79
+    private $clientService;
80
+
81
+    /** @var ICloudIdManager  */
82
+    private $cloudIdManager;
83
+
84
+    /**
85
+     * MountPublicLinkController constructor.
86
+     *
87
+     * @param string $appName
88
+     * @param IRequest $request
89
+     * @param FederatedShareProvider $federatedShareProvider
90
+     * @param IManager $shareManager
91
+     * @param AddressHandler $addressHandler
92
+     * @param ISession $session
93
+     * @param IL10N $l
94
+     * @param IUserSession $userSession
95
+     * @param IClientService $clientService
96
+     * @param ICloudIdManager $cloudIdManager
97
+     */
98
+    public function __construct($appName,
99
+                                IRequest $request,
100
+                                FederatedShareProvider $federatedShareProvider,
101
+                                IManager $shareManager,
102
+                                AddressHandler $addressHandler,
103
+                                ISession $session,
104
+                                IL10N $l,
105
+                                IUserSession $userSession,
106
+                                IClientService $clientService,
107
+                                ICloudIdManager $cloudIdManager
108
+    ) {
109
+        parent::__construct($appName, $request);
110
+
111
+        $this->federatedShareProvider = $federatedShareProvider;
112
+        $this->shareManager = $shareManager;
113
+        $this->addressHandler = $addressHandler;
114
+        $this->session = $session;
115
+        $this->l = $l;
116
+        $this->userSession = $userSession;
117
+        $this->clientService = $clientService;
118
+        $this->cloudIdManager = $cloudIdManager;
119
+    }
120
+
121
+    /**
122
+     * send federated share to a user of a public link
123
+     *
124
+     * @NoCSRFRequired
125
+     * @PublicPage
126
+     * @BruteForceProtection(action=publicLink2FederatedShare)
127
+     *
128
+     * @param string $shareWith
129
+     * @param string $token
130
+     * @param string $password
131
+     * @return JSONResponse
132
+     */
133
+    public function createFederatedShare($shareWith, $token, $password = '') {
134
+        if (!$this->federatedShareProvider->isOutgoingServer2serverShareEnabled()) {
135
+            return new JSONResponse(
136
+                ['message' => 'This server doesn\'t support outgoing federated shares'],
137
+                Http::STATUS_BAD_REQUEST
138
+            );
139
+        }
140
+
141
+        try {
142
+            [, $server] = $this->addressHandler->splitUserRemote($shareWith);
143
+            $share = $this->shareManager->getShareByToken($token);
144
+        } catch (HintException $e) {
145
+            $response = new JSONResponse(['message' => $e->getHint()], Http::STATUS_BAD_REQUEST);
146
+            $response->throttle();
147
+            return $response;
148
+        }
149
+
150
+        // make sure that user is authenticated in case of a password protected link
151
+        $storedPassword = $share->getPassword();
152
+        $authenticated = $this->session->get('public_link_authenticated') === $share->getId() ||
153
+            $this->shareManager->checkPassword($share, $password);
154
+        if (!empty($storedPassword) && !$authenticated) {
155
+            $response = new JSONResponse(
156
+                ['message' => 'No permission to access the share'],
157
+                Http::STATUS_BAD_REQUEST
158
+            );
159
+            $response->throttle();
160
+            return $response;
161
+        }
162
+
163
+        if (($share->getPermissions() & Constants::PERMISSION_READ) === 0) {
164
+            $response = new JSONResponse(
165
+                ['message' => 'Mounting file drop not supported'],
166
+                Http::STATUS_BAD_REQUEST
167
+            );
168
+            $response->throttle();
169
+            return $response;
170
+        }
171
+
172
+        $share->setSharedWith($shareWith);
173
+        $share->setShareType(IShare::TYPE_REMOTE);
174
+
175
+        try {
176
+            $this->federatedShareProvider->create($share);
177
+        } catch (\Exception $e) {
178
+            \OC::$server->getLogger()->logException($e, [
179
+                'level' => ILogger::WARN,
180
+                'app' => 'federatedfilesharing',
181
+            ]);
182
+            return new JSONResponse(['message' => $e->getMessage()], Http::STATUS_BAD_REQUEST);
183
+        }
184
+
185
+        return new JSONResponse(['remoteUrl' => $server]);
186
+    }
187
+
188
+    /**
189
+     * ask other server to get a federated share
190
+     *
191
+     * @NoAdminRequired
192
+     *
193
+     * @param string $token
194
+     * @param string $remote
195
+     * @param string $password
196
+     * @param string $owner (only for legacy reasons, can be removed with legacyMountPublicLink())
197
+     * @param string $ownerDisplayName (only for legacy reasons, can be removed with legacyMountPublicLink())
198
+     * @param string $name (only for legacy reasons, can be removed with legacyMountPublicLink())
199
+     * @return JSONResponse
200
+     */
201
+    public function askForFederatedShare($token, $remote, $password = '', $owner = '', $ownerDisplayName = '', $name = '') {
202
+        // check if server admin allows to mount public links from other servers
203
+        if ($this->federatedShareProvider->isIncomingServer2serverShareEnabled() === false) {
204
+            return new JSONResponse(['message' => $this->l->t('Server to server sharing is not enabled on this server')], Http::STATUS_BAD_REQUEST);
205
+        }
206
+
207
+        $cloudId = $this->cloudIdManager->getCloudId($this->userSession->getUser()->getUID(), $this->addressHandler->generateRemoteURL());
208
+
209
+        $httpClient = $this->clientService->newClient();
210
+
211
+        try {
212
+            $response = $httpClient->post($remote . '/index.php/apps/federatedfilesharing/createFederatedShare',
213
+                [
214
+                    'body' =>
215
+                        [
216
+                            'token' => $token,
217
+                            'shareWith' => rtrim($cloudId->getId(), '/'),
218
+                            'password' => $password
219
+                        ],
220
+                    'connect_timeout' => 10,
221
+                ]
222
+            );
223
+        } catch (\Exception $e) {
224
+            if (empty($password)) {
225
+                $message = $this->l->t("Couldn't establish a federated share.");
226
+            } else {
227
+                $message = $this->l->t("Couldn't establish a federated share, maybe the password was wrong.");
228
+            }
229
+            return new JSONResponse(['message' => $message], Http::STATUS_BAD_REQUEST);
230
+        }
231
+
232
+        $body = $response->getBody();
233
+        $result = json_decode($body, true);
234
+
235
+        if (is_array($result) && isset($result['remoteUrl'])) {
236
+            return new JSONResponse(['message' => $this->l->t('Federated Share request sent, you will receive an invitation. Check your notifications.')]);
237
+        }
238
+
239
+        // if we doesn't get the expected response we assume that we try to add
240
+        // a federated share from a Nextcloud <= 9 server
241
+        $message = $this->l->t("Couldn't establish a federated share, it looks like the server to federate with is too old (Nextcloud <= 9).");
242
+        return new JSONResponse(['message' => $message], Http::STATUS_BAD_REQUEST);
243
+    }
244 244
 }
Please login to merge, or discard this patch.