Validate domain names, allow saving login data

This commit is contained in:
Bernhard Rosenkränzer 2024-09-03 21:52:55 +02:00
parent 2216fedeaa
commit dc0e633e46
7 changed files with 40 additions and 5 deletions

View File

@ -4,7 +4,7 @@
#include <QJsonDocument>
#include <QJsonObject>
Configurator::Configurator(int &argc, char **&argv):QApplication(argc, argv),_nam(this) {
Configurator::Configurator(int &argc, char **&argv):QApplication(argc, argv),_nam(this),_settings("federated.computer", "configurator", this) {
setQuitOnLastWindowClosed(false);
_loginDialog = new FederatedLogin(0);
_loginDialog->setModal(true);

View File

@ -3,6 +3,7 @@
#include <QApplication>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QSettings>
#include <dialogs/FederatedLogin.h>
#include <lib/Service.h>
@ -18,10 +19,12 @@ public:
QByteArray download(QUrl const &url);
void waitForDownload(QNetworkReply *r) const;
Services const &services() const { return _services; }
QSettings &settings() { return _settings; }
protected Q_SLOTS:
void loginRequested();
private:
QNetworkAccessManager _nam;
QSettings _settings;
FederatedLogin * _loginDialog;
Services _services;
};

View File

@ -1,4 +1,6 @@
#include "FederatedLogin.h"
#include "DomainValidator.h"
#include <Configurator.h>
FederatedLogin::FederatedLogin(QWidget *parent):QDialog(parent),_layout(this) {
int y = 0;
@ -7,20 +9,23 @@ FederatedLogin::FederatedLogin(QWidget *parent):QDialog(parent),_layout(this) {
_layout.addWidget(_message, y, 0, 1, 2);
_domainLbl = new QLabel(tr("&Domain:"), this);
_layout.addWidget(_domainLbl, ++y, 0);
_domain = new QLineEdit("f11890a1.fedcom.net", this);
_domain = new QLineEdit(app->settings().value("domain").toString(), this);
_domain->setValidator(new DomainValidator(_domain));
_domainLbl->setBuddy(_domain);
_layout.addWidget(_domain, y, 1);
_userLbl = new QLabel(tr("&User/Email:"), this);
_layout.addWidget(_userLbl, ++y, 0);
_user = new QLineEdit(this);
_user = new QLineEdit(app->settings().value("user").toString(), this);
_userLbl->setBuddy(_user);
_layout.addWidget(_user, y, 1);
_passwordLbl = new QLabel(tr("&Password:"), this);
_layout.addWidget(_passwordLbl, ++y, 0);
_password = new QLineEdit(this);
_password = new QLineEdit(app->settings().value("password").toString(), this);
_password->setEchoMode(QLineEdit::Password);
_passwordLbl->setBuddy(_password);
_layout.addWidget(_password, y, 1);
_storeLogin = new QCheckBox(tr("&Store authentication information for next login"), this);
_layout.addWidget(_storeLogin, ++y, 0, 1, 2);
_buttons = new ButtonRow(this);
_layout.addWidget(_buttons, ++y, 0, 1, 2);
_ok = new QPushButton(tr("&OK"), _buttons);
@ -28,6 +33,7 @@ FederatedLogin::FederatedLogin(QWidget *parent):QDialog(parent),_layout(this) {
_buttons->add(_ok);
_cancel = new QPushButton(tr("&Cancel"), _buttons);
_buttons->add(_cancel);
connect(_ok, &QPushButton::clicked, this, &FederatedLogin::storeLogin);
connect(_ok, &QPushButton::clicked, this, &QDialog::accept);
connect(_cancel, &QPushButton::clicked, this, &QDialog::reject);
}
@ -36,3 +42,11 @@ void FederatedLogin::setMessage(QString const &m) {
_message->setText(m);
_message->setHidden(m.isEmpty());
}
void FederatedLogin::storeLogin() {
if(_storeLogin->isChecked()) {
app->settings().setValue("domain", _domain->text());
app->settings().setValue("user", _user->text());
app->settings().setValue("password", _password->text());
}
}

View File

@ -4,6 +4,7 @@
#include <QGridLayout>
#include <QLabel>
#include <QLineEdit>
#include <QCheckBox>
#include <QPushButton>
#include "../lib/ButtonRow.h"
@ -16,6 +17,8 @@ public:
QString user() const { return _user->text(); }
QString password() const { return _password->text(); }
void setMessage(QString const &m);
protected Q_SLOTS:
void storeLogin();
protected:
QGridLayout _layout;
QLabel * _message;
@ -25,6 +28,7 @@ protected:
QLineEdit * _user;
QLabel * _passwordLbl;
QLineEdit * _password;
QCheckBox * _storeLogin;
ButtonRow * _buttons;
QPushButton * _ok;
QPushButton * _cancel;

View File

@ -1,4 +1,4 @@
include_directories(${CMAKE_SOURCE_DIR})
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
add_library(fed STATIC Service.cpp ButtonRow.cpp ServiceWidget.cpp)
add_library(fed STATIC Service.cpp ButtonRow.cpp ServiceWidget.cpp DomainValidator.cpp)
target_link_libraries(fed Qt${QT_VERSION}::Core Qt${QT_VERSION}::Gui Qt${QT_VERSION}::Widgets Qt${QT_VERSION}::Network)

4
lib/DomainValidator.cpp Normal file
View File

@ -0,0 +1,4 @@
#include "DomainValidator.h"
DomainValidator::DomainValidator(QObject * parent):QRegularExpressionValidator(QRegularExpression("^((?!-)[A-Za-z0-9-]{1,63}(?<!-)\\.)+[A-Za-z]{2,63}$"), parent) {
}

10
lib/DomainValidator.h Normal file
View File

@ -0,0 +1,10 @@
#pragma once
#include <QRegularExpressionValidator>
#include <QRegularExpression>
class DomainValidator:public QRegularExpressionValidator {
Q_OBJECT
public:
DomainValidator(QObject * parent = nullptr);
};