Skip to content

Commit 62b2c1f

Browse files
committedJan 24, 2015
db manager: request credentials directly when connection failed (fixes #11886)
1 parent f8f2713 commit 62b2c1f

File tree

2 files changed

+37
-40
lines changed

2 files changed

+37
-40
lines changed
 

‎python/plugins/db_manager/db_plugins/postgis/connector.py

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
from ..connector import DBConnector
2929
from ..plugin import ConnectionError, DbError, Table
3030

31+
from qgis.core import QgsCredentials
32+
3133
import os
3234
import psycopg2
3335
import psycopg2.extensions
@@ -45,23 +47,50 @@ def __init__(self, uri):
4547

4648
self.host = uri.host() or os.environ.get('PGHOST')
4749
self.port = uri.port() or os.environ.get('PGPORT')
48-
self.user = uri.username() or os.environ.get('PGUSER') or os.environ.get('USER')
49-
self.dbname = uri.database() or os.environ.get('PGDATABASE') or self.user
50-
self.passwd = uri.password() or os.environ.get('PGPASSWORD')
50+
51+
username = uri.username() or os.environ.get('PGUSER') or os.environ.get('USER')
52+
password = uri.password() or os.environ.get('PGPASSWORD')
5153

5254
try:
5355
self.connection = psycopg2.connect( self._connectionInfo().encode('utf-8') )
54-
self.connection.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
5556
except self.connection_error_types(), e:
56-
raise ConnectionError(e)
57+
err = str(e)
58+
uri = self.uri()
59+
conninfo = uri.connectionInfo()
60+
61+
for i in range(3):
62+
(ok, username, password) = QgsCredentials.instance().get(conninfo, username, password, err)
63+
if not ok:
64+
raise ConnectionError(e)
65+
66+
if username:
67+
uri.setUsername( username )
68+
69+
if password:
70+
uri.setPassword( password )
71+
72+
try:
73+
self.connection = psycopg2.connect( uri.connectionInfo().encode('utf-8') )
74+
QgsCredentials.instance().put(conninfo, username, password)
75+
except self.connection_error_types(), e:
76+
if i == 2:
77+
raise ConnectionError(e)
78+
79+
err = str(e)
80+
81+
self.connection.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
82+
83+
c = self._execute(None, u"SELECT current_user")
84+
self.user = self._fetchone(c)
85+
self._close_cursor(c)
5786

5887
self._checkSpatial()
5988
self._checkRaster()
6089
self._checkGeometryColumnsTable()
6190
self._checkRasterColumnsTable()
6291

6392
def _connectionInfo(self):
64-
return unicode(self._uri.connectionInfo())
93+
return unicode(self.uri().connectionInfo())
6594

6695
def _checkSpatial(self):
6796
""" check whether postgis_version is present in catalog """
@@ -158,7 +187,7 @@ def fieldTypes(self):
158187

159188
def getDatabasePrivileges(self):
160189
""" db privileges: (can create schemas, can create temp. tables) """
161-
sql = u"SELECT has_database_privilege(%(d)s, 'CREATE'), has_database_privilege(%(d)s, 'TEMP')" % { 'd' : self.quoteString(self.dbname) }
190+
sql = u"SELECT has_database_privilege(current_database(), 'CREATE'), has_database_privilege(current_database(), 'TEMP')"
162191
c = self._execute(None, sql)
163192
res = self._fetchone(c)
164193
self._close_cursor(c)

‎python/plugins/db_manager/db_plugins/postgis/plugin.py

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@
3636

3737
from ..html_elems import HtmlParagraph, HtmlList, HtmlTable
3838

39-
from qgis.core import QgsCredentials
40-
41-
4239
def classFactory():
4340
return PostGisDBPlugin
4441

@@ -96,39 +93,10 @@ def connect(self, parent=None):
9693

9794
uri.setUseEstimatedMetadata(useEstimatedMetadata)
9895

99-
err = u""
10096
try:
10197
return self.connectToUri(uri)
10298
except ConnectionError, e:
103-
err = str(e)
104-
105-
# ask for valid credentials
106-
max_attempts = 3
107-
for i in range(max_attempts):
108-
(ok, username, password) = QgsCredentials.instance().get(uri.connectionInfo(), username, password, err)
109-
110-
if not ok:
111-
return False
112-
113-
if service != "":
114-
uri.setConnection(service, database, username, password, sslmode)
115-
else:
116-
uri.setConnection(host, port, database, username, password, sslmode)
117-
118-
try:
119-
self.connectToUri(uri)
120-
except ConnectionError, e:
121-
if i == max_attempts-1: # failed the last attempt
122-
raise e
123-
err = str(e)
124-
continue
125-
126-
QgsCredentials.instance().put(uri.connectionInfo(), username, password)
127-
128-
return True
129-
130-
return False
131-
99+
return False
132100

133101
class PGDatabase(Database):
134102
def __init__(self, connection, uri):

0 commit comments

Comments
 (0)
Please sign in to comment.