8 #include <QNetworkReply>
9 #include <QNetworkRequest>
10 #include <QScopedPointer>
11 #include <QScopedPointerDeleteLater>
28 using namespace swift::misc::network;
32 const QStringList &CDatabaseAuthenticationService::getLogCategories()
38 CDatabaseAuthenticationService::CDatabaseAuthenticationService(QObject *parent) : QObject(parent)
45 if (m_shutdown) {
return; }
63 if (!
sApp || m_shutdown)
69 const QString un(username.trimmed());
70 const QString pw(password.trimmed());
73 if (!msgs.
isEmpty()) {
return msgs; }
77 if (!CNetworkUtils::canConnect(url, msg))
84 params.addQueryItem(
"username", un);
85 params.addQueryItem(
"password", pw);
88 const QString query = params.toString();
89 const QNetworkRequest request(CNetworkUtils::getSwiftNetworkRequest(url, CNetworkUtils::PostUrlEncoded,
92 { this, &CDatabaseAuthenticationService::parseServerResponse });
93 static const QString rm(
"Sent request to authentication server '%1'");
100 if (!
sApp) {
return; }
103 QNetworkRequest request(CNetworkUtils::getSwiftNetworkRequest(url));
104 sApp->
getFromNetwork(request, {
this, &CDatabaseAuthenticationService::parseServerResponse });
108 void CDatabaseAuthenticationService::parseServerResponse(QNetworkReply *nwReplyPtr)
111 QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> nwReply(nwReplyPtr);
113 if (m_shutdown) {
return; }
114 QString urlString(nwReply->url().toString());
115 if (urlString.contains(
"logoff", Qt::CaseInsensitive))
122 if (nwReply->error() == QNetworkReply::NoError)
124 const QString json(nwReply->readAll().trimmed());
127 CLogMessage(
this).
error(u
"Authentication failed, no response from '%1'") << urlString;
130 if (!json::looksLikeJson(json))
133 << CNetworkUtils::removeHtmlPartsFromPhpErrorMessage(json);
140 CAuthenticatedUser::fromDatabaseJson(jsonObj.contains(
"user") ? jsonObj[
"user"].toObject() : jsonObj);
142 if (jsonObj.contains(
"messages"))
177 m_swiftDbUser.
set(user);
182 CLogMessage(
this).
error(u
"Authentication failed, %1") << nwReply->errorString();
187 void CDatabaseAuthenticationService::userChanged()
SWIFT_CORE_EXPORT swift::core::CApplication * sApp
Single instance of application object.
QNetworkReply * getFromNetwork(const swift::misc::network::CUrl &url, const CallbackSlot &callback, int maxRedirects=DefaultMaxRedirects)
Request to get network reply.
data::CGlobalSetup getGlobalSetup() const
Global setup.
static constexpr int NoLogRequestId
network request without logging
QNetworkReply * postToNetwork(const QNetworkRequest &request, int logId, const QByteArray &data, const CallbackSlot &callback)
Post to network.
void deleteAllCookies()
Delete all cookies from cookie manager.
const QString & getApplicationNameAndVersion() const
Application name and version.
bool dbDebugFlag() const
Debug flag.
swift::misc::network::CUrl getDbLoginServiceUrl() const
Login service.
void userAuthenticationFinished(const swift::misc::network::CAuthenticatedUser &user, const swift::misc::CStatusMessageList &loginStatus)
User authenticated.
void logoffFinished()
Logoff completed.
swift::misc::CStatusMessageList login(const QString &id, const QString &password)
Try to login to authentication web service.
swift::misc::network::CAuthenticatedUser getDbUser() const
DB user.
void gracefulShutdown()
Shutdown.
bool isUserAuthenticated() const
User authenticated.
T get() const
Get a copy of the current value.
CStatusMessage set(const typename Trait::type &value, qint64 timestamp=0)
Write a new value. Must be called from the thread in which the owner lives.
static const QString & swiftDbWebservice()
Webservice with swift DB.
A sequence of log categories.
Class for emitting a log message.
Derived & error(const char16_t(&format)[N])
Set the severity to error, providing a format string.
void push_back(const T &value)
Appends an element at the end of the sequence.
bool isEmpty() const
Synonym for empty.
Streamable status message, e.g.
constexpr static auto SeverityError
Status severities.
constexpr static auto SeverityInfo
Status severities.
Status messages, e.g. from Core -> GUI.
void setCategories(const CLogCategoryList &categories)
Reset the categories of all messages in the list.
bool hasErrorMessages() const
Error messages.
static CStatusMessageList fromDatabaseJson(const QJsonArray &array)
From our database JSON format.
QString toQString(bool i18n=false) const
Cast as QString.
Value object encapsulating information of an authentiated user.
bool isAuthenticated() const
Authenticated.
bool isEnabled() const
Enabled.
const CRoleList & getRoles() const
Roles.
void setEnabled(bool enabled)
Enabled.
bool isValid() const
Valid user object?
bool hasRole(const QString &roleName) const
Has role?
Value object encapsulating information of a location, kind of simplified CValueObject compliant versi...
void setQuery(const QString &query)
Set query.
QJsonObject jsonObjectFromString(const QString &json, bool acceptCacheFormat)
JSON Object from string.
Classes interacting with the swift database (aka "datastore").
Free functions in swift::misc.