Enforce fetching of user id
authorFelix Weilbach <felix.weilbach@nextcloud.com>
Wed, 11 Aug 2021 13:18:22 +0000 (15:18 +0200)
committerMatthieu Gallien (Rebase PR Action) <matthieu_gallien@yahoo.fr>
Wed, 11 Aug 2021 16:16:12 +0000 (16:16 +0000)
With the change of commit 3e61bdc4318827d486221e56fbca3804b639fdd7 and
the relase of v3.3.0 users that had their email address used as login
are not able to login anymore. The dav_user should be empty if users
tried to create a account in the meantime. Therefore we fetch the user
id in the case dav_user (and then Account::_davUser) is empty. We then
store the user id in dav_user.

Signed-off-by: Felix Weilbach <felix.weilbach@nextcloud.com>
src/gui/accountmanager.cpp
src/libsync/account.cpp

index 150fa454513e05cf0d1b38a192f06c93ffd0473d..cfa569b6a6d3aaf70a09cecf4ebe24d0ef789323 100644 (file)
@@ -317,7 +317,7 @@ AccountPtr AccountManager::loadAccountHelper(QSettings &settings)
     qCInfo(lcAccountManager) << "Account for" << acc->url() << "using auth type" << authType;
 
     acc->_serverVersion = settings.value(QLatin1String(serverVersionC)).toString();
-    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));
index 5b3289f1d65c9eca8b3c1a48c5edf5f22145af70..52a8c1c4d52ca94eb808ccfcc2f980b8234d02b6 100644 (file)
@@ -488,7 +488,27 @@ void Account::slotHandleSslErrors(QNetworkReply *reply, QList<QSslError> errors)
 
 void Account::slotCredentialsFetched()
 {
-    emit credentialsFetched(_credentials.data());
+    if (_davUser.isEmpty()) {
+        qCDebug(lcAccount) << "User id not set. Fetch it.";
+        const auto fetchUserNameJob = new JsonApiJob(sharedFromThis(), QStringLiteral("/ocs/v1.php/cloud/user"));
+        connect(fetchUserNameJob, &JsonApiJob::jsonReceived, this, [this, fetchUserNameJob](const QJsonDocument &json, int statusCode) {
+            fetchUserNameJob->deleteLater();
+            if (statusCode != 100) {
+                qCWarning(lcAccount) << "Could not fetch user id. Login will probably not work.";
+                emit credentialsFetched(_credentials.data());
+                return;
+            }
+
+            const auto objData = json.object().value("ocs").toObject().value("data").toObject();
+            const auto userId = objData.value("id").toString("");
+            setDavUser(userId);
+            emit credentialsFetched(_credentials.data());
+        });
+        fetchUserNameJob->start();
+    } else {
+        qCDebug(lcAccount) << "User id already fetched.";
+        emit credentialsFetched(_credentials.data());
+    }
 }
 
 void Account::slotCredentialsAsked()