18#include "ui_qgsauthidentitieseditor.h"
39 mAuthNotifyLayout =
new QVBoxLayout;
40 this->setLayout( mAuthNotifyLayout );
42 mAuthNotifyLayout->addWidget( mAuthNotify );
47 connect( btnAddIdentity, &QToolButton::clicked,
this, &QgsAuthIdentitiesEditor::btnAddIdentity_clicked );
48 connect( btnRemoveIdentity, &QToolButton::clicked,
this, &QgsAuthIdentitiesEditor::btnRemoveIdentity_clicked );
49 connect( btnInfoIdentity, &QToolButton::clicked,
this, &QgsAuthIdentitiesEditor::btnInfoIdentity_clicked );
50 connect( btnGroupByOrg, &QToolButton::toggled,
this, &QgsAuthIdentitiesEditor::btnGroupByOrg_toggled );
53 this, &QgsAuthIdentitiesEditor::authMessageLog );
56 this, &QgsAuthIdentitiesEditor::refreshIdentitiesView );
58 setupIdentitiesTree();
60 connect( treeIdentities->selectionModel(), &QItemSelectionModel::selectionChanged,
61 this, &QgsAuthIdentitiesEditor::selectionChanged );
63 connect( treeIdentities, &QTreeWidget::itemDoubleClicked,
64 this, &QgsAuthIdentitiesEditor::handleDoubleClick );
66 connect( btnViewRefresh, &QAbstractButton::clicked,
this, &QgsAuthIdentitiesEditor::refreshIdentitiesView );
68 btnGroupByOrg->setChecked(
false );
71 btnGroupByOrg->setChecked( sortbyval.toBool() );
73 populateIdentitiesView();
78static void setItemBold_( QTreeWidgetItem *item )
80 item->setFirstColumnSpanned(
true );
81 QFont secf( item->font( 0 ) );
83 item->setFont( 0, secf );
86void QgsAuthIdentitiesEditor::setupIdentitiesTree()
88 treeIdentities->setColumnCount( 3 );
89 treeIdentities->setHeaderLabels(
90 QStringList() << tr(
"Common Name" )
92 << tr(
"Expiry Date" ) );
93 treeIdentities->setColumnWidth( 0, 300 );
94 treeIdentities->setColumnWidth( 1, 75 );
97 mRootCertIdentItem =
new QTreeWidgetItem(
99 QStringList( tr(
"Certificate Bundles" ) ),
100 static_cast<int>( QgsAuthIdentitiesEditor::Section ) );
101 setItemBold_( mRootCertIdentItem );
102 mRootCertIdentItem->setFlags( Qt::ItemIsEnabled );
103 mRootCertIdentItem->setExpanded(
true );
104 treeIdentities->insertTopLevelItem( 0, mRootCertIdentItem );
107static void removeChildren_( QTreeWidgetItem *item )
109 const auto constTakeChildren = item->takeChildren();
110 for ( QTreeWidgetItem *child : constTakeChildren )
116void QgsAuthIdentitiesEditor::populateIdentitiesView()
118 removeChildren_( mRootCertIdentItem );
120 populateIdentitiesSection( mRootCertIdentItem,
122 QgsAuthIdentitiesEditor::CertIdentity );
125void QgsAuthIdentitiesEditor::refreshIdentitiesView()
127 populateIdentitiesView();
130void QgsAuthIdentitiesEditor::populateIdentitiesSection( QTreeWidgetItem *item,
const QList<QSslCertificate> &certs,
131 QgsAuthIdentitiesEditor::IdentityType identype )
133 if ( btnGroupByOrg->isChecked() )
135 appendIdentitiesToGroup( certs, identype, item );
139 appendIdentitiesToItem( certs, identype, item );
143void QgsAuthIdentitiesEditor::appendIdentitiesToGroup(
const QList<QSslCertificate> &certs,
144 QgsAuthIdentitiesEditor::IdentityType identype,
145 QTreeWidgetItem *parent )
152 parent = treeIdentities->currentItem();
156 const QMap< QString, QList<QSslCertificate> > orgcerts(
159 QMap< QString, QList<QSslCertificate> >::const_iterator it = orgcerts.constBegin();
160 for ( ; it != orgcerts.constEnd(); ++it )
162 QTreeWidgetItem *grpitem(
new QTreeWidgetItem( parent,
163 QStringList() << it.key(),
164 static_cast<int>( QgsAuthIdentitiesEditor::OrgName ) ) );
165 grpitem->setFirstColumnSpanned(
true );
166 grpitem->setFlags( Qt::ItemIsEnabled );
167 grpitem->setExpanded(
true );
169 QBrush orgb( grpitem->foreground( 0 ) );
170 orgb.setColor( QColor::fromRgb( 90, 90, 90 ) );
171 grpitem->setForeground( 0, orgb );
172 QFont grpf( grpitem->font( 0 ) );
173 grpf.setItalic(
true );
174 grpitem->setFont( 0, grpf );
176 appendIdentitiesToItem( it.value(), identype, grpitem );
179 parent->sortChildren( 0, Qt::AscendingOrder );
182void QgsAuthIdentitiesEditor::appendIdentitiesToItem(
const QList<QSslCertificate> &certs,
183 QgsAuthIdentitiesEditor::IdentityType identype,
184 QTreeWidgetItem *parent )
191 parent = treeIdentities->currentItem();
197 const auto constCerts = certs;
198 for (
const QSslCertificate &cert : constCerts )
204 coltxts << QString( cert.serialNumber() );
205 coltxts << cert.expiryDate().toString();
207 QTreeWidgetItem *item(
new QTreeWidgetItem( parent, coltxts,
static_cast<int>( identype ) ) );
212 item->setForeground( 2, redb );
216 item->setData( 0, Qt::UserRole,
id );
219 parent->sortChildren( 0, Qt::AscendingOrder );
222void QgsAuthIdentitiesEditor::showCertInfo( QTreeWidgetItem *item )
227 const QString digest( item->data( 0, Qt::UserRole ).toString() );
231 QgsDebugError( QStringLiteral(
"Certificate identity does not exist in database" ) );
238 dlg->setWindowModality( Qt::WindowModal );
239 dlg->resize( 675, 500 );
244void QgsAuthIdentitiesEditor::selectionChanged(
const QItemSelection &selected,
const QItemSelection &deselected )
247 Q_UNUSED( deselected )
251void QgsAuthIdentitiesEditor::checkSelection()
254 if ( treeIdentities->selectionModel()->selection().length() > 0 )
256 QTreeWidgetItem *item( treeIdentities->currentItem() );
258 switch ( ( QgsAuthIdentitiesEditor::IdentityType )item->type() )
260 case QgsAuthIdentitiesEditor::CertIdentity:
268 btnRemoveIdentity->setEnabled( iscert );
269 btnInfoIdentity->setEnabled( iscert );
272void QgsAuthIdentitiesEditor::handleDoubleClick( QTreeWidgetItem *item,
int col )
277 switch ( ( QgsAuthIdentitiesEditor::IdentityType )item->type() )
279 case QgsAuthIdentitiesEditor::Section:
282 case QgsAuthIdentitiesEditor::OrgName:
291 showCertInfo( item );
295void QgsAuthIdentitiesEditor::btnAddIdentity_clicked()
298 dlg->setWindowModality( Qt::WindowModal );
299 dlg->resize( 400, dlg->height() );
307 messageBar()->
pushMessage( tr(
"ERROR storing identity bundle in authentication database." ),
310 populateIdentitiesView();
311 mRootCertIdentItem->setExpanded(
true );
317void QgsAuthIdentitiesEditor::btnRemoveIdentity_clicked()
319 QTreeWidgetItem *item( treeIdentities->currentItem() );
323 QgsDebugMsgLevel( QStringLiteral(
"Current tree widget item not set" ), 2 );
327 const QString digest( item->data( 0, Qt::UserRole ).toString() );
329 if ( digest.isEmpty() )
331 messageBar()->
pushMessage( tr(
"Certificate id missing." ),
338 QgsDebugError( QStringLiteral(
"Certificate identity does not exist in database" ) );
342 if ( QMessageBox::warning(
343 this, tr(
"Remove Certificate Identity" ),
344 tr(
"Are you sure you want to remove the selected "
345 "certificate identity from the database?\n\n"
346 "Operation can NOT be undone!" ),
347 QMessageBox::Ok | QMessageBox::Cancel,
348 QMessageBox::Cancel ) == QMessageBox::Cancel )
355 messageBar()->
pushMessage( tr(
"ERROR removing cert identity from authentication database for id %1:" ).arg( digest ),
360 item->parent()->removeChild( item );
364void QgsAuthIdentitiesEditor::btnInfoIdentity_clicked()
366 if ( treeIdentities->selectionModel()->selection().length() > 0 )
368 QTreeWidgetItem *item( treeIdentities->currentItem() );
369 handleDoubleClick( item, 0 );
373void QgsAuthIdentitiesEditor::btnGroupByOrg_toggled(
bool checked )
377 authMessageLog( QObject::tr(
"Could not store sort by preference." ),
378 QObject::tr(
"Authentication Identities" ),
381 populateIdentitiesView();
384void QgsAuthIdentitiesEditor::authMessageLog(
const QString &message,
const QString &authtag,
Qgis::MessageLevel level )
386 messageBar()->
pushMessage( authtag, message, level, 7 );
393 treeIdentities->setFocus();
395 QWidget::showEvent( e );
403int QgsAuthIdentitiesEditor::messageTimeout()
406 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.
@ Warning
Warning message.
@ Critical
Critical/error message.
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.
Dialog wrapper for widget displaying detailed info on a certificate and its hierarchical trust chain.
static QString resolvedCertName(const QSslCertificate &cert, bool issuer=false)
Gets the general name via RFC 5280 resolution.
static QMap< QString, QList< QSslCertificate > > certsGroupedByOrg(const QList< QSslCertificate > &certs)
Map certificates to their oraganization.
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 QColor redColor()
Red color representing invalid, untrusted, etc. certificate.
void showEvent(QShowEvent *e) override
Overridden show event of base widget.
QgsAuthIdentitiesEditor(QWidget *parent=nullptr)
Widget for editing authentication configurations directly in database.
Widget for importing an identity certificate/key bundle into the authentication database.
QgsAuthImportIdentityDialog::IdentityType identityType()
Gets identity type.
const QPair< QSslCertificate, QSslKey > certBundleToImport()
Gets certificate/key bundle to be imported.
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 ))
void messageLog(const QString &message, const QString &tag=QgsAuthManager::AUTH_MAN_TAG, Qgis::MessageLevel level=Qgis::MessageLevel::Info) const
Custom logging signal to relay to console output and QgsMessageLog.
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.
This class is a composition of two QSettings instances:
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
#define QgsDebugMsgLevel(str, level)
#define QgsDebugError(str)