Skip to content

Commit

Permalink
Implement import/export UI
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn committed May 12, 2021
1 parent f916c06 commit 1bbd477
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 10 deletions.
5 changes: 5 additions & 0 deletions python/gui/auto_generated/auth/qgsauthconfigeditor.sip.in
Expand Up @@ -33,6 +33,11 @@ Widget for editing authentication configurations directly in database
void toggleTitleVisibility( bool visible );
%Docstring
Hide the widget's title, e.g. when embedding
%End

QStringList selectedAuthenticationConfigIds() const;
%Docstring
Returns the list of selected authentication configuration IDs
%End

public slots:
Expand Down
48 changes: 39 additions & 9 deletions src/gui/auth/qgsauthconfigeditor.cpp
Expand Up @@ -90,13 +90,17 @@ QgsAuthConfigEditor::QgsAuthConfigEditor( QWidget *parent, bool showUtilities, b
checkSelection();

// set up utility actions menu
mActionImportAuthenticationConfigs = new QAction( tr( "Import authentication configurations from file" ), this );
mActionExportSelectedAuthenticationConfigs = new QAction( tr( "Export selected authentication configurations to file" ), this );
mActionSetMasterPassword = new QAction( QStringLiteral( "Input master password" ), this );
mActionClearCachedMasterPassword = new QAction( QStringLiteral( "Clear cached master password" ), this );
mActionResetMasterPassword = new QAction( QStringLiteral( "Reset master password" ), this );
mActionClearCachedAuthConfigs = new QAction( QStringLiteral( "Clear cached authentication configurations" ), this );
mActionRemoveAuthConfigs = new QAction( QStringLiteral( "Remove all authentication configurations" ), this );
mActionEraseAuthDatabase = new QAction( QStringLiteral( "Erase authentication database" ), this );

connect( mActionImportAuthenticationConfigs, &QAction::triggered, this, &QgsAuthConfigEditor::importAuthenticationConfigs );
connect( mActionExportSelectedAuthenticationConfigs, &QAction::triggered, this, &QgsAuthConfigEditor::exportSelectedAuthenticationConfigs );
connect( mActionSetMasterPassword, &QAction::triggered, this, &QgsAuthConfigEditor::setMasterPassword );
connect( mActionClearCachedMasterPassword, &QAction::triggered, this, &QgsAuthConfigEditor::clearCachedMasterPassword );
connect( mActionResetMasterPassword, &QAction::triggered, this, &QgsAuthConfigEditor::resetMasterPassword );
Expand All @@ -112,13 +116,26 @@ QgsAuthConfigEditor::QgsAuthConfigEditor( QWidget *parent, bool showUtilities, b
mAuthUtilitiesMenu->addAction( mActionClearCachedAuthConfigs );
mAuthUtilitiesMenu->addAction( mActionRemoveAuthConfigs );
mAuthUtilitiesMenu->addSeparator();
mAuthUtilitiesMenu->addAction( mActionImportAuthenticationConfigs );
mAuthUtilitiesMenu->addAction( mActionExportSelectedAuthenticationConfigs );
mAuthUtilitiesMenu->addSeparator();
mAuthUtilitiesMenu->addAction( mActionEraseAuthDatabase );

btnAuthUtilities->setMenu( mAuthUtilitiesMenu );
lblAuthConfigDb->setVisible( false );
}
}

void QgsAuthConfigEditor::importAuthenticationConfigs()
{
QgsAuthGuiUtils::importAuthenticationConfigs( messageBar() );
}

void QgsAuthConfigEditor::exportSelectedAuthenticationConfigs()
{
QgsAuthGuiUtils::exportSelectedAuthenticationConfigs( selectedAuthenticationConfigIds(), messageBar() );
}

void QgsAuthConfigEditor::setMasterPassword()
{
QgsAuthGuiUtils::setMasterPassword( messageBar() );
Expand Down Expand Up @@ -163,6 +180,17 @@ void QgsAuthConfigEditor::toggleTitleVisibility( bool visible )
}
}

