mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-17 00:04:02 -04:00
processing: verify postgresql access and request credentials if necessary (fixes #12137)
This commit is contained in:
parent
e96b204b09
commit
f540cd16a2
@ -27,6 +27,7 @@ __revision__ = '$Format:%H$'
|
|||||||
|
|
||||||
import re
|
import re
|
||||||
import os
|
import os
|
||||||
|
import psycopg2
|
||||||
|
|
||||||
from qgis.core import QgsDataSourceURI, QgsCredentials
|
from qgis.core import QgsDataSourceURI, QgsCredentials
|
||||||
|
|
||||||
@ -54,14 +55,28 @@ class OgrAlgorithm(GdalAlgorithm):
|
|||||||
# key='gid' estimatedmetadata=true srid=4326 type=MULTIPOLYGON
|
# key='gid' estimatedmetadata=true srid=4326 type=MULTIPOLYGON
|
||||||
# table="t4" (geom) sql=
|
# table="t4" (geom) sql=
|
||||||
dsUri = QgsDataSourceURI(layer.dataProvider().dataSourceUri())
|
dsUri = QgsDataSourceURI(layer.dataProvider().dataSourceUri())
|
||||||
connInfo = dsUri.connectionInfo()
|
conninfo = dsUri.connectionInfo()
|
||||||
(success, user, passwd ) = QgsCredentials.instance().get(connInfo, None, None)
|
|
||||||
if success:
|
conn = None
|
||||||
QgsCredentials.instance().put(connInfo, user, passwd)
|
while not conn:
|
||||||
ogrstr = ("PG:dbname='%s' host='%s' port='%s' user='%s' password='%s'"
|
try:
|
||||||
% (dsUri.database(), dsUri.host(), dsUri.port(), user, passwd))
|
conn = psycopg2.connect(dsUri.connectionInfo())
|
||||||
|
except psycopg2.OperationalError, e:
|
||||||
|
(ok, user, passwd ) = QgsCredentials.instance().get(conninfo, dsUri.username(), dsUri.password())
|
||||||
|
if not ok:
|
||||||
|
break
|
||||||
|
|
||||||
|
dsUri.setUsername( user )
|
||||||
|
dsUri.setPassword( passwd )
|
||||||
|
|
||||||
|
if not conn:
|
||||||
|
raise RuntimeError('Could not connect to PostgreSQL database - check connection info')
|
||||||
|
|
||||||
|
QgsCredentials.instance().put(conninfo, user, passwd)
|
||||||
|
ogrstr = "PG:%s" % dsUri.connectionInfo()
|
||||||
else:
|
else:
|
||||||
ogrstr = unicode(layer.source()).split("|")[0]
|
ogrstr = unicode(layer.source()).split("|")[0]
|
||||||
|
|
||||||
return '"' + ogrstr + '"'
|
return '"' + ogrstr + '"'
|
||||||
|
|
||||||
def ogrLayerName(self, uri):
|
def ogrLayerName(self, uri):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user