QGIS API Documentation 4.1.0-Master (376402f9aeb)
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"
28#include "qgsvariantutils.h"
29
30#include <QPushButton>
31#include <QString>
32
33#include "moc_qgsauthtrustedcasdialog.cpp"
34
35using namespace Qt::StringLiterals;
36
37QgsAuthTrustedCAsDialog::QgsAuthTrustedCAsDialog( QWidget *parent, const QList<QSslCertificate> &trustedCAs )
38 : QDialog( parent )
39 , mTrustedCAs( trustedCAs )
40{
41 if ( QgsApplication::authManager()->isDisabled() )
42 {
43 mDisabled = true;
44 mAuthNotifyLayout = new QVBoxLayout;
45 this->setLayout( mAuthNotifyLayout );
46 mAuthNotify = new QLabel( QgsApplication::authManager()->disabledMessage(), this );
47 mAuthNotifyLayout->addWidget( mAuthNotify );
48 }
49 else
50 {
51 setupUi( this );
52 connect( btnInfoCa, &QToolButton::clicked, this, &QgsAuthTrustedCAsDialog::btnInfoCa_clicked );
53 connect( btnGroupByOrg, &QToolButton::toggled, this, &QgsAuthTrustedCAsDialog::btnGroupByOrg_toggled );
54
55 connect( QgsApplication::authManager(), &QgsAuthManager::messageLog, this, &QgsAuthTrustedCAsDialog::authMessageLog );
56
57 setupCaCertsTree();
58
59 connect( treeTrustedCAs->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsAuthTrustedCAsDialog::selectionChanged );
60
61 connect( treeTrustedCAs, &QTreeWidget::itemDoubleClicked, this, &QgsAuthTrustedCAsDialog::handleDoubleClick );
62
63
64 btnGroupByOrg->setChecked( false );
65 const QVariant sortbyval = QgsApplication::authManager()->authSetting( u"trustedcasortby"_s, QVariant( false ) );
66 if ( !QgsVariantUtils::isNull( sortbyval ) )
67 btnGroupByOrg->setChecked( sortbyval.toBool() );
68
69 populateCaCertsView();
70 checkSelection();
71 }
72}
73
74void QgsAuthTrustedCAsDialog::setupCaCertsTree()
75{
76 treeTrustedCAs->setColumnCount( 3 );
77 treeTrustedCAs->setHeaderLabels( QStringList() << tr( "Common Name" ) << tr( "Serial #" ) << tr( "Expiry Date" ) );
78 treeTrustedCAs->setColumnWidth( 0, 300 );
79 treeTrustedCAs->setColumnWidth( 1, 75 );
80
81 // add root section
82 mRootCaSecItem = new QTreeWidgetItem( treeTrustedCAs, QStringList( tr( "Authorities/Issuers" ) ), static_cast<int>( QgsAuthTrustedCAsDialog::Section ) );
83 QgsAuthGuiUtils::setItemBold( mRootCaSecItem );
84 mRootCaSecItem->setFlags( Qt::ItemIsEnabled );
85 mRootCaSecItem->setExpanded( true );
86 treeTrustedCAs->insertTopLevelItem( 0, mRootCaSecItem );
87}
88
89void QgsAuthTrustedCAsDialog::populateCaCertsView()
90{
91 QgsAuthGuiUtils::removeChildren( mRootCaSecItem );
92
93 if ( mTrustedCAs.isEmpty() )
94 {
96 }
97
98 populateCaCertsSection( mRootCaSecItem, mTrustedCAs, QgsAuthTrustedCAsDialog::CaCert );
99}
100
101void QgsAuthTrustedCAsDialog::populateCaCertsSection( QTreeWidgetItem *item, const QList<QSslCertificate> &certs, QgsAuthTrustedCAsDialog::CaType catype )
102{
103 if ( btnGroupByOrg->isChecked() )
104 {
105 appendCertsToGroup( certs, catype, item );
106 }
107 else
108 {
109 appendCertsToItem( certs, catype, item );
110 }
111}
112
113void QgsAuthTrustedCAsDialog::appendCertsToGroup( const QList<QSslCertificate> &certs, QgsAuthTrustedCAsDialog::CaType catype, QTreeWidgetItem *parent )
114{
115 if ( certs.empty() )
116 return;
117
118 if ( !parent )
119 {
120 parent = treeTrustedCAs->currentItem();
121 }
122
123 // TODO: find all organizational name, sort and make subsections
124 const QMap<QString, QList<QSslCertificate>> orgcerts( QgsAuthCertUtils::certsGroupedByOrg( certs ) );
125
126 QMap<QString, QList<QSslCertificate>>::const_iterator it = orgcerts.constBegin();
127 for ( ; it != orgcerts.constEnd(); ++it )
128 {
129 QTreeWidgetItem *grpitem( new QTreeWidgetItem( parent, QStringList() << it.key(), static_cast<int>( QgsAuthTrustedCAsDialog::OrgName ) ) );
130 grpitem->setFirstColumnSpanned( true );
131 grpitem->setFlags( Qt::ItemIsEnabled );
132 grpitem->setExpanded( true );
133
134 QBrush orgb( grpitem->foreground( 0 ) );
135 orgb.setColor( QColor::fromRgb( 90, 90, 90 ) );
136 grpitem->setForeground( 0, orgb );
137 QFont grpf( grpitem->font( 0 ) );
138 grpf.setItalic( true );
139 grpitem->setFont( 0, grpf );
140
141 appendCertsToItem( it.value(), catype, grpitem );
142 }
143
144 parent->sortChildren( 0, Qt::AscendingOrder );
145}
146
147void QgsAuthTrustedCAsDialog::appendCertsToItem( const QList<QSslCertificate> &certs, QgsAuthTrustedCAsDialog::CaType catype, QTreeWidgetItem *parent )
148{
149 if ( certs.empty() )
150 return;
151
152 if ( !parent )
153 {
154 parent = treeTrustedCAs->currentItem();
155 }
156
157 const QBrush redb( QgsAuthGuiUtils::redColor() );
158
159 // Columns: Common Name, Serial #, Expiry Date
160 const auto constCerts = certs;
161 for ( const QSslCertificate &cert : constCerts )
162 {
163 const QString id( QgsAuthCertUtils::shaHexForCert( cert ) );
164
165 QStringList coltxts;
166 coltxts << QgsAuthCertUtils::resolvedCertName( cert );
167 coltxts << QString( cert.serialNumber() );
168 coltxts << cert.expiryDate().toString();
169
170 QTreeWidgetItem *item( new QTreeWidgetItem( parent, coltxts, static_cast<int>( catype ) ) );
171
172 item->setIcon( 0, QgsApplication::getThemeIcon( u"/mIconCertificate.svg"_s ) );
173 if ( !QgsAuthCertUtils::certIsViable( cert ) )
174 {
175 item->setForeground( 2, redb );
176 item->setIcon( 0, QgsApplication::getThemeIcon( u"/mIconCertificateUntrusted.svg"_s ) );
177 }
178
179 item->setData( 0, Qt::UserRole, id );
180 }
181
182 parent->sortChildren( 0, Qt::AscendingOrder );
183}
184
185void QgsAuthTrustedCAsDialog::showCertInfo( QTreeWidgetItem *item )
186{
187 if ( !item )
188 return;
189
190 const QString digest( item->data( 0, Qt::UserRole ).toString() );
191
192 const QMap<QString, QPair<QgsAuthCertUtils::CaCertSource, QSslCertificate>> cacertscache( QgsApplication::authManager()->caCertsCache() );
193
194 if ( !cacertscache.contains( digest ) )
195 {
196 QgsDebugError( u"Certificate Authority not in CA certs cache"_s );
197 return;
198 }
199
200 const QSslCertificate cert( cacertscache.value( digest ).second );
201
202 QgsAuthCertInfoDialog *dlg = new QgsAuthCertInfoDialog( cert, false, this );
203 dlg->setWindowModality( Qt::WindowModal );
204 dlg->resize( 675, 500 );
205 dlg->exec();
206 dlg->deleteLater();
207}
208
209void QgsAuthTrustedCAsDialog::selectionChanged( const QItemSelection &selected, const QItemSelection &deselected )
210{
211 Q_UNUSED( selected )
212 Q_UNUSED( deselected )
213 checkSelection();
214}
215
216void QgsAuthTrustedCAsDialog::checkSelection()
217{
218 bool iscert = false;
219 if ( treeTrustedCAs->selectionModel()->selection().length() > 0 )
220 {
221 QTreeWidgetItem *item( treeTrustedCAs->currentItem() );
222
223 switch ( ( QgsAuthTrustedCAsDialog::CaType ) item->type() )
224 {
225 case QgsAuthTrustedCAsDialog::CaCert:
226 iscert = true;
227 break;
228 default:
229 break;
230 }
231 }
232
233 btnInfoCa->setEnabled( iscert );
234}
235
236void QgsAuthTrustedCAsDialog::handleDoubleClick( QTreeWidgetItem *item, int col )
237{
238 Q_UNUSED( col )
239 bool iscert = true;
240
241 switch ( ( QgsAuthTrustedCAsDialog::CaType ) item->type() )
242 {
243 case QgsAuthTrustedCAsDialog::Section:
244 iscert = false;
245 break;
246 case QgsAuthTrustedCAsDialog::OrgName:
247 iscert = false;
248 break;
249 default:
250 break;
251 }
252
253 if ( iscert )
254 {
255 showCertInfo( item );
256 }
257}
258
259void QgsAuthTrustedCAsDialog::btnInfoCa_clicked()
260{
261 if ( treeTrustedCAs->selectionModel()->selection().length() > 0 )
262 {
263 QTreeWidgetItem *item( treeTrustedCAs->currentItem() );
264 handleDoubleClick( item, 0 );
265 }
266}
267
268void QgsAuthTrustedCAsDialog::btnGroupByOrg_toggled( bool checked )
269{
270 if ( !QgsApplication::authManager()->storeAuthSetting( u"trustedcasortby"_s, QVariant( checked ) ) )
271 {
272 authMessageLog( QObject::tr( "Could not store sort by preference" ), QObject::tr( "Trusted Authorities/Issuers" ), Qgis::MessageLevel::Warning );
273 }
274 populateCaCertsView();
275}
276
277void QgsAuthTrustedCAsDialog::authMessageLog( const QString &message, const QString &authtag, Qgis::MessageLevel level )
278{
279 messageBar()->pushMessage( authtag, message, level, 7 );
280}
281
283{
284 if ( !mDisabled )
285 {
286 treeTrustedCAs->setFocus();
287 }
288 QDialog::showEvent( e );
289}
290
291QgsMessageBar *QgsAuthTrustedCAsDialog::messageBar()
292{
293 return msgBar;
294}
295
296int QgsAuthTrustedCAsDialog::messageTimeout()
297{
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.
T value(const QString &dynamicKeyPart=QString()) const
Returns settings value.
static const QgsSettingsEntryInteger * settingsMessageTimeout
Settings entry message timeout in seconds.
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