Completed
Push — federated-circles ( 74a72b...08a2a5 )
by Maxence
02:33
created

FederatedController::requestedLink()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
rs 9.0534
cc 4
eloc 13
nc 4
nop 3
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 $sourceId
72
	 * @param $sourceName
73
	 * @param string $circleName
74
	 *
75
	 * @return DataResponse
76
	 */
77
	public function requestedLink($sourceId, $sourceName, $circleName) {
78
79
		if (!$this->configService->isFederatedAllowed()) {
80
			return $this->federatedFail('federated_not_allowed');
81
		}
82
83
		$circle = $this->circlesService->infoCircleByName($circleName);
84
		if ($circle === null) {
85
			return $this->federatedFail('circle_does_not_exist');
86
		}
87
88
		$link = new FederatedLink();
89
		$link->setRemoteCircleId($sourceId)
90
			 ->setRemoteCircleName($sourceName);
91
92
		if ($this->federatedService->initiateLink($circle, $link)) {
93
			return $this->federatedSuccess(['status' => $link->getStatus()], $link);
94
		} else {
95
			return $this->federatedFail('link_failed');
96
		}
97
	}
98
99
100
	/** @noinspection PhpSignatureMismatchDuringInheritanceInspection */
101
	/**
102
	 * @param array $data
103
	 * @param FederatedLink $link
104
	 *
105
	 * @return DataResponse
106
	 */
107
	protected function federatedSuccess($data, $link) {
108
		return new DataResponse(
109
			array_merge($data, ['token' => $link->getToken()], Http::STATUS_OK)
110
		);
111
112
	}
113
114
115
	protected function federatedFail($reason) {
116
		return new DataResponse(
117
			[
118
				'status' => FederatedService::STATUS_ERROR,
119
				'reason' => $reason
120
			],
121
			Http::STATUS_OK
122
		);
123
	}
124
}