QStringList QgsAuthConfigEditor::selectedAuthenticationConfigIds() const
{
QStringList ids;
QModelIndexList selection = tableViewConfigs->selectionModel()->selectedRows( 0 );
for ( QModelIndex index : selection )
{
ids << index.sibling( index.row(), 0 ).data().toString();
}
return ids;
}

void QgsAuthConfigEditor::setShowUtilitiesButton( bool show )
{
if ( !mDisabled )
Expand Down Expand Up @@ -255,16 +283,18 @@ void QgsAuthConfigEditor::btnRemoveConfig_clicked()
if ( selection.empty() )
return;

QModelIndex indx = selection.at( 0 );
QString name = indx.sibling( indx.row(), 1 ).data().toString();

if ( QMessageBox::warning( this, tr( "Remove Configuration" ),
tr( "Are you sure you want to remove '%1'?\n\n"
"Operation can NOT be undone!" ).arg( name ),
QMessageBox::Ok | QMessageBox::Cancel,
QMessageBox::Cancel ) == QMessageBox::Ok )
for ( QModelIndex index : selection )
{
mConfigModel->removeRow( indx.row() );
QString name = index.sibling( index.row(), 1 ).data().toString();

if ( QMessageBox::warning( this, tr( "Remove Configuration" ),
tr( "Are you sure you want to remove '%1'?\n\n"
"Operation can NOT be undone!" ).arg( name ),
QMessageBox::Ok | QMessageBox::Cancel,
QMessageBox::Cancel ) == QMessageBox::Ok )
{
mConfigModel->removeRow( index.row() );
}
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/gui/auth/qgsauthconfigeditor.h
Expand Up @@ -48,6 +48,9 @@ class GUI_EXPORT QgsAuthConfigEditor : public QWidget, private Ui::QgsAuthConfig
//! Hide the widget's title, e.g. when embedding
void toggleTitleVisibility( bool visible );

//! Returns the list of selected authentication configuration IDs
QStringList selectedAuthenticationConfigIds() const;

public slots:
//! Sets whether to show the widget's utilities button, e.g. when embedding
void setShowUtilitiesButton( bool show = true );
Expand All @@ -59,6 +62,12 @@ class GUI_EXPORT QgsAuthConfigEditor : public QWidget, private Ui::QgsAuthConfig
//! Repopulate the view with table contents
void refreshTableView();

//! Import authentication configurations from a XML file
void importAuthenticationConfigs();

//! Exports selected authentication configurations to a XML file
void exportSelectedAuthenticationConfigs();

//! Sets the cached master password (and verifies it if its hash is in authentication database)
void setMasterPassword();

Expand Down Expand Up @@ -100,6 +109,8 @@ class GUI_EXPORT QgsAuthConfigEditor : public QWidget, private Ui::QgsAuthConfig
QSqlTableModel *mConfigModel = nullptr;

QMenu *mAuthUtilitiesMenu = nullptr;
QAction *mActionImportAuthenticationConfigs = nullptr;
QAction *mActionExportSelectedAuthenticationConfigs = nullptr;
QAction *mActionSetMasterPassword = nullptr;
QAction *mActionClearCachedMasterPassword = nullptr;
QAction *mActionResetMasterPassword = nullptr;
Expand Down
20 changes: 20 additions & 0 deletions src/gui/auth/qgsautheditorwidgets.cpp
Expand Up @@ -142,6 +142,8 @@ void QgsAuthEditorWidgets::setupUtilitiesMenu()
this, &QgsAuthEditorWidgets::authMessageOut );

// set up utility actions menu
mActionImportAuthenticationConfigs = new QAction( tr( "Import authentication configurations from file" ), this );
mActionExportSelectedAuthenticationConfigs = new QAction( tr( "Export selected authentication configurations to file" ), this );
mActionSetMasterPassword = new QAction( tr( "Input master password" ), this );
mActionClearCachedMasterPassword = new QAction( tr( "Clear cached master password" ), this );
mActionResetMasterPassword = new QAction( tr( "Reset master password" ), this );
Expand All @@ -168,6 +170,8 @@ void QgsAuthEditorWidgets::setupUtilitiesMenu()
mActionPasswordHelperLoggingEnable->setCheckable( true );
mActionPasswordHelperLoggingEnable->setChecked( QgsApplication::authManager()->passwordHelperLoggingEnabled() );

connect( mActionImportAuthenticationConfigs, &QAction::triggered, this, &QgsAuthEditorWidgets::importAuthenticationConfigs );
connect( mActionExportSelectedAuthenticationConfigs, &QAction::triggered, this, &QgsAuthEditorWidgets::exportSelectedAuthenticationConfigs );
connect( mActionSetMasterPassword, &QAction::triggered, this, &QgsAuthEditorWidgets::setMasterPassword );
connect( mActionClearCachedMasterPassword, &QAction::triggered, this, &QgsAuthEditorWidgets::clearCachedMasterPassword );
connect( mActionResetMasterPassword, &QAction::triggered, this, &QgsAuthEditorWidgets::resetMasterPassword );
Expand Down Expand Up @@ -206,11 +210,27 @@ void QgsAuthEditorWidgets::setupUtilitiesMenu()
mAuthUtilitiesMenu->addAction( mActionClearCachedAuthConfigs );
mAuthUtilitiesMenu->addAction( mActionRemoveAuthConfigs );
mAuthUtilitiesMenu->addSeparator();
mAuthUtilitiesMenu->addAction( mActionImportAuthenticationConfigs );
mAuthUtilitiesMenu->addAction( mActionExportSelectedAuthenticationConfigs );
mAuthUtilitiesMenu->addSeparator();
mAuthUtilitiesMenu->addAction( mActionEraseAuthDatabase );

btnAuthUtilities->setMenu( mAuthUtilitiesMenu );
}

