Skip to content

Commit 9754590

Browse files
authored
Merge pull request #4228 from tomkralidis/metasearch-http-basicauth
add support for HTTP Basic authentication
2 parents e23dcc8 + d672361 commit 9754590

File tree

3 files changed

+124
-19
lines changed

3 files changed

+124
-19
lines changed

python/plugins/MetaSearch/dialogs/maindialog.py

+22-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# Alexander Bruy (alexander.bruy@gmail.com),
1515
# Maxim Dubinin (sim@gis-lab.info)
1616
#
17-
# Copyright (C) 2014 Tom Kralidis (tomkralidis@gmail.com)
17+
# Copyright (C) 2017 Tom Kralidis (tomkralidis@gmail.com)
1818
#
1919
# This source is free software; you can redistribute it and/or modify it under
2020
# the terms of the GNU General Public License as published by the Free
@@ -34,7 +34,7 @@
3434

3535
import json
3636
import os.path
37-
from urllib.request import build_opener, install_opener, ProxyHandler
37+
from urllib.request import build_opener, HTTPError, install_opener, HTTPBasicAuthHandler, HTTPHandler, ProxyHandler
3838

3939
from qgis.PyQt.QtCore import Qt
4040
from qgis.PyQt.QtWidgets import QApplication, QDialog, QDialogButtonBox, QMessageBox, QTreeWidgetItem, QWidget
@@ -77,6 +77,8 @@ def __init__(self, iface):
7777
self.settings = QgsSettings()
7878
self.catalog = None
7979
self.catalog_url = None
80+
self.catalog_username = None
81+
self.catalog_password = None
8082
self.context = StaticContext()
8183

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

150152
key = '/MetaSearch/%s' % self.cmbConnectionsSearch.currentText()
151153
self.catalog_url = self.settings.value('%s/url' % key)
154+
self.catalog_username = self.settings.value('%s/username' % key)
155+
self.catalog_password = self.settings.value('%s/password' % key)
152156

153157
self.set_bbox_global()
154158

@@ -252,6 +256,8 @@ def save_connection(self):
252256

253257
if caller == 'cmbConnectionsSearch': # bind to service in search tab
254258
self.catalog_url = self.settings.value('%s/url' % key)
259+
self.catalog_username = self.settings.value('%s/username' % key)
260+
self.catalog_password = self.settings.value('%s/password' % key)
255261

256262
if caller == 'cmbConnectionsServices': # clear server metadata
257263
self.textMetadata.clear()
@@ -264,6 +270,8 @@ def connection_info(self):
264270
current_text = self.cmbConnectionsServices.currentText()
265271
key = '/MetaSearch/%s' % current_text
266272
self.catalog_url = self.settings.value('%s/url' % key)
273+
self.catalog_username = self.settings.value('%s/username' % key)
274+
self.catalog_password = self.settings.value('%s/password' % key)
267275

268276
# connect to the server
269277
if not self._get_csw():
@@ -301,6 +309,9 @@ def edit_connection(self):
301309
conn_edit.setWindowTitle(self.tr('Edit Catalogue service'))
302310
conn_edit.leName.setText(current_text)
303311
conn_edit.leURL.setText(url)
312+
conn_edit.leUsername.setText(self.settings.value('/MetaSearch/%s/username' % current_text))
313+
conn_edit.lePassword.setText(self.settings.value('/MetaSearch/%s/password' % current_text))
314+
304315
if conn_edit.exec_() == QDialog.Accepted: # update service list
305316
self.populate_connection_list()
306317

@@ -433,6 +444,8 @@ def search(self):
433444
current_text = self.cmbConnectionsSearch.currentText()
434445
key = '/MetaSearch/%s' % current_text
435446
self.catalog_url = self.settings.value('%s/url' % key)
447+
self.catalog_username = self.settings.value('%s/username' % key)
448+
self.catalog_password = self.settings.value('%s/password' % key)
436449

437450
# start position and number of records to return
438451
self.startfrom = 0
@@ -771,7 +784,9 @@ def show_metadata(self):
771784

772785
try:
773786
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
774-
cat = CatalogueServiceWeb(self.catalog_url, timeout=self.timeout)
787+
cat = CatalogueServiceWeb(self.catalog_url, timeout=self.timeout,
788+
username=self.catalog_username,
789+
password=self.catalog_password)
775790
cat.getrecordbyid(
776791
[self.catalog.records[identifier].identifier])
777792
except ExceptionReport as err:
@@ -850,7 +865,9 @@ def _get_csw(self):
850865
try:
851866
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
852867
self.catalog = CatalogueServiceWeb(self.catalog_url,
853-
timeout=self.timeout)
868+
timeout=self.timeout,
869+
username=self.catalog_username,
870+
password=self.catalog_password)
854871
return True
855872
except ExceptionReport as err:
856873
msg = self.tr('Error connecting to service: {0}').format(err)
@@ -859,8 +876,8 @@ def _get_csw(self):
859876
except Exception as err:
860877
msg = self.tr('Unknown Error: {0}').format(err)
861878

862-
QMessageBox.warning(self, self.tr('CSW Connection error'), msg)
863879
QApplication.restoreOverrideCursor()
880+
QMessageBox.warning(self, self.tr('CSW Connection error'), msg)
864881
return False
865882

866883
def install_proxy(self):

