Completed
Push — master ( ba9b17...94004c )
by Lukas
05:19 queued 04:59
created

UpdateLookupServer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 14

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 14
nc 1
nop 7
dl 15
loc 15
rs 9.4285
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\Manager;
27
use OC\Security\IdentityProof\Signer;
28
use OCA\LookupServerConnector\BackgroundJobs\RetryJob;
29
use OCP\BackgroundJob\IJobList;
30
use OCP\Http\Client\IClientService;
31
use OCP\IConfig;
32
use OCP\IUser;
33
use OCP\Security\ISecureRandom;
34
35
/**
36
 * Class UpdateLookupServer
37
 *
38
 * @package OCA\LookupServerConnector
39
 */
40
class UpdateLookupServer {
41
	/** @var AccountManager */
42
	private $accountManager;
43
	/** @var IConfig */
44
	private $config;
45
	/** @var ISecureRandom */
46
	private $secureRandom;
47
	/** @var IClientService */
48
	private $clientService;
49
	/** @var Manager */
50
	private $keyManager;
51
	/** @var Signer */
52
	private $signer;
53
	/** @var IJobList */
54
	private $jobList;
55
	/** @var string URL point to lookup server */
56
	private $lookupServer = 'https://lookup.nextcloud.com/users';
57
58
	/**
59
	 * @param AccountManager $accountManager
60
	 * @param IConfig $config
61
	 * @param ISecureRandom $secureRandom
62
	 * @param IClientService $clientService
63
	 * @param Manager $manager
64
	 * @param Signer $signer
65
	 * @param IJobList $jobList
66
	 */
67 View Code Duplication
	public function __construct(AccountManager $accountManager,
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68
								IConfig $config,
69
								ISecureRandom $secureRandom,
70
								IClientService $clientService,
71
								Manager $manager,
72
								Signer $signer,
73
								IJobList $jobList) {
74
		$this->accountManager = $accountManager;
75
		$this->config = $config;
76
		$this->secureRandom = $secureRandom;
77
		$this->clientService = $clientService;
78
		$this->keyManager = $manager;
79
		$this->signer = $signer;
80
		$this->jobList = $jobList;
81
	}
82
83
	/**
84
	 * @param IUser $user
85
	 */
86
	public function userUpdated(IUser $user) {
87
		$userData = $this->accountManager->getUser($user);
88
		$publicData = [];
89
90
		foreach ($userData as $key => $data) {
91
			if ($data['scope'] === AccountManager::VISIBILITY_PUBLIC) {
92
				$publicData[$key] = $data;
93
			}
94
		}
95
96
		if (!empty($publicData)) {
97
			$this->sendToLookupServer($user, $publicData);
98
		}
99
	}
100
101
	/**
102
	 * send public user data to the lookup server
103
	 *
104
	 * @param IUser $user
105
	 * @param array $publicData
106
	 */
107
	protected function sendToLookupServer(IUser $user, array $publicData) {
108
		$dataArray = [
109
			'federationId' => $user->getCloudId(),
110
			'name' => isset($publicData[AccountManager::PROPERTY_DISPLAYNAME]) ? $publicData[AccountManager::PROPERTY_DISPLAYNAME]['value'] : '',
111
			'email' => isset($publicData[AccountManager::PROPERTY_EMAIL]) ? $publicData[AccountManager::PROPERTY_EMAIL]['value'] : '',
112
			'address' => isset($publicData[AccountManager::PROPERTY_ADDRESS]) ? $publicData[AccountManager::PROPERTY_ADDRESS]['value'] : '',
113
			'website' => isset($publicData[AccountManager::PROPERTY_WEBSITE]) ? $publicData[AccountManager::PROPERTY_WEBSITE]['value'] : '',
114
			'twitter' => isset($publicData[AccountManager::PROPERTY_TWITTER]) ? $publicData[AccountManager::PROPERTY_TWITTER]['value'] : '',
115
			'phone' => isset($publicData[AccountManager::PROPERTY_PHONE]) ? $publicData[AccountManager::PROPERTY_PHONE]['value'] : '',
116
		];
117
		$dataArray = $this->signer->sign('lookupserver', $dataArray, $user);
118
		$httpClient = $this->clientService->newClient();
119
		try {
120
			$httpClient->post($this->lookupServer,
121
				[
122
					'body' => json_encode($dataArray),
123
					'timeout' => 10,
124
					'connect_timeout' => 3,
125
				]
126
			);
127
		} catch (\Exception $e) {
128
			$this->jobList->add(RetryJob::class,
129
				[
130
					'dataArray' => $dataArray,
131
					'retryNo' => 0,
132
				]
133
			);
134
		}
135
	}
136
}
137