From d672361597694407eea19c339dd275a902564709 Mon Sep 17 00:00:00 2001 From: Tom Kralidis Date: Mon, 6 Mar 2017 16:45:30 -0500 Subject: [PATCH] add support for HTTP Basic authentication (http://hub.qgis.org/issues/16298) --- .../plugins/MetaSearch/dialogs/maindialog.py | 27 ++++- .../MetaSearch/dialogs/newconnectiondialog.py | 11 +- .../MetaSearch/ui/newconnectiondialog.ui | 105 +++++++++++++++--- 3 files changed, 124 insertions(+), 19 deletions(-) diff --git a/python/plugins/MetaSearch/dialogs/maindialog.py b/python/plugins/MetaSearch/dialogs/maindialog.py index baebb9bb0f7a..7997141865d4 100644 --- a/python/plugins/MetaSearch/dialogs/maindialog.py +++ b/python/plugins/MetaSearch/dialogs/maindialog.py @@ -14,7 +14,7 @@ # Alexander Bruy (alexander.bruy@gmail.com), # Maxim Dubinin (sim@gis-lab.info) # -# Copyright (C) 2014 Tom Kralidis (tomkralidis@gmail.com) +# Copyright (C) 2017 Tom Kralidis (tomkralidis@gmail.com) # # This source is free software; you can redistribute it and/or modify it under # the terms of the GNU General Public License as published by the Free @@ -34,7 +34,7 @@ import json import os.path -from urllib.request import build_opener, install_opener, ProxyHandler +from urllib.request import build_opener, HTTPError, install_opener, HTTPBasicAuthHandler, HTTPHandler, ProxyHandler from qgis.PyQt.QtCore import Qt from qgis.PyQt.QtWidgets import QApplication, QDialog, QDialogButtonBox, QMessageBox, QTreeWidgetItem, QWidget @@ -77,6 +77,8 @@ def __init__(self, iface): self.settings = QgsSettings() self.catalog = None self.catalog_url = None + self.catalog_username = None + self.catalog_password = None self.context = StaticContext() version = self.context.metadata.get('general', 'version') @@ -149,6 +151,8 @@ def manageGui(self): key = '/MetaSearch/%s' % self.cmbConnectionsSearch.currentText() self.catalog_url = self.settings.value('%s/url' % key) + self.catalog_username = self.settings.value('%s/username' % key) + self.catalog_password = self.settings.value('%s/password' % key) self.set_bbox_global() @@ -252,6 +256,8 @@ def save_connection(self): if caller == 'cmbConnectionsSearch': # bind to service in search tab self.catalog_url = self.settings.value('%s/url' % key) + self.catalog_username = self.settings.value('%s/username' % key) + self.catalog_password = self.settings.value('%s/password' % key) if caller == 'cmbConnectionsServices': # clear server metadata self.textMetadata.clear() @@ -264,6 +270,8 @@ def connection_info(self): current_text = self.cmbConnectionsServices.currentText() key = '/MetaSearch/%s' % current_text self.catalog_url = self.settings.value('%s/url' % key) + self.catalog_username = self.settings.value('%s/username' % key) + self.catalog_password = self.settings.value('%s/password' % key) # connect to the server if not self._get_csw(): @@ -301,6 +309,9 @@ def edit_connection(self): conn_edit.setWindowTitle(self.tr('Edit Catalogue service')) conn_edit.leName.setText(current_text) conn_edit.leURL.setText(url) + conn_edit.leUsername.setText(self.settings.value('/MetaSearch/%s/username' % current_text)) + conn_edit.lePassword.setText(self.settings.value('/MetaSearch/%s/password' % current_text)) + if conn_edit.exec_() == QDialog.Accepted: # update service list self.populate_connection_list() @@ -433,6 +444,8 @@ def search(self): current_text = self.cmbConnectionsSearch.currentText() key = '/MetaSearch/%s' % current_text self.catalog_url = self.settings.value('%s/url' % key) + self.catalog_username = self.settings.value('%s/username' % key) + self.catalog_password = self.settings.value('%s/password' % key) # start position and number of records to return self.startfrom = 0 @@ -771,7 +784,9 @@ def show_metadata(self): try: QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) - cat = CatalogueServiceWeb(self.catalog_url, timeout=self.timeout) + cat = CatalogueServiceWeb(self.catalog_url, timeout=self.timeout, + username=self.catalog_username, + password=self.catalog_password) cat.getrecordbyid( [self.catalog.records[identifier].identifier]) except ExceptionReport as err: @@ -850,7 +865,9 @@ def _get_csw(self): try: QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) self.catalog = CatalogueServiceWeb(self.catalog_url, - timeout=self.timeout) + timeout=self.timeout, + username=self.catalog_username, + password=self.catalog_password) return True except ExceptionReport as err: msg = self.tr('Error connecting to service: {0}').format(err) @@ -859,8 +876,8 @@ def _get_csw(self): except Exception as err: msg = self.tr('Unknown Error: {0}').format(err) - QMessageBox.warning(self, self.tr('CSW Connection error'), msg) QApplication.restoreOverrideCursor() + QMessageBox.warning(self, self.tr('CSW Connection error'), msg) return False def install_proxy(self): diff --git a/python/plugins/MetaSearch/dialogs/newconnectiondialog.py b/python/plugins/MetaSearch/dialogs/newconnectiondialog.py index 72c038efe9b9..18fa7b9bd40f 100644 --- a/python/plugins/MetaSearch/dialogs/newconnectiondialog.py +++ b/python/plugins/MetaSearch/dialogs/newconnectiondialog.py @@ -9,7 +9,7 @@ # Alexander Bruy (alexander.bruy@gmail.com), # Maxim Dubinin (sim@gis-lab.info) # -# Copyright (C) 2014 Tom Kralidis (tomkralidis@gmail.com) +# Copyright (C) 2017 Tom Kralidis (tomkralidis@gmail.com) # # This source is free software; you can redistribute it and/or modify it under # the terms of the GNU General Public License as published by the Free @@ -47,12 +47,16 @@ def __init__(self, conn_name=None): self.settings = QgsSettings() self.conn_name = None self.conn_name_orig = conn_name + self.username = None + self.password = None def accept(self): """add CSW entry""" conn_name = self.leName.text().strip() conn_url = self.leURL.text().strip() + conn_username = self.leUsername.text().strip() + conn_password = self.lePassword.text().strip() if any([conn_name == '', conn_url == '']): QMessageBox.warning(self, self.tr('Save connection'), @@ -86,6 +90,11 @@ def accept(self): self.settings.setValue(keyurl, conn_url) self.settings.setValue('/MetaSearch/selected', conn_name) + if conn_username != '': + self.settings.setValue('%s/username' % key, conn_username) + if conn_password != '': + self.settings.setValue('%s/password' % key, conn_password) + QDialog.accept(self) def reject(self): diff --git a/python/plugins/MetaSearch/ui/newconnectiondialog.ui b/python/plugins/MetaSearch/ui/newconnectiondialog.ui index 191d72451d1b..8ef804fa92ed 100644 --- a/python/plugins/MetaSearch/ui/newconnectiondialog.ui +++ b/python/plugins/MetaSearch/ui/newconnectiondialog.ui @@ -6,35 +6,35 @@ 0 0 - 368 - 120 + 494 + 224 Create a new Catalogue connection - - + + + + + - Name + URL - - - - + - URL + Name - - + + - + Qt::Horizontal @@ -44,8 +44,87 @@ + + + + 0 + + + + Authentication + + + + + 10 + 10 + 421 + 16 + + + + If the service requires basic authentication, enter a user name and optional password + + + + + + 10 + 30 + 51 + 16 + + + + User name + + + + + + 10 + 60 + 46 + 13 + + + + Password + + + + + + 80 + 30 + 341 + 20 + + + + + + + 80 + 60 + 341 + 20 + + + + + + + + leName + leURL + leUsername + lePassword + tabNewConnectionOptions + buttonBox +