Passed
Push — master ( bbb168...138fdf )
by Roeland
14:18 queued 10s
created

UpdateLookupServer::shouldUpdateLookupServer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Bjoern Schiessle <[email protected]>
4
 * @copyright Copyright (c) 2016 Lukas Reschke <[email protected]>
5
 *
6
 * @license GNU AGPL version 3 or any later version
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License as
10
 * published by the Free Software Foundation, either version 3 of the
11
 * License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
23
namespace OCA\LookupServerConnector;
24
25
use OC\Accounts\AccountManager;
26
use OC\Security\IdentityProof\Signer;
27
use OCA\LookupServerConnector\BackgroundJobs\RetryJob;
28
use OCP\BackgroundJob\IJobList;
29
use OCP\Http\Client\IClientService;
30
use OCP\IConfig;
31
use OCP\IUser;
32
33
/**
34
 * Class UpdateLookupServer
35
 *
36
 * @package OCA\LookupServerConnector
37
 */
38
class UpdateLookupServer {
39
	/** @var AccountManager */
40
	private $accountManager;
41
	/** @var IClientService */
42
	private $clientService;
43
	/** @var Signer */
44
	private $signer;
45
	/** @var IJobList */
46
	private $jobList;
47
	/** @var string URL point to lookup server */
48
	private $lookupServer;
49
	/** @var bool  */
50
	private $lookupServerEnabled;
51
52
	/**
53
	 * @param AccountManager $accountManager
54
	 * @param IClientService $clientService
55
	 * @param Signer $signer
56
	 * @param IJobList $jobList
57
	 * @param IConfig $config
58
	 */
59
	public function __construct(AccountManager $accountManager,
60
								IClientService $clientService,
61
								Signer $signer,
62
								IJobList $jobList,
63
								IConfig $config) {
64
		$this->accountManager = $accountManager;
65
		$this->clientService = $clientService;
66
		$this->signer = $signer;
67
		$this->jobList = $jobList;
68
69
		if($config->getSystemValue('has_internet_connection', true) === false) {
70
			return;
71
		}
72
73
		$this->lookupServerEnabled = $this->config->getAppValue('files_sharing', 'lookupServerUploadEnabled', 'yes') === 'yes';
0 ignored issues
show
Bug Best Practice introduced by
The property config does not exist on OCA\LookupServerConnector\UpdateLookupServer. Did you maybe forget to declare it?
Loading history...
74
75
		$this->lookupServer = $config->getSystemValue('lookup_server', 'https://lookup.nextcloud.com');
76
		if(!empty($this->lookupServer)) {
77
			$this->lookupServer = rtrim($this->lookupServer, '/');
78
			$this->lookupServer .= '/users';
79
		}
80
	}
81
82
	/**
83
	 * @param IUser $user
84
	 */
85
	public function userUpdated(IUser $user) {
86
87
		if (!$this->shouldUpdateLookupServer()) {
88
			return;
89
		}
90
91
		$userData = $this->accountManager->getUser($user);
92
		$publicData = [];
93
94
		foreach ($userData as $key => $data) {
95
			if ($data['scope'] === AccountManager::VISIBILITY_PUBLIC) {
96
				$publicData[$key] = $data;
97
			}
98
		}
99
100
		$this->sendToLookupServer($user, $publicData);
101
	}
102
103
	/**
104
	 * send public user data to the lookup server
105
	 *
106
	 * @param IUser $user
107
	 * @param array $publicData
108
	 */
109
	protected function sendToLookupServer(IUser $user, array $publicData) {
110
111
		$dataArray = ['federationId' => $user->getCloudId()];
112
113
		if (!empty($publicData)) {
114
			$dataArray['name'] = isset($publicData[AccountManager::PROPERTY_DISPLAYNAME]) ? $publicData[AccountManager::PROPERTY_DISPLAYNAME]['value'] : '';
115
			$dataArray['email'] = isset($publicData[AccountManager::PROPERTY_EMAIL]) ? $publicData[AccountManager::PROPERTY_EMAIL]['value'] : '';
116
			$dataArray['address'] = isset($publicData[AccountManager::PROPERTY_ADDRESS]) ? $publicData[AccountManager::PROPERTY_ADDRESS]['value'] : '';
117
			$dataArray['website'] = isset($publicData[AccountManager::PROPERTY_WEBSITE]) ? $publicData[AccountManager::PROPERTY_WEBSITE]['value'] : '';
118
			$dataArray['twitter'] = isset($publicData[AccountManager::PROPERTY_TWITTER]) ? $publicData[AccountManager::PROPERTY_TWITTER]['value'] : '';
119
			$dataArray['phone'] = isset($publicData[AccountManager::PROPERTY_PHONE]) ? $publicData[AccountManager::PROPERTY_PHONE]['value'] : '';
120
			$dataArray['twitter_signature'] = isset($publicData[AccountManager::PROPERTY_TWITTER]['signature']) ? $publicData[AccountManager::PROPERTY_TWITTER]['signature'] : '';
121
			$dataArray['website_signature'] = isset($publicData[AccountManager::PROPERTY_WEBSITE]['signature']) ? $publicData[AccountManager::PROPERTY_WEBSITE]['signature'] : '';
122
			$dataArray['verificationStatus'] =
123
				[
124
					AccountManager::PROPERTY_WEBSITE => isset($publicData[AccountManager::PROPERTY_WEBSITE]) ? $publicData[AccountManager::PROPERTY_WEBSITE]['verified'] : '',
125
					AccountManager::PROPERTY_TWITTER => isset($publicData[AccountManager::PROPERTY_TWITTER]) ? $publicData[AccountManager::PROPERTY_TWITTER]['verified'] : '',
126
				];
127
		}
128
129
		$dataArray = $this->signer->sign('lookupserver', $dataArray, $user);
130
		$httpClient = $this->clientService->newClient();
131
		try {
132
			if (empty($publicData)) {
133
				$httpClient->delete($this->lookupServer,
134
					[
135
						'body' => json_encode($dataArray),
136
						'timeout' => 10,
137
						'connect_timeout' => 3,
138
					]
139
				);
140
			} else {
141
				$httpClient->post($this->lookupServer,
142
					[
143
						'body' => json_encode($dataArray),
144
						'timeout' => 10,
145
						'connect_timeout' => 3,
146
					]
147
				);
148
			}
149
		} catch (\Exception $e) {
150
			$this->jobList->add(RetryJob::class,
151
				[
152
					'dataArray' => $dataArray,
153
					'retryNo' => 0,
154
				]
155
			);
156
		}
157
	}
158
159
	/**
160
	 * check if we should update the lookup server, we only do it if
161
	 *
162
	 * * we have a valid URL
163
	 * * the lookup server update was enabled by the admin
164
	 *
165
	 * @return bool
166
	 */
167
	private function shouldUpdateLookupServer() {
168
		return $this->lookupServerEnabled || !empty($this->lookupServer);
169
	}
170
171
}
172