add support for HTTP Basic authentication (http://hub.qgis.org/issues/16298)

This commit is contained in:
Tom Kralidis 2017-03-06 16:45:30 -05:00
parent 7dbe4943d6
commit d672361597
3 changed files with 126 additions and 21 deletions

View File

@ -14,7 +14,7 @@ from builtins import range
# 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 @@ from builtins import range
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 @@ class MetaSearchDialog(QDialog, BASE_CLASS):
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 @@ class MetaSearchDialog(QDialog, BASE_CLASS):
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 @@ class MetaSearchDialog(QDialog, BASE_CLASS):
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 @@ class MetaSearchDialog(QDialog, BASE_CLASS):
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 @@ class MetaSearchDialog(QDialog, BASE_CLASS):
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 @@ class MetaSearchDialog(QDialog, BASE_CLASS):
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 @@ class MetaSearchDialog(QDialog, BASE_CLASS):
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 @@ class MetaSearchDialog(QDialog, BASE_CLASS):
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 @@ class MetaSearchDialog(QDialog, BASE_CLASS):
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):

View File

@ -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 @@ class NewConnectionDialog(QDialog, BASE_CLASS):
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 @@ class NewConnectionDialog(QDialog, BASE_CLASS):
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):

View File

@ -6,35 +6,35 @@
<rect>
<x>0</x>
<y>0</y>
<width>368</width>
<height>120</height>
<width>494</width>
<height>224</height>
</rect>
</property>
<property name="windowTitle">
<string>Create a new Catalogue connection</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Name</string>
</property>
</widget>
<item row="2" column="2">
<widget class="QLineEdit" name="leURL"/>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="leName"/>
</item>
<item row="1" column="0">
<item row="2" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>URL</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="leURL"/>
<item row="1" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Name</string>
</property>
</widget>
</item>
<item row="2" column="0" colspan="2">
<item row="1" column="2">
<widget class="QLineEdit" name="leName"/>
</item>
<item row="5" column="2">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
@ -44,8 +44,87 @@
</property>
</widget>
</item>
<item row="3" column="2">
<widget class="QTabWidget" name="tabNewConnectionOptions">
<property name="currentIndex">
<number>0</number>
</property>
<widget class="QWidget" name="tab">
<attribute name="title">
<string>Authentication</string>
</attribute>
<widget class="QLabel" name="label_3">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>421</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>If the service requires basic authentication, enter a user name and optional password</string>
</property>
</widget>
<widget class="QLabel" name="label_4">
<property name="geometry">
<rect>
<x>10</x>
<y>30</y>
<width>51</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>User name</string>
</property>
</widget>
<widget class="QLabel" name="label_5">
<property name="geometry">
<rect>
<x>10</x>
<y>60</y>
<width>46</width>
<height>13</height>
</rect>
</property>
<property name="text">
<string>Password</string>
</property>
</widget>
<widget class="QLineEdit" name="leUsername">
<property name="geometry">
<rect>
<x>80</x>
<y>30</y>
<width>341</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QLineEdit" name="lePassword">
<property name="geometry">
<rect>
<x>80</x>
<y>60</y>
<width>341</width>
<height>20</height>
</rect>
</property>
</widget>
</widget>
</widget>
</item>
</layout>
</widget>
<tabstops>
<tabstop>leName</tabstop>
<tabstop>leURL</tabstop>
<tabstop>leUsername</tabstop>
<tabstop>lePassword</tabstop>
<tabstop>tabNewConnectionOptions</tabstop>
<tabstop>buttonBox</tabstop>
</tabstops>
<resources/>
<connections>
<connection>