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 (
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 Q_FOREACH ( QTreeWidgetItem *child, item->takeChildren() )
115 void QgsAuthIdentitiesEditor::populateIdentitiesView()
117 removeChildren_( mRootCertIdentItem );
119 populateIdentitiesSection( mRootCertIdentItem,
121 QgsAuthIdentitiesEditor::CertIdentity );
124 void QgsAuthIdentitiesEditor::refreshIdentitiesView()
126 populateIdentitiesView();
129 void QgsAuthIdentitiesEditor::populateIdentitiesSection( QTreeWidgetItem *item,
const QList<QSslCertificate> &certs,
130 QgsAuthIdentitiesEditor::IdentityType identype )
132 if ( btnGroupByOrg->isChecked() )
134 appendIdentitiesToGroup( certs, identype, item );
138 appendIdentitiesToItem( certs, identype, item );
142 void QgsAuthIdentitiesEditor::appendIdentitiesToGroup(
const QList<QSslCertificate> &certs,
143 QgsAuthIdentitiesEditor::IdentityType identype,
144 QTreeWidgetItem *parent )
151 parent = treeIdentities->currentItem();
155 QMap< QString, QList<QSslCertificate> > orgcerts(
158 QMap< QString, QList<QSslCertificate> >::const_iterator it = orgcerts.constBegin();
159 for ( ; it != orgcerts.constEnd(); ++it )
161 QTreeWidgetItem *grpitem(
new QTreeWidgetItem( parent,
162 QStringList() << it.key(),
163 ( int )QgsAuthIdentitiesEditor::OrgName ) );
164 grpitem->setFirstColumnSpanned(
true );
165 grpitem->setFlags( Qt::ItemIsEnabled );
166 grpitem->setExpanded(
true );
168 QBrush orgb( grpitem->foreground( 0 ) );
169 orgb.setColor( QColor::fromRgb( 90, 90, 90 ) );
170 grpitem->setForeground( 0, orgb );
171 QFont grpf( grpitem->font( 0 ) );
172 grpf.setItalic(
true );
173 grpitem->setFont( 0, grpf );
175 appendIdentitiesToItem( it.value(), identype, grpitem );
178 parent->sortChildren( 0, Qt::AscendingOrder );
181 void QgsAuthIdentitiesEditor::appendIdentitiesToItem(
const QList<QSslCertificate> &certs,
182 QgsAuthIdentitiesEditor::IdentityType identype,
183 QTreeWidgetItem *parent )
190 parent = treeIdentities->currentItem();
196 Q_FOREACH (
const QSslCertificate &cert, certs )
202 coltxts << QString( cert.serialNumber() );
203 coltxts << cert.expiryDate().toString();
205 QTreeWidgetItem *item(
new QTreeWidgetItem( parent, coltxts, (
int )identype ) );
210 item->setForeground( 2, redb );
214 item->setData( 0, Qt::UserRole,
id );
217 parent->sortChildren( 0, Qt::AscendingOrder );
220 void QgsAuthIdentitiesEditor::showCertInfo( QTreeWidgetItem *item )
225 QString digest( item->data( 0, Qt::UserRole ).toString() );
229 QgsDebugMsg(
"Certificate identity does not exist in database" );
236 dlg->setWindowModality( Qt::WindowModal );
237 dlg->resize( 675, 500 );
242 void QgsAuthIdentitiesEditor::selectionChanged(
const QItemSelection &selected,
const QItemSelection &deselected )
244 Q_UNUSED( selected );
245 Q_UNUSED( deselected );
249 void QgsAuthIdentitiesEditor::checkSelection()
252 if ( treeIdentities->selectionModel()->selection().length() > 0 )
254 QTreeWidgetItem *item( treeIdentities->currentItem() );
256 switch ( ( QgsAuthIdentitiesEditor::IdentityType )item->type() )
258 case QgsAuthIdentitiesEditor::CertIdentity:
266 btnRemoveIdentity->setEnabled( iscert );
267 btnInfoIdentity->setEnabled( iscert );
270 void QgsAuthIdentitiesEditor::handleDoubleClick( QTreeWidgetItem *item,
int col )
275 switch ( ( QgsAuthIdentitiesEditor::IdentityType )item->type() )
277 case QgsAuthIdentitiesEditor::Section:
280 case QgsAuthIdentitiesEditor::OrgName:
289 showCertInfo( item );
293 void QgsAuthIdentitiesEditor::btnAddIdentity_clicked()
296 dlg->setWindowModality( Qt::WindowModal );
297 dlg->resize( 400, dlg->height() );
305 messageBar()->
pushMessage( tr(
"ERROR storing identity bundle in authentication database." ),
308 populateIdentitiesView();
309 mRootCertIdentItem->setExpanded(
true );
315 void QgsAuthIdentitiesEditor::btnRemoveIdentity_clicked()
317 QTreeWidgetItem *item( treeIdentities->currentItem() );
325 QString digest( item->data( 0, Qt::UserRole ).toString() );
327 if ( digest.isEmpty() )
329 messageBar()->
pushMessage( tr(
"Certificate id missing." ),
336 QgsDebugMsg(
"Certificate identity does not exist in database" );
340 if ( QMessageBox::warning(
341 this, tr(
"Remove Certificate Identity" ),
342 tr(
"Are you sure you want to remove the selected " 343 "certificate identity from the database?\n\n" 344 "Operation can NOT be undone!" ),
345 QMessageBox::Ok | QMessageBox::Cancel,
346 QMessageBox::Cancel ) == QMessageBox::Cancel )
353 messageBar()->
pushMessage( tr(
"ERROR removing cert identity from authentication database for id %1:" ).arg( digest ),
358 item->parent()->removeChild( item );
362 void QgsAuthIdentitiesEditor::btnInfoIdentity_clicked()
364 if ( treeIdentities->selectionModel()->selection().length() > 0 )
366 QTreeWidgetItem *item( treeIdentities->currentItem() );
367 handleDoubleClick( item, 0 );
371 void QgsAuthIdentitiesEditor::btnGroupByOrg_toggled(
bool checked )
375 authMessageOut( QObject::tr(
"Could not store sort by preference." ),
376 QObject::tr(
"Authentication Identities" ),
379 populateIdentitiesView();
384 int levelint = ( int )level;
392 treeIdentities->setFocus();
394 QWidget::showEvent( e );
402 int QgsAuthIdentitiesEditor::messageTimeout()
405 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.