UpdateLookupServer.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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\Manager;
  25. use OC\Security\IdentityProof\Signer;
  26. use OCA\LookupServerConnector\BackgroundJobs\RetryJob;
  27. use OCP\BackgroundJob\IJobList;
  28. use OCP\Http\Client\IClientService;
  29. use OCP\IConfig;
  30. use OCP\IUser;
  31. use OCP\Security\ISecureRandom;
  32. /**
  33. * Class UpdateLookupServer
  34. *
  35. * @package OCA\LookupServerConnector
  36. */
  37. class UpdateLookupServer {
  38. /** @var AccountManager */
  39. private $accountManager;
  40. /** @var IConfig */
  41. private $config;
  42. /** @var ISecureRandom */
  43. private $secureRandom;
  44. /** @var IClientService */
  45. private $clientService;
  46. /** @var Manager */
  47. private $keyManager;
  48. /** @var Signer */
  49. private $signer;
  50. /** @var IJobList */
  51. private $jobList;
  52. /** @var string URL point to lookup server */
  53. private $lookupServer = 'https://lookup.nextcloud.com/users';
  54. /**
  55. * @param AccountManager $accountManager
  56. * @param IConfig $config
  57. * @param ISecureRandom $secureRandom
  58. * @param IClientService $clientService
  59. * @param Manager $manager
  60. * @param Signer $signer
  61. * @param IJobList $jobList
  62. */
  63. public function __construct(AccountManager $accountManager,
  64. IConfig $config,
  65. ISecureRandom $secureRandom,
  66. IClientService $clientService,
  67. Manager $manager,
  68. Signer $signer,
  69. IJobList $jobList) {
  70. $this->accountManager = $accountManager;
  71. $this->config = $config;
  72. $this->secureRandom = $secureRandom;
  73. $this->clientService = $clientService;
  74. $this->keyManager = $manager;
  75. $this->signer = $signer;
  76. $this->jobList = $jobList;
  77. }
  78. /**
  79. * @param IUser $user
  80. */
  81. public function userUpdated(IUser $user) {
  82. $userData = $this->accountManager->getUser($user);
  83. $publicData = [];
  84. foreach ($userData as $key => $data) {
  85. if ($data['scope'] === AccountManager::VISIBILITY_PUBLIC) {
  86. $publicData[$key] = $data;
  87. }
  88. }
  89. $this->sendToLookupServer($user, $publicData);
  90. }
  91. /**
  92. * send public user data to the lookup server
  93. *
  94. * @param IUser $user
  95. * @param array $publicData
  96. */
  97. protected function sendToLookupServer(IUser $user, array $publicData) {
  98. $dataArray = ['federationId' => $user->getCloudId()];
  99. if (!empty($publicData)) {
  100. $dataArray['name'] = isset($publicData[AccountManager::PROPERTY_DISPLAYNAME]) ? $publicData[AccountManager::PROPERTY_DISPLAYNAME]['value'] : '';
  101. $dataArray['email'] = isset($publicData[AccountManager::PROPERTY_EMAIL]) ? $publicData[AccountManager::PROPERTY_EMAIL]['value'] : '';
  102. $dataArray['address'] = isset($publicData[AccountManager::PROPERTY_ADDRESS]) ? $publicData[AccountManager::PROPERTY_ADDRESS]['value'] : '';
  103. $dataArray['website'] = isset($publicData[AccountManager::PROPERTY_WEBSITE]) ? $publicData[AccountManager::PROPERTY_WEBSITE]['value'] : '';
  104. $dataArray['twitter'] = isset($publicData[AccountManager::PROPERTY_TWITTER]) ? $publicData[AccountManager::PROPERTY_TWITTER]['value'] : '';
  105. $dataArray['phone'] = isset($publicData[AccountManager::PROPERTY_PHONE]) ? $publicData[AccountManager::PROPERTY_PHONE]['value'] : '';
  106. }
  107. $dataArray = $this->signer->sign('lookupserver', $dataArray, $user);
  108. $httpClient = $this->clientService->newClient();
  109. try {
  110. if (empty($publicData)) {
  111. $httpClient->delete($this->lookupServer,
  112. [
  113. 'body' => json_encode($dataArray),
  114. 'timeout' => 10,
  115. 'connect_timeout' => 3,
  116. ]
  117. );
  118. } else {
  119. $httpClient->post($this->lookupServer,
  120. [
  121. 'body' => json_encode($dataArray),
  122. 'timeout' => 10,
  123. 'connect_timeout' => 3,
  124. ]
  125. );
  126. }
  127. } catch (\Exception $e) {
  128. $this->jobList->add(RetryJob::class,
  129. [
  130. 'dataArray' => $dataArray,
  131. 'retryNo' => 0,
  132. ]
  133. );
  134. }
  135. }
  136. }