python/plugins/MetaSearch/dialogs/newconnectiondialog.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
# Alexander Bruy (alexander.bruy@gmail.com),
1010
# Maxim Dubinin (sim@gis-lab.info)
1111
#
12-
# Copyright (C) 2014 Tom Kralidis (tomkralidis@gmail.com)
12+
# Copyright (C) 2017 Tom Kralidis (tomkralidis@gmail.com)
1313
#
1414
# This source is free software; you can redistribute it and/or modify it under
1515
# the terms of the GNU General Public License as published by the Free
@@ -47,12 +47,16 @@ def __init__(self, conn_name=None):
4747
self.settings = QgsSettings()
4848
self.conn_name = None
4949
self.conn_name_orig = conn_name
50+
self.username = None
51+
self.password = None
5052

5153
def accept(self):
5254
"""add CSW entry"""
5355

5456
conn_name = self.leName.text().strip()
5557
conn_url = self.leURL.text().strip()
58+
conn_username = self.leUsername.text().strip()
59+
conn_password = self.lePassword.text().strip()
5660

5761
if any([conn_name == '', conn_url == '']):
5862
QMessageBox.warning(self, self.tr('Save connection'),
@@ -86,6 +90,11 @@ def accept(self):
8690
self.settings.setValue(keyurl, conn_url)
8791
self.settings.setValue('/MetaSearch/selected', conn_name)
8892

93+
if conn_username != '':
94+
self.settings.setValue('%s/username' % key, conn_username)
95+
if conn_password != '':
96+
self.settings.setValue('%s/password' % key, conn_password)
97+
8998
QDialog.accept(self)
9099

91100
def reject(self):

python/plugins/MetaSearch/ui/newconnectiondialog.ui

+92-13
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,35 @@
66
<rect>
77
<x>0</x>
88
<y>0</y>
9-
<width>368</width>
10-
<height>120</height>
9+
<width>494</width>
10+
<height>224</height>
1111
</rect>
1212
</property>
1313
<property name="windowTitle">
1414
<string>Create a new Catalogue connection</string>
1515
</property>
1616
<layout class="QGridLayout" name="gridLayout">
17-
<item row="0" column="0">
18-
<widget class="QLabel" name="label">
17+
<item row="2" column="2">
18+
<widget class="QLineEdit" name="leURL"/>
19+
</item>
20+
<item row="2" column="0">
21+
<widget class="QLabel" name="label_2">
1922
<property name="text">
20-
<string>Name</string>
23+
<string>URL</string>
2124
</property>
2225
</widget>
2326
</item>
24-
<item row="0" column="1">
25-
<widget class="QLineEdit" name="leName"/>
26-
</item>
2727
<item row="1" column="0">
28-
<widget class="QLabel" name="label_2">
28+
<widget class="QLabel" name="label">
2929
<property name="text">
30-
<string>URL</string>
30+
<string>Name</string>
3131
</property>
3232
</widget>
3333
</item>
34-
<item row="1" column="1">
35-
<widget class="QLineEdit" name="leURL"/>
34+
<item row="1" column="2">
35+
<widget class="QLineEdit" name="leName"/>
3636
</item>
37-
<item row="2" column="0" colspan="2">
37+
<item row="5" column="2">
3838
<widget class="QDialogButtonBox" name="buttonBox">
3939
<property name="orientation">
4040
<enum>Qt::Horizontal</enum>
@@ -44,8 +44,87 @@
4444
</property>
4545
</widget>
4646
</item>
47+
<item row="3" column="2">
48+
<widget class="QTabWidget" name="tabNewConnectionOptions">
49+
<property name="currentIndex">
50+
<number>0</number>
51+
</property>
52+
<widget class="QWidget" name="tab">
53+
<attribute name="title">
54+
<string>Authentication</string>
55+
</attribute>
56+
<widget class="QLabel" name="label_3">
57+
<property name="geometry">
58+
<rect>
59+
<x>10</x>
60+
<y>10</y>
61+
<width>421</width>
62+
<height>16</height>
63+
</rect>
64+
</property>
65+
<property name="text">
66+
<string>If the service requires basic authentication, enter a user name and optional password</string>
67+
</property>
68+
</widget>
69+
<widget class="QLabel" name="label_4">
70+
<property name="geometry">
71+
<rect>
72+
<x>10</x>
73+
<y>30</y>
74+
<width>51</width>
75+
<height>16</height>
76+
</rect>
77+
</property>
78+
<property name="text">
79+
<string>User name</string>
80+
</property>
81+
</widget>
82+
<widget class="QLabel" name="label_5">
83+
<property name="geometry">
84+
<rect>
85+
<x>10</x>
86+
<y>60</y>
87+
<width>46</width>
88+
<height>13</height>
89+
</rect>
90+
</property>
91+
<property name="text">
92+
<string>Password</string>
93+
</property>
94+
</widget>
95+
<widget class="QLineEdit" name="leUsername">
96+
<property name="geometry">
97+
<rect>
98+
<x>80</x>
99+
<y>30</y>
100+
<width>341</width>
101+
<height>20</height>
102+
</rect>
103+
</property>
104+
</widget>
105+
<widget class="QLineEdit" name="lePassword">
106+
<property name="geometry">
107+
<rect>
108+
<x>80</x>
109+
<y>60</y>
110+
<width>341</width>
111+
<height>20</height>
112+
</rect>
113+
</property>
114+
</widget>
115+
</widget>
116+
</widget>
117+
</item>
47118
</layout>
48119
</widget>
120+
<tabstops>
121+
<tabstop>leName</tabstop>
122+
<tabstop>leURL</tabstop>
123+
<tabstop>leUsername</tabstop>
124+
<tabstop>lePassword</tabstop>
125+
<tabstop>tabNewConnectionOptions</tabstop>
126+
<tabstop>buttonBox</tabstop>
127+
</tabstops>
49128
<resources/>
50129
<connections>
51130
<connection>

0 commit comments

Comments
 (0)