Completed
Push — federated-circles ( 08a2a5...ec0d47 )
by Maxence
08:34
created

FederatedController::requestedLink()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 22
rs 8.9197
c 1
b 0
f 0
cc 4
eloc 14
nc 4
nop 4
1
<?php
2
/**
3
 * Circles - Bring cloud-users closer together.
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2017
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
namespace OCA\Circles\Controller;
28
29
use OC\AppFramework\Http;
30
use OCA\Circles\Model\FederatedLink;
31
use OCA\Circles\Service\FederatedService;
32
use OCA\Circles\Service\CirclesService;
33
use OCA\Circles\Service\ConfigService;
34
use OCA\Circles\Service\MembersService;
35
use OCA\Circles\Service\MiscService;
36
use OCA\Circles\Service\SharesService;
37
use OCP\AppFramework\Http\DataResponse;
38
use OCP\IL10N;
39
40
class FederatedController extends BaseController {
41
42
	/** @var string */
43
	protected $userId;
44
45
	/** @var IL10N */
46
	protected $l10n;
47
48
	/** @var ConfigService */
49
	protected $configService;
50
51
	/** @var CirclesService */
52
	protected $circlesService;
53
54
	/** @var MembersService */
55
	protected $membersService;
56
57
	/** @var SharesService */
58
	protected $sharesService;
59
60
	/** @var FederatedService */
61
	protected $federatedService;
62
63
	/** @var MiscService */
64
	protected $miscService;
65
66
67
	/**
68
	 * @PublicPage
69
	 * @NoCSRFRequired
70
	 *
71
	 * @param $token
72
	 * @param $source
73
	 * @param $linkTo
74
	 * @param $address
75
	 *
76
	 * @return DataResponse
77
	 */
78
	public function requestedLink($token, $source, $linkTo, $address) {
79
80
		if (!$this->configService->isFederatedAllowed()) {
81
			return $this->federatedFail('federated_not_allowed');
82
		}
83
84
		$circle = $this->circlesService->infoCircleByName($linkTo);
85
		if ($circle === null) {
86
			return $this->federatedFail('circle_does_not_exist');
87
		}
88
89
		$link = new FederatedLink();
90
		$link->setToken($token)
91
			 ->setRemoteCircleName($source)
92
			 ->setAddress($address);
93
94
		if ($this->federatedService->initiateLink($circle, $link)) {
95
			return $this->federatedSuccess(['status' => $link->getStatus()], $link);
96
		} else {
97
			return $this->federatedFail('link_failed');
98
		}
99
	}
100
101
102
	/** @noinspection PhpSignatureMismatchDuringInheritanceInspection */
103
	/**
104
	 * @param array $data
105
	 * @param FederatedLink $link
106
	 *
107
	 * @return DataResponse
108
	 */
109
	protected function federatedSuccess($data, $link) {
110
		return new DataResponse(
111
			array_merge($data, ['token' => $link->getToken()]), Http::STATUS_OK
112
		);
113
114
	}
115
116
117
	protected function federatedFail($reason) {
118
		return new DataResponse(
119
			[
120
				'status' => FederatedService::STATUS_ERROR,
121
				'reason' => $reason
122
			],
123
			Http::STATUS_OK
124
		);
125
	}
126
}