QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
Loading...
Searching...
No Matches
qgsauthtrustedcasdialog.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsauthtrustedcasdialog.cpp
3 ---------------------
4 begin : May 9, 2015
5 copyright : (C) 2015 by Boundless Spatial, Inc. USA
6 author : Larry Shaffer
7 email : lshaffer at boundlessgeo dot com
8 ***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16
17#include "ui_qgsauthtrustedcasdialog.h"
19
20#include "qgsapplication.h"
22#include "qgsauthcertutils.h"
23#include "qgsauthguiutils.h"
24#include "qgsauthmanager.h"
25#include "qgslogger.h"
26#include "qgssettings.h"
27#include "qgsvariantutils.h"
28
29#include <QPushButton>
30#include <QString>
31
32#include "moc_qgsauthtrustedcasdialog.cpp"
33
34using namespace Qt::StringLiterals;
35
36QgsAuthTrustedCAsDialog::QgsAuthTrustedCAsDialog( QWidget *parent, const QList<QSslCertificate> &trustedCAs )
37 : QDialog( parent )
38 , mTrustedCAs( trustedCAs )
39{
40 if ( QgsApplication::authManager()->isDisabled() )
41 {
42 mDisabled = true;
43 mAuthNotifyLayout = new QVBoxLayout;
44 this->setLayout( mAuthNotifyLayout );
45 mAuthNotify = new QLabel( QgsApplication::authManager()->disabledMessage(), this );
46 mAuthNotifyLayout->addWidget( mAuthNotify );
47 }
48 else
49 {
50 setupUi( this );
51 connect( btnInfoCa, &QToolButton::clicked, this, &QgsAuthTrustedCAsDialog::btnInfoCa_clicked );
52 connect( btnGroupByOrg, &QToolButton::toggled, this, &QgsAuthTrustedCAsDialog::btnGroupByOrg_toggled );
53
54 connect( QgsApplication::authManager(), &QgsAuthManager::messageLog, this, &QgsAuthTrustedCAsDialog::authMessageLog );
55
56 setupCaCertsTree();
57
58 connect( treeTrustedCAs->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsAuthTrustedCAsDialog::selectionChanged );
59
60 connect( treeTrustedCAs, &QTreeWidget::itemDoubleClicked, this, &QgsAuthTrustedCAsDialog::handleDoubleClick );
61
62
63 btnGroupByOrg->setChecked( false );
64 const QVariant sortbyval = QgsApplication::authManager()->authSetting( u"trustedcasortby"_s, QVariant( false ) );
65 if ( !QgsVariantUtils::isNull( sortbyval ) )
66 btnGroupByOrg->setChecked( sortbyval.toBool() );
67
68 populateCaCertsView();
69 checkSelection();
70 }
71}
72
73void QgsAuthTrustedCAsDialog::setupCaCertsTree()
74{
75 treeTrustedCAs->setColumnCount( 3 );
76 treeTrustedCAs->setHeaderLabels( QStringList() << tr( "Common Name" ) << tr( "Serial #" ) << tr( "Expiry Date" ) );
77 treeTrustedCAs->setColumnWidth( 0, 300 );
78 treeTrustedCAs->setColumnWidth( 1, 75 );
79
80 // add root section
81 mRootCaSecItem = new QTreeWidgetItem( treeTrustedCAs, QStringList( tr( "Authorities/Issuers" ) ), static_cast<int>( QgsAuthTrustedCAsDialog::Section ) );
82 QgsAuthGuiUtils::setItemBold( mRootCaSecItem );
83 mRootCaSecItem->setFlags( Qt::ItemIsEnabled );
84 mRootCaSecItem->setExpanded( true );
85 treeTrustedCAs->insertTopLevelItem( 0, mRootCaSecItem );
86}
87
88void QgsAuthTrustedCAsDialog::populateCaCertsView()
89{
90 QgsAuthGuiUtils::removeChildren( mRootCaSecItem );
91
92 if ( mTrustedCAs.isEmpty() )
93 {
95 }
96
97 populateCaCertsSection( mRootCaSecItem, mTrustedCAs, QgsAuthTrustedCAsDialog::CaCert );
98}
99
100void QgsAuthTrustedCAsDialog::populateCaCertsSection( QTreeWidgetItem *item, const QList<QSslCertificate> &certs, QgsAuthTrustedCAsDialog::CaType catype )
101{
102 if ( btnGroupByOrg->isChecked() )
103 {
104 appendCertsToGroup( certs, catype, item );
105 }
106 else
107 {
108 appendCertsToItem( certs, catype, item );
109 }
110}
111
112void QgsAuthTrustedCAsDialog::appendCertsToGroup( const QList<QSslCertificate> &certs, QgsAuthTrustedCAsDialog::CaType catype, QTreeWidgetItem *parent )
113{
114 if ( certs.empty() )
115 return;
116
117 if ( !parent )
118 {
119 parent = treeTrustedCAs->currentItem();
120 }
121
122 // TODO: find all organizational name, sort and make subsections
123 const QMap<QString, QList<QSslCertificate>> orgcerts( QgsAuthCertUtils::certsGroupedByOrg( certs ) );
124
125 QMap<QString, QList<QSslCertificate>>::const_iterator it = orgcerts.constBegin();
126 for ( ; it != orgcerts.constEnd(); ++it )
127 {
128 QTreeWidgetItem *grpitem( new QTreeWidgetItem( parent, QStringList() << it.key(), static_cast<int>( QgsAuthTrustedCAsDialog::OrgName ) ) );
129 grpitem->setFirstColumnSpanned( true );
130 grpitem->setFlags( Qt::ItemIsEnabled );
131 grpitem->setExpanded( true );
132
133 QBrush orgb( grpitem->foreground( 0 ) );
134 orgb.setColor( QColor::fromRgb( 90, 90, 90 ) );
135 grpitem->setForeground( 0, orgb );
136 QFont grpf( grpitem->font( 0 ) );
137 grpf.setItalic( true );
138 grpitem->setFont( 0, grpf );
139
140 appendCertsToItem( it.value(), catype, grpitem );
141 }
142
143 parent->sortChildren( 0, Qt::AscendingOrder );
144}
145
146void QgsAuthTrustedCAsDialog::appendCertsToItem( const QList<QSslCertificate> &certs, QgsAuthTrustedCAsDialog::CaType catype, QTreeWidgetItem *parent )
147{
148 if ( certs.empty() )
149 return;
150
151 if ( !parent )
152 {
153 parent = treeTrustedCAs->currentItem();
154 }
155
156 const QBrush redb( QgsAuthGuiUtils::redColor() );
157
158 // Columns: Common Name, Serial #, Expiry Date
159 const auto constCerts = certs;
160 for ( const QSslCertificate &cert : constCerts )
161 {
162 const QString id( QgsAuthCertUtils::shaHexForCert( cert ) );
163
164 QStringList coltxts;
165 coltxts << QgsAuthCertUtils::resolvedCertName( cert );
166 coltxts << QString( cert.serialNumber() );
167 coltxts << cert.expiryDate().toString();
168
169 QTreeWidgetItem *item( new QTreeWidgetItem( parent, coltxts, static_cast<int>( catype ) ) );
170
171 item->setIcon( 0, QgsApplication::getThemeIcon( u"/mIconCertificate.svg"_s ) );
172 if ( !QgsAuthCertUtils::certIsViable( cert ) )
173 {
174 item->setForeground( 2, redb );
175 item->setIcon( 0, QgsApplication::getThemeIcon( u"/mIconCertificateUntrusted.svg"_s ) );
176 }
177
178 item->setData( 0, Qt::UserRole, id );
179 }
180
181 parent->sortChildren( 0, Qt::AscendingOrder );
182}
183
184void QgsAuthTrustedCAsDialog::showCertInfo( QTreeWidgetItem *item )
185{
186 if ( !item )
187 return;
188
189 const QString digest( item->data( 0, Qt::UserRole ).toString() );
190
191 const QMap<QString, QPair<QgsAuthCertUtils::CaCertSource, QSslCertificate>> cacertscache( QgsApplication::authManager()->caCertsCache() );
192
193 if ( !cacertscache.contains( digest ) )
194 {
195 QgsDebugError( u"Certificate Authority not in CA certs cache"_s );
196 return;
197 }
198
199 const QSslCertificate cert( cacertscache.value( digest ).second );
200
201 QgsAuthCertInfoDialog *dlg = new QgsAuthCertInfoDialog( cert, false, this );
202 dlg->setWindowModality( Qt::WindowModal );
203 dlg->resize( 675, 500 );
204 dlg->exec();
205 dlg->deleteLater();
206}
207
208void QgsAuthTrustedCAsDialog::selectionChanged( const QItemSelection &selected, const QItemSelection &deselected )
209{
210 Q_UNUSED( selected )
211 Q_UNUSED( deselected )
212 checkSelection();
213}
214
215void QgsAuthTrustedCAsDialog::checkSelection()
216{
217 bool iscert = false;
218 if ( treeTrustedCAs->selectionModel()->selection().length() > 0 )
219 {
220 QTreeWidgetItem *item( treeTrustedCAs->currentItem() );
221
222 switch ( ( QgsAuthTrustedCAsDialog::CaType ) item->type() )
223 {
224 case QgsAuthTrustedCAsDialog::CaCert:
225 iscert = true;
226 break;
227 default:
228 break;
229 }
230 }
231
232 btnInfoCa->setEnabled( iscert );
233}
234
235void QgsAuthTrustedCAsDialog::handleDoubleClick( QTreeWidgetItem *item, int col )
236{
237 Q_UNUSED( col )
238 bool iscert = true;
239
240 switch ( ( QgsAuthTrustedCAsDialog::CaType ) item->type() )
241 {
242 case QgsAuthTrustedCAsDialog::Section:
243 iscert = false;
244 break;
245 case QgsAuthTrustedCAsDialog::OrgName:
246 iscert = false;
247 break;
248 default:
249 break;
250 }
251
252 if ( iscert )
253 {
254 showCertInfo( item );
255 }
256}
257
258void QgsAuthTrustedCAsDialog::btnInfoCa_clicked()
259{
260 if ( treeTrustedCAs->selectionModel()->selection().length() > 0 )
261 {
262 QTreeWidgetItem *item( treeTrustedCAs->currentItem() );
263 handleDoubleClick( item, 0 );
264 }
265}
266
267void QgsAuthTrustedCAsDialog::btnGroupByOrg_toggled( bool checked )
268{
269 if ( !QgsApplication::authManager()->storeAuthSetting( u"trustedcasortby"_s, QVariant( checked ) ) )
270 {
271 authMessageLog( QObject::tr( "Could not store sort by preference" ), QObject::tr( "Trusted Authorities/Issuers" ), Qgis::MessageLevel::Warning );
272 }
273 populateCaCertsView();
274}
275
276void QgsAuthTrustedCAsDialog::authMessageLog( const QString &message, const QString &authtag, Qgis::MessageLevel level )
277{
278 messageBar()->pushMessage( authtag, message, level, 7 );
279}
280
282{
283 if ( !mDisabled )
284 {
285 treeTrustedCAs->setFocus();
286 }
287 QDialog::showEvent( e );
288}
289
290QgsMessageBar *QgsAuthTrustedCAsDialog::messageBar()
291{
292 return msgBar;
293}
294
295int QgsAuthTrustedCAsDialog::messageTimeout()
296{
297 const QgsSettings settings;
298 return settings.value( u"qgis/messageTimeout"_s, 5 ).toInt();
299}
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition qgis.h:160
@ Warning
Warning message.
Definition qgis.h:162
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 void setItemBold(QTreeWidgetItem *item)
Call setFirstColumnSpanned(true) on the item and make its font bold.
static void removeChildren(QTreeWidgetItem *item)
Remove the children of the passed item.
static QColor redColor()
Red color representing invalid, untrusted, etc. certificate.
QVariant authSetting(const QString &key, const QVariant &defaultValue=QVariant(), bool decrypt=false)
Returns a previously set authentication setting.
const QList< QSslCertificate > trustedCaCerts(bool includeinvalid=false)
trustedCaCerts get list of all trusted CA certificates
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.
void showEvent(QShowEvent *e) override
QgsAuthTrustedCAsDialog(QWidget *parent=nullptr, const QList< QSslCertificate > &trustedCAs=QList< QSslCertificate >())
Construct a dialog that will list the trusted Certificate Authorities.
A bar for displaying non-blocking messages to the user.
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 QgsDebugError(str)
Definition qgslogger.h:59