Skip to content

Commit

Permalink
[FEATURE] allow postgres layers without saved username & password by …
Browse files Browse the repository at this point in the history
…asking for credentials

git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@12859 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed Feb 1, 2010
1 parent b49cd95 commit 392c608
Show file tree
Hide file tree
Showing 18 changed files with 591 additions and 345 deletions.
93 changes: 69 additions & 24 deletions src/app/postgres/qgspgnewconnection.cpp
Expand Up @@ -24,6 +24,7 @@
#include "qgscontexthelp.h"
#include "qgsdatasourceuri.h"
#include "qgslogger.h"
#include "qgscredentialdialog.h"

extern "C"
{
Expand Down Expand Up @@ -55,7 +56,6 @@ QgsPgNewConnection::QgsPgNewConnection( QWidget *parent, const QString& connName
port = "5432";
}
txtPort->setText( port );
txtUsername->setText( settings.value( key + "/username" ).toString() );
Qt::CheckState s = Qt::Checked;
if ( ! settings.value( key + "/publicOnly", false ).toBool() )
s = Qt::Unchecked;
Expand All @@ -69,11 +69,30 @@ QgsPgNewConnection::QgsPgNewConnection( QWidget *parent, const QString& connName

cbxSSLmode->setCurrentIndex( cbxSSLmode->findData( settings.value( key + "/sslmode", QgsDataSourceURI::SSLprefer ).toInt() ) );

if ( settings.value( key + "/save" ).toString() == "true" )
if ( settings.value( key + "/saveUsername" ).toString() == "true" )
{
txtUsername->setText( settings.value( key + "/username" ).toString() );
chkStoreUsername->setChecked( true );
}

if ( settings.value( key + "/savePassword" ).toString() == "true" )
{
txtPassword->setText( settings.value( key + "/password" ).toString() );
chkStorePassword->setChecked( true );
}

// Old save setting
if ( settings.contains( key + "/save" ) )
{
txtUsername->setText( settings.value( key + "/username" ).toString() );
chkStoreUsername->setChecked( !txtUsername->text().isEmpty() );

if ( settings.value( key + "/save" ).toString() == "true" )
txtPassword->setText( settings.value( key + "/password" ).toString() );

chkStorePassword->setChecked( true );
}

txtName->setText( connName );
}
}
Expand Down Expand Up @@ -106,12 +125,16 @@ void QgsPgNewConnection::accept()
settings.setValue( baseKey + "/host", txtHost->text() );
settings.setValue( baseKey + "/database", txtDatabase->text() );
settings.setValue( baseKey + "/port", txtPort->text() );
settings.setValue( baseKey + "/username", txtUsername->text() );
settings.setValue( baseKey + "/username", chkStoreUsername->isChecked() ? txtUsername->text() : "" );
settings.setValue( baseKey + "/password", chkStorePassword->isChecked() ? txtPassword->text() : "" );
settings.setValue( baseKey + "/publicOnly", cb_publicSchemaOnly->isChecked() );
settings.setValue( baseKey + "/geometryColumnsOnly", cb_geometryColumnsOnly->isChecked() );
settings.setValue( baseKey + "/save", chkStorePassword->isChecked() ? "true" : "false" );
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" );

// remove old save setting
settings.remove( baseKey + "/save" );

QDialog::accept();
}
Expand All @@ -138,45 +161,67 @@ QgsPgNewConnection::~QgsPgNewConnection()
void QgsPgNewConnection::testConnection()
{
QgsDataSourceURI uri;
uri.setConnection( txtHost->text(), txtPort->text(), txtDatabase->text(), txtUsername->text(), txtPassword->text(), ( QgsDataSourceURI::SSLmode ) cbxSSLmode->itemData( cbxSSLmode->currentIndex() ).toInt() );

QgsDebugMsg( "PQconnectdb(\"" + uri.connectionInfo() + "\");" );

PGconn *pd = PQconnectdb( uri.connectionInfo().toLocal8Bit() ); // use what is set based on locale; after connecting, use Utf8
uri.setConnection( txtHost->text(), txtPort->text(), txtDatabase->text(),
chkStoreUsername->isChecked() ? txtUsername->text() : "",
chkStorePassword->isChecked() ? txtPassword->text() : "",
( QgsDataSourceURI::SSLmode ) cbxSSLmode->itemData( cbxSSLmode->currentIndex() ).toInt() );
QString conninfo = uri.connectionInfo();
QgsDebugMsg( "PQconnectdb(\"" + conninfo + "\");" );

PGconn *pd = PQconnectdb( conninfo.toLocal8Bit() ); // use what is set based on locale; after connecting, use Utf8
// check the connection status
if ( PQstatus( pd ) != CONNECTION_OK && QString::fromUtf8( PQerrorMessage( pd ) ) == PQnoPasswordSupplied )
{
QString password = QString::null;
QString username = txtUsername->text();
QString password = txtPassword->text();

uri.setUsername( "" );
uri.setPassword( "" );

while ( PQstatus( pd ) != CONNECTION_OK )
{
bool ok = true;
password = QInputDialog::getText( this,
tr( "Enter password" ),
tr( "Error: %1Enter password for %2" )
.arg( QString::fromUtf8( PQerrorMessage( pd ) ) )
.arg( uri.connectionInfo() ),
QLineEdit::Password,
password,
&ok );
bool ok = QgsCredentials::instance()->get( conninfo, username, password, QString::fromUtf8( PQerrorMessage( pd ) ) );
if ( !ok )
break;

::PQfinish( pd );

if ( !ok )
break;
QgsDataSourceURI uri( conninfo );

if ( !username.isEmpty() )
uri.setUsername( username );

if ( !password.isEmpty() )
uri.setPassword( password );

QgsDebugMsg( "PQconnectdb(\"" + uri.connectionInfo() + "\");" );
pd = PQconnectdb( uri.connectionInfo().toLocal8Bit() );
}

if ( PQstatus( pd ) == CONNECTION_OK )
{
if ( chkStoreUsername->isChecked() )
txtUsername->setText( username );
if ( chkStorePassword->isChecked() )
txtPassword->setText( password );

pd = PQconnectdb( QString( "%1 password='%2'" ).arg( uri.connectionInfo() ).arg( password ).toLocal8Bit() );
QgsCredentials::instance()->put( conninfo, username, password );
}
}

if ( PQstatus( pd ) == CONNECTION_OK )
{
// Database successfully opened; we can now issue SQL commands.
QMessageBox::information( this, tr( "Test connection" ), tr( "Connection to %1 was successful" ).arg( txtDatabase->text() ) );
QMessageBox::information( this,
tr( "Test connection" ),
tr( "Connection to %1 was successful" ).arg( txtDatabase->text() ) );
}
else
{
QMessageBox::information( this, tr( "Test connection" ), tr( "Connection failed - Check settings and try again.\n\nExtended error information:\n%1" ).arg( QString::fromUtf8( PQerrorMessage( pd ) ) ) );
QMessageBox::information( this,
tr( "Test connection" ),
tr( "Connection failed - Check settings and try again.\n\nExtended error information:\n%1" )
.arg( QString::fromUtf8( PQerrorMessage( pd ) ) ) );
}
// free pg connection resources
PQfinish( pd );
Expand Down
35 changes: 20 additions & 15 deletions src/app/postgres/qgspgsourceselect.cpp
Expand Up @@ -28,6 +28,7 @@ email : sherman at mrcc.com
#include "qgsquerybuilder.h"
#include "qgsdatasourceuri.h"
#include "qgsvectorlayer.h"
#include "qgscredentials.h"

#include <QInputDialog>
#include <QMessageBox>
Expand Down Expand Up @@ -122,6 +123,8 @@ void QgsPgSourceSelect::on_btnDelete_clicked()
settings.remove( key + "/sslmode" );
settings.remove( key + "/publicOnly" );
settings.remove( key + "/geometryColumnsOnly" );
settings.remove( key + "/saveUsername" );
settings.remove( key + "/savePassword" );
settings.remove( key + "/save" );

populateConnectionList();
Expand Down Expand Up @@ -403,7 +406,7 @@ void QgsPgSourceSelect::on_btnConnect_clicked()
uri.setConnection( settings.value( key + "/host" ).toString(),
settings.value( key + "/port" ).toString(),
database,
settings.value( key + "/username" ).toString(),
username,
password,
( QgsDataSourceURI::SSLmode ) settings.value( key + "/sslmode", QgsDataSourceURI::SSLprefer ).toInt() );

Expand All @@ -426,28 +429,30 @@ void QgsPgSourceSelect::on_btnConnect_clicked()
// check the connection status
if ( PQstatus( pd ) != CONNECTION_OK && QString::fromUtf8( PQerrorMessage( pd ) ) == PQnoPasswordSupplied )
{
QString password = QString::null;

while ( PQstatus( pd ) != CONNECTION_OK )
{
bool ok = true;
password = QInputDialog::getText( this,
tr( "Enter password" ),
tr( "Error: %1Enter password for %2" )
.arg( QString::fromUtf8( PQerrorMessage( pd ) ) )
.arg( m_connInfo ),
QLineEdit::Password,
password,
&ok );
bool ok = QgsCredentials::instance()->get( m_connInfo, username, password, QString::fromUtf8( PQerrorMessage( pd ) ) );
if ( !ok )
break;

::PQfinish( pd );

if ( !ok )
break;
QgsDataSourceURI uri( m_connInfo );
if ( !username.isEmpty() )
uri.setUsername( username );

m_privConnInfo = QString( "%1 password='%2'" ).arg( m_connInfo ).arg( password );
if ( !password.isEmpty() )
uri.setPassword( password );

m_privConnInfo = uri.connectionInfo();
QgsDebugMsg( "connecting " + m_privConnInfo );
pd = PQconnectdb( m_privConnInfo.toLocal8Bit() );
}

if ( PQstatus( pd ) == CONNECTION_OK )
{
QgsCredentials::instance()->put( m_connInfo, username, password );
}
}

if ( PQstatus( pd ) == CONNECTION_OK )
Expand Down
10 changes: 7 additions & 3 deletions src/app/qgisapp.cpp
Expand Up @@ -142,6 +142,7 @@
#include "ogr/qgsopenvectorlayerdialog.h"
#include "qgsattributetabledialog.h"
#include "qgsvectorfilewriter.h"
#include "qgscredentialdialog.h"

#ifdef HAVE_QWT
#include "qgsgpsinformationwidget.h"
Expand Down Expand Up @@ -265,12 +266,11 @@ static void setTitleBarText_( QWidget & qgisApp )
/**
Creator function for output viewer
*/
static QgsMessageOutput* messageOutputViewer_()
static QgsMessageOutput *messageOutputViewer_()
{
return new QgsMessageViewer();
return new QgsMessageViewer( QgisApp::instance() );
}


/**
* This function contains forced validation of CRS used in QGIS.
* There are 3 options depending on the settings:
Expand Down Expand Up @@ -400,9 +400,13 @@ QgisApp::QgisApp( QSplashScreen *splash, QWidget * parent, Qt::WFlags fl )

// set QGIS specific srs validation
QgsCoordinateReferenceSystem::setCustomSrsValidation( customSrsValidation_ );

// set graphical message output
QgsMessageOutput::setMessageOutputCreator( messageOutputViewer_ );

// set graphical credential requester
new QgsCredentialDialog( this );

fileNew(); // prepare empty project
qApp->processEvents();

Expand Down
39 changes: 26 additions & 13 deletions src/app/qgsmanageconnectionsdialog.cpp
Expand Up @@ -289,10 +289,23 @@ QDomDocument QgsManageConnectionsDialog::savePgConnections( const QStringList &c
el.setAttribute( "name", connections[ i ] );
el.setAttribute( "host", settings.value( path + "/host", "" ).toString() );
el.setAttribute( "port", settings.value( path + "/port", "" ).toString() );
el.setAttribute( "db", settings.value( path + "/database", "" ).toString() );
el.setAttribute( "username", settings.value( path + "/username", "" ).toString() );
el.setAttribute( "password", settings.value( path + "/password", "" ).toString() );
el.setAttribute( "database", settings.value( path + "/database", "" ).toString() );
el.setAttribute( "sslmode", settings.value( path + "/sslmode", "1" ).toString() );

el.setAttribute( "saveUsername", settings.value( path + "/saveUsername", "false" ).toString() );

if ( settings.value( path + "/saveUsername", "false" ).toString() == "true" )
{
el.setAttribute( "username", settings.value( path + "/username", "" ).toString() );
}

el.setAttribute( "savePassword", settings.value( path + "/savePassword", "false" ).toString() );

if ( settings.value( path + "/savePassword", "false" ).toString() == "true" )
{
el.setAttribute( "password", settings.value( path + "/password", "" ).toString() );
}

root.appendChild( el );
}

Expand Down Expand Up @@ -359,7 +372,8 @@ void QgsManageConnectionsDialog::loadPgConnections( const QDomDocument &doc, con
QDomElement root = doc.documentElement();
if ( root.tagName() != "qgsPgConnections" )
{
QMessageBox::information( this, tr( "Loading connections" ),
QMessageBox::information( this,
tr( "Loading connections" ),
tr( "The file is not an PostGIS connections exchange file." ) );
return;
}
Expand All @@ -382,7 +396,8 @@ void QgsManageConnectionsDialog::loadPgConnections( const QDomDocument &doc, con
// check for duplicates
if ( keys.contains( connectionName ) )
{
int res = QMessageBox::warning( this, tr( "Loading conections" ),
int res = QMessageBox::warning( this,
tr( "Loading connections" ),
tr( "Connection with name %1 already exists. Overwrite?" )
.arg( connectionName ),
QMessageBox::Yes | QMessageBox::No );
Expand All @@ -395,19 +410,17 @@ void QgsManageConnectionsDialog::loadPgConnections( const QDomDocument &doc, con

//no dups detected or overwrite is allowed
settings.beginGroup( "/PostgreSQL/connections/" + connectionName );

settings.setValue( "/host", child.attribute( "host" ) );
settings.setValue( "/port", child.attribute( "port" ) );
settings.setValue( "/database", child.attribute( "db" ) );
settings.setValue( "/database", child.attribute( "database" ) );
settings.setValue( "/sslmode", child.attribute( "sslmode" ) );
if ( !child.attribute( "username" ).isEmpty() )
{
settings.setValue( "/username", child.attribute( "username" ) );
settings.setValue( "/password", child.attribute( "password" ) );
settings.setValue( "/save", "true" );
}
settings.setValue( "/saveUsername", child.attribute( "saveUsername" ) );
settings.setValue( "/username", child.attribute( "username" ) );
settings.setValue( "/savePassword", child.attribute( "savePassword" ) );
settings.setValue( "/password", child.attribute( "password" ) );
settings.endGroup();

child = child.nextSiblingElement();
}
}

3 changes: 3 additions & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -57,6 +57,7 @@ SET(QGIS_CORE_SRCS
qgsmaprenderer.cpp
qgsmaptopixel.cpp
qgsmessageoutput.cpp
qgscredentials.cpp
qgsoverlayobject.cpp
qgspalgeometry.cpp
qgspalobjectpositionmanager.cpp
Expand Down Expand Up @@ -223,6 +224,7 @@ SET(QGIS_CORE_MOC_HDRS
qgsmaplayerregistry.h
qgsmaprenderer.h
qgsmessageoutput.h
qgscredentials.h
qgspluginlayer.h
qgsproject.h
qgsrunprocess.h
Expand Down Expand Up @@ -389,6 +391,7 @@ SET(QGIS_CORE_HDRS
qgsmaprenderer.h
qgsmaptopixel.h
qgsmessageoutput.h
qgscredentials.h
qgsoverlayobjectpositionmanager.h
qgspalobjectpositionmanager.h
qgspluginlayer.h
Expand Down

0 comments on commit 392c608

Please sign in to comment.