Skip to content

Commit

Permalink
Merge pull request #4228 from tomkralidis/metasearch-http-basicauth
Browse files Browse the repository at this point in the history
add support for HTTP Basic authentication
  • Loading branch information
tomkralidis committed Mar 7, 2017
2 parents e23dcc8 + d672361 commit 9754590
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 19 deletions.
27 changes: 22 additions & 5 deletions python/plugins/MetaSearch/dialogs/maindialog.py
Expand Up @@ -14,7 +14,7 @@
# Alexander Bruy (alexander.bruy@gmail.com), # Alexander Bruy (alexander.bruy@gmail.com),
# Maxim Dubinin (sim@gis-lab.info) # 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 # 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 # the terms of the GNU General Public License as published by the Free
Expand All @@ -34,7 +34,7 @@


import json import json
import os.path 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.QtCore import Qt
from qgis.PyQt.QtWidgets import QApplication, QDialog, QDialogButtonBox, QMessageBox, QTreeWidgetItem, QWidget from qgis.PyQt.QtWidgets import QApplication, QDialog, QDialogButtonBox, QMessageBox, QTreeWidgetItem, QWidget
Expand Down Expand Up @@ -77,6 +77,8 @@ def __init__(self, iface):
self.settings = QgsSettings() self.settings = QgsSettings()
self.catalog = None self.catalog = None
self.catalog_url = None self.catalog_url = None
self.catalog_username = None
self.catalog_password = None
self.context = StaticContext() self.context = StaticContext()


version = self.context.metadata.get('general', 'version') version = self.context.metadata.get('general', 'version')
Expand Down Expand Up @@ -149,6 +151,8 @@ def manageGui(self):


key = '/MetaSearch/%s' % self.cmbConnectionsSearch.currentText() key = '/MetaSearch/%s' % self.cmbConnectionsSearch.currentText()
self.catalog_url = self.settings.value('%s/url' % key) 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() self.set_bbox_global()


Expand Down Expand Up @@ -252,6 +256,8 @@ def save_connection(self):


if caller == 'cmbConnectionsSearch': # bind to service in search tab if caller == 'cmbConnectionsSearch': # bind to service in search tab
self.catalog_url = self.settings.value('%s/url' % key) 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 if caller == 'cmbConnectionsServices': # clear server metadata
self.textMetadata.clear() self.textMetadata.clear()
Expand All @@ -264,6 +270,8 @@ def connection_info(self):
current_text = self.cmbConnectionsServices.currentText() current_text = self.cmbConnectionsServices.currentText()
key = '/MetaSearch/%s' % current_text key = '/MetaSearch/%s' % current_text
self.catalog_url = self.settings.value('%s/url' % key) 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 # connect to the server
if not self._get_csw(): if not self._get_csw():
Expand Down Expand Up @@ -301,6 +309,9 @@ def edit_connection(self):
conn_edit.setWindowTitle(self.tr('Edit Catalogue service')) conn_edit.setWindowTitle(self.tr('Edit Catalogue service'))
conn_edit.leName.setText(current_text) conn_edit.leName.setText(current_text)
conn_edit.leURL.setText(url) 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 if conn_edit.exec_() == QDialog.Accepted: # update service list
self.populate_connection_list() self.populate_connection_list()


Expand Down Expand Up @@ -433,6 +444,8 @@ def search(self):
current_text = self.cmbConnectionsSearch.currentText() current_text = self.cmbConnectionsSearch.currentText()
key = '/MetaSearch/%s' % current_text key = '/MetaSearch/%s' % current_text
self.catalog_url = self.settings.value('%s/url' % key) 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 # start position and number of records to return
self.startfrom = 0 self.startfrom = 0
Expand Down Expand Up @@ -771,7 +784,9 @@ def show_metadata(self):


