Passed
Push — master ( 2866f4...9a69b8 )
by Roeland
16:42 queued 13s
created
apps/federatedfilesharing/lib/Controller/MountPublicLinkController.php 1 patch
Indentation   +182 added lines, -182 removed lines patch added patch discarded remove patch
@@ -57,186 +57,186 @@
 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
-			return new JSONResponse(['message' => $e->getHint()], Http::STATUS_BAD_REQUEST);
146
-		}
147
-
148
-		// make sure that user is authenticated in case of a password protected link
149
-		$storedPassword = $share->getPassword();
150
-		$authenticated = $this->session->get('public_link_authenticated') === $share->getId() ||
151
-			$this->shareManager->checkPassword($share, $password);
152
-		if (!empty($storedPassword) && !$authenticated) {
153
-			$response = new JSONResponse(
154
-				['message' => 'No permission to access the share'],
155
-				Http::STATUS_BAD_REQUEST
156
-			);
157
-			$response->throttle();
158
-			return $response;
159
-		}
160
-
161
-		if (($share->getPermissions() & Constants::PERMISSION_READ) === 0) {
162
-			$response = new JSONResponse(
163
-				['message' => 'Mounting file drop not supported'],
164
-				Http::STATUS_BAD_REQUEST
165
-			);
166
-			$response->throttle();
167
-			return $response;
168
-		}
169
-
170
-		$share->setSharedWith($shareWith);
171
-		$share->setShareType(IShare::TYPE_REMOTE);
172
-
173
-		try {
174
-			$this->federatedShareProvider->create($share);
175
-		} catch (\Exception $e) {
176
-			\OC::$server->getLogger()->logException($e, [
177
-				'level' => ILogger::WARN,
178
-				'app' => 'federatedfilesharing',
179
-			]);
180
-			return new JSONResponse(['message' => $e->getMessage()], Http::STATUS_BAD_REQUEST);
181
-		}
182
-
183
-		return new JSONResponse(['remoteUrl' => $server]);
184
-	}
185
-
186
-	/**
187
-	 * ask other server to get a federated share
188
-	 *
189
-	 * @NoAdminRequired
190
-	 *
191
-	 * @param string $token
192
-	 * @param string $remote
193
-	 * @param string $password
194
-	 * @param string $owner (only for legacy reasons, can be removed with legacyMountPublicLink())
195
-	 * @param string $ownerDisplayName (only for legacy reasons, can be removed with legacyMountPublicLink())
196
-	 * @param string $name (only for legacy reasons, can be removed with legacyMountPublicLink())
197
-	 * @return JSONResponse
198
-	 */
199
-	public function askForFederatedShare($token, $remote, $password = '', $owner = '', $ownerDisplayName = '', $name = '') {
200
-		// check if server admin allows to mount public links from other servers
201
-		if ($this->federatedShareProvider->isIncomingServer2serverShareEnabled() === false) {
202
-			return new JSONResponse(['message' => $this->l->t('Server to server sharing is not enabled on this server')], Http::STATUS_BAD_REQUEST);
203
-		}
204
-
205
-		$cloudId = $this->cloudIdManager->getCloudId($this->userSession->getUser()->getUID(), $this->addressHandler->generateRemoteURL());
206
-
207
-		$httpClient = $this->clientService->newClient();
208
-
209
-		try {
210
-			$response = $httpClient->post($remote . '/index.php/apps/federatedfilesharing/createFederatedShare',
211
-				[
212
-					'body' =>
213
-						[
214
-							'token' => $token,
215
-							'shareWith' => rtrim($cloudId->getId(), '/'),
216
-							'password' => $password
217
-						],
218
-					'connect_timeout' => 10,
219
-				]
220
-			);
221
-		} catch (\Exception $e) {
222
-			if (empty($password)) {
223
-				$message = $this->l->t("Couldn't establish a federated share.");
224
-			} else {
225
-				$message = $this->l->t("Couldn't establish a federated share, maybe the password was wrong.");
226
-			}
227
-			return new JSONResponse(['message' => $message], Http::STATUS_BAD_REQUEST);
228
-		}
229
-
230
-		$body = $response->getBody();
231
-		$result = json_decode($body, true);
232
-
233
-		if (is_array($result) && isset($result['remoteUrl'])) {
234
-			return new JSONResponse(['message' => $this->l->t('Federated Share request sent, you will receive an invitation. Check your notifications.')]);
235
-		}
236
-
237
-		// if we doesn't get the expected response we assume that we try to add
238
-		// a federated share from a Nextcloud <= 9 server
239
-		$message = $this->l->t("Couldn't establish a federated share, it looks like the server to federate with is too old (Nextcloud <= 9).");
240
-		return new JSONResponse(['message' => $message], Http::STATUS_BAD_REQUEST);
241
-	}
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
+            return new JSONResponse(['message' => $e->getHint()], Http::STATUS_BAD_REQUEST);
146
+        }
147
+
148
+        // make sure that user is authenticated in case of a password protected link
149
+        $storedPassword = $share->getPassword();
150
+        $authenticated = $this->session->get('public_link_authenticated') === $share->getId() ||
151
+            $this->shareManager->checkPassword($share, $password);
152
+        if (!empty($storedPassword) && !$authenticated) {
153
+            $response = new JSONResponse(
154
+                ['message' => 'No permission to access the share'],
155
+                Http::STATUS_BAD_REQUEST
156
+            );
157
+            $response->throttle();
158
+            return $response;
159
+        }
160
+
161
+        if (($share->getPermissions() & Constants::PERMISSION_READ) === 0) {
162
+            $response = new JSONResponse(
163
+                ['message' => 'Mounting file drop not supported'],
164
+                Http::STATUS_BAD_REQUEST
165
+            );
166
+            $response->throttle();
167
+            return $response;
168
+        }
169
+
170
+        $share->setSharedWith($shareWith);
171
+        $share->setShareType(IShare::TYPE_REMOTE);
172
+
173
+        try {
174
+            $this->federatedShareProvider->create($share);
175
+        } catch (\Exception $e) {
176
+            \OC::$server->getLogger()->logException($e, [
177
+                'level' => ILogger::WARN,
178
+                'app' => 'federatedfilesharing',
179
+            ]);
180
+            return new JSONResponse(['message' => $e->getMessage()], Http::STATUS_BAD_REQUEST);
181
+        }
182
+
183
+        return new JSONResponse(['remoteUrl' => $server]);
184
+    }
185
+
186
+    /**
187
+     * ask other server to get a federated share
188
+     *
189
+     * @NoAdminRequired
190
+     *
191
+     * @param string $token
192
+     * @param string $remote
193
+     * @param string $password
194
+     * @param string $owner (only for legacy reasons, can be removed with legacyMountPublicLink())
195
+     * @param string $ownerDisplayName (only for legacy reasons, can be removed with legacyMountPublicLink())
196
+     * @param string $name (only for legacy reasons, can be removed with legacyMountPublicLink())
197
+     * @return JSONResponse
198
+     */
199
+    public function askForFederatedShare($token, $remote, $password = '', $owner = '', $ownerDisplayName = '', $name = '') {
200
+        // check if server admin allows to mount public links from other servers
201
+        if ($this->federatedShareProvider->isIncomingServer2serverShareEnabled() === false) {
202
+            return new JSONResponse(['message' => $this->l->t('Server to server sharing is not enabled on this server')], Http::STATUS_BAD_REQUEST);
203
+        }
204
+
205
+        $cloudId = $this->cloudIdManager->getCloudId($this->userSession->getUser()->getUID(), $this->addressHandler->generateRemoteURL());
206
+
207
+        $httpClient = $this->clientService->newClient();
208
+
209
+        try {
210
+            $response = $httpClient->post($remote . '/index.php/apps/federatedfilesharing/createFederatedShare',
211
+                [
212
+                    'body' =>
213
+                        [
214
+                            'token' => $token,
215
+                            'shareWith' => rtrim($cloudId->getId(), '/'),
216
+                            'password' => $password
217
+                        ],
218
+                    'connect_timeout' => 10,
219
+                ]
220
+            );
221
+        } catch (\Exception $e) {
222
+            if (empty($password)) {
223
+                $message = $this->l->t("Couldn't establish a federated share.");
224
+            } else {
225
+                $message = $this->l->t("Couldn't establish a federated share, maybe the password was wrong.");
226
+            }
227
+            return new JSONResponse(['message' => $message], Http::STATUS_BAD_REQUEST);
228
+        }
229
+
230
+        $body = $response->getBody();
231
+        $result = json_decode($body, true);
232
+
233
+        if (is_array($result) && isset($result['remoteUrl'])) {
234
+            return new JSONResponse(['message' => $this->l->t('Federated Share request sent, you will receive an invitation. Check your notifications.')]);
235
+        }
236
+
237
+        // if we doesn't get the expected response we assume that we try to add
238
+        // a federated share from a Nextcloud <= 9 server
239
+        $message = $this->l->t("Couldn't establish a federated share, it looks like the server to federate with is too old (Nextcloud <= 9).");
240
+        return new JSONResponse(['message' => $message], Http::STATUS_BAD_REQUEST);
241
+    }
242 242
 }
Please login to merge, or discard this patch.