Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 17 additions & 21 deletions ConstraintChecker/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@
# Import the PyQt and QGIS libraries
from qgis.PyQt.QtCore import Qt, QAbstractTableModel, QModelIndex, QAbstractItemModel, QSettings
from qgis.PyQt.QtWidgets import QMessageBox
from qgis.core import QgsAuthMethodConfig, QgsApplication
from qgis.core import Qgis, QgsMessageLog

from .constraint_results_dialog import ConstraintResultsDialog
from .utils import DEBUG, get_db_conn_details


class ResultModel(QAbstractTableModel):
Expand Down Expand Up @@ -147,28 +148,23 @@ def getDbCursor(self):
# Looks like the preferred connection could not be found
raise Exception('The preferred PostGIS connection, '
'%s could not be found, please check your Constrain Checker settings')
database = str(s.value("PostgreSQL/connections/%s/database" % selectedConnection, ''))
user = str(s.value("PostgreSQL/connections/%s/username" % selectedConnection, ''))
password = str(s.value("PostgreSQL/connections/%s/password" % selectedConnection, ''))
port = int(s.value("PostgreSQL/connections/%s/port" % selectedConnection, 5432))

auth_manager = QgsApplication.authManager()
conf = QgsAuthMethodConfig()
configs = {v.name(): k for k, v in auth_manager.availableAuthMethodConfigs().items()}
# name of config in auth must match the name of selected connection
try:
auth_manager.loadAuthenticationConfig(configs[selectedConnection], conf, True)
if conf.id():
user = conf.config('username', '')
password = conf.config('password', '')
except KeyError:
pass
host, database, username, password, port = get_db_conn_details(selectedConnection)
if DEBUG:
conn_info = f"host: {host}, db:{database}, user: {username}, pass={len(password)}, port={port}"
QgsMessageLog.logMessage(f"Constraint checker conn info: {conn_info}", 'Constraint Checker', level=Qgis.Info)

dbConn = psycopg2.connect(database=database,
user=user,
password=password,
host=host,
port=port)
if not username or not password:
# Active Directory authentication possible
dbConn = psycopg2.connect(database=database,
host=host,
port=port)
else:
dbConn = psycopg2.connect(database=database,
user=username,
password=password,
host=host,
port=port)
dbConn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
return dbConn.cursor()

Expand Down
5 changes: 3 additions & 2 deletions ConstraintChecker/metadata.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ name=Constraint Checker
qgisMinimumVersion=3.4
qgisMaximumVersion=3.99
description=Generate reports of constraints (e.g. planning constraints) applicable to an area of interest.
version=1.2
version=1.3
author=Lutra Consulting for Dartmoor National Park Authority
[email protected]

Expand All @@ -15,7 +15,8 @@ about=Generate reports of constraints (e.g. planning constraints) applicable to
# Optional items:

# Uncomment the following line and add your changelog entries:
changelog=1.2 - Migration to QGIS 3.x
changelog=1.3 Fix for psycopg2 connection when Active Directory is used for authentication
<p>1.2 - Migration to QGIS 3.x
<p>1.1.2 - Bug fixes:
- rubber band color, geometry column configuration (from Matt Travis)
<p>1.1.1 - Bug fixes:
Expand Down
33 changes: 32 additions & 1 deletion ConstraintChecker/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
"""

import string
from qgis.core import QgsApplication, QgsAuthMethodConfig, QgsSettings

DEBUG = False


def dbSafe(s):
""" Returns true if this string is considered safe to use as a DB
Expand All @@ -29,7 +33,34 @@ def dbSafe(s):
safeChars = string.digits + string.ascii_letters + '_ '

for char in s:
if not char in safeChars:
if char not in safeChars:
return False
return True


def get_db_conn_details(conn_name):
s = QgsSettings()
s.beginGroup("/PostgreSQL/connections")
if conn_name not in s.childGroups():
raise Exception(f"No connection named {conn_name} in QGIS settings!")
s.endGroup()
s.beginGroup(f"/PostgreSQL/connections/{conn_name}")
host = s.value("host", "")
database = s.value("database", "")
username = s.value("username", "")
password = s.value("password", "")
port = int(s.value("port", 5432))
authcfg = s.value("authcfg", None)

if DEBUG:
print(f"Conn details: {host}, {database}, {username}, {len(password)}, {port}")

if authcfg:
conf = QgsAuthMethodConfig()
auth_manager = QgsApplication.authManager()
auth_manager.loadAuthenticationConfig(authcfg, conf, True)
if conf.id():
username = conf.config("username", "")
password = conf.config("password", "")

return host, database, username, password, port