void QgsAuthEditorWidgets::importAuthenticationConfigs()
{
QgsAuthGuiUtils::importAuthenticationConfigs( messageBar() );
}

void QgsAuthEditorWidgets::exportSelectedAuthenticationConfigs()
{
if ( !wdgtConfigEditor )
return;

QgsAuthGuiUtils::exportSelectedAuthenticationConfigs( wdgtConfigEditor->selectedAuthenticationConfigIds(), messageBar() );
}

void QgsAuthEditorWidgets::setMasterPassword()
{
QgsAuthGuiUtils::setMasterPassword( messageBar() );
Expand Down
9 changes: 9 additions & 0 deletions src/gui/auth/qgsautheditorwidgets.h
Expand Up @@ -70,6 +70,12 @@ class GUI_EXPORT QgsAuthEditorWidgets : public QWidget, private Ui::QgsAuthEdito
void btnCertManager_clicked();
void btnAuthPlugins_clicked();

//! Import authentication configurations from a XML file
void importAuthenticationConfigs();

//! Exports selected authentication configurations to a XML file
void exportSelectedAuthenticationConfigs();

//! Sets the cached master password (and verifies it if its hash is in authentication database)
void setMasterPassword();

Expand Down Expand Up @@ -109,6 +115,8 @@ class GUI_EXPORT QgsAuthEditorWidgets : public QWidget, private Ui::QgsAuthEdito
QgsMessageBar *messageBar();

QMenu *mAuthUtilitiesMenu = nullptr;
QAction *mActionExportSelectedAuthenticationConfigs = nullptr;
QAction *mActionImportAuthenticationConfigs = nullptr;
QAction *mActionSetMasterPassword = nullptr;
QAction *mActionClearCachedMasterPassword = nullptr;
QAction *mActionResetMasterPassword = nullptr;
Expand All @@ -121,6 +129,7 @@ class GUI_EXPORT QgsAuthEditorWidgets : public QWidget, private Ui::QgsAuthEdito
QAction *mActionPasswordHelperLoggingEnable = nullptr;
QAction *mActionClearAccessCacheNow = nullptr;
QAction *mActionAutoClearAccessCache = nullptr;

};

#endif // QGSAUTHEDITORWIDGETS_H
51 changes: 51 additions & 0 deletions src/gui/auth/qgsauthguiutils.cpp
Expand Up @@ -17,6 +17,7 @@
#include "qgsauthguiutils.h"

