From f69dea0a9417828291e2e64c8f5c04b370ae7c02 Mon Sep 17 00:00:00 2001 From: Camila Date: Tue, 15 Aug 2023 18:20:18 +0200 Subject: [PATCH] Migrate http_user to webflow_user and dav_user. - Add {} for single line condition, const auto and fix comments. - Save the legacy account data first in _settingsMap, so the legacy config file does not get changed in the migration process. - Legacy dav_user should stay only as dav_user in the new config. - Compare davUser and prettyName only when new user id needs to be retrieved. Signed-off-by: Camila --- src/gui/accountmanager.cpp | 28 ++++++++++++++++------------ src/gui/creds/webflowcredentials.cpp | 22 +--------------------- src/gui/userinfo.cpp | 22 ++++++++++++++-------- 3 files changed, 31 insertions(+), 41 deletions(-) diff --git a/src/gui/accountmanager.cpp b/src/gui/accountmanager.cpp index 98b250b3f..520cd886e 100644 --- a/src/gui/accountmanager.cpp +++ b/src/gui/accountmanager.cpp @@ -107,8 +107,9 @@ AccountManager::AccountsRestoreResult AccountManager::restore(const bool alsoRes if (auto accState = AccountState::loadFromSettings(acc, *settings)) { auto jar = qobject_cast(acc->_am->cookieJar()); ASSERT(jar); - if (jar) + if (jar) { jar->restore(acc->cookieJarPath()); + } addAccountState(accState); } } @@ -303,6 +304,7 @@ void AccountManager::saveAccountState(AccountState *a) void AccountManager::saveAccountHelper(Account *acc, QSettings &settings, bool saveCredentials) { + qCDebug(lcAccountManager) << "Saving settings to" << settings.fileName(); settings.setValue(QLatin1String(versionC), maxAccountVersion); settings.setValue(QLatin1String(urlC), acc->_url.toString()); settings.setValue(QLatin1String(davUserC), acc->_davUser); @@ -332,8 +334,9 @@ void AccountManager::saveAccountHelper(Account *acc, QSettings &settings, bool s settings.setValue(QLatin1String(authTypeC), acc->_credentials->authType()); // HACK: Save http_user also as user - if (acc->_settingsMap.contains(httpUserC)) + if (acc->_settingsMap.contains(httpUserC)) { settings.setValue(userC, acc->_settingsMap.value(httpUserC)); + } } // Save accepted certificates. @@ -376,8 +379,8 @@ AccountPtr AccountManager::loadAccountHelper(QSettings &settings) auto authType = settings.value(QLatin1String(authTypeC)).toString(); // There was an account-type saving bug when 'skip folder config' was used - // See #5408. This attempts to fix up the "dummy" authType - if (authType == QLatin1String(dummyAuthTypeC)) { + // See owncloud#5408. This attempts to fix up the "dummy" or empty authType + if (authType == QLatin1String(dummyAuthTypeC) || authType.isEmpty()) { if (settings.contains(QLatin1String(httpUserC))) { authType = httpAuthTypeC; } else if (settings.contains(QLatin1String(shibbolethUserC))) { @@ -401,15 +404,16 @@ AccountPtr AccountManager::loadAccountHelper(QSettings &settings) // Migrate to webflow if (authType == QLatin1String(httpAuthTypeC)) { authType = webflowAuthTypeC; - settings.setValue(QLatin1String(authTypeC), authType); + acc->_settingsMap.insert(QLatin1String(authTypeC), authType); const auto settingsChildKeys = settings.childKeys(); for (const auto &key : settingsChildKeys) { - if (!key.startsWith(httpAuthPrefix)) + if (!key.startsWith(httpAuthPrefix)) { continue; + } + const auto newkey = QString::fromLatin1(webflowAuthPrefix).append(key.mid(5)); - settings.setValue(newkey, settings.value((key))); - settings.remove(key); + acc->_settingsMap.insert(newkey, settings.value(key)); } } @@ -419,16 +423,16 @@ AccountPtr AccountManager::loadAccountHelper(QSettings &settings) acc->_serverColor = settings.value(QLatin1String(serverColorC)).value(); acc->_serverTextColor = settings.value(QLatin1String(serverTextColorC)).value(); acc->_skipE2eeMetadataChecksumValidation = settings.value(QLatin1String(skipE2eeMetadataChecksumValidationC), {}).toBool(); - acc->_davUser = settings.value(QLatin1String(davUserC), "").toString(); + acc->_davUser = settings.value(QLatin1String(davUserC)).toString(); - // We want to only restore settings for that auth type and the user value acc->_settingsMap.insert(QLatin1String(userC), settings.value(userC)); acc->_displayName = settings.value(QLatin1String(displayNameC), "").toString(); - QString authTypePrefix = authType + "_"; + const QString authTypePrefix = authType + "_"; const auto settingsChildKeys = settings.childKeys(); for (const auto &key : settingsChildKeys) { - if (!key.startsWith(authTypePrefix)) + if (!key.startsWith(authTypePrefix)) { continue; + } acc->_settingsMap.insert(key, settings.value(key)); } diff --git a/src/gui/creds/webflowcredentials.cpp b/src/gui/creds/webflowcredentials.cpp index 9958d1b7d..f878a9bab 100644 --- a/src/gui/creds/webflowcredentials.cpp +++ b/src/gui/creds/webflowcredentials.cpp @@ -180,29 +180,9 @@ void WebFlowCredentials::askFromUser() { void WebFlowCredentials::slotAskFromUserCredentialsProvided(const QString &user, const QString &pass, const QString &host) { Q_UNUSED(host) - // Compare the re-entered username case-insensitive and save the new value (avoid breaking the account) - // See issue: https://github.com/nextcloud/desktop/issues/1741 - if (QString::compare(_user, user, Qt::CaseInsensitive) == 0) { - _user = user; - } else { - qCInfo(lcWebFlowCredentials()) << "Authed with the wrong user!"; - - QString msg = tr("Please login with the account: %1") - .arg(_account->prettyName()); - _askDialog->setError(msg); - - if (!_askDialog->isUsingFlow2()) { - QUrl url = _account->url(); - QString path = url.path() + "/index.php/login/flow"; - url.setPath(path); - _askDialog->setUrl(url); - } - - return; - } - qCInfo(lcWebFlowCredentials()) << "Obtained a new password"; + _user = user; _password = pass; _ready = true; _credentialsValid = true; diff --git a/src/gui/userinfo.cpp b/src/gui/userinfo.cpp index 1c111d736..15106b119 100644 --- a/src/gui/userinfo.cpp +++ b/src/gui/userinfo.cpp @@ -110,17 +110,23 @@ void UserInfo::slotUpdateLastInfo(const QJsonDocument &json) AccountPtr account = _accountState->account(); - // User Info - QString user = objData.value("id").toString(); - if (!user.isEmpty()) { - account->setDavUser(user); + if (const auto newUserId = objData.value("id").toString();!newUserId.isEmpty()) { + if (QString::compare(account->davUser(), newUserId, Qt::CaseInsensitive) != 0) { + // TODO: the error message should be in the UI + qInfo() << "Authed with the wrong user! Please login with the account:" << account->prettyName(); + if (const auto cred = account->credentials()) { + account->credentials()->askFromUser(); + } + return; + } + account->setDavUser(newUserId); } + QString displayName = objData.value("display-name").toString(); if (!displayName.isEmpty()) { account->setDavDisplayName(displayName); } - // Quota auto objQuota = objData.value("quota").toObject(); qint64 used = objQuota.value("used").toDouble(); qint64 total = objQuota.value("quota").toDouble(); @@ -134,15 +140,15 @@ void UserInfo::slotUpdateLastInfo(const QJsonDocument &json) _jobRestartTimer.start(defaultIntervalT); _lastInfoReceived = QDateTime::currentDateTime(); - // Avatar Image if(_fetchAvatarImage) { auto *job = new AvatarJob(account, account->davUser(), 128, this); job->setTimeout(20 * 1000); QObject::connect(job, &AvatarJob::avatarPixmap, this, &UserInfo::slotAvatarImage); job->start(); + return; } - else - emit fetchedLastInfo(this); + + emit fetchedLastInfo(this); } void UserInfo::slotAvatarImage(const QImage &img) -- 2.30.2