Skip to content
Permalink
Browse files

[auth system] Data source integration for Postgres and Spit plugin

  • Loading branch information
dakcarto committed Sep 21, 2015
1 parent e13be21 commit 0d2b945c055a494ab354c44ad5e8b38a5abf71e5
@@ -55,14 +55,16 @@ ADD_LIBRARY (spitplugin MODULE ${SPIT_PLUGIN_SRCS} ${SPIT_PLUGIN_MOC_SRCS} ${SPI

INCLUDE_DIRECTORIES(
${CMAKE_CURRENT_BINARY_DIR}
../../core ../../core/geometry ../../core/raster
../../gui
../../core ../../core/auth ../../core/geometry ../../core/raster
../../gui ../../gui/auth
../../ui
${CMAKE_CURRENT_BINARY_DIR}/../../ui
../../app
../../providers/postgres
..
${POSTGRES_INCLUDE_DIR}
${GDAL_INCLUDE_DIR}
${QCA_INCLUDE_DIR}
)

TARGET_LINK_LIBRARIES(spitplugin
@@ -149,6 +149,7 @@ void QgsSpit::removeConnection()
settings.remove( key + "/publicOnly" );
settings.remove( key + "/geometryColumnsOnly" );
settings.remove( key + "/save" );
settings.remove( key + "/authcfg" );
settings.remove( key );

populateConnectionList();
@@ -391,10 +392,11 @@ void QgsSpit::dbConnect()
QString database = settings.value( key + "/database" ).toString();
QString username = settings.value( key + "/username" ).toString();
QString password = settings.value( key + "/password" ).toString();
QString authcfg = settings.value( key + "/authcfg" ).toString();

bool makeConnection = true;

if ( password.isEmpty() )
if ( authcfg.isEmpty() && password.isEmpty() )
{
// get password from user
password = QInputDialog::getText( this, tr( "Password for %1" ).arg( username ),
@@ -411,7 +413,8 @@ void QgsSpit::dbConnect()
database,
settings.value( key + "/username" ).toString(),
password,
( QgsDataSourceURI::SSLmode ) settings.value( key + "/sslmode", QgsDataSourceURI::SSLprefer ).toInt() );
( QgsDataSourceURI::SSLmode ) settings.value( key + "/sslmode", QgsDataSourceURI::SSLprefer ).toInt(),
authcfg );

conn = PQconnectdb( uri.connectionInfo().toUtf8() );
}
@@ -38,12 +38,16 @@ QT4_WRAP_CPP(PG_MOC_SRCS ${PG_MOC_HDRS})

INCLUDE_DIRECTORIES(
../../core
../../core/auth
../../core/geometry
${POSTGRES_INCLUDE_DIR}
${GEOS_INCLUDE_DIR}
${QSCINTILLA_INCLUDE_DIR}
${QCA_INCLUDE_DIR}
../../core
../../gui
../../gui/auth
../../ui
${CMAKE_CURRENT_BINARY_DIR}/../../ui
)

@@ -20,12 +20,14 @@
#include <QInputDialog>

#include "qgspgnewconnection.h"
#include "qgsauthmanager.h"
#include "qgscontexthelp.h"
#include "qgsdatasourceuri.h"
#include "qgspostgresconn.h"

QgsPgNewConnection::QgsPgNewConnection( QWidget *parent, const QString& connName, Qt::WindowFlags fl )
: QDialog( parent, fl ), mOriginalConnName( connName )
, mAuthConfigSelect( 0 )
{
setupUi( this );

@@ -34,6 +36,9 @@ QgsPgNewConnection::QgsPgNewConnection( QWidget *parent, const QString& connName
cbxSSLmode->addItem( tr( "prefer" ), QgsDataSourceURI::SSLprefer );
cbxSSLmode->addItem( tr( "require" ), QgsDataSourceURI::SSLrequire );

mAuthConfigSelect = new QgsAuthConfigSelect( this, "postgres" );
tabAuthentication->insertTab( 1, mAuthConfigSelect, tr( "Configurations" ) );

if ( !connName.isEmpty() )
{
// populate the dialog with the information stored for the connection
@@ -85,6 +90,14 @@ QgsPgNewConnection::QgsPgNewConnection( QWidget *parent, const QString& connName
chkStorePassword->setChecked( true );
}

QString authcfg = settings.value( key + "/authcfg" ).toString();
mAuthConfigSelect->setConfigId( authcfg );
if ( !authcfg.isEmpty() )
{
tabAuthentication->setCurrentIndex( tabAuthentication->indexOf( mAuthConfigSelect ) );
}


txtName->setText( connName );
}
}
@@ -94,8 +107,9 @@ void QgsPgNewConnection::accept()
QSettings settings;
QString baseKey = "/PostgreSQL/connections/";
settings.setValue( baseKey + "selected", txtName->text() );
bool hasAuthConfigID = !mAuthConfigSelect->configId().isEmpty();

if ( chkStorePassword->isChecked() &&
if ( !hasAuthConfigID && chkStorePassword->isChecked() &&
QMessageBox::question( this,
tr( "Saving passwords" ),
tr( "WARNING: You have opted to save your password. It will be stored in plain text in your project files and in your home directory on Unix-like systems, or in your user profile on Windows. If you do not want this to happen, please press the Cancel button.\n" ),
@@ -128,15 +142,16 @@ void QgsPgNewConnection::accept()
settings.setValue( baseKey + "/host", txtHost->text() );
settings.setValue( baseKey + "/port", txtPort->text() );
settings.setValue( baseKey + "/database", txtDatabase->text() );
settings.setValue( baseKey + "/username", chkStoreUsername->isChecked() ? txtUsername->text() : "" );
settings.setValue( baseKey + "/password", chkStorePassword->isChecked() ? txtPassword->text() : "" );
settings.setValue( baseKey + "/username", chkStoreUsername->isChecked() && !hasAuthConfigID ? txtUsername->text() : "" );
settings.setValue( baseKey + "/password", chkStorePassword->isChecked() && !hasAuthConfigID ? txtPassword->text() : "" );
settings.setValue( baseKey + "/authcfg", mAuthConfigSelect->configId() );
settings.setValue( baseKey + "/publicOnly", cb_publicSchemaOnly->isChecked() );
settings.setValue( baseKey + "/geometryColumnsOnly", cb_geometryColumnsOnly->isChecked() );
settings.setValue( baseKey + "/dontResolveType", cb_dontResolveType->isChecked() );
settings.setValue( baseKey + "/allowGeometrylessTables", cb_allowGeometrylessTables->isChecked() );
settings.setValue( baseKey + "/sslmode", cbxSSLmode->itemData( cbxSSLmode->currentIndex() ).toInt() );
settings.setValue( baseKey + "/saveUsername", chkStoreUsername->isChecked() ? "true" : "false" );
settings.setValue( baseKey + "/savePassword", chkStorePassword->isChecked() ? "true" : "false" );
settings.setValue( baseKey + "/saveUsername", chkStoreUsername->isChecked() && !hasAuthConfigID ? "true" : "false" );
settings.setValue( baseKey + "/savePassword", chkStorePassword->isChecked() && !hasAuthConfigID ? "true" : "false" );
settings.setValue( baseKey + "/estimatedMetadata", cb_useEstimatedMetadata->isChecked() );

// remove old save setting
@@ -171,14 +186,17 @@ void QgsPgNewConnection::testConnection()
{
uri.setConnection( txtService->text(), txtDatabase->text(),
txtUsername->text(), txtPassword->text(),
( QgsDataSourceURI::SSLmode ) cbxSSLmode->itemData( cbxSSLmode->currentIndex() ).toInt() );
( QgsDataSourceURI::SSLmode ) cbxSSLmode->itemData( cbxSSLmode->currentIndex() ).toInt(),
mAuthConfigSelect->configId() );
}
else
{
uri.setConnection( txtHost->text(), txtPort->text(), txtDatabase->text(),
txtUsername->text(), txtPassword->text(),
( QgsDataSourceURI::SSLmode ) cbxSSLmode->itemData( cbxSSLmode->currentIndex() ).toInt() );
( QgsDataSourceURI::SSLmode ) cbxSSLmode->itemData( cbxSSLmode->currentIndex() ).toInt(),
mAuthConfigSelect->configId() );
}

QString conninfo = uri.connectionInfo();

QgsPostgresConn *conn = QgsPostgresConn::connectDb( conninfo, true );
@@ -19,6 +19,7 @@
#include "ui_qgspgnewconnectionbase.h"
#include "qgisgui.h"
#include "qgscontexthelp.h"
#include "qgsauthconfigselect.h"

/** \class QgsPgNewConnection
* \brief Dialog to allow the user to configure and save connection
@@ -41,6 +42,7 @@ class QgsPgNewConnection : public QDialog, private Ui::QgsPgNewConnectionBase
void on_buttonBox_helpRequested() { QgsContextHelp::run( metaObject()->className() ); }
private:
QString mOriginalConnName; //store initial name to delete entry in case of rename
QgsAuthConfigSelect * mAuthConfigSelect;
};

#endif // QGSPGNEWCONNECTIONBASE_H
@@ -489,7 +489,7 @@ void QgsPgSourceSelect::addTables()
if ( idx.column() != QgsPgTableModel::dbtmTable )
continue;

QString uri = mTableModel.layerURI( mProxyModel.mapToSource( idx ), mConnInfo, mUseEstimatedMetadata );
QString uri = mTableModel.layerURI( mProxyModel.mapToSource( idx ), connectionInfo( false ), mUseEstimatedMetadata );
if ( uri.isNull() )
continue;

@@ -528,7 +528,7 @@ void QgsPgSourceSelect::on_btnConnect_clicked()

QgsDebugMsg( "Connection info: " + uri.connectionInfo() );

mConnInfo = uri.connectionInfo();
mDataSrcUri = uri;
mUseEstimatedMetadata = uri.useEstimatedMetadata();

QApplication::setOverrideCursor( Qt::BusyCursor );
@@ -578,9 +578,14 @@ QStringList QgsPgSourceSelect::selectedTables()
return mSelectedTables;
}

QString QgsPgSourceSelect::connectionInfo()
QString QgsPgSourceSelect::connectionInfo( bool expandAuthCfg )
{
return mConnInfo;
return mDataSrcUri.connectionInfo( expandAuthCfg );
}

QgsDataSourceURI QgsPgSourceSelect::dataSourceUri()
{
return mDataSrcUri;
}

void QgsPgSourceSelect::setSql( const QModelIndex &index )
@@ -594,7 +599,7 @@ void QgsPgSourceSelect::setSql( const QModelIndex &index )
QModelIndex idx = mProxyModel.mapToSource( index );
QString tableName = mTableModel.itemFromIndex( idx.sibling( idx.row(), QgsPgTableModel::dbtmTable ) )->text();

QString uri = mTableModel.layerURI( idx, mConnInfo, mUseEstimatedMetadata );
QString uri = mTableModel.layerURI( idx, connectionInfo(), mUseEstimatedMetadata );
if ( uri.isNull() )
{
QgsDebugMsg( "no uri" );
@@ -19,6 +19,7 @@

#include "ui_qgsdbsourceselectbase.h"
#include "qgisgui.h"
#include "qgsdatasourceuri.h"
#include "qgsdbfilterproxymodel.h"
#include "qgspgtablemodel.h"
#include "qgscontexthelp.h"
@@ -70,7 +71,9 @@ class QgsPgSourceSelect : public QDialog, private Ui::QgsDbSourceSelectBase
//! String list containing the selected tables
QStringList selectedTables();
//! Connection info (database, host, user, password)
QString connectionInfo();
QString connectionInfo( bool expandAuthCfg = true );
//! Data source URI
QgsDataSourceURI dataSourceUri();

signals:
void addDatabaseLayers( QStringList const & layerPathList, QString const & providerKey );
@@ -139,7 +142,7 @@ class QgsPgSourceSelect : public QDialog, private Ui::QgsDbSourceSelectBase
QStringList mColumnLabels;
// Our thread for doing long running queries
QgsGeomColumnTypeThread* mColumnTypeThread;
QString mConnInfo;
QgsDataSourceURI mDataSrcUri;
QStringList mSelectedTables;
bool mUseEstimatedMetadata;
// Storage for the range of layer type icons
@@ -383,6 +383,6 @@ QString QgsPgTableModel::layerURI( const QModelIndex &index, const QString& conn
uri.setSrid( srid );
uri.disableSelectAtId( !selectAtId );

QgsDebugMsg( QString( "returning uri %1" ).arg( uri.uri() ) );
return uri.uri();
QgsDebugMsg( QString( "returning uri %1" ).arg( uri.uri( false ) ) );
return uri.uri( false );
}
@@ -16,6 +16,7 @@
***************************************************************************/

#include "qgspostgresconn.h"
#include "qgsauthmanager.h"
#include "qgslogger.h"
#include "qgsdatasourceuri.h"
#include "qgsmessagelog.h"
@@ -1639,14 +1640,22 @@ QgsDataSourceURI QgsPostgresConn::connUri( QString theConnName )
}
}

QString authcfg = settings.value( key + "/authcfg" ).toString();

if ( !authcfg.isEmpty() )
{
username.clear();
password.clear();
}

QgsDataSourceURI uri;
if ( !service.isEmpty() )
{
uri.setConnection( service, database, username, password, ( QgsDataSourceURI::SSLmode ) sslmode );
uri.setConnection( service, database, username, password, ( QgsDataSourceURI::SSLmode ) sslmode, authcfg );
}
else
{
uri.setConnection( host, port, database, username, password, ( QgsDataSourceURI::SSLmode ) sslmode );
uri.setConnection( host, port, database, username, password, ( QgsDataSourceURI::SSLmode ) sslmode, authcfg );
}
uri.setUseEstimatedMetadata( useEstimatedMetadata );

@@ -1698,6 +1707,7 @@ void QgsPostgresConn::deleteConnection( QString theConnName )
settings.remove( key + "/saveUsername" );
settings.remove( key + "/savePassword" );
settings.remove( key + "/save" );
settings.remove( key + "/authcfg" );
settings.remove( key );
}

@@ -434,13 +434,13 @@ QString QgsPGLayerItem::createUri()
return QString::null;
}