#include <QFileDialog>
#include <QInputDialog>
#include <QLineEdit>
#include <QMessageBox>

Expand Down Expand Up @@ -75,6 +76,56 @@ bool QgsAuthGuiUtils::isDisabled( QgsMessageBar *msgbar )
return false;
}

void QgsAuthGuiUtils::exportSelectedAuthenticationConfigs( QStringList authenticationConfigIds, QgsMessageBar *msgbar )
{
QString password = QInputDialog::getText( msgbar, QObject::tr( "Export Authentication Configurations" ),
QObject::tr( "Enter a password encrypt the configuration file:" ), QLineEdit::Password );
if ( password.isEmpty() )
{
if ( QMessageBox::warning( msgbar,
QObject::tr( "Export Authentication Configurations" ),
QObject::tr( "Exporting authentication configurations with a blank password will result in a plain text file which may contain sensitive information. Are you sure you want to do this?" ),
QMessageBox::Ok | QMessageBox::Cancel,
QMessageBox::Cancel ) == QMessageBox::Cancel )
{
return;
}
}

QString fileName = QFileDialog::getSaveFileName( msgbar, QObject::tr( "Export Authentication Configurations" ), QDir::homePath(),
QObject::tr( "XML files (*.xml *.XML)" ) );
if ( fileName.isEmpty() )
return;

bool ok = QgsApplication::authManager()->exportAuthenticationConfigsToXml( fileName, authenticationConfigIds, password );
if ( !ok )
{
msgbar->pushMessage( QgsApplication::authManager()->authManTag(),
QObject::tr( "Export of authentication configurations failed." ),
Qgis::Critical );
}
}

void QgsAuthGuiUtils::importAuthenticationConfigs( QgsMessageBar *msgbar )
{

QString fileName = QFileDialog::getOpenFileName( msgbar, QObject::tr( "Export Authentication Configurations" ), QDir::homePath(),
QObject::tr( "XML files (*.xml *.XML)" ) );
if ( fileName.isEmpty() )
return;

QString password = QInputDialog::getText( msgbar, QObject::tr( "Import Authentication Configurations" ),
QObject::tr( "Enter the password to decrypt the configurations file:" ), QLineEdit::Password );

bool ok = QgsApplication::authManager()->importAuthenticationConfigsFromXml( fileName, password );
if ( !ok )
{
msgbar->pushMessage( QgsApplication::authManager()->authManTag(),
QObject::tr( "Import of authentication configurations failed." ),
Qgis::Critical );
}
}

void QgsAuthGuiUtils::setMasterPassword( QgsMessageBar *msgbar )
{
if ( QgsAuthGuiUtils::isDisabled( msgbar ) )
Expand Down
6 changes: 6 additions & 0 deletions src/gui/auth/qgsauthguiutils.h
Expand Up @@ -60,6 +60,12 @@ class GUI_EXPORT QgsAuthGuiUtils
//! Verify the authentication system is active, else notify user
static bool isDisabled( QgsMessageBar *msgbar );

//! Import authentication configurations from a XML file
static void importAuthenticationConfigs( QgsMessageBar *msgbar );

//! Exports selected authentication configurations to a XML file
static void exportSelectedAuthenticationConfigs( QStringList authenticationConfigIds, QgsMessageBar *msgbar );

//! Sets the cached master password (and verifies it if its hash is in authentication database)
static void setMasterPassword( QgsMessageBar *msgbar );

Expand Down
2 changes: 1 addition & 1 deletion src/ui/auth/qgsauthconfigeditor.ui
Expand Up @@ -128,7 +128,7 @@
<bool>true</bool>
</property>
<property name="selectionMode">
<enum>QAbstractItemView::SingleSelection</enum>
<enum>QAbstractItemView::ExtendedSelection</enum>
</property>
<property name="selectionBehavior">
<enum>QAbstractItemView::SelectRows</enum>
Expand Down

0 comments on commit 1bbd477

Please sign in to comment.