Skip to content

Commit

Permalink
add support for HTTP Basic authentication (http://hub.qgis.org/issues…
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkralidis committed Mar 7, 2017
1 parent 7dbe494 commit d672361
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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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()
Expand All @@ -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():
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand All @@ -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):
Expand Down
11 changes: 10 additions & 1 deletion python/plugins/MetaSearch/dialogs/newconnectiondialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'),
Expand Down Expand Up @@ -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):
Expand Down
105 changes: 92 additions & 13 deletions python/plugins/MetaSearch/ui/newconnectiondialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -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">
<item row="2" column="2">
<widget class="QLineEdit" name="leURL"/>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Name</string>
<string>URL</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="leName"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_2">
<widget class="QLabel" name="label">
<property name="text">
<string>URL</string>
<string>Name</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="leURL"/>
<item row="1" column="2">
<widget class="QLineEdit" name="leName"/>
</item>
<item row="2" column="0" colspan="2">
<item row="5" column="2">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
Expand All @@ -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>
Expand Down

0 comments on commit d672361

Please sign in to comment.