QgsDataSourceURI uri( QgsPostgresConn::connUri( connItem->name() ).connectionInfo() );
QgsDataSourceURI uri( QgsPostgresConn::connUri( connItem->name() ).connectionInfo( false ) );
uri.setDataSource( mLayerProperty.schemaName, mLayerProperty.tableName, mLayerProperty.geometryColName, mLayerProperty.sql, pkColName );
uri.setWkbType( mLayerProperty.types[0] );
if ( uri.wkbType() != QGis::WKBNoGeometry )
uri.setSrid( QString::number( mLayerProperty.srids[0] ) );
QgsDebugMsg( QString( "layer uri: %1" ).arg( uri.uri() ) );
return uri.uri();
QgsDebugMsg( QString( "layer uri: %1" ).arg( uri.uri( false ) ) );
return uri.uri( false );
}

// ---------------------------------------------------------------------------
@@ -210,7 +210,7 @@ QgsPostgresProvider::QgsPostgresProvider( QString const & uri )
if ( mValid )
{
mUri.setKeyColumn( key );
setDataSourceUri( mUri.uri() );
setDataSourceUri( mUri.uri( false ) );
}
else
{
@@ -2479,7 +2479,7 @@ bool QgsPostgresProvider::setSubsetString( QString theSQL, bool updateFeatureCou
mUri.setSql( theSQL );
// Update yet another copy of the uri. Why are there 3 copies of the
// uri? Perhaps this needs some rationalisation.....
setDataSourceUri( mUri.uri() );
setDataSourceUri( mUri.uri( false ) );

if ( updateFeatureCount )
{

0 comments on commit 0d2b945

Please sign in to comment.
You can’t perform that action at this time.