refs #497 Login component integrated with authentication service

This commit is contained in:
Klaus Basan
2015-10-21 18:58:24 +02:00
committed by Mathew Sutcliffe
parent 7cb4c6a6c6
commit 5ae502af34
7 changed files with 455 additions and 66 deletions

View File

@@ -0,0 +1,146 @@
/* Copyright (C) 2015
* swift project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
#include "databaseauthentication.h"
#include "blackmisc/network/networkutils.h"
#include "blackmisc/network/url.h"
#include "blackmisc/logmessage.h"
#include <QString>
#include <QUrlQuery>
#include <QJsonDocument>
#include <QHttpPart>
#include <QHttpMultiPart>
using namespace BlackMisc;
using namespace BlackMisc::Network;
namespace BlackCore
{
CDatabaseAuthenticationService::CDatabaseAuthenticationService(QObject *parent) :
QObject(parent),
m_networkManager(new QNetworkAccessManager(this))
{
this->connect(this->m_networkManager, &QNetworkAccessManager::finished, this, &CDatabaseAuthenticationService::ps_parseServerResponse);
}
const Network::CAuthenticatedUser &CDatabaseAuthenticationService::getUser() const
{
return m_user;
}
void CDatabaseAuthenticationService::gracefulShutdown()
{
if (this->m_shutdown) { return; }
this->m_shutdown = true;
this->logoff();
}
BlackMisc::CStatusMessageList CDatabaseAuthenticationService::login(const QString &username, const QString &password)
{
CStatusMessageList msgs;
static const CLogCategoryList cats(CLogCategoryList(this).join({ CLogCategory::validation()}));
if (this->m_shutdown) { msgs.push_back(CStatusMessage(cats, CStatusMessage::SeverityError, "Shutdown in progress")); return msgs; }
QString un(username.trimmed());
QString pw(password.trimmed());
if (un.isEmpty()) { msgs.push_back(CStatusMessage(cats, CStatusMessage::SeverityError, "No user name/id")); }
if (pw.isEmpty()) { msgs.push_back(CStatusMessage(cats, CStatusMessage::SeverityError, "No password")); }
if (!msgs.isEmpty()) { return msgs; }
CUrl url(this->m_setup.get().dbLoginService());
QString msg;
if (!CNetworkUtils::canConnect(url, msg))
{
msgs.push_back(CStatusMessage(cats, CStatusMessage::SeverityError, msg));
return msgs;
}
QUrlQuery params;
params.addQueryItem("username", un);
params.addQueryItem("password", pw);
if (m_setup.get().dbDebugFlag()) { CNetworkUtils::addDebugFlag(params); }
QString query = params.toString();
QNetworkRequest request(CNetworkUtils::getNetworkRequest(url, CNetworkUtils::PostUrlEncoded));
QNetworkReply *r = this->m_networkManager->post(request, query.toUtf8());
if (!r)
{
QString rm("Cannot send request to authentication server %1");
msgs.push_back(CStatusMessage(cats, CStatusMessage::SeverityError, rm.arg(url.toQString())));
}
else
{
QString rm("Sent request to authentication server %1");
msgs.push_back(CStatusMessage(cats, CStatusMessage::SeverityInfo, rm.arg(url.toQString())));
}
return msgs;
}
void CDatabaseAuthenticationService::logoff()
{
CUrl url(this->m_setup.get().dbLoginService());
url.setQuery("logoff=true");
QNetworkRequest request(CNetworkUtils::getNetworkRequest(url));
this->m_networkManager->get(request);
}
void CDatabaseAuthenticationService::ps_parseServerResponse(QNetworkReply *nwReplyPtr)
{
// always cleanup reply
QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> nwReply(nwReplyPtr);
if (this->m_shutdown) { return; }
QString urlString(nwReply->url().toString());
if (urlString.toLower().contains("logoff"))
{
emit logoffFinished();
return;
}
if (nwReply->error() == QNetworkReply::NoError)
{
QString json(nwReply->readAll());
if (json.isEmpty())
{
CLogMessage(this).error("Authentication failed, no response from %1") << urlString;
return;
}
QJsonObject jsonObj(Json::jsonObjectFromString(json));
CAuthenticatedUser user(CAuthenticatedUser::fromDatabaseJson(jsonObj));
CStatusMessageList msgs;
static const CLogCategoryList cats(CLogCategoryList(this).join({ CLogCategory::validation()}));
if (!user.isAuthenticated() || !user.isValid())
{
msgs.push_back(CStatusMessage(cats, CStatusMessage::SeverityError, "Cannot login, user or password wrong"));
}
else
{
if (!user.isEnabled())
{
msgs.push_back(CStatusMessage(cats, CStatusMessage::SeverityError, "User is disabled"));
}
if (user.getRoles().isEmpty())
{
msgs.push_back(CStatusMessage(cats, CStatusMessage::SeverityError, "User has no roles"));
}
}
emit userAuthenticationFinished(user, msgs);
}
else
{
CLogMessage(this).error("Authentication failed, %1") << nwReply->errorString();
return;
}
}
} // namespace

View File

@@ -0,0 +1,66 @@
/* Copyright (C) 2015
* swift project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
#ifndef BLACKCORE_DATABASE_USER_H
#define BLACKCORE_DATABASE_USER_H
//! \file
#include "blackcore/blackcoreexport.h"
#include "blackmisc/network/authenticateduser.h"
#include "blackcore/data/globalsetup.h"
#include "blackmisc/statusmessagelist.h"
#include <QString>
#include <QNetworkReply>
namespace BlackCore
{
//! Databse user used with swift DB. Features role and cookie handling.
class BLACKCORE_EXPORT CDatabaseAuthenticationService: public QObject
{
Q_OBJECT
public:
//! Constructor
CDatabaseAuthenticationService(QObject *parent = nullptr);
//! Get the user
const BlackMisc::Network::CAuthenticatedUser &getUser() const;
//! Shutdown
void gracefulShutdown();
public slots:
//! Try to login to authentication web service
BlackMisc::CStatusMessageList login(const QString &id, const QString &password);
//! Logoff
void logoff();
signals:
//! User authenticated
void userAuthenticationFinished(const BlackMisc::Network::CAuthenticatedUser &user, const BlackMisc::CStatusMessageList &loginStatus);
//! Logoff completed
void logoffFinished();
private slots:
//! Parse login answer
void ps_parseServerResponse(QNetworkReply *nwReplyPtr);
private:
BlackMisc::Network::CAuthenticatedUser m_user;
CData<BlackCore::Data::GlobalSetup> m_setup {this}; //!< data cache
QNetworkAccessManager *m_networkManager = nullptr;
bool m_shutdown = false;
};
} // namespace
#endif // guard

View File

@@ -13,6 +13,7 @@
#include "blackmisc/network/url.h"
#include "blackmisc/logmessage.h"
using namespace BlackCore;
using namespace BlackMisc;
using namespace BlackMisc::Network;
@@ -25,13 +26,12 @@ namespace BlackGui
ui(new Ui::CDbLoginComponent)
{
ui->setupUi(this);
CUrl url(m_setup.get().dbHomePage());
ui->lbl_SwiftDB->setText("<a href=\"" + url.getFullUrl() + "\">swift DB@" + url.getHost() + "</a>");
ui->lbl_SwiftDB->setTextFormat(Qt::RichText);
ui->lbl_SwiftDB->setTextInteractionFlags(Qt::TextBrowserInteraction);
ui->lbl_SwiftDB->setOpenExternalLinks(true);
this->setModeLogin(true);
this->ps_setupChanged();
connect(ui->pb_Login, &QPushButton::clicked, this, &CDbLoginComponent::ps_onLoginClicked);
connect(ui->pb_Logoff, &QPushButton::clicked, this, &CDbLoginComponent::ps_onLogoffClicked);
connect(&m_loginService, &CDatabaseAuthenticationService::userAuthenticationFinished, this, &CDbLoginComponent::ps_AuthenticationFinished);
}
CDbLoginComponent::~CDbLoginComponent()
@@ -48,19 +48,59 @@ namespace BlackGui
void CDbLoginComponent::ps_onLoginClicked()
{
CStatusMessageList msgs;
static const CLogCategoryList cats(CLogCategoryList(this).join({ CLogCategory::validation()}));
QString un(ui->le_Username->text().trimmed());
QString pw(ui->le_Password->text().trimmed());
if (un.isEmpty()) { msgs.push_back(CStatusMessage::CStatusMessage(cats, CStatusMessage::SeverityError, "No user name")); }
if (pw.isEmpty()) { msgs.push_back(CStatusMessage::CStatusMessage(cats, CStatusMessage::SeverityError, "No password")); }
CStatusMessageList msgs = m_loginService.login(un, pw);
if (msgs.hasWarningOrErrorMessages())
{
CLogMessage::preformatted(msgs);
displayOverlayMessages(msgs);
return;
}
else if (!msgs.empty())
{
CLogMessage::preformatted(msgs);
}
}
void CDbLoginComponent::ps_onLogoffClicked()
{
this->m_loginService.logoff();
this->setModeLogin(true);
}
void CDbLoginComponent::ps_AuthenticationFinished(const CAuthenticatedUser &user, const CStatusMessageList &status)
{
bool ok = !status.hasErrorMessages();
if (ok)
{
CLogMessage(this).info("User authenticated: %1") << user.toQString();
this->setModeLogin(false);
this->ui->le_Name->setText(user.getRealNameAndId());
this->ui->te_Roles->setPlainText(user.getRolesAsString());
}
else
{
this->setModeLogin(true);
this->displayOverlayMessages(status);
CLogMessage(this).preformatted(status);
}
}
void CDbLoginComponent::ps_setupChanged()
{
CUrl url(m_setup.get().dbHomePage());
ui->lbl_SwiftDB->setText("<a href=\"" + url.getFullUrl() + "\">swift DB@" + url.getHost() + "</a>");
ui->lbl_SwiftDB->setTextFormat(Qt::RichText);
ui->lbl_SwiftDB->setTextInteractionFlags(Qt::TextBrowserInteraction);
ui->lbl_SwiftDB->setOpenExternalLinks(true);
}
void CDbLoginComponent::setModeLogin(bool modeLogin)
{
this->ui->fr_Login->setVisible(modeLogin);
this->ui->fr_Logoff->setVisible(!modeLogin);
}
} // ns

View File

@@ -13,6 +13,7 @@
#define BLACKGUI_COMPONENTS_DBLOGINCOMPONENT_H
#include "blackgui/blackguiexport.h"
#include "blackcore/databaseauthentication.h"
#include "blackcore/data/globalsetup.h"
#include <QFrame>
#include <QScopedPointer>
@@ -39,17 +40,28 @@ namespace BlackGui
private:
QScopedPointer<Ui::CDbLoginComponent> ui;
BlackCore::CData<BlackCore::Data::GlobalSetup> m_setup {this}; //!< data cache
BlackCore::CData<BlackCore::Data::GlobalSetup> m_setup {this, &CDbLoginComponent::ps_setupChanged}; //!< data cache
BlackCore::CDatabaseAuthenticationService m_loginService {this}; //!< login service
//! Overlay messages
void displayOverlayMessages(const BlackMisc::CStatusMessageList &msgs);
//! Mode login
void setModeLogin(bool modeLogin);
private slots:
//! Login
void ps_onLoginClicked();
};
//! Logoff
void ps_onLogoffClicked();
//! User authentication completed
void ps_AuthenticationFinished(const BlackMisc::Network::CAuthenticatedUser &user, const BlackMisc::CStatusMessageList &status);
//! Setup changed
void ps_setupChanged();
};
} // ns
} // ns

View File

@@ -2,6 +2,14 @@
<ui version="4.0">
<class>CDbLoginComponent</class>
<widget class="QFrame" name="CDbLoginComponent">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>333</width>
<height>299</height>
</rect>
</property>
<property name="minimumSize">
<size>
<width>300</width>
@@ -17,66 +25,170 @@
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="1" column="2">
<widget class="QLineEdit" name="le_Username">
<property name="maxLength">
<number>40</number>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QFrame" name="fr_Header">
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="placeholderText">
<string>user name or id</string>
</property>
<property name="clearButtonEnabled">
<bool>true</bool>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="lbl_SwiftDBIcon">
<property name="maximumSize">
<size>
<width>40</width>
<height>16777215</height>
</size>
</property>
<property name="text">
<string/>
</property>
<property name="pixmap">
<pixmap resource="../../blackmisc/blackmisc.qrc">:/own/icons/own/swift/swift32Database.png</pixmap>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="lbl_SwiftDB">
<property name="text">
<string>DB</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="lbl_SwiftDBIcon">
<property name="text">
<string/>
<item>
<widget class="QFrame" name="fr_Login">
<property name="minimumSize">
<size>
<width>0</width>
<height>100</height>
</size>
</property>
<property name="pixmap">
<pixmap resource="../../blackmisc/blackmisc.qrc">:/own/icons/own/swift/swift32Database.png</pixmap>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="1">
<widget class="QLineEdit" name="le_Username">
<property name="maxLength">
<number>40</number>
</property>
<property name="placeholderText">
<string>user name or id</string>
</property>
<property name="clearButtonEnabled">
<bool>true</bool>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="lbl_User">
<property name="text">
<string>User:</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="lbl_Password">
<property name="text">
<string>Password:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="le_Password">
<property name="echoMode">
<enum>QLineEdit::PasswordEchoOnEdit</enum>
</property>
<property name="placeholderText">
<string>password</string>
</property>
<property name="clearButtonEnabled">
<bool>true</bool>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QPushButton" name="pb_Login">
<property name="text">
<string>login</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="3" column="2">
<widget class="QPushButton" name="pb_Login">
<property name="text">
<string>login</string>
<item>
<widget class="QFrame" name="fr_Logoff">
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="lbl_Password">
<property name="text">
<string>Password:</string>
</property>
</widget>
</item>
<item row="2" column="2">
<widget class="QLineEdit" name="le_Password">
<property name="placeholderText">
<string>password</string>
</property>
<property name="clearButtonEnabled">
<bool>true</bool>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="lbl_User">
<property name="text">
<string>User:</string>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QLabel" name="lbl_SwiftDB">
<property name="text">
<string>DB</string>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<widget class="QLabel" name="lbl_Name">
<property name="text">
<string>Name:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="le_Name">
<property name="readOnly">
<bool>true</bool>
</property>
<property name="clearButtonEnabled">
<bool>false</bool>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="lbl_Roles">
<property name="text">
<string>Roles:</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QPushButton" name="pb_Logoff">
<property name="text">
<string>logoff</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QPlainTextEdit" name="te_Roles">
<property name="minimumSize">
<size>
<width>0</width>
<height>40</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>40</height>
</size>
</property>
<property name="documentTitle">
<string>User roles</string>
</property>
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>

View File

@@ -13,8 +13,8 @@
#include "blackmisc/logmessage.h"
#include "blackmisc/project.h"
#include "blackgui/guiutility.h"
#include "blackgui/roles.h"
using namespace BlackCore;
using namespace BlackMisc;
using namespace BlackMisc::Aviation;
using namespace BlackMisc::Simulation;
@@ -32,6 +32,8 @@ namespace BlackGui
COverlayMessagesFrame(parent),
ui(new Ui::CDbMappingComponent)
{
m_authenticationService = new CDatabaseAuthenticationService(this);
ui->setupUi(this);
this->ui->tvp_AircraftModelsForVPilot->setAircraftModelMode(CAircraftModelListModel::VPilotRuleModel);
connect(ui->editor_Model, &CModelMappingForm::requestSave, this, &CDbMappingComponent::save);
@@ -55,7 +57,7 @@ namespace BlackGui
void CDbMappingComponent::initVPilotLoading()
{
if (CRoles::roles().isAdmin() &&
if (m_authenticationService->getUser().isAdmin() &&
CProject::isRunningOnWindowsNtPlatform() &&
CProject::isCompiledWithMsFlightSimulatorSupport())
{
@@ -112,6 +114,7 @@ namespace BlackGui
this->disconnect();
CWebDataServicesAware::gracefulShutdown();
this->m_vPilotReader.gracefulShutdown();
this->m_authenticationService->gracefulShutdown();
if (this->m_modelLoader) { this->m_modelLoader->gracefulShutdown(); }
}
@@ -368,12 +371,17 @@ namespace BlackGui
CDbMappingComponent *mapComp = qobject_cast<CDbMappingComponent *>(this->parent());
Q_ASSERT_X(mapComp, Q_FUNC_INFO, "Cannot access parent");
if (CRoles::roles().isAdmin())
if (this->mappingComponent()->m_authenticationService->getUser().isAdmin())
{
menu.addAction(CIcons::appMappings16(), "Load vPilot Rules", mapComp, SLOT(ps_loadVPilotData()));
menu.addSeparator();
}
}
CDbMappingComponent *CDbMappingComponent::CMappingVPilotMenu::mappingComponent() const
{
return qobject_cast<CDbMappingComponent *>(this->parent());
}
} // ns
} // ns

View File

@@ -12,6 +12,7 @@
#ifndef BLACKGUI_COMPONENTS_DBMAPPINGCOMPONENT_H
#define BLACKGUI_COMPONENTS_DBMAPPINGCOMPONENT_H
#include "blackcore/databaseauthentication.h"
#include "blackgui/blackguiexport.h"
#include "blackgui/overlaymessagesframe.h"
#include "blackgui/menudelegate.h"
@@ -106,6 +107,7 @@ namespace BlackGui
QScopedPointer<Ui::CDbMappingComponent> ui;
BlackMisc::Simulation::FsCommon::CVPilotRulesReader m_vPilotReader;
std::unique_ptr<BlackMisc::Simulation::IAircraftModelLoader> m_modelLoader;
BlackCore::CDatabaseAuthenticationService *m_authenticationService = nullptr;
bool m_withVPilot = false;
//! Consolidated aircraft model
@@ -117,7 +119,7 @@ namespace BlackGui
//! Init model loader
bool initModelLoader(const BlackMisc::Simulation::CSimulatorInfo &simInfo);
// -------------------- component specifi menus --------------------------
// -------------------- component specific menus --------------------------
//! The menu for loading and handling own models for mapping
//! \note This is specific for that very component
@@ -145,6 +147,9 @@ namespace BlackGui
//! \copydoc IMenuDelegate::customMenu
virtual void customMenu(QMenu &menu) const override;
private:
CDbMappingComponent *mappingComponent() const;
};
};