UpdateLookupServer.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. /**
  47. * @param AccountManager $accountManager
  48. * @param IClientService $clientService
  49. * @param Signer $signer
  50. * @param IJobList $jobList
  51. * @param IConfig $config
  52. */
  53. public function __construct(AccountManager $accountManager,
  54. IClientService $clientService,
  55. Signer $signer,
  56. IJobList $jobList,
  57. IConfig $config) {
  58. $this->accountManager = $accountManager;
  59. $this->clientService = $clientService;
  60. $this->signer = $signer;
  61. $this->jobList = $jobList;
  62. if($config->getSystemValue('has_internet_connection', true) === false) {
  63. return;
  64. }
  65. $this->lookupServer = $config->getSystemValue('lookup_server', 'https://lookup.nextcloud.com');
  66. if(!empty($this->lookupServer)) {
  67. $this->lookupServer = rtrim($this->lookupServer, '/');
  68. $this->lookupServer .= '/users';
  69. }
  70. }
  71. /**
  72. * @param IUser $user
  73. */
  74. public function userUpdated(IUser $user) {
  75. if(empty($this->lookupServer)) {
  76. return;
  77. }
  78. $userData = $this->accountManager->getUser($user);
  79. $publicData = [];
  80. foreach ($userData as $key => $data) {
  81. if ($data['scope'] === AccountManager::VISIBILITY_PUBLIC) {
  82. $publicData[$key] = $data;
  83. }
  84. }
  85. $this->sendToLookupServer($user, $publicData);
  86. }
  87. /**
  88. * send public user data to the lookup server
  89. *
  90. * @param IUser $user
  91. * @param array $publicData
  92. */
  93. protected function sendToLookupServer(IUser $user, array $publicData) {
  94. $dataArray = ['federationId' => $user->getCloudId()];
  95. if (!empty($publicData)) {
  96. $dataArray['name'] = isset($publicData[AccountManager::PROPERTY_DISPLAYNAME]) ? $publicData[AccountManager::PROPERTY_DISPLAYNAME]['value'] : '';
  97. $dataArray['email'] = isset($publicData[AccountManager::PROPERTY_EMAIL]) ? $publicData[AccountManager::PROPERTY_EMAIL]['value'] : '';
  98. $dataArray['address'] = isset($publicData[AccountManager::PROPERTY_ADDRESS]) ? $publicData[AccountManager::PROPERTY_ADDRESS]['value'] : '';
  99. $dataArray['website'] = isset($publicData[AccountManager::PROPERTY_WEBSITE]) ? $publicData[AccountManager::PROPERTY_WEBSITE]['value'] : '';
  100. $dataArray['twitter'] = isset($publicData[AccountManager::PROPERTY_TWITTER]) ? $publicData[AccountManager::PROPERTY_TWITTER]['value'] : '';
  101. $dataArray['phone'] = isset($publicData[AccountManager::PROPERTY_PHONE]) ? $publicData[AccountManager::PROPERTY_PHONE]['value'] : '';
  102. $dataArray['twitter_signature'] = isset($publicData[AccountManager::PROPERTY_TWITTER]['signature']) ? $publicData[AccountManager::PROPERTY_TWITTER]['signature'] : '';
  103. $dataArray['website_signature'] = isset($publicData[AccountManager::PROPERTY_WEBSITE]['signature']) ? $publicData[AccountManager::PROPERTY_WEBSITE]['signature'] : '';
  104. $dataArray['verificationStatus'] =
  105. [
  106. AccountManager::PROPERTY_WEBSITE => isset($publicData[AccountManager::PROPERTY_WEBSITE]) ? $publicData[AccountManager::PROPERTY_WEBSITE]['verified'] : '',
  107. AccountManager::PROPERTY_TWITTER => isset($publicData[AccountManager::PROPERTY_TWITTER]) ? $publicData[AccountManager::PROPERTY_TWITTER]['verified'] : '',
  108. ];
  109. }
  110. $dataArray = $this->signer->sign('lookupserver', $dataArray, $user);
  111. $httpClient = $this->clientService->newClient();
  112. try {
  113. if (empty($publicData)) {
  114. $httpClient->delete($this->lookupServer,
  115. [
  116. 'body' => json_encode($dataArray),
  117. 'timeout' => 10,
  118. 'connect_timeout' => 3,
  119. ]
  120. );
  121. } else {
  122. $httpClient->post($this->lookupServer,
  123. [
  124. 'body' => json_encode($dataArray),
  125. 'timeout' => 10,
  126. 'connect_timeout' => 3,
  127. ]
  128. );
  129. }
  130. } catch (\Exception $e) {
  131. $this->jobList->add(RetryJob::class,
  132. [
  133. 'dataArray' => $dataArray,
  134. 'retryNo' => 0,
  135. ]
  136. );
  137. }
  138. }
  139. }