try: try:
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) 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( cat.getrecordbyid(
[self.catalog.records[identifier].identifier]) [self.catalog.records[identifier].identifier])
except ExceptionReport as err: except ExceptionReport as err:
Expand Down Expand Up @@ -850,7 +865,9 @@ def _get_csw(self):
try: try:
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
self.catalog = CatalogueServiceWeb(self.catalog_url, self.catalog = CatalogueServiceWeb(self.catalog_url,
timeout=self.timeout) timeout=self.timeout,
username=self.catalog_username,
password=self.catalog_password)
return True return True
except ExceptionReport as err: except ExceptionReport as err:
msg = self.tr('Error connecting to service: {0}').format(err) msg = self.tr('Error connecting to service: {0}').format(err)
Expand All @@ -859,8 +876,8 @@ def _get_csw(self):
except Exception as err: except Exception as err:
msg = self.tr('Unknown Error: {0}').format(err) msg = self.tr('Unknown Error: {0}').format(err)


QMessageBox.warning(self, self.tr('CSW Connection error'), msg)
QApplication.restoreOverrideCursor() QApplication.restoreOverrideCursor()
QMessageBox.warning(self, self.tr('CSW Connection error'), msg)
return False return False


def install_proxy(self): def install_proxy(self):
Expand Down
11 changes: 10 additions & 1 deletion python/plugins/MetaSearch/dialogs/newconnectiondialog.py
Expand Up @@ -9,7 +9,7 @@
# Alexander Bruy (alexander.bruy@gmail.com), # Alexander Bruy (alexander.bruy@gmail.com),
# Maxim Dubinin (sim@gis-lab.info) # 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 # 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 # the terms of the GNU General Public License as published by the Free
Expand Down Expand Up @@ -47,12 +47,16 @@ def __init__(self, conn_name=None):
self.settings = QgsSettings() self.settings = QgsSettings()
self.conn_name = None self.conn_name = None
self.conn_name_orig = conn_name self.conn_name_orig = conn_name
self.username = None
self.password = None


def accept(self): def accept(self):
"""add CSW entry""" """add CSW entry"""


conn_name = self.leName.text().strip() conn_name = self.leName.text().strip()
conn_url = self.leURL.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 == '']): if any([conn_name == '', conn_url == '']):
QMessageBox.warning(self, self.tr('Save connection'), QMessageBox.warning(self, self.tr('Save connection'),
Expand Down Expand Up @@ -86,6 +90,11 @@ def accept(self):
self.settings.setValue(keyurl, conn_url) self.settings.setValue(keyurl, conn_url)
self.settings.setValue('/MetaSearch/selected', conn_name) 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) QDialog.accept(self)


def reject(self): def reject(self):
Expand Down
105 changes: 92 additions & 13 deletions python/plugins/MetaSearch/ui/newconnectiondialog.ui
Expand Up @@ -6,35 +6,35 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>368</width> <width>494</width>
<height>120</height> <height>224</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
<string>Create a new Catalogue connection</string> <string>Create a new Catalogue connection</string>
</property> </property>
<layout class="QGridLayout" name="gridLayout"> <layout class="QGridLayout" name="gridLayout">
<item row="0" column="0"> <item row="2" column="2">
<widget class="QLabel" name="label"> <widget class="QLineEdit" name="leURL"/>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_2">
<property name="text"> <property name="text">
<string>Name</string> <string>URL</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="0" column="1">
<widget class="QLineEdit" name="leName"/>
</item>
<item row="1" column="0"> <item row="1" column="0">
<widget class="QLabel" name="label_2"> <widget class="QLabel" name="label">
<property name="text"> <property name="text">
<string>URL</string> <string>Name</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="1" column="1"> <item row="1" column="2">
<widget class="QLineEdit" name="leURL"/> <widget class="QLineEdit" name="leName"/>
</item> </item>
<item row="2" column="0" colspan="2"> <item row="5" column="2">
<widget class="QDialogButtonBox" name="buttonBox"> <widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation"> <property name="orientation">
<enum>Qt::Horizontal</enum> <enum>Qt::Horizontal</enum>
Expand All @@ -44,8 +44,87 @@
</property> </property>
</widget> </widget>
</item> </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> </layout>
</widget> </widget>
<tabstops>
<tabstop>leName</tabstop>
<tabstop>leURL</tabstop>
<tabstop>leUsername</tabstop>
<tabstop>lePassword</tabstop>
<tabstop>tabNewConnectionOptions</tabstop>
<tabstop>buttonBox</tabstop>
</tabstops>
<resources/> <resources/>
<connections> <connections>
<connection> <connection>
Expand Down

0 comments on commit 9754590

Please sign in to comment.