2016-11-17 14:14:13 +03:00
|
|
|
<?php
|
2019-12-03 21:57:53 +03:00
|
|
|
|
2019-04-17 18:31:22 +03:00
|
|
|
declare(strict_types=1);
|
2019-12-03 21:57:53 +03:00
|
|
|
|
2016-11-17 14:14:13 +03:00
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2016 Bjoern Schiessle <bjoern@schiessle.org>
|
2019-04-17 18:31:22 +03:00
|
|
|
* @copyright Copyright (c) 2019 Joas Schilling <coding@schilljs.com>
|
2016-11-17 14:14:13 +03:00
|
|
|
*
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
|
|
|
* @author Bjoern Schiessle <bjoern@schiessle.org>
|
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
*
|
2016-11-17 14:14:13 +03:00
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2016-11-17 14:14:13 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\LookupServerConnector\BackgroundJobs;
|
|
|
|
|
2019-04-17 18:31:22 +03:00
|
|
|
use OC\Security\IdentityProof\Signer;
|
|
|
|
use OCP\Accounts\IAccountManager;
|
2019-04-17 16:40:17 +03:00
|
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
2016-11-18 17:33:51 +03:00
|
|
|
use OCP\BackgroundJob\IJobList;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCP\BackgroundJob\Job;
|
2016-11-18 17:33:51 +03:00
|
|
|
use OCP\Http\Client\IClientService;
|
2017-05-10 12:59:14 +03:00
|
|
|
use OCP\IConfig;
|
2017-04-27 17:45:13 +03:00
|
|
|
use OCP\ILogger;
|
2019-04-17 18:31:22 +03:00
|
|
|
use OCP\IUser;
|
|
|
|
use OCP\IUserManager;
|
2016-11-17 14:14:13 +03:00
|
|
|
|
|
|
|
class RetryJob extends Job {
|
2016-11-18 17:33:51 +03:00
|
|
|
/** @var IClientService */
|
|
|
|
private $clientService;
|
|
|
|
/** @var string */
|
2017-05-10 12:59:14 +03:00
|
|
|
private $lookupServer;
|
2019-03-05 12:08:27 +03:00
|
|
|
/** @var IConfig */
|
|
|
|
private $config;
|
2019-04-17 18:31:22 +03:00
|
|
|
/** @var IUserManager */
|
|
|
|
private $userManager;
|
|
|
|
/** @var IAccountManager */
|
|
|
|
private $accountManager;
|
|
|
|
/** @var Signer */
|
|
|
|
private $signer;
|
|
|
|
/** @var int */
|
|
|
|
protected $retries = 0;
|
|
|
|
/** @var bool */
|
|
|
|
protected $retainJob = false;
|
2016-11-18 17:33:51 +03:00
|
|
|
|
|
|
|
/**
|
2019-04-17 16:40:17 +03:00
|
|
|
* @param ITimeFactory $time
|
2017-04-27 17:45:13 +03:00
|
|
|
* @param IClientService $clientService
|
2017-05-10 12:59:14 +03:00
|
|
|
* @param IConfig $config
|
2019-04-17 18:31:22 +03:00
|
|
|
* @param IUserManager $userManager
|
|
|
|
* @param IAccountManager $accountManager
|
|
|
|
* @param Signer $signer
|
2016-11-18 17:33:51 +03:00
|
|
|
*/
|
2019-04-17 16:40:17 +03:00
|
|
|
public function __construct(ITimeFactory $time,
|
|
|
|
IClientService $clientService,
|
2019-04-17 18:31:22 +03:00
|
|
|
IConfig $config,
|
|
|
|
IUserManager $userManager,
|
|
|
|
IAccountManager $accountManager,
|
|
|
|
Signer $signer) {
|
2019-04-17 16:40:17 +03:00
|
|
|
parent::__construct($time);
|
2017-04-27 17:45:13 +03:00
|
|
|
$this->clientService = $clientService;
|
2019-03-05 12:08:27 +03:00
|
|
|
$this->config = $config;
|
2019-04-17 18:31:22 +03:00
|
|
|
$this->userManager = $userManager;
|
|
|
|
$this->accountManager = $accountManager;
|
|
|
|
$this->signer = $signer;
|
2018-10-17 14:09:11 +03:00
|
|
|
|
2017-05-10 12:59:14 +03:00
|
|
|
$this->lookupServer = $config->getSystemValue('lookup_server', 'https://lookup.nextcloud.com');
|
2018-10-17 14:09:11 +03:00
|
|
|
if (!empty($this->lookupServer)) {
|
|
|
|
$this->lookupServer = rtrim($this->lookupServer, '/');
|
|
|
|
$this->lookupServer .= '/users';
|
|
|
|
}
|
2017-04-27 17:45:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* run the job, then remove it from the jobList
|
|
|
|
*
|
2019-04-17 16:40:17 +03:00
|
|
|
* @param IJobList $jobList
|
2017-07-19 17:06:22 +03:00
|
|
|
* @param ILogger|null $logger
|
2017-04-27 17:45:13 +03:00
|
|
|
*/
|
2020-11-24 16:18:47 +03:00
|
|
|
public function execute(IJobList $jobList, ILogger $logger = null): void {
|
2019-04-17 18:31:22 +03:00
|
|
|
if (!isset($this->argument['userId'])) {
|
|
|
|
// Old background job without user id, just drop it.
|
2017-04-27 17:45:13 +03:00
|
|
|
$jobList->remove($this, $this->argument);
|
2019-04-17 18:31:22 +03:00
|
|
|
return;
|
2016-11-18 17:33:51 +03:00
|
|
|
}
|
2019-04-17 18:31:22 +03:00
|
|
|
|
|
|
|
$this->retries = (int) $this->config->getUserValue($this->argument['userId'], 'lookup_server_connector', 'update_retries', 0);
|
|
|
|
|
|
|
|
if ($this->shouldRemoveBackgroundJob()) {
|
|
|
|
$jobList->remove($this, $this->argument);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->shouldRun()) {
|
|
|
|
parent::execute($jobList, $logger);
|
|
|
|
if (!$this->retainJob) {
|
|
|
|
$jobList->remove($this, $this->argument);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if we should kill the background job:
|
|
|
|
*
|
|
|
|
* - internet connection is disabled
|
|
|
|
* - no valid lookup server URL given
|
|
|
|
* - lookup server was disabled by the admin
|
|
|
|
* - max retries are reached (set to 5)
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function shouldRemoveBackgroundJob(): bool {
|
|
|
|
return $this->config->getSystemValueBool('has_internet_connection', true) === false ||
|
|
|
|
$this->config->getSystemValueString('lookup_server', 'https://lookup.nextcloud.com') === '' ||
|
|
|
|
$this->config->getAppValue('files_sharing', 'lookupServerUploadEnabled', 'yes') !== 'yes' ||
|
|
|
|
$this->retries >= 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function shouldRun(): bool {
|
|
|
|
$delay = 100 * 6 ** $this->retries;
|
|
|
|
return ($this->time->getTime() - $this->lastRun) > $delay;
|
2016-11-18 17:33:51 +03:00
|
|
|
}
|
2016-11-17 14:14:13 +03:00
|
|
|
|
2019-04-17 16:40:17 +03:00
|
|
|
protected function run($argument): void {
|
2019-04-17 18:31:22 +03:00
|
|
|
$user = $this->userManager->get($this->argument['userId']);
|
|
|
|
if (!$user instanceof IUser) {
|
|
|
|
// User does not exist anymore
|
2016-11-18 17:33:51 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-17 18:31:22 +03:00
|
|
|
$data = $this->getUserAccountData($user);
|
|
|
|
$signedData = $this->signer->sign('lookupserver', $data, $user);
|
2016-11-18 17:33:51 +03:00
|
|
|
$client = $this->clientService->newClient();
|
|
|
|
|
|
|
|
try {
|
2019-04-17 18:31:22 +03:00
|
|
|
if (count($data) === 1) {
|
2021-04-28 15:25:17 +03:00
|
|
|
$dataOnLookupServer = $this->config->getUserValue($user->getUID(), 'lookup_server_connector', 'dataSend', '0') === '1';
|
|
|
|
|
|
|
|
if (!$dataOnLookupServer) {
|
|
|
|
// We never send data to the lookupserver so no need to delete it
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// There is data on the lookup server so we must delete it
|
2019-04-17 16:37:23 +03:00
|
|
|
$client->delete($this->lookupServer,
|
|
|
|
[
|
2019-04-17 18:31:22 +03:00
|
|
|
'body' => json_encode($signedData),
|
2019-04-17 16:37:23 +03:00
|
|
|
'timeout' => 10,
|
|
|
|
'connect_timeout' => 3,
|
|
|
|
]
|
|
|
|
);
|
2021-04-28 15:25:17 +03:00
|
|
|
|
|
|
|
$this->config->setUserValue($user->getUID(), 'lookup_server_connector', 'dataSend', '0');
|
2019-04-17 16:37:23 +03:00
|
|
|
} else {
|
|
|
|
$client->post($this->lookupServer,
|
|
|
|
[
|
2019-04-17 18:31:22 +03:00
|
|
|
'body' => json_encode($signedData),
|
2019-04-17 16:37:23 +03:00
|
|
|
'timeout' => 10,
|
|
|
|
'connect_timeout' => 3,
|
|
|
|
]
|
|
|
|
);
|
2021-04-28 15:25:17 +03:00
|
|
|
$this->config->setUserValue($user->getUID(), 'lookup_server_connector', 'dataSend', '1');
|
2019-04-17 16:37:23 +03:00
|
|
|
}
|
2019-04-17 18:31:22 +03:00
|
|
|
|
|
|
|
// Reset retry counter
|
|
|
|
$this->config->deleteUserValue(
|
|
|
|
$user->getUID(),
|
|
|
|
'lookup_server_connector',
|
|
|
|
'update_retries'
|
2016-11-18 17:33:51 +03:00
|
|
|
);
|
2019-04-17 18:31:22 +03:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
// An error occurred, retry later
|
|
|
|
$this->retainJob = true;
|
|
|
|
$this->config->setUserValue(
|
|
|
|
$user->getUID(),
|
|
|
|
'lookup_server_connector',
|
|
|
|
'update_retries',
|
|
|
|
$this->retries + 1
|
|
|
|
);
|
2016-11-18 17:33:51 +03:00
|
|
|
}
|
2016-11-17 14:14:13 +03:00
|
|
|
}
|
2017-05-10 12:54:27 +03:00
|
|
|
|
2019-04-17 18:31:22 +03:00
|
|
|
protected function getUserAccountData(IUser $user): array {
|
|
|
|
$account = $this->accountManager->getAccount($user);
|
2019-01-07 14:15:11 +03:00
|
|
|
|
2019-04-17 18:31:22 +03:00
|
|
|
$publicData = [];
|
|
|
|
foreach ($account->getProperties() as $property) {
|
2021-03-23 16:47:10 +03:00
|
|
|
if ($property->getScope() === IAccountManager::SCOPE_PUBLISHED) {
|
2019-04-17 18:31:22 +03:00
|
|
|
$publicData[$property->getName()] = $property->getValue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = ['federationId' => $user->getCloudId()];
|
|
|
|
if (!empty($publicData)) {
|
|
|
|
$data['name'] = $publicData[IAccountManager::PROPERTY_DISPLAYNAME]['value'] ?? '';
|
|
|
|
$data['email'] = $publicData[IAccountManager::PROPERTY_EMAIL]['value'] ?? '';
|
|
|
|
$data['address'] = $publicData[IAccountManager::PROPERTY_ADDRESS]['value'] ?? '';
|
|
|
|
$data['website'] = $publicData[IAccountManager::PROPERTY_WEBSITE]['value'] ?? '';
|
|
|
|
$data['twitter'] = $publicData[IAccountManager::PROPERTY_TWITTER]['value'] ?? '';
|
|
|
|
$data['phone'] = $publicData[IAccountManager::PROPERTY_PHONE]['value'] ?? '';
|
|
|
|
$data['twitter_signature'] = $publicData[IAccountManager::PROPERTY_TWITTER]['signature'] ?? '';
|
|
|
|
$data['website_signature'] = $publicData[IAccountManager::PROPERTY_WEBSITE]['signature'] ?? '';
|
|
|
|
$data['verificationStatus'] = [
|
|
|
|
IAccountManager::PROPERTY_WEBSITE => $publicData[IAccountManager::PROPERTY_WEBSITE]['verified'] ?? '',
|
|
|
|
IAccountManager::PROPERTY_TWITTER => $publicData[IAccountManager::PROPERTY_TWITTER]['verified'] ?? '',
|
|
|
|
];
|
|
|
|
}
|
2019-01-07 14:15:11 +03:00
|
|
|
|
2019-04-17 18:31:22 +03:00
|
|
|
return $data;
|
2019-01-07 14:15:11 +03:00
|
|
|
}
|
2016-11-17 14:14:13 +03:00
|
|
|
}
|