UpdateLookupServer.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Bjoern Schiessle <bjoern@schiessle.org>
  4. * @copyright Copyright (c) 2016 Lukas Reschke <lukas@statuscode.ch>
  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. namespace OCA\LookupServerConnector;
  23. use OC\Accounts\AccountManager;
  24. use OC\Security\IdentityProof\Signer;
  25. use OCA\LookupServerConnector\BackgroundJobs\RetryJob;
  26. use OCP\BackgroundJob\IJobList;
  27. use OCP\Http\Client\IClientService;
  28. use OCP\IConfig;
  29. use OCP\IUser;
  30. /**
  31. * Class UpdateLookupServer
  32. *
  33. * @package OCA\LookupServerConnector
  34. */
  35. class UpdateLookupServer {
  36. /** @var AccountManager */
  37. private $accountManager;
  38. /** @var IClientService */
  39. private $clientService;
  40. /** @var Signer */
  41. private $signer;
  42. /** @var IJobList */
  43. private $jobList;
  44. /** @var string URL point to lookup server */
  45. private $lookupServer;
  46. /** @var bool */
  47. private $lookupServerEnabled;
  48. /**
  49. * @param AccountManager $accountManager
  50. * @param IClientService $clientService
  51. * @param Signer $signer
  52. * @param IJobList $jobList
  53. * @param IConfig $config
  54. */
  55. public function __construct(AccountManager $accountManager,
  56. IClientService $clientService,
  57. Signer $signer,
  58. IJobList $jobList,
  59. IConfig $config) {
  60. $this->accountManager = $accountManager;
  61. $this->clientService = $clientService;
  62. $this->signer = $signer;
  63. $this->jobList = $jobList;
  64. if($config->getSystemValue('has_internet_connection', true) === false) {
  65. return;
  66. }
  67. $this->lookupServerEnabled = $config->getAppValue('files_sharing', 'lookupServerUploadEnabled', 'yes') === 'yes';
  68. $this->lookupServer = $config->getSystemValue('lookup_server', 'https://lookup.nextcloud.com');
  69. if(!empty($this->lookupServer)) {
  70. $this->lookupServer = rtrim($this->lookupServer, '/');
  71. $this->lookupServer .= '/users';
  72. }
  73. }
  74. /**
  75. * @param IUser $user
  76. */
  77. public function userUpdated(IUser $user) {
  78. if (!$this->shouldUpdateLookupServer()) {
  79. return;
  80. }
  81. $userData = $this->accountManager->getUser($user);
  82. $publicData = [];
  83. foreach ($userData as $key => $data) {
  84. if ($data['scope'] === AccountManager::VISIBILITY_PUBLIC) {
  85. $publicData[$key] = $data;
  86. }
  87. }
  88. $this->sendToLookupServer($user, $publicData);
  89. }
  90. /**
  91. * send public user data to the lookup server
  92. *
  93. * @param IUser $user
  94. * @param array $publicData
  95. */
  96. protected function sendToLookupServer(IUser $user, array $publicData) {
  97. $dataArray = ['federationId' => $user->getCloudId()];
  98. if (!empty($publicData)) {
  99. $dataArray['name'] = isset($publicData[AccountManager::PROPERTY_DISPLAYNAME]) ? $publicData[AccountManager::PROPERTY_DISPLAYNAME]['value'] : '';
  100. $dataArray['email'] = isset($publicData[AccountManager::PROPERTY_EMAIL]) ? $publicData[AccountManager::PROPERTY_EMAIL]['value'] : '';
  101. $dataArray['address'] = isset($publicData[AccountManager::PROPERTY_ADDRESS]) ? $publicData[AccountManager::PROPERTY_ADDRESS]['value'] : '';
  102. $dataArray['website'] = isset($publicData[AccountManager::PROPERTY_WEBSITE]) ? $publicData[AccountManager::PROPERTY_WEBSITE]['value'] : '';
  103. $dataArray['twitter'] = isset($publicData[AccountManager::PROPERTY_TWITTER]) ? $publicData[AccountManager::PROPERTY_TWITTER]['value'] : '';
  104. $dataArray['phone'] = isset($publicData[AccountManager::PROPERTY_PHONE]) ? $publicData[AccountManager::PROPERTY_PHONE]['value'] : '';
  105. $dataArray['twitter_signature'] = isset($publicData[AccountManager::PROPERTY_TWITTER]['signature']) ? $publicData[AccountManager::PROPERTY_TWITTER]['signature'] : '';
  106. $dataArray['website_signature'] = isset($publicData[AccountManager::PROPERTY_WEBSITE]['signature']) ? $publicData[AccountManager::PROPERTY_WEBSITE]['signature'] : '';
  107. $dataArray['verificationStatus'] =
  108. [
  109. AccountManager::PROPERTY_WEBSITE => isset($publicData[AccountManager::PROPERTY_WEBSITE]) ? $publicData[AccountManager::PROPERTY_WEBSITE]['verified'] : '',
  110. AccountManager::PROPERTY_TWITTER => isset($publicData[AccountManager::PROPERTY_TWITTER]) ? $publicData[AccountManager::PROPERTY_TWITTER]['verified'] : '',
  111. ];
  112. }
  113. $dataArray = $this->signer->sign('lookupserver', $dataArray, $user);
  114. $httpClient = $this->clientService->newClient();
  115. try {
  116. if (empty($publicData)) {
  117. $httpClient->delete($this->lookupServer,
  118. [
  119. 'body' => json_encode($dataArray),
  120. 'timeout' => 10,
  121. 'connect_timeout' => 3,
  122. ]
  123. );
  124. } else {
  125. $httpClient->post($this->lookupServer,
  126. [
  127. 'body' => json_encode($dataArray),
  128. 'timeout' => 10,
  129. 'connect_timeout' => 3,
  130. ]
  131. );
  132. }
  133. } catch (\Exception $e) {
  134. $this->jobList->add(RetryJob::class,
  135. [
  136. 'dataArray' => $dataArray,
  137. 'retryNo' => 0,
  138. ]
  139. );
  140. }
  141. }
  142. /**
  143. * check if we should update the lookup server, we only do it if
  144. *
  145. * * we have a valid URL
  146. * * the lookup server update was enabled by the admin
  147. *
  148. * @return bool
  149. */
  150. private function shouldUpdateLookupServer() {
  151. return $this->lookupServerEnabled && !empty($this->lookupServer);
  152. }
  153. }