18 #include "ui_qgsauthidentitieseditor.h" 21 #include <QMessageBox> 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::authMessageOut );
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 );
70 if ( !sortbyval.isNull() )
71 btnGroupByOrg->setChecked( sortbyval.toBool() );
73 populateIdentitiesView();
78 static void setItemBold_( QTreeWidgetItem *item )
80 item->setFirstColumnSpanned(
true );
81 QFont secf( item->font( 0 ) );
83 item->setFont( 0, secf );
86 void 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 );
107 static void removeChildren_( QTreeWidgetItem *item )
109 const auto constTakeChildren = item->takeChildren();
110 for ( QTreeWidgetItem *child : constTakeChildren )
116 void QgsAuthIdentitiesEditor::populateIdentitiesView()
118 removeChildren_( mRootCertIdentItem );
120 populateIdentitiesSection( mRootCertIdentItem,
122 QgsAuthIdentitiesEditor::CertIdentity );
125 void QgsAuthIdentitiesEditor::refreshIdentitiesView()
127 populateIdentitiesView();
130 void 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 );
143 void QgsAuthIdentitiesEditor::appendIdentitiesToGroup(
const QList<QSslCertificate> &certs,
144 QgsAuthIdentitiesEditor::IdentityType identype,
145 QTreeWidgetItem *parent )
152 parent = treeIdentities->currentItem();
156 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 );
182 void 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 );
222 void QgsAuthIdentitiesEditor::showCertInfo( QTreeWidgetItem *item )
227 QString digest( item->data( 0, Qt::UserRole ).toString() );
231 QgsDebugMsg( QStringLiteral(
"Certificate identity does not exist in database" ) );
238 dlg->setWindowModality( Qt::WindowModal );
239 dlg->resize( 675, 500 );
244 void QgsAuthIdentitiesEditor::selectionChanged(
const QItemSelection &selected,
const QItemSelection &deselected )
247 Q_UNUSED( deselected )
251 void 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 );
272 void QgsAuthIdentitiesEditor::handleDoubleClick( QTreeWidgetItem *item,
int col )
277 switch ( ( QgsAuthIdentitiesEditor::IdentityType )item->type() )
279 case QgsAuthIdentitiesEditor::Section:
282 case QgsAuthIdentitiesEditor::OrgName:
291 showCertInfo( item );
295 void 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 );
317 void QgsAuthIdentitiesEditor::btnRemoveIdentity_clicked()
319 QTreeWidgetItem *item( treeIdentities->currentItem() );
323 QgsDebugMsg( QStringLiteral(
"Current tree widget item not set" ) );
327 QString digest( item->data( 0, Qt::UserRole ).toString() );
329 if ( digest.isEmpty() )
331 messageBar()->
pushMessage( tr(
"Certificate id missing." ),
338 QgsDebugMsg( 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 );
364 void QgsAuthIdentitiesEditor::btnInfoIdentity_clicked()
366 if ( treeIdentities->selectionModel()->selection().length() > 0 )
368 QTreeWidgetItem *item( treeIdentities->currentItem() );
369 handleDoubleClick( item, 0 );
373 void QgsAuthIdentitiesEditor::btnGroupByOrg_toggled(
bool checked )
377 authMessageOut( QObject::tr(
"Could not store sort by preference." ),
378 QObject::tr(
"Authentication Identities" ),
381 populateIdentitiesView();
386 int levelint =
static_cast<int>( level );
394 treeIdentities->setFocus();
396 QWidget::showEvent( e );
404 int QgsAuthIdentitiesEditor::messageTimeout()
407 return settings.
value( QStringLiteral(
"qgis/messageTimeout" ), 5 ).toInt();
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.
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.
A bar for displaying non-blocking messages to the user.
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
Widget for importing an identity certificate/key bundle into the authentication database.
QgsAuthImportIdentityDialog::IdentityType identityType()
Gets identity type.
MessageLevel
Level for messages This will be used both for message log and message bar in application.
MessageLevel
Message log level (mirrors that of QgsMessageLog, so it can also output there)
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.
Dialog wrapper for widget displaying detailed info on a certificate and its hierarchical trust chain...
void showEvent(QShowEvent *e) override
Overridden show event of base widget.
bool storeCertIdentity(const QSslCertificate &cert, const QSslKey &key)
Store a certificate identity.
void authDatabaseChanged()
Emitted when the authentication db is significantly changed, e.g. large record removal, erased, etc.
void pushMessage(const QString &text, Qgis::MessageLevel level=Qgis::Info, int duration=5)
convenience method for pushing a message to the bar
static QString shaHexForCert(const QSslCertificate &cert, bool formatted=false)
Gets the sha1 hash for certificate.
static QgsAuthManager * authManager()
Returns the application's authentication manager instance.
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 )) ...
static QMap< QString, QList< QSslCertificate > > certsGroupedByOrg(const QList< QSslCertificate > &certs)
Map certificates to their oraganization.
bool existsCertIdentity(const QString &id)
Check if a certificate identity exists.
const QPair< QSslCertificate, QSslKey > certBundleToImport()
Gets certificate/key bundle to be imported.
static QString resolvedCertName(const QSslCertificate &cert, bool issuer=false)
Gets the general name via RFC 5280 resolution.
QgsAuthIdentitiesEditor(QWidget *parent=nullptr)
Widget for editing authentication configurations directly in database.