pgpass.diff

patch to make postgis password optional - Jürgen Fischer, 2009-12-23 05:03 PM

Download (6.47 KB)

View differences:

src/app/qgspgsourceselect.cpp (working copy)
406 406
  if ( pd != 0 )
407 407
    PQfinish( pd );
408 408

  
409
  pd = PQconnectdb( m_connectionInfo.toLocal8Bit() );  // use what is set based on locale; after connecting, use Utf8
410

  
411
#if defined(PG_VERSION_NUM) && PG_VERSION_NUM >= 80300
412
  // if the connection needs a password ask for one
413
  if ( PQstatus( pd ) == CONNECTION_BAD && PQconnectionNeedsPassword( pd ) )
414
#else
415
  // if the connection failed and we didn't have a password, ask for one and retry
416
  if ( PQstatus( pd ) == CONNECTION_BAD && password.isEmpty() )
417
#endif
409
  pd = PQconnectdb( uri.connectionInfo().toLocal8Bit() );  // use what is set based on locale; after connecting, use Utf8
410
  // check the connection status
411
  if ( PQstatus( pd ) != CONNECTION_OK && QString::fromUtf8( PQerrorMessage( pd ) ) == PQnoPasswordSupplied )
418 412
  {
419
    // get password from user
420
    bool makeConnection = false;
421
    password = QInputDialog::getText( this, tr( "Password for " ) + username,
422
                                      tr( "Please enter your password:" ),
423
                                      QLineEdit::Password, QString::null, &makeConnection );
424
    // allow null password entry in case its valid for the database
425
    if ( makeConnection )
413
    QString password = QString::null;
414

  
415
    while ( PQstatus( pd ) != CONNECTION_OK )
426 416
    {
427
      uri.setConnection( settings.value( key + "/host" ).toString(),
428
                         settings.value( key + "/port" ).toString(),
429
                         database,
430
                         settings.value( key + "/username" ).toString(),
431
                         password,
432
                         ( QgsDataSourceURI::SSLmode ) settings.value( key + "/sslmode", QgsDataSourceURI::SSLprefer ).toInt() );
417
      bool ok = true;
418
      password = QInputDialog::getText( this,
419
                                        tr( "Enter password" ),
420
                                        tr( "Error: %1\nEnter password for %2" )
421
                                        .arg( QString::fromUtf8( PQerrorMessage( pd ) ) )
422
                                        .arg( uri.connectionInfo() ),
423
                                        QLineEdit::Password,
424
                                        password,
425
                                        &ok );
433 426

  
434
      m_connectionInfo = uri.connectionInfo();
435
      PQfinish( pd );
436
      pd = PQconnectdb( m_connectionInfo.toLocal8Bit() );  // use what is set based on locale; after connecting, use Utf8
427
      ::PQfinish( pd );
428

  
429
      if ( !ok )
430
        break;
431

  
432
      pd = PQconnectdb( QString( "%1 password='%2'" ).arg( uri.connectionInfo() ).arg( password ).toLocal8Bit() );
437 433
    }
438 434
  }
439 435

  
src/app/qgsnewconnection.cpp (working copy)
18 18

  
19 19
#include <QSettings>
20 20
#include <QMessageBox>
21
#include <QInputDialog>
21 22

  
22 23
#include "qgsnewconnection.h"
23 24
#include "qgscontexthelp.h"
......
139 140
  QgsDataSourceURI uri;
140 141
  uri.setConnection( txtHost->text(), txtPort->text(), txtDatabase->text(), txtUsername->text(), txtPassword->text(), ( QgsDataSourceURI::SSLmode ) cbxSSLmode->itemData( cbxSSLmode->currentIndex() ).toInt() );
141 142

  
142
  QgsDebugMsg( "PQconnectdb(" + uri.connectionInfo() + ");" );
143
  QgsDebugMsg( "PQconnectdb(\"" + uri.connectionInfo() + "\");" );
143 144

  
144
  PGconn *pd = PQconnectdb( uri.connectionInfo().toLocal8Bit().data() );
145
  PGconn *pd = PQconnectdb( uri.connectionInfo().toLocal8Bit() );  // use what is set based on locale; after connecting, use Utf8
146
  // check the connection status
147
  if ( PQstatus( pd ) != CONNECTION_OK && QString::fromUtf8( PQerrorMessage( pd ) ) == PQnoPasswordSupplied )
148
  {
149
    QString password = QString::null;
150

  
151
    while ( PQstatus( pd ) != CONNECTION_OK )
152
    {
153
      bool ok = true;
154
      password = QInputDialog::getText( this,
155
                                        tr( "Enter password" ),
156
                                        tr( "Error: %1\nEnter password for %2" )
157
                                        .arg( QString::fromUtf8( PQerrorMessage( pd ) ) )
158
                                        .arg( uri.connectionInfo() ),
159
                                        QLineEdit::Password,
160
                                        password,
161
                                        &ok );
162

  
163
      ::PQfinish( pd );
164

  
165
      if ( !ok )
166
        break;
167

  
168
      pd = PQconnectdb( QString( "%1 password='%2'" ).arg( uri.connectionInfo() ).arg( password ).toLocal8Bit() );
169
    }
170
  }
171

  
145 172
  if ( PQstatus( pd ) == CONNECTION_OK )
146 173
  {
147 174
    // Database successfully opened; we can now issue SQL commands.
src/providers/postgres/qgspostgresprovider.cpp (working copy)
44 44
#include "qgspostgisbox3d.h"
45 45
#include "qgslogger.h"
46 46

  
47
#include <QInputDialog>
48

  
47 49
const QString POSTGRES_KEY = "postgres";
48 50
const QString POSTGRES_DESCRIPTION = "PostgreSQL/PostGIS data provider";
49 51

  
......
304 306

  
305 307
  PGconn *pd = PQconnectdb( conninfo.toLocal8Bit() );  // use what is set based on locale; after connecting, use Utf8
306 308
  // check the connection status
309
  if ( PQstatus( pd ) != CONNECTION_OK && QString::fromUtf8( PQerrorMessage( pd ) ) == PQnoPasswordSupplied )
310
  {
311
    QString password = QString::null;
312

  
313
    while ( PQstatus( pd ) != CONNECTION_OK )
314
    {
315
      bool ok = true;
316
      password = QInputDialog::getText( 0,
317
                                        tr( "Enter password" ),
318
                                        tr( "Error: %1\nEnter password for %2" )
319
                                        .arg( QString::fromUtf8( PQerrorMessage( pd ) ) )
320
                                        .arg( conninfo ),
321
                                        QLineEdit::Password,
322
                                        password,
323
                                        &ok );
324

  
325
      ::PQfinish( pd );
326

  
327
      if ( !ok )
328
        break;
329

  
330
      pd = PQconnectdb( QString( "%1 password='%2'" ).arg( conninfo ).arg( password ).toLocal8Bit() );
331
    }
332
  }
333

  
307 334
  if ( PQstatus( pd ) != CONNECTION_OK )
308 335
  {
336
    ::PQfinish( pd );
309 337
    QgsDebugMsg( "Connection to database failed" );
310 338
    return NULL;
311 339
  }