2016-04-25 15:56:11 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
2016-11-11 16:36:17 +03:00
|
|
|
* @copyright Copyright (c) 2016, Björn Schießle
|
2017-11-06 17:56:42 +03:00
|
|
|
*
|
|
|
|
* @author Bjoern Schiessle <bjoern@schiessle.org>
|
|
|
|
* @author Björn Schießle <bjoern@schiessle.org>
|
2019-12-19 15:16:31 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
|
|
|
* @author Daniel Kesselberg <mail@danielkesselberg.de>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Julius Härtl <jus@bitgrid.net>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2017-11-06 17:56:42 +03:00
|
|
|
*
|
2016-04-25 15:56:11 +03:00
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* 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, version 3,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2016-04-25 15:56:11 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\Accounts;
|
|
|
|
|
2020-12-02 18:03:08 +03:00
|
|
|
use libphonenumber\NumberParseException;
|
|
|
|
use libphonenumber\PhoneNumber;
|
|
|
|
use libphonenumber\PhoneNumberFormat;
|
|
|
|
use libphonenumber\PhoneNumberUtil;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCA\Settings\BackgroundJobs\VerifyUserData;
|
2018-10-12 10:29:55 +03:00
|
|
|
use OCP\Accounts\IAccount;
|
|
|
|
use OCP\Accounts\IAccountManager;
|
2017-04-21 18:36:03 +03:00
|
|
|
use OCP\BackgroundJob\IJobList;
|
2020-12-02 16:11:47 +03:00
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
2020-12-03 13:12:41 +03:00
|
|
|
use OCP\IConfig;
|
2016-04-25 15:56:11 +03:00
|
|
|
use OCP\IDBConnection;
|
2016-11-11 16:36:17 +03:00
|
|
|
use OCP\IUser;
|
2020-10-13 17:33:53 +03:00
|
|
|
use Psr\Log\LoggerInterface;
|
2016-11-11 16:36:17 +03:00
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
|
|
|
use Symfony\Component\EventDispatcher\GenericEvent;
|
2019-12-13 14:39:29 +03:00
|
|
|
use function json_decode;
|
|
|
|
use function json_last_error;
|
2016-04-25 15:56:11 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class AccountManager
|
|
|
|
*
|
|
|
|
* Manage system accounts table
|
|
|
|
*
|
|
|
|
* @group DB
|
|
|
|
* @package OC\Accounts
|
|
|
|
*/
|
2018-10-12 10:29:55 +03:00
|
|
|
class AccountManager implements IAccountManager {
|
2017-03-23 18:06:57 +03:00
|
|
|
|
2016-04-25 15:56:11 +03:00
|
|
|
/** @var IDBConnection database connection */
|
|
|
|
private $connection;
|
|
|
|
|
2020-12-03 13:12:41 +03:00
|
|
|
/** @var IConfig */
|
|
|
|
private $config;
|
|
|
|
|
2016-04-25 15:56:11 +03:00
|
|
|
/** @var string table name */
|
|
|
|
private $table = 'accounts';
|
|
|
|
|
2020-12-02 13:40:36 +03:00
|
|
|
/** @var string table name */
|
|
|
|
private $dataTable = 'accounts_data';
|
|
|
|
|
2016-11-11 16:36:17 +03:00
|
|
|
/** @var EventDispatcherInterface */
|
|
|
|
private $eventDispatcher;
|
|
|
|
|
2017-04-21 18:36:03 +03:00
|
|
|
/** @var IJobList */
|
|
|
|
private $jobList;
|
|
|
|
|
2020-10-13 17:33:53 +03:00
|
|
|
/** @var LoggerInterface */
|
2019-12-13 14:39:29 +03:00
|
|
|
private $logger;
|
|
|
|
|
2017-04-21 18:36:03 +03:00
|
|
|
public function __construct(IDBConnection $connection,
|
2020-12-03 13:12:41 +03:00
|
|
|
IConfig $config,
|
2017-04-21 18:36:03 +03:00
|
|
|
EventDispatcherInterface $eventDispatcher,
|
2019-12-13 14:39:29 +03:00
|
|
|
IJobList $jobList,
|
2020-10-13 17:33:53 +03:00
|
|
|
LoggerInterface $logger) {
|
2016-04-25 15:56:11 +03:00
|
|
|
$this->connection = $connection;
|
2020-12-03 13:12:41 +03:00
|
|
|
$this->config = $config;
|
2016-11-11 16:36:17 +03:00
|
|
|
$this->eventDispatcher = $eventDispatcher;
|
2017-04-21 18:36:03 +03:00
|
|
|
$this->jobList = $jobList;
|
2019-12-13 14:39:29 +03:00
|
|
|
$this->logger = $logger;
|
2016-04-25 15:56:11 +03:00
|
|
|
}
|
|
|
|
|
2020-12-03 13:12:41 +03:00
|
|
|
/**
|
|
|
|
* @param string $input
|
|
|
|
* @return string Provided phone number in E.164 format when it was a valid number
|
|
|
|
* @throws \InvalidArgumentException When the phone number was invalid or no default region is set and the number doesn't start with a country code
|
|
|
|
*/
|
|
|
|
protected function parsePhoneNumber(string $input): string {
|
|
|
|
$defaultRegion = $this->config->getSystemValueString('default_phone_region', '');
|
|
|
|
|
|
|
|
if ($defaultRegion === '') {
|
|
|
|
// When no default region is set, only +49… numbers are valid
|
|
|
|
if (strpos($input, '+') !== 0) {
|
|
|
|
throw new \InvalidArgumentException(self::PROPERTY_PHONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
$defaultRegion = 'EN';
|
|
|
|
}
|
|
|
|
|
|
|
|
$phoneUtil = PhoneNumberUtil::getInstance();
|
|
|
|
try {
|
|
|
|
$phoneNumber = $phoneUtil->parse($input, $defaultRegion);
|
|
|
|
if ($phoneNumber instanceof PhoneNumber && $phoneUtil->isValidNumber($phoneNumber)) {
|
|
|
|
return $phoneUtil->format($phoneNumber, PhoneNumberFormat::E164);
|
|
|
|
}
|
|
|
|
} catch (NumberParseException $e) {
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new \InvalidArgumentException(self::PROPERTY_PHONE);
|
|
|
|
}
|
|
|
|
|
2016-11-11 16:36:17 +03:00
|
|
|
/**
|
|
|
|
* update user record
|
|
|
|
*
|
|
|
|
* @param IUser $user
|
2020-12-02 18:03:08 +03:00
|
|
|
* @param array $data
|
2020-12-03 14:16:39 +03:00
|
|
|
* @param bool $throwOnData Set to true if you can inform the user about invalid data
|
2020-12-02 18:03:08 +03:00
|
|
|
* @return array The potentially modified data (e.g. phone numbers are converted to E.164 format)
|
|
|
|
* @throws \InvalidArgumentException Message is the property that was invalid
|
2016-11-11 16:36:17 +03:00
|
|
|
*/
|
2020-12-03 14:16:39 +03:00
|
|
|
public function updateUser(IUser $user, array $data, bool $throwOnData = false): array {
|
2016-11-11 16:36:17 +03:00
|
|
|
$userData = $this->getUser($user);
|
2016-11-23 23:19:06 +03:00
|
|
|
$updated = true;
|
2020-12-02 18:03:08 +03:00
|
|
|
|
2020-12-03 14:16:39 +03:00
|
|
|
if (isset($data[self::PROPERTY_PHONE]) && $data[self::PROPERTY_PHONE]['value'] !== '') {
|
2021-02-01 01:53:45 +03:00
|
|
|
// Sanitize null value.
|
|
|
|
$data[self::PROPERTY_PHONE]['value'] = $data[self::PROPERTY_PHONE]['value'] ?? '';
|
|
|
|
|
2020-12-03 14:16:39 +03:00
|
|
|
try {
|
|
|
|
$data[self::PROPERTY_PHONE]['value'] = $this->parsePhoneNumber($data[self::PROPERTY_PHONE]['value']);
|
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
if ($throwOnData) {
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
$data[self::PROPERTY_PHONE]['value'] = '';
|
|
|
|
}
|
2020-12-02 18:03:08 +03:00
|
|
|
}
|
|
|
|
|
2021-04-06 19:00:44 +03:00
|
|
|
// set a max length
|
|
|
|
foreach ($data as $propertyName => $propertyData) {
|
|
|
|
if (isset($data[$propertyName]) && isset($data[$propertyName]['value']) && strlen($data[$propertyName]['value']) > 2048) {
|
|
|
|
if ($throwOnData) {
|
|
|
|
throw new \InvalidArgumentException($propertyName);
|
|
|
|
} else {
|
|
|
|
$data[$propertyName]['value'] = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-24 14:32:06 +03:00
|
|
|
$allowedScopes = [
|
|
|
|
self::SCOPE_PRIVATE,
|
|
|
|
self::SCOPE_LOCAL,
|
|
|
|
self::SCOPE_FEDERATED,
|
|
|
|
self::SCOPE_PUBLISHED,
|
|
|
|
self::VISIBILITY_PRIVATE,
|
|
|
|
self::VISIBILITY_CONTACTS_ONLY,
|
|
|
|
self::VISIBILITY_PUBLIC,
|
|
|
|
];
|
|
|
|
|
|
|
|
// validate and convert scope values
|
|
|
|
foreach ($data as $propertyName => $propertyData) {
|
|
|
|
if (isset($propertyData['scope'])) {
|
|
|
|
if ($throwOnData && !in_array($propertyData['scope'], $allowedScopes, true)) {
|
|
|
|
throw new \InvalidArgumentException('scope');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
$propertyData['scope'] === self::SCOPE_PRIVATE
|
|
|
|
&& ($propertyName === self::PROPERTY_DISPLAYNAME || $propertyName === self::PROPERTY_EMAIL)
|
|
|
|
) {
|
2021-03-24 19:06:04 +03:00
|
|
|
if ($throwOnData) {
|
|
|
|
// v2-private is not available for these fields
|
|
|
|
throw new \InvalidArgumentException('scope');
|
|
|
|
} else {
|
|
|
|
// default to local
|
|
|
|
$data[$propertyName]['scope'] = self::SCOPE_LOCAL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// migrate scope values to the new format
|
|
|
|
// invalid scopes are mapped to a default value
|
|
|
|
$data[$propertyName]['scope'] = AccountProperty::mapScopeToV2($propertyData['scope']);
|
2021-03-24 14:32:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-25 15:56:11 +03:00
|
|
|
if (empty($userData)) {
|
2016-11-11 16:36:17 +03:00
|
|
|
$this->insertNewUser($user, $data);
|
2016-11-23 23:19:06 +03:00
|
|
|
} elseif ($userData !== $data) {
|
2017-04-26 16:20:41 +03:00
|
|
|
$data = $this->checkEmailVerification($userData, $data, $user);
|
2017-03-23 18:06:57 +03:00
|
|
|
$data = $this->updateVerifyStatus($userData, $data);
|
2016-11-11 16:36:17 +03:00
|
|
|
$this->updateExistingUser($user, $data);
|
2016-11-23 23:19:06 +03:00
|
|
|
} else {
|
|
|
|
// nothing needs to be done if new and old data set are the same
|
|
|
|
$updated = false;
|
2016-04-25 15:56:11 +03:00
|
|
|
}
|
2016-11-11 16:36:17 +03:00
|
|
|
|
2016-11-23 23:19:06 +03:00
|
|
|
if ($updated) {
|
|
|
|
$this->eventDispatcher->dispatch(
|
|
|
|
'OC\AccountManager::userUpdated',
|
2017-04-10 11:22:57 +03:00
|
|
|
new GenericEvent($user, $data)
|
2016-11-23 23:19:06 +03:00
|
|
|
);
|
|
|
|
}
|
2020-12-02 18:03:08 +03:00
|
|
|
|
|
|
|
return $data;
|
2016-04-25 15:56:11 +03:00
|
|
|
}
|
|
|
|
|
2017-05-15 14:30:52 +03:00
|
|
|
/**
|
|
|
|
* delete user from accounts table
|
|
|
|
*
|
|
|
|
* @param IUser $user
|
|
|
|
*/
|
|
|
|
public function deleteUser(IUser $user) {
|
|
|
|
$uid = $user->getUID();
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$query->delete($this->table)
|
|
|
|
->where($query->expr()->eq('uid', $query->createNamedParameter($uid)))
|
|
|
|
->execute();
|
2020-12-02 13:40:36 +03:00
|
|
|
|
|
|
|
$this->deleteUserData($user);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* delete user from accounts table
|
|
|
|
*
|
|
|
|
* @param IUser $user
|
|
|
|
*/
|
|
|
|
public function deleteUserData(IUser $user): void {
|
|
|
|
$uid = $user->getUID();
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$query->delete($this->dataTable)
|
|
|
|
->where($query->expr()->eq('uid', $query->createNamedParameter($uid)))
|
|
|
|
->execute();
|
2017-05-15 14:30:52 +03:00
|
|
|
}
|
|
|
|
|
2016-04-25 15:56:11 +03:00
|
|
|
/**
|
|
|
|
* get stored data from a given user
|
|
|
|
*
|
2016-11-11 16:36:17 +03:00
|
|
|
* @param IUser $user
|
2016-04-25 15:56:11 +03:00
|
|
|
* @return array
|
2021-03-24 19:06:04 +03:00
|
|
|
*
|
|
|
|
* @deprecated use getAccount instead to make sure migrated properties work correctly
|
2016-04-25 15:56:11 +03:00
|
|
|
*/
|
2016-11-11 16:36:17 +03:00
|
|
|
public function getUser(IUser $user) {
|
|
|
|
$uid = $user->getUID();
|
2016-04-25 15:56:11 +03:00
|
|
|
$query = $this->connection->getQueryBuilder();
|
2020-10-06 11:34:34 +03:00
|
|
|
$query->select('data')
|
|
|
|
->from($this->table)
|
2016-04-25 15:56:11 +03:00
|
|
|
->where($query->expr()->eq('uid', $query->createParameter('uid')))
|
|
|
|
->setParameter('uid', $uid);
|
2020-10-06 11:34:34 +03:00
|
|
|
$result = $query->execute();
|
|
|
|
$accountData = $result->fetchAll();
|
|
|
|
$result->closeCursor();
|
2016-04-25 15:56:11 +03:00
|
|
|
|
2020-10-06 11:34:34 +03:00
|
|
|
if (empty($accountData)) {
|
2016-11-11 16:36:17 +03:00
|
|
|
$userData = $this->buildDefaultUserRecord($user);
|
|
|
|
$this->insertNewUser($user, $userData);
|
|
|
|
return $userData;
|
2016-04-25 15:56:11 +03:00
|
|
|
}
|
|
|
|
|
2020-10-06 11:34:34 +03:00
|
|
|
$userDataArray = json_decode($accountData[0]['data'], true);
|
2019-12-13 14:39:29 +03:00
|
|
|
$jsonError = json_last_error();
|
2020-05-17 16:30:36 +03:00
|
|
|
if ($userDataArray === null || $userDataArray === [] || $jsonError !== JSON_ERROR_NONE) {
|
2019-12-13 14:39:29 +03:00
|
|
|
$this->logger->critical("User data of $uid contained invalid JSON (error $jsonError), hence falling back to a default user record");
|
2019-10-09 18:43:54 +03:00
|
|
|
return $this->buildDefaultUserRecord($user);
|
|
|
|
}
|
2017-04-24 13:20:01 +03:00
|
|
|
|
|
|
|
$userDataArray = $this->addMissingDefaultValues($userDataArray);
|
|
|
|
|
|
|
|
return $userDataArray;
|
2016-04-25 15:56:11 +03:00
|
|
|
}
|
|
|
|
|
2020-12-02 16:11:47 +03:00
|
|
|
public function searchUsers(string $property, array $values): array {
|
2021-03-12 10:49:23 +03:00
|
|
|
$chunks = array_chunk($values, 500);
|
2020-12-02 16:11:47 +03:00
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$query->select('*')
|
|
|
|
->from($this->dataTable)
|
|
|
|
->where($query->expr()->eq('name', $query->createNamedParameter($property)))
|
2021-03-12 10:49:23 +03:00
|
|
|
->andWhere($query->expr()->in('value', $query->createParameter('values')));
|
2020-12-02 16:11:47 +03:00
|
|
|
|
|
|
|
$matches = [];
|
2021-03-12 10:49:23 +03:00
|
|
|
foreach ($chunks as $chunk) {
|
|
|
|
$query->setParameter('values', $chunk, IQueryBuilder::PARAM_STR_ARRAY);
|
|
|
|
$result = $query->execute();
|
2020-12-02 16:11:47 +03:00
|
|
|
|
2021-03-12 10:49:23 +03:00
|
|
|
while ($row = $result->fetch()) {
|
|
|
|
$matches[$row['value']] = $row['uid'];
|
|
|
|
}
|
|
|
|
$result->closeCursor();
|
2020-12-02 16:11:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $matches;
|
|
|
|
}
|
|
|
|
|
2017-04-21 18:36:03 +03:00
|
|
|
/**
|
|
|
|
* check if we need to ask the server for email verification, if yes we create a cronjob
|
|
|
|
*
|
|
|
|
* @param $oldData
|
|
|
|
* @param $newData
|
|
|
|
* @param IUser $user
|
2017-04-26 16:20:41 +03:00
|
|
|
* @return array
|
2017-04-21 18:36:03 +03:00
|
|
|
*/
|
|
|
|
protected function checkEmailVerification($oldData, $newData, IUser $user) {
|
|
|
|
if ($oldData[self::PROPERTY_EMAIL]['value'] !== $newData[self::PROPERTY_EMAIL]['value']) {
|
2018-01-26 01:16:13 +03:00
|
|
|
$this->jobList->add(VerifyUserData::class,
|
2017-04-21 18:36:03 +03:00
|
|
|
[
|
|
|
|
'verificationCode' => '',
|
|
|
|
'data' => $newData[self::PROPERTY_EMAIL]['value'],
|
|
|
|
'type' => self::PROPERTY_EMAIL,
|
|
|
|
'uid' => $user->getUID(),
|
|
|
|
'try' => 0,
|
|
|
|
'lastRun' => time()
|
|
|
|
]
|
|
|
|
);
|
2020-12-01 16:33:22 +03:00
|
|
|
$newData[self::PROPERTY_EMAIL]['verified'] = self::VERIFICATION_IN_PROGRESS;
|
2017-04-21 18:36:03 +03:00
|
|
|
}
|
2017-04-26 16:20:41 +03:00
|
|
|
|
|
|
|
return $newData;
|
2017-04-21 18:36:03 +03:00
|
|
|
}
|
|
|
|
|
2017-04-24 13:20:01 +03:00
|
|
|
/**
|
|
|
|
* make sure that all expected data are set
|
|
|
|
*
|
|
|
|
* @param array $userData
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function addMissingDefaultValues(array $userData) {
|
|
|
|
foreach ($userData as $key => $value) {
|
|
|
|
if (!isset($userData[$key]['verified'])) {
|
|
|
|
$userData[$key]['verified'] = self::NOT_VERIFIED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $userData;
|
|
|
|
}
|
|
|
|
|
2017-03-23 18:06:57 +03:00
|
|
|
/**
|
|
|
|
* reset verification status if personal data changed
|
|
|
|
*
|
|
|
|
* @param array $oldData
|
|
|
|
* @param array $newData
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function updateVerifyStatus($oldData, $newData) {
|
|
|
|
|
2017-04-21 17:36:52 +03:00
|
|
|
// which account was already verified successfully?
|
2017-03-23 18:06:57 +03:00
|
|
|
$twitterVerified = isset($oldData[self::PROPERTY_TWITTER]['verified']) && $oldData[self::PROPERTY_TWITTER]['verified'] === self::VERIFIED;
|
|
|
|
$websiteVerified = isset($oldData[self::PROPERTY_WEBSITE]['verified']) && $oldData[self::PROPERTY_WEBSITE]['verified'] === self::VERIFIED;
|
|
|
|
$emailVerified = isset($oldData[self::PROPERTY_EMAIL]['verified']) && $oldData[self::PROPERTY_EMAIL]['verified'] === self::VERIFIED;
|
|
|
|
|
2017-04-21 17:36:52 +03:00
|
|
|
// keep old verification status if we don't have a new one
|
2020-04-10 15:19:56 +03:00
|
|
|
if (!isset($newData[self::PROPERTY_TWITTER]['verified'])) {
|
2017-04-26 11:03:21 +03:00
|
|
|
// keep old verification status if value didn't changed and an old value exists
|
|
|
|
$keepOldStatus = $newData[self::PROPERTY_TWITTER]['value'] === $oldData[self::PROPERTY_TWITTER]['value'] && isset($oldData[self::PROPERTY_TWITTER]['verified']);
|
|
|
|
$newData[self::PROPERTY_TWITTER]['verified'] = $keepOldStatus ? $oldData[self::PROPERTY_TWITTER]['verified'] : self::NOT_VERIFIED;
|
2017-04-21 17:36:52 +03:00
|
|
|
}
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
if (!isset($newData[self::PROPERTY_WEBSITE]['verified'])) {
|
2017-04-26 11:03:21 +03:00
|
|
|
// keep old verification status if value didn't changed and an old value exists
|
|
|
|
$keepOldStatus = $newData[self::PROPERTY_WEBSITE]['value'] === $oldData[self::PROPERTY_WEBSITE]['value'] && isset($oldData[self::PROPERTY_WEBSITE]['verified']);
|
|
|
|
$newData[self::PROPERTY_WEBSITE]['verified'] = $keepOldStatus ? $oldData[self::PROPERTY_WEBSITE]['verified'] : self::NOT_VERIFIED;
|
2017-04-21 17:36:52 +03:00
|
|
|
}
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
if (!isset($newData[self::PROPERTY_EMAIL]['verified'])) {
|
2017-04-26 11:03:21 +03:00
|
|
|
// keep old verification status if value didn't changed and an old value exists
|
|
|
|
$keepOldStatus = $newData[self::PROPERTY_EMAIL]['value'] === $oldData[self::PROPERTY_EMAIL]['value'] && isset($oldData[self::PROPERTY_EMAIL]['verified']);
|
|
|
|
$newData[self::PROPERTY_EMAIL]['verified'] = $keepOldStatus ? $oldData[self::PROPERTY_EMAIL]['verified'] : self::VERIFICATION_IN_PROGRESS;
|
2017-04-21 17:36:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// reset verification status if a value from a previously verified data was changed
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($twitterVerified &&
|
2017-03-23 18:06:57 +03:00
|
|
|
$oldData[self::PROPERTY_TWITTER]['value'] !== $newData[self::PROPERTY_TWITTER]['value']
|
|
|
|
) {
|
2017-04-24 13:20:01 +03:00
|
|
|
$newData[self::PROPERTY_TWITTER]['verified'] = self::NOT_VERIFIED;
|
2017-03-23 18:06:57 +03:00
|
|
|
}
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($websiteVerified &&
|
2017-03-23 18:06:57 +03:00
|
|
|
$oldData[self::PROPERTY_WEBSITE]['value'] !== $newData[self::PROPERTY_WEBSITE]['value']
|
|
|
|
) {
|
2017-04-24 13:20:01 +03:00
|
|
|
$newData[self::PROPERTY_WEBSITE]['verified'] = self::NOT_VERIFIED;
|
2017-03-23 18:06:57 +03:00
|
|
|
}
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($emailVerified &&
|
2017-03-23 18:06:57 +03:00
|
|
|
$oldData[self::PROPERTY_EMAIL]['value'] !== $newData[self::PROPERTY_EMAIL]['value']
|
|
|
|
) {
|
2017-04-24 13:20:01 +03:00
|
|
|
$newData[self::PROPERTY_EMAIL]['verified'] = self::NOT_VERIFIED;
|
2017-03-23 18:06:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $newData;
|
|
|
|
}
|
|
|
|
|
2016-04-25 15:56:11 +03:00
|
|
|
/**
|
|
|
|
* add new user to accounts table
|
|
|
|
*
|
2016-11-11 16:36:17 +03:00
|
|
|
* @param IUser $user
|
2016-04-25 15:56:11 +03:00
|
|
|
* @param array $data
|
|
|
|
*/
|
2020-12-02 13:40:36 +03:00
|
|
|
protected function insertNewUser(IUser $user, array $data): void {
|
2016-11-11 16:36:17 +03:00
|
|
|
$uid = $user->getUID();
|
2016-04-25 15:56:11 +03:00
|
|
|
$jsonEncodedData = json_encode($data);
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$query->insert($this->table)
|
|
|
|
->values(
|
|
|
|
[
|
|
|
|
'uid' => $query->createNamedParameter($uid),
|
|
|
|
'data' => $query->createNamedParameter($jsonEncodedData),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
->execute();
|
2020-12-02 13:40:36 +03:00
|
|
|
|
|
|
|
$this->deleteUserData($user);
|
|
|
|
$this->writeUserData($user, $data);
|
2016-04-25 15:56:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* update existing user in accounts table
|
|
|
|
*
|
2016-11-11 16:36:17 +03:00
|
|
|
* @param IUser $user
|
2016-04-25 15:56:11 +03:00
|
|
|
* @param array $data
|
|
|
|
*/
|
2020-12-02 13:40:36 +03:00
|
|
|
protected function updateExistingUser(IUser $user, array $data): void {
|
2016-11-11 16:36:17 +03:00
|
|
|
$uid = $user->getUID();
|
2016-04-25 15:56:11 +03:00
|
|
|
$jsonEncodedData = json_encode($data);
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$query->update($this->table)
|
|
|
|
->set('data', $query->createNamedParameter($jsonEncodedData))
|
|
|
|
->where($query->expr()->eq('uid', $query->createNamedParameter($uid)))
|
|
|
|
->execute();
|
2020-12-02 13:40:36 +03:00
|
|
|
|
|
|
|
$this->deleteUserData($user);
|
|
|
|
$this->writeUserData($user, $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function writeUserData(IUser $user, array $data): void {
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$query->insert($this->dataTable)
|
|
|
|
->values(
|
|
|
|
[
|
|
|
|
'uid' => $query->createNamedParameter($user->getUID()),
|
|
|
|
'name' => $query->createParameter('name'),
|
|
|
|
'value' => $query->createParameter('value'),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
foreach ($data as $propertyName => $property) {
|
|
|
|
if ($propertyName === self::PROPERTY_AVATAR) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$query->setParameter('name', $propertyName)
|
2021-03-24 14:32:06 +03:00
|
|
|
->setParameter('value', $property['value'] ?? '');
|
2020-12-02 13:40:36 +03:00
|
|
|
$query->execute();
|
|
|
|
}
|
2016-04-25 15:56:11 +03:00
|
|
|
}
|
2016-11-11 16:36:17 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* build default user record in case not data set exists yet
|
|
|
|
*
|
|
|
|
* @param IUser $user
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function buildDefaultUserRecord(IUser $user) {
|
|
|
|
return [
|
|
|
|
self::PROPERTY_DISPLAYNAME =>
|
|
|
|
[
|
|
|
|
'value' => $user->getDisplayName(),
|
2021-03-23 16:47:10 +03:00
|
|
|
'scope' => self::SCOPE_FEDERATED,
|
2017-04-26 14:19:15 +03:00
|
|
|
'verified' => self::NOT_VERIFIED,
|
2016-11-11 16:36:17 +03:00
|
|
|
],
|
|
|
|
self::PROPERTY_ADDRESS =>
|
|
|
|
[
|
|
|
|
'value' => '',
|
2021-03-23 16:47:10 +03:00
|
|
|
'scope' => self::SCOPE_LOCAL,
|
2017-04-24 13:20:01 +03:00
|
|
|
'verified' => self::NOT_VERIFIED,
|
2016-11-11 16:36:17 +03:00
|
|
|
],
|
|
|
|
self::PROPERTY_WEBSITE =>
|
|
|
|
[
|
|
|
|
'value' => '',
|
2021-03-23 16:47:10 +03:00
|
|
|
'scope' => self::SCOPE_LOCAL,
|
2017-04-24 13:20:01 +03:00
|
|
|
'verified' => self::NOT_VERIFIED,
|
2016-11-11 16:36:17 +03:00
|
|
|
],
|
|
|
|
self::PROPERTY_EMAIL =>
|
|
|
|
[
|
|
|
|
'value' => $user->getEMailAddress(),
|
2021-03-23 16:47:10 +03:00
|
|
|
'scope' => self::SCOPE_FEDERATED,
|
2017-04-24 13:20:01 +03:00
|
|
|
'verified' => self::NOT_VERIFIED,
|
2016-11-11 16:36:17 +03:00
|
|
|
],
|
|
|
|
self::PROPERTY_AVATAR =>
|
|
|
|
[
|
2021-03-23 16:47:10 +03:00
|
|
|
'scope' => self::SCOPE_FEDERATED
|
2016-11-11 16:36:17 +03:00
|
|
|
],
|
|
|
|
self::PROPERTY_PHONE =>
|
|
|
|
[
|
|
|
|
'value' => '',
|
2021-03-23 16:47:10 +03:00
|
|
|
'scope' => self::SCOPE_LOCAL,
|
2017-04-24 13:20:01 +03:00
|
|
|
'verified' => self::NOT_VERIFIED,
|
2016-11-11 16:36:17 +03:00
|
|
|
],
|
|
|
|
self::PROPERTY_TWITTER =>
|
|
|
|
[
|
|
|
|
'value' => '',
|
2021-03-23 16:47:10 +03:00
|
|
|
'scope' => self::SCOPE_LOCAL,
|
2017-04-24 13:20:01 +03:00
|
|
|
'verified' => self::NOT_VERIFIED,
|
2016-11-11 16:36:17 +03:00
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2018-10-12 10:29:55 +03:00
|
|
|
private function parseAccountData(IUser $user, $data): Account {
|
|
|
|
$account = new Account($user);
|
2020-04-10 15:19:56 +03:00
|
|
|
foreach ($data as $property => $accountData) {
|
2021-03-23 16:47:10 +03:00
|
|
|
$account->setProperty($property, $accountData['value'] ?? '', $accountData['scope'] ?? self::SCOPE_LOCAL, $accountData['verified'] ?? self::NOT_VERIFIED);
|
2018-10-12 10:29:55 +03:00
|
|
|
}
|
|
|
|
return $account;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAccount(IUser $user): IAccount {
|
|
|
|
return $this->parseAccountData($user, $this->getUser($user));
|
|
|
|
}
|
2021-03-31 16:18:25 +03:00
|
|
|
|
|
|
|
public function updateAccount(IAccount $account): void {
|
|
|
|
$data = [];
|
|
|
|
|
|
|
|
foreach ($account->getProperties() as $property) {
|
|
|
|
$data[$property->getName()] = [
|
|
|
|
'value' => $property->getValue(),
|
|
|
|
'scope' => $property->getScope(),
|
|
|
|
'verified' => $property->getVerified(),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->updateUser($account->getUser(), $data, true);
|
|
|
|
}
|
2016-04-25 15:56:11 +03:00
|
|
|
}
|