Skip to content

Commit 53c507d

Browse files
committed
db manager: add credentials to postgis rasters (fixes #13594)
1 parent e225385 commit 53c507d

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

python/core/qgserror.sip

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class QgsError
7171
*/
7272
QString message( QgsErrorMessage::Format theFormat = QgsErrorMessage::Html ) const;
7373

74-
/** Short error descriprion, usually the first error in chain, the real error.
74+
/** Short error description, usually the first error in chain, the real error.
7575
* @return error description
7676
*/
7777
QString summary() const;

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

+20-3
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,9 @@ def info(self):
258258

259259
return PGRasterTableInfo(self)
260260

261-
def gdalUri(self):
262-
uri = self.database().uri()
261+
def gdalUri(self, uri=None):
262+
if not uri:
263+
uri = self.database().uri()
263264
schema = (u'schema=%s' % self.schemaName()) if self.schemaName() else ''
264265
dbname = (u'dbname=%s' % uri.database()) if uri.database() else ''
265266
host = (u'host=%s' % uri.host()) if uri.host() else ''
@@ -285,9 +286,25 @@ def mimeUri(self):
285286
return uri
286287

287288
def toMapLayer(self):
288-
from qgis.core import QgsRasterLayer, QgsContrastEnhancement
289+
from qgis.core import QgsRasterLayer, QgsContrastEnhancement, QgsDataSourceURI, QgsCredentials
289290

290291
rl = QgsRasterLayer(self.gdalUri(), self.name)
292+
if not rl.isValid():
293+
err = rl.error().summary()
294+
uri = QgsDataSourceURI(self.database().uri())
295+
conninfo = uri.connectionInfo()
296+
username = uri.username()
297+
password = uri.password()
298+
299+
for i in range(3):
300+
(ok, username, password) = QgsCredentials.instance().get(conninfo, username, password, err)
301+
if ok:
302+
uri.setUsername(username)
303+
uri.setPassword(password)
304+
rl = QgsRasterLayer(self.gdalUri(uri), self.name)
305+
if rl.isValid():
306+
break
307+
291308
if rl.isValid():
292309
rl.setContrastEnhancement(QgsContrastEnhancement.StretchToMinimumMaximum)
293310
return rl

src/core/qgserror.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class CORE_EXPORT QgsError
108108
*/
109109
QString message( QgsErrorMessage::Format theFormat = QgsErrorMessage::Html ) const;
110110

111-
/** Short error descriprion, usually the first error in chain, the real error.
111+
/** Short error description, usually the first error in chain, the real error.
112112
* @return error description
113113
*/
114114
QString summary() const;

0 commit comments

Comments
 (0)