cleanup login/logout in menu, logout only when in

This commit is contained in:
Stephen Birarda 2014-02-24 11:11:36 -08:00
parent 6165ca658a
commit a8493ecaa2
9 changed files with 86 additions and 24 deletions

View file

@ -70,7 +70,7 @@ DomainServer::DomainServer(int argc, char* argv[]) :
// TODO: failure case for not receiving a token // TODO: failure case for not receiving a token
accountManager.requestAccessToken(username, password); accountManager.requestAccessToken(username, password);
connect(&accountManager, &AccountManager::receivedAccessToken, this, &DomainServer::requestCreationFromDataServer); connect(&accountManager, &AccountManager::loginComplete, this, &DomainServer::requestCreationFromDataServer);
} else { } else {
qDebug() << "Authentication was requested against" << qPrintable(_nodeAuthenticationURL.toString()) qDebug() << "Authentication was requested against" << qPrintable(_nodeAuthenticationURL.toString())
@ -301,7 +301,7 @@ void DomainServer::populateDefaultStaticAssignmentsExcludingTypes(const QSet<Ass
void DomainServer::requestAuthenticationFromPotentialNode(const HifiSockAddr& senderSockAddr) { void DomainServer::requestAuthenticationFromPotentialNode(const HifiSockAddr& senderSockAddr) {
// this is a node we do not recognize and we need authentication - ask them to do so // this is a node we do not recognize and we need authentication - ask them to do so
// by providing them the hostname they should authenticate with // by providing them the hostname they should authenticate with
QByteArray authenticationRequestPacket = byteArrayWithPopluatedHeader(PacketTypeDomainServerAuthRequest); QByteArray authenticationRequestPacket = byteArrayWithPopulatedHeader(PacketTypeDomainServerAuthRequest);
QDataStream authPacketStream(&authenticationRequestPacket, QIODevice::Append); QDataStream authPacketStream(&authenticationRequestPacket, QIODevice::Append);
authPacketStream << _nodeAuthenticationURL; authPacketStream << _nodeAuthenticationURL;
@ -417,7 +417,7 @@ NodeSet DomainServer::nodeInterestListFromPacket(const QByteArray& packet, int n
void DomainServer::sendDomainListToNode(const SharedNodePointer& node, const HifiSockAddr &senderSockAddr, void DomainServer::sendDomainListToNode(const SharedNodePointer& node, const HifiSockAddr &senderSockAddr,
const NodeSet& nodeInterestList) { const NodeSet& nodeInterestList) {
QByteArray broadcastPacket = byteArrayWithPopluatedHeader(PacketTypeDomainList); QByteArray broadcastPacket = byteArrayWithPopulatedHeader(PacketTypeDomainList);
// always send the node their own UUID back // always send the node their own UUID back
QDataStream broadcastDataStream(&broadcastPacket, QIODevice::Append); QDataStream broadcastDataStream(&broadcastPacket, QIODevice::Append);

View file

@ -227,9 +227,11 @@ Application::Application(int& argc, char** argv, timeval &startup_time) :
connect(nodeList, SIGNAL(nodeAdded(SharedNodePointer)), &_voxels, SLOT(nodeAdded(SharedNodePointer))); connect(nodeList, SIGNAL(nodeAdded(SharedNodePointer)), &_voxels, SLOT(nodeAdded(SharedNodePointer)));
connect(nodeList, SIGNAL(nodeKilled(SharedNodePointer)), &_voxels, SLOT(nodeKilled(SharedNodePointer))); connect(nodeList, SIGNAL(nodeKilled(SharedNodePointer)), &_voxels, SLOT(nodeKilled(SharedNodePointer)));
connect(nodeList, &NodeList::uuidChanged, this, &Application::updateWindowTitle); connect(nodeList, &NodeList::uuidChanged, this, &Application::updateWindowTitle);
connect(&AccountManager::getInstance(), SIGNAL(authenticationRequired()), // connect to appropriate slots on AccountManager
Menu::getInstance(), SLOT(loginForCurrentDomain())); AccountManager& accountManager = AccountManager::getInstance();
connect(&accountManager, &AccountManager::authRequired, Menu::getInstance(), &Menu::loginForCurrentDomain);
connect(&accountManager, &AccountManager::usernameChanged, this, &Application::updateWindowTitle);
_settings = new QSettings(this); _settings = new QSettings(this);

View file

@ -71,7 +71,8 @@ Menu::Menu() :
_voxelSizeScale(DEFAULT_OCTREE_SIZE_SCALE), _voxelSizeScale(DEFAULT_OCTREE_SIZE_SCALE),
_boundaryLevelAdjust(0), _boundaryLevelAdjust(0),
_maxVoxelPacketsPerSecond(DEFAULT_MAX_VOXEL_PPS), _maxVoxelPacketsPerSecond(DEFAULT_MAX_VOXEL_PPS),
_lastAdjust(usecTimestampNow()) _lastAdjust(usecTimestampNow()),
_loginAction(NULL)
{ {
Application *appInstance = Application::getInstance(); Application *appInstance = Application::getInstance();
@ -86,13 +87,17 @@ Menu::Menu() :
QAction::AboutRole); QAction::AboutRole);
#endif #endif
(addActionToQMenuAndActionHash(fileMenu, AccountManager& accountManager = AccountManager::getInstance();
MenuOption::Login,
0,
this,
SLOT(loginForCurrentDomain())));
addActionToQMenuAndActionHash(fileMenu, MenuOption::Logout, 0, &AccountManager::getInstance(), SLOT(logout())); _loginAction = addActionToQMenuAndActionHash(fileMenu, MenuOption::Logout);
// call our toggle login function now so the menu option is setup properly
toggleLoginMenuItem();
// connect to the appropriate slots of the AccountManager so that we can change the Login/Logout menu item
connect(&accountManager, &AccountManager::loginComplete, this, &Menu::toggleLoginMenuItem);
connect(&accountManager, &AccountManager::logoutComplete, this, &Menu::toggleLoginMenuItem);
connect(&accountManager, &AccountManager::authEndpointChanged, this, &Menu::toggleLoginMenuItem);
addDisabledActionAndSeparator(fileMenu, "Scripts"); addDisabledActionAndSeparator(fileMenu, "Scripts");
addActionToQMenuAndActionHash(fileMenu, MenuOption::LoadScript, Qt::CTRL | Qt::Key_O, appInstance, SLOT(loadDialog())); addActionToQMenuAndActionHash(fileMenu, MenuOption::LoadScript, Qt::CTRL | Qt::Key_O, appInstance, SLOT(loadDialog()));
@ -1114,6 +1119,32 @@ void Menu::pasteToVoxel() {
sendFakeEnterEvent(); sendFakeEnterEvent();
} }
void Menu::toggleLoginMenuItem() {
AccountManager& accountManager = AccountManager::getInstance();
disconnect(_loginAction, 0, 0, 0);
if (accountManager.isLoggedIn()) {
// change the menu item to logout
_loginAction->setText("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() { void Menu::bandwidthDetails() {
if (! _bandwidthDialog) { if (! _bandwidthDialog) {
_bandwidthDialog = new BandwidthDialog(Application::getInstance()->getGLWidget(), _bandwidthDialog = new BandwidthDialog(Application::getInstance()->getGLWidget(),

View file

@ -111,6 +111,8 @@ public slots:
void exportSettings(); void exportSettings();
void goTo(); void goTo();
void pasteToVoxel(); void pasteToVoxel();
void toggleLoginMenuItem();
private slots: private slots:
void aboutApp(); void aboutApp();
@ -172,6 +174,7 @@ private:
QMenu* _activeScriptsMenu; QMenu* _activeScriptsMenu;
QString replaceLastOccurrence(QChar search, QChar replace, QString string); QString replaceLastOccurrence(QChar search, QChar replace, QString string);
quint64 _lastAdjust; quint64 _lastAdjust;
QAction* _loginAction;
}; };
namespace MenuOption { namespace MenuOption {

View file

@ -34,7 +34,6 @@ const QString ACCOUNTS_GROUP = "accounts";
AccountManager::AccountManager() : AccountManager::AccountManager() :
_rootURL(), _rootURL(),
_username(),
_networkAccessManager(), _networkAccessManager(),
_pendingCallbackMap(), _pendingCallbackMap(),
_accounts() _accounts()
@ -76,18 +75,22 @@ void AccountManager::logout() {
settings.remove(keyURLString); settings.remove(keyURLString);
qDebug() << "Removed account info for" << _rootURL << "from in-memory accounts and .ini file"; 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) { void AccountManager::setRootURL(const QUrl& rootURL) {
if (_rootURL != rootURL) { if (_rootURL != rootURL) {
_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() << "URL for node authentication has been changed to" << qPrintable(_rootURL.toString());
qDebug() << "Re-setting authentication flow."; qDebug() << "Re-setting authentication flow.";
// tell listeners that the auth endpoint has changed
emit authEndpointChanged();
} }
} }
@ -216,7 +219,7 @@ bool AccountManager::checkAndSignalForAccessToken() {
if (!hasToken) { if (!hasToken) {
// emit a signal so somebody can call back to us and request an access token given a username and password // 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; return hasToken;
@ -265,7 +268,9 @@ void AccountManager::requestFinished() {
DataServerAccountInfo freshAccountInfo(rootObject); DataServerAccountInfo freshAccountInfo(rootObject);
_accounts.insert(rootURL, freshAccountInfo); _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 // store this access token into the local settings
QSettings localSettings; QSettings localSettings;

View file

@ -42,6 +42,7 @@ public:
const QByteArray& dataByteArray = QByteArray()); const QByteArray& dataByteArray = QByteArray());
void setRootURL(const QUrl& rootURL); void setRootURL(const QUrl& rootURL);
bool hasAuthEndpoint() { return !_rootURL.isEmpty(); }
bool isLoggedIn() { return !_rootURL.isEmpty() && hasValidAccessToken(); } bool isLoggedIn() { return !_rootURL.isEmpty() && hasValidAccessToken(); }
bool hasValidAccessToken(); bool hasValidAccessToken();
@ -56,9 +57,11 @@ public slots:
void requestError(QNetworkReply::NetworkError error); void requestError(QNetworkReply::NetworkError error);
void logout(); void logout();
signals: signals:
void authenticationRequired(); void authRequired();
void receivedAccessToken(const QUrl& rootURL); void authEndpointChanged();
void usernameChanged(const QString& username); void usernameChanged(const QString& username);
void loginComplete(const QUrl& rootURL);
void logoutComplete();
private slots: private slots:
void passSuccessToCallback(); void passSuccessToCallback();
void passErrorToCallback(QNetworkReply::NetworkError errorCode); void passErrorToCallback(QNetworkReply::NetworkError errorCode);
@ -71,7 +74,6 @@ private:
const JSONCallbackParameters& callbackParams, const QByteArray& dataByteArray); const JSONCallbackParameters& callbackParams, const QByteArray& dataByteArray);
QUrl _rootURL; QUrl _rootURL;
QString _username;
QNetworkAccessManager _networkAccessManager; QNetworkAccessManager _networkAccessManager;
QMap<QNetworkReply*, JSONCallbackParameters> _pendingCallbackMap; QMap<QNetworkReply*, JSONCallbackParameters> _pendingCallbackMap;

View file

@ -8,6 +8,8 @@
#include <QtCore/QJsonObject> #include <QtCore/QJsonObject>
#include "AccountManager.h"
#include "DomainInfo.h" #include "DomainInfo.h"
DomainInfo::DomainInfo() : DomainInfo::DomainInfo() :
@ -20,7 +22,8 @@ DomainInfo::DomainInfo() :
_publicKey(), _publicKey(),
_isConnected(false) _isConnected(false)
{ {
// clear appropriate variables after a domain-server logout
connect(&AccountManager::getInstance(), &AccountManager::logoutComplete, this, &DomainInfo::logout);
} }
void DomainInfo::reset() { 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;
}
}

View file

@ -56,6 +56,8 @@ public:
private slots: private slots:
void completedHostnameLookup(const QHostInfo& hostInfo); void completedHostnameLookup(const QHostInfo& hostInfo);
void logout();
signals: signals:
void hostnameChanged(const QString& hostname); void hostnameChanged(const QString& hostname);
void connectedToDomain(const QString& hostname); void connectedToDomain(const QString& hostname);

View file

@ -76,6 +76,9 @@ NodeList::NodeList(char newOwnerType, unsigned short int newSocketListenPort) :
// clear our NodeList when the domain changes // clear our NodeList when the domain changes
connect(&_domainInfo, &DomainInfo::hostnameChanged, this, &NodeList::reset); 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) { bool NodeList::packetVersionAndHashMatch(const QByteArray& packet) {