18 #include "ui_qgsauthserverseditor.h"
22 #include <QMessageBox>
24 #include "qgssettings.h"
38 mAuthNotifyLayout =
new QVBoxLayout;
39 this->setLayout( mAuthNotifyLayout );
41 mAuthNotifyLayout->addWidget( mAuthNotify );
46 connect( btnAddServer, &QToolButton::clicked,
this, &QgsAuthServersEditor::btnAddServer_clicked );
47 connect( btnRemoveServer, &QToolButton::clicked,
this, &QgsAuthServersEditor::btnRemoveServer_clicked );
48 connect( btnEditServer, &QToolButton::clicked,
this, &QgsAuthServersEditor::btnEditServer_clicked );
49 connect( btnGroupByOrg, &QToolButton::toggled,
this, &QgsAuthServersEditor::btnGroupByOrg_toggled );
52 this, &QgsAuthServersEditor::authMessageOut );
55 this, &QgsAuthServersEditor::refreshSslConfigsView );
57 setupSslConfigsTree();
59 connect( treeServerConfigs->selectionModel(), &QItemSelectionModel::selectionChanged,
60 this, &QgsAuthServersEditor::selectionChanged );
62 connect( treeServerConfigs, &QTreeWidget::itemDoubleClicked,
63 this, &QgsAuthServersEditor::handleDoubleClick );
65 connect( btnViewRefresh, &QAbstractButton::clicked,
this, &QgsAuthServersEditor::refreshSslConfigsView );
67 btnGroupByOrg->setChecked(
false );
69 if ( !sortbyval.isNull() )
70 btnGroupByOrg->setChecked( sortbyval.toBool() );
72 populateSslConfigsView();
77 static void setItemBold_( QTreeWidgetItem *item )
79 item->setFirstColumnSpanned(
true );
80 QFont secf( item->font( 0 ) );
82 item->setFont( 0, secf );
86 void QgsAuthServersEditor::setupSslConfigsTree()
88 treeServerConfigs->setColumnCount( 3 );
89 treeServerConfigs->setHeaderLabels(
90 QStringList() << tr(
"Common Name" )
92 << tr(
"Expiry Date" ) );
93 treeServerConfigs->setColumnWidth( 0, 275 );
94 treeServerConfigs->setColumnWidth( 1, 200 );
97 mRootSslConfigItem =
new QTreeWidgetItem(
99 QStringList( tr(
"SSL Server Configurations" ) ),
100 static_cast<int>( QgsAuthServersEditor::Section ) );
101 setItemBold_( mRootSslConfigItem );
102 mRootSslConfigItem->setFlags( Qt::ItemIsEnabled );
103 mRootSslConfigItem->setExpanded(
true );
104 treeServerConfigs->insertTopLevelItem( 0, mRootSslConfigItem );
107 static void removeChildren_( QTreeWidgetItem *item )
109 const auto constTakeChildren = item->takeChildren();
110 for ( QTreeWidgetItem *child : constTakeChildren )
116 void QgsAuthServersEditor::populateSslConfigsView()
118 removeChildren_( mRootSslConfigItem );
120 populateSslConfigsSection( mRootSslConfigItem,
122 QgsAuthServersEditor::ServerConfig );
125 void QgsAuthServersEditor::refreshSslConfigsView()
127 populateSslConfigsView();
130 void QgsAuthServersEditor::populateSslConfigsSection( QTreeWidgetItem *item,
131 const QList<QgsAuthConfigSslServer> &configs,
132 QgsAuthServersEditor::ConfigType conftype )
134 if ( btnGroupByOrg->isChecked() )
136 appendSslConfigsToGroup( configs, conftype, item );
140 appendSslConfigsToItem( configs, conftype, item );
144 void QgsAuthServersEditor::appendSslConfigsToGroup(
const QList<QgsAuthConfigSslServer> &configs,
145 QgsAuthServersEditor::ConfigType conftype,
146 QTreeWidgetItem *parent )
148 if ( configs.empty() )
153 parent = treeServerConfigs->currentItem();
157 QMap< QString, QList<QgsAuthConfigSslServer> > orgconfigs(
160 QMap< QString, QList<QgsAuthConfigSslServer> >::const_iterator it = orgconfigs.constBegin();
161 for ( ; it != orgconfigs.constEnd(); ++it )
163 QTreeWidgetItem *grpitem(
new QTreeWidgetItem( parent,
164 QStringList() << it.key(),
165 static_cast<int>( QgsAuthServersEditor::OrgName ) ) );
166 grpitem->setFirstColumnSpanned(
true );
167 grpitem->setFlags( Qt::ItemIsEnabled );
168 grpitem->setExpanded(
true );
170 QBrush orgb( grpitem->foreground( 0 ) );
171 orgb.setColor( QColor::fromRgb( 90, 90, 90 ) );
172 grpitem->setForeground( 0, orgb );
173 QFont grpf( grpitem->font( 0 ) );
174 grpf.setItalic(
true );
175 grpitem->setFont( 0, grpf );
177 appendSslConfigsToItem( it.value(), conftype, grpitem );
180 parent->sortChildren( 0, Qt::AscendingOrder );
183 void QgsAuthServersEditor::appendSslConfigsToItem(
const QList<QgsAuthConfigSslServer> &configs,
184 QgsAuthServersEditor::ConfigType conftype,
185 QTreeWidgetItem *parent )
187 if ( configs.empty() )
192 parent = treeServerConfigs->currentItem();
198 const auto constConfigs = configs;
201 QSslCertificate cert( config.sslCertificate() );
206 coltxts << QString( config.sslHostPort() );
207 coltxts << cert.expiryDate().toString();
209 QTreeWidgetItem *item(
new QTreeWidgetItem( parent, coltxts,
static_cast<int>( conftype ) ) );
214 item->setForeground( 2, redb );
218 item->setData( 0, Qt::UserRole,
id );
221 parent->sortChildren( 0, Qt::AscendingOrder );
224 void QgsAuthServersEditor::selectionChanged(
const QItemSelection &selected,
const QItemSelection &deselected )
227 Q_UNUSED( deselected )
231 void QgsAuthServersEditor::checkSelection()
233 bool isconfig =
false;
234 if ( treeServerConfigs->selectionModel()->selection().length() > 0 )
236 QTreeWidgetItem *item( treeServerConfigs->currentItem() );
238 switch ( ( QgsAuthServersEditor::ConfigType )item->type() )
240 case QgsAuthServersEditor::ServerConfig :
248 btnRemoveServer->setEnabled( isconfig );
249 btnEditServer->setEnabled( isconfig );
252 void QgsAuthServersEditor::handleDoubleClick( QTreeWidgetItem *item,
int col )
255 bool isconfig =
true;
257 switch ( ( QgsAuthServersEditor::ConfigType )item->type() )
259 case QgsAuthServersEditor::Section:
262 case QgsAuthServersEditor::OrgName:
271 btnEditServer_clicked();
275 void QgsAuthServersEditor::btnAddServer_clicked()
278 dlg->setWindowModality( Qt::WindowModal );
279 dlg->resize( 580, 512 );
282 refreshSslConfigsView();
287 void QgsAuthServersEditor::btnRemoveServer_clicked()
289 QTreeWidgetItem *item( treeServerConfigs->currentItem() );
293 QgsDebugMsg( QStringLiteral(
"Current tree widget item not set" ) );
297 QString digest( item->data( 0, Qt::UserRole ).toString() );
298 QString hostport( item->text( 1 ) );
300 if ( digest.isEmpty() )
302 messageBar()->
pushMessage( tr(
"SSL custom config id missing" ),
303 Qgis::MessageLevel::Warning );
306 if ( hostport.isEmpty() )
308 messageBar()->
pushMessage( tr(
"SSL custom config host:port missing" ),
309 Qgis::MessageLevel::Warning );
315 QgsDebugMsg( QStringLiteral(
"SSL custom config does not exist in database for host:port, id %1:" )
316 .arg( hostport, digest ) );
320 if ( QMessageBox::warning(
321 this, tr(
"Remove SSL Custom Configuration" ),
322 tr(
"Are you sure you want to remove the selected "
323 "SSL custom configuration from the database?\n\n"
324 "Operation can NOT be undone!" ),
325 QMessageBox::Ok | QMessageBox::Cancel,
326 QMessageBox::Cancel ) == QMessageBox::Cancel )
333 messageBar()->
pushMessage( tr(
"ERROR removing SSL custom config from authentication database for host:port, id %1:" )
334 .arg( hostport, digest ),
335 Qgis::MessageLevel::Critical );
339 item->parent()->removeChild( item );
343 void QgsAuthServersEditor::btnEditServer_clicked()
345 QTreeWidgetItem *item( treeServerConfigs->currentItem() );
349 QgsDebugMsg( QStringLiteral(
"Current tree widget item not set" ) );
353 QString digest( item->data( 0, Qt::UserRole ).toString() );
354 QString hostport( item->text( 1 ) );
356 if ( digest.isEmpty() )
358 messageBar()->
pushMessage( tr(
"SSL custom config id missing." ),
359 Qgis::MessageLevel::Warning );
362 if ( hostport.isEmpty() )
364 messageBar()->
pushMessage( tr(
"SSL custom config host:port missing." ),
365 Qgis::MessageLevel::Warning );
371 QgsDebugMsg( QStringLiteral(
"SSL custom config does not exist in database" ) );
376 QSslCertificate cert( config.sslCertificate() );
380 dlg->setWindowModality( Qt::WindowModal );
381 dlg->resize( 500, 500 );
384 refreshSslConfigsView();
389 void QgsAuthServersEditor::btnGroupByOrg_toggled(
bool checked )
393 authMessageOut( QObject::tr(
"Could not store sort by preference." ),
394 QObject::tr(
"Authentication SSL Configs" ),
397 populateSslConfigsView();
402 int levelint =
static_cast<int>( level );
410 treeServerConfigs->setFocus();
412 QWidget::showEvent( e );
420 int QgsAuthServersEditor::messageTimeout()
422 QgsSettings settings;
423 return settings.value( QStringLiteral(
"qgis/messageTimeout" ), 5 ).toInt();
MessageLevel
Level for messages This will be used both for message log and message bar in application.
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
static QgsAuthManager * authManager()
Returns the application's authentication manager instance.
static QString resolvedCertName(const QSslCertificate &cert, bool issuer=false)
Gets the general name via RFC 5280 resolution.
static QString shaHexForCert(const QSslCertificate &cert, bool formatted=false)
Gets the sha1 hash for certificate.
static bool certIsViable(const QSslCertificate &cert)
certIsViable checks for viability errors of cert and whether it is NULL
static QMap< QString, QList< QgsAuthConfigSslServer > > sslConfigsGroupedByOrg(const QList< QgsAuthConfigSslServer > &configs)
Map SSL custom configs' certificates to their oraganization.
Configuration container for SSL server connection exceptions or overrides.
static QColor redColor()
Red color representing invalid, untrusted, etc. certificate.
MessageLevel
Message log level (mirrors that of QgsMessageLog, so it can also output there)
void messageOut(const QString &message, const QString &tag=QgsAuthManager::AUTH_MAN_TAG, QgsAuthManager::MessageLevel level=QgsAuthManager::INFO) const
Custom logging signal to relay to console output and QgsMessageLog.
void authDatabaseChanged()
Emitted when the authentication db is significantly changed, e.g. large record removal,...
QVariant authSetting(const QString &key, const QVariant &defaultValue=QVariant(), bool decrypt=false)
authSetting get an authentication setting (retrieved as string and returned as QVariant( QString ))
QgsAuthServersEditor(QWidget *parent=nullptr)
Widget for editing authentication configurations directly in database.
void showEvent(QShowEvent *e) override
Dialog wrapper of widget for editing an SSL server configuration.
QgsAuthSslConfigWidget * sslCustomConfigWidget()
Access the embedded SSL server configuration widget.
Widget for importing an SSL server certificate exception into the authentication database.
A bar for displaying non-blocking messages to the user.
void pushMessage(const QString &text, Qgis::MessageLevel level=Qgis::MessageLevel::Info, int duration=-1)
A convenience method for pushing a message with the specified text to the bar.