From 189de4e66f8eef07a910c2ac0281c071f2683655 Mon Sep 17 00:00:00 2001
From: atraczyk <andreastraczyk@gmail.com>
Date: Fri, 1 Sep 2017 14:36:58 -0400
Subject: [PATCH] ringaccount: (fix)sets the ringId/username for disabled
 accounts also

- Commit e57038fe...(ringaccount: sets the ringId/username for disabled
  accounts also) caused the deviceId to be loaded into ringAccountId_
  for disabled accounts. This patch load devices, contacts, and trust
  requests for disabled accounts while exiting from loadAccount as early
  as possible.

Change-Id: If45ab84ac301faa5776d6af5436e672195c51501
---
 src/ringdht/ringaccount.cpp | 81 +++++++++++++++++++------------------
 1 file changed, 42 insertions(+), 39 deletions(-)

diff --git a/src/ringdht/ringaccount.cpp b/src/ringdht/ringaccount.cpp
index 4c1815745c..1c20fb65b4 100644
--- a/src/ringdht/ringaccount.cpp
+++ b/src/ringdht/ringaccount.cpp
@@ -1341,21 +1341,11 @@ RingAccount::loadAccount(const std::string& archive_password, const std::string&
     if (registrationState_ == RegistrationState::INITIALIZING)
         return;
 
-    // load identity immediately so we can access the ringIds of disabled accounts
-    auto id = loadIdentity(tlsCertificateFile_, tlsPrivateKeyFile_, tlsPassword_);
-
-    if (not isEnabled()) {
-        if (id.first) {
-            ringAccountId_ = id.first->getPublicKey().getId().toString();
-            username_ = RING_URI_PREFIX + ringAccountId_;
-        }
-        return;
-    }
-
     RING_DBG("[Account %s] loading Ring account", getAccountID().c_str());
     try {
+        auto id = loadIdentity(tlsCertificateFile_, tlsPrivateKeyFile_, tlsPassword_);
         bool hasArchive = not archivePath_.empty()
-                          and fileutils::isFile(fileutils::getFullPath(idPath_, archivePath_));
+            and fileutils::isFile(fileutils::getFullPath(idPath_, archivePath_));
         if (useIdentity(id)) {
             // normal loading path
             loadKnownDevices();
@@ -1363,37 +1353,49 @@ RingAccount::loadAccount(const std::string& archive_password, const std::string&
             loadTrustRequests();
             if (not hasArchive)
                 RING_WARN("[Account %s] account archive not found, won't be able to add new devices", getAccountID().c_str());
-        } else if (hasArchive) {
-            if (needsMigration(id)) {
-                RING_WARN("[Account %s] account certificate needs update", getAccountID().c_str());
-                migrateAccount(archive_password, id);
-            } else {
-                RING_WARN("[Account %s] archive present but no valid receipt: creating new device", getAccountID().c_str());
-                try {
-                    initRingDevice(readArchive(archive_password));
-                } catch (...) {
-                    Migration::setState(accountID_, Migration::State::INVALID);
-                    return;
-                }
-                Migration::setState(accountID_, Migration::State::SUCCESS);
+            if (not isEnabled()) {
                 setRegistrationState(RegistrationState::UNREGISTERED);
             }
-            Manager::instance().saveConfig();
-            loadAccount(archive_password);
-        } else {
-            // no receipt or archive, creating new account
-            if (not archive_path.empty()) {
-                // import account from file
-                loadAccountFromFile(archive_path, archive_password);
-            } else if (not archive_pin.empty()) {
-                // import account from DHT
-                loadAccountFromDHT(archive_password, archive_pin);
-            } else {
-                // create new account
-                createAccount(archive_password, std::move(id));
+        }
+        else if (isEnabled()) {
+            if (hasArchive) {
+                if (needsMigration(id)) {
+                    RING_WARN("[Account %s] account certificate needs update", getAccountID().c_str());
+                    migrateAccount(archive_password, id);
+                }
+                else {
+                    RING_WARN("[Account %s] archive present but no valid receipt: creating new device", getAccountID().c_str());
+                    try {
+                        initRingDevice(readArchive(archive_password));
+                    }
+                    catch (...) {
+                        Migration::setState(accountID_, Migration::State::INVALID);
+                        return;
+                    }
+                    Migration::setState(accountID_, Migration::State::SUCCESS);
+                    setRegistrationState(RegistrationState::UNREGISTERED);
+                }
+                Manager::instance().saveConfig();
+                loadAccount(archive_password);
+            }
+            else {
+                // no receipt or archive, creating new account
+                if (not archive_path.empty()) {
+                    // import account from file
+                    loadAccountFromFile(archive_path, archive_password);
+                }
+                else if (not archive_pin.empty()) {
+                    // import account from DHT
+                    loadAccountFromDHT(archive_password, archive_pin);
+                }
+                else {
+                    // create new account
+                    createAccount(archive_password, std::move(id));
+                }
             }
         }
-    } catch (const std::exception& e) {
+    }
+    catch (const std::exception& e) {
         RING_WARN("[Account %s] error loading account: %s", getAccountID().c_str(), e.what());
         identity_ = dht::crypto::Identity{};
         setRegistrationState(RegistrationState::ERROR_GENERIC);
@@ -3282,3 +3284,4 @@ RingAccount::registerDhtAddress(IceTransport& ice)
 }
 
 } // namespace ring
+
-- 
GitLab