From a8493ecaa22c0b6dc5c2cb8c01107571ce7d37a9 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 24 Feb 2014 11:11:36 -0800 Subject: [PATCH] cleanup login/logout in menu, logout only when in --- domain-server/src/DomainServer.cpp | 6 ++-- interface/src/Application.cpp | 8 +++-- interface/src/Menu.cpp | 45 +++++++++++++++++++++---- interface/src/Menu.h | 3 ++ libraries/shared/src/AccountManager.cpp | 19 +++++++---- libraries/shared/src/AccountManager.h | 8 +++-- libraries/shared/src/DomainInfo.cpp | 16 ++++++++- libraries/shared/src/DomainInfo.h | 2 ++ libraries/shared/src/NodeList.cpp | 3 ++ 9 files changed, 86 insertions(+), 24 deletions(-) diff --git a/domain-server/src/DomainServer.cpp b/domain-server/src/DomainServer.cpp index 15fe92be65..8d826c324f 100644 --- a/domain-server/src/DomainServer.cpp +++ b/domain-server/src/DomainServer.cpp @@ -70,7 +70,7 @@ DomainServer::DomainServer(int argc, char* argv[]) : // TODO: failure case for not receiving a token accountManager.requestAccessToken(username, password); - connect(&accountManager, &AccountManager::receivedAccessToken, this, &DomainServer::requestCreationFromDataServer); + connect(&accountManager, &AccountManager::loginComplete, this, &DomainServer::requestCreationFromDataServer); } else { qDebug() << "Authentication was requested against" << qPrintable(_nodeAuthenticationURL.toString()) @@ -301,7 +301,7 @@ void DomainServer::populateDefaultStaticAssignmentsExcludingTypes(const QSetsetText("Logout " + accountManager.getUsername()); + connect(_loginAction, &QAction::triggered, &accountManager, &AccountManager::logout); + + _loginAction->setEnabled(true); + } else { + // change the menu item to login + _loginAction->setText("Login"); + + // if we don't have a rootURL in the AccountManager we're in a domain that doesn't use auth + // so setup the menu item according to the presence of that root URL + if (accountManager.hasAuthEndpoint()) { + connect(_loginAction, &QAction::triggered, this, &Menu::loginForCurrentDomain); + _loginAction->setEnabled(true); + } else { + _loginAction->setEnabled(false); + } + } +} + void Menu::bandwidthDetails() { if (! _bandwidthDialog) { _bandwidthDialog = new BandwidthDialog(Application::getInstance()->getGLWidget(), diff --git a/interface/src/Menu.h b/interface/src/Menu.h index 8560e73a35..5034a5bfea 100644 --- a/interface/src/Menu.h +++ b/interface/src/Menu.h @@ -111,6 +111,8 @@ public slots: void exportSettings(); void goTo(); void pasteToVoxel(); + + void toggleLoginMenuItem(); private slots: void aboutApp(); @@ -172,6 +174,7 @@ private: QMenu* _activeScriptsMenu; QString replaceLastOccurrence(QChar search, QChar replace, QString string); quint64 _lastAdjust; + QAction* _loginAction; }; namespace MenuOption { diff --git a/libraries/shared/src/AccountManager.cpp b/libraries/shared/src/AccountManager.cpp index e14f7524b3..953b6fd178 100644 --- a/libraries/shared/src/AccountManager.cpp +++ b/libraries/shared/src/AccountManager.cpp @@ -34,7 +34,6 @@ const QString ACCOUNTS_GROUP = "accounts"; AccountManager::AccountManager() : _rootURL(), - _username(), _networkAccessManager(), _pendingCallbackMap(), _accounts() @@ -76,18 +75,22 @@ void AccountManager::logout() { settings.remove(keyURLString); qDebug() << "Removed account info for" << _rootURL << "from in-memory accounts and .ini file"; + + emit logoutComplete(); + // the username has changed to blank + emit usernameChanged(QString()); + } void AccountManager::setRootURL(const QUrl& rootURL) { if (_rootURL != rootURL) { _rootURL = rootURL; - // we have an auth URL change, set the username empty - // we will need to ask for profile information again - _username.clear(); - qDebug() << "URL for node authentication has been changed to" << qPrintable(_rootURL.toString()); qDebug() << "Re-setting authentication flow."; + + // tell listeners that the auth endpoint has changed + emit authEndpointChanged(); } } @@ -216,7 +219,7 @@ bool AccountManager::checkAndSignalForAccessToken() { if (!hasToken) { // emit a signal so somebody can call back to us and request an access token given a username and password - emit authenticationRequired(); + emit authRequired(); } return hasToken; @@ -265,7 +268,9 @@ void AccountManager::requestFinished() { DataServerAccountInfo freshAccountInfo(rootObject); _accounts.insert(rootURL, freshAccountInfo); - emit receivedAccessToken(rootURL); + emit loginComplete(rootURL); + // the username has changed to whatever came back + emit usernameChanged(freshAccountInfo.getUsername()); // store this access token into the local settings QSettings localSettings; diff --git a/libraries/shared/src/AccountManager.h b/libraries/shared/src/AccountManager.h index 8093f28bfb..6bdb4f92e2 100644 --- a/libraries/shared/src/AccountManager.h +++ b/libraries/shared/src/AccountManager.h @@ -42,6 +42,7 @@ public: const QByteArray& dataByteArray = QByteArray()); void setRootURL(const QUrl& rootURL); + bool hasAuthEndpoint() { return !_rootURL.isEmpty(); } bool isLoggedIn() { return !_rootURL.isEmpty() && hasValidAccessToken(); } bool hasValidAccessToken(); @@ -56,9 +57,11 @@ public slots: void requestError(QNetworkReply::NetworkError error); void logout(); signals: - void authenticationRequired(); - void receivedAccessToken(const QUrl& rootURL); + void authRequired(); + void authEndpointChanged(); void usernameChanged(const QString& username); + void loginComplete(const QUrl& rootURL); + void logoutComplete(); private slots: void passSuccessToCallback(); void passErrorToCallback(QNetworkReply::NetworkError errorCode); @@ -71,7 +74,6 @@ private: const JSONCallbackParameters& callbackParams, const QByteArray& dataByteArray); QUrl _rootURL; - QString _username; QNetworkAccessManager _networkAccessManager; QMap _pendingCallbackMap; diff --git a/libraries/shared/src/DomainInfo.cpp b/libraries/shared/src/DomainInfo.cpp index cbdb0955e5..258254be94 100644 --- a/libraries/shared/src/DomainInfo.cpp +++ b/libraries/shared/src/DomainInfo.cpp @@ -8,6 +8,8 @@ #include +#include "AccountManager.h" + #include "DomainInfo.h" DomainInfo::DomainInfo() : @@ -20,7 +22,8 @@ DomainInfo::DomainInfo() : _publicKey(), _isConnected(false) { - + // clear appropriate variables after a domain-server logout + connect(&AccountManager::getInstance(), &AccountManager::logoutComplete, this, &DomainInfo::logout); } void DomainInfo::reset() { @@ -98,3 +101,14 @@ void DomainInfo::setIsConnected(bool isConnected) { } } } + +void DomainInfo::logout() { + // clear any information related to auth for this domain, assuming it had requested auth + if (!_rootAuthenticationURL.isEmpty()) { + _rootAuthenticationURL = QUrl(); + _connectionSecret = QUuid(); + _registrationToken = QByteArray(); + _publicKey = QString(); + _isConnected = false; + } +} diff --git a/libraries/shared/src/DomainInfo.h b/libraries/shared/src/DomainInfo.h index 17c17a726f..0c5344a0c5 100644 --- a/libraries/shared/src/DomainInfo.h +++ b/libraries/shared/src/DomainInfo.h @@ -56,6 +56,8 @@ public: private slots: void completedHostnameLookup(const QHostInfo& hostInfo); + + void logout(); signals: void hostnameChanged(const QString& hostname); void connectedToDomain(const QString& hostname); diff --git a/libraries/shared/src/NodeList.cpp b/libraries/shared/src/NodeList.cpp index 319ac54860..8f8d9dfac3 100644 --- a/libraries/shared/src/NodeList.cpp +++ b/libraries/shared/src/NodeList.cpp @@ -76,6 +76,9 @@ NodeList::NodeList(char newOwnerType, unsigned short int newSocketListenPort) : // clear our NodeList when the domain changes connect(&_domainInfo, &DomainInfo::hostnameChanged, this, &NodeList::reset); + + // clear our NodeList when logout is requested + connect(&AccountManager::getInstance(), &AccountManager::logoutComplete , this, &NodeList::reset); } bool NodeList::packetVersionAndHashMatch(const QByteArray& packet) {