You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UpdateLookupServer.php 4.9KB

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