14

Is it possible to disable the option of saving passwords for web map services or web feature services in the QGIS data source manager?

enter image description here

There is a big warning for all users, but not all employees of our company heed this warning. Therefore I would like to disable the option completely.

Is there any possibility for deactivation, for example in the qgis_global_settings.ini or in the QGIS3.ini or elsewhere ?

3 Answers 3

5

Not a off-the-shelf solution, but a hint for a workaround:

You could edit the startup.py script to remove connection passwords each time QGIS is launched by a user.

A good starting point could be these questions:

Getting active PostgreSQL connection's username as variable in QGIS?

Getting active PostgreSQL connection username when stored through authentication configuration in QGIS 3

Info about startup.py:

https://docs.qgis.org/3.16/en/docs/pyqgis_developer_cookbook/intro.html#the-startup-py-file

5
  • 1
    Thank you for your advise, but I´m new to PyQGIS and I can't find the right command to read and reset the user data (name, password) of a WMS/WFS service.
    – joe_gerner
    Mar 22, 2022 at 13:12
  • And you want to do that in PyQGIS? Or in "plain" QGIS? Mar 22, 2022 at 14:00
  • 1
    I would try it with the startup.py file
    – joe_gerner
    Mar 24, 2022 at 12:11
  • 1
    That would not really prevent data leakage, the data would still be written to disk and stay there until a user might at some point in the future launch QGIS again with such startup.py. Apr 6, 2022 at 9:53
  • 1
    @bugmenot123 you are right, the one I proposed is not the perfect solution, especially in a potential hostile environment. A middleground would maybe chmodding the password file to make it only readable by the respective user... still not an easy/complete solution. Apr 6, 2022 at 12:36
4
+200

You can create a button (in a toolbar or here in the code in the messageBar) to create a new WMS connection without this basic tab :

DLG_WMS_CONNECTION = None

def update_browsers():
    """Updates Browser DockWidgets to refresh WMS connections"""
    browsers_names = ["Browser", "Browser2"]
    for browser in browsers_names:
        iface.mainWindow().findChild(QDockWidget, browser).refresh()


def display_new_connection_dlg():
    global DLG_WMS_CONNECTION
    DLG_WMS_CONNECTION = QgsNewHttpConnection()
    DLG_WMS_CONNECTION.accepted.connect(update_browsers)
    tab_auth = DLG_WMS_CONNECTION.findChild(QTabWidget, "tabAuth")
    # Hide basic auth tab
    tab_auth.setTabVisible(1, False)
    DLG_WMS_CONNECTION.show()


msgbar_auth = iface.messageBar()
pb_auth = QPushButton("Create a new WMS connection")
pb_auth.setMaximumWidth(300)
# Orange, 12 points and bold text
pb_auth.setStyleSheet(
    "QPushButton {font-size: 12pt; font-weight: bold; color: #ff6600;}"
)
pb_auth.clicked.connect(display_new_connection_dlg)
msgbar_auth.pushWidget(pb_auth, Qgis.Info, 0)
2
  • Hi, thank you for your solution. Is it possibile to change this in the qgis system files? So that I do not create a new button, but simply remove the option in the existing interface
    – joe_gerner
    Apr 27, 2022 at 8:15
  • It's possible, but you have to compile the whole QGIS modified version to create a specific QGIS without this tab. The dialog is called at different places and don't emit a specific signal we can catch, so the solution I proposed is the easiest and fastest to set up. Apr 27, 2022 at 8:23
1

As a workaround, I suggest deleting the credentials in the existing *.qgs file.

Here is the python code that does this:

import os  


def delete_properties(qgs_file, properties):
    """
    Only works with *.qgs (not *.qgz - it is a zipped *.qgs)

    :param qgs_file: Path to *.qgs file
    :param properties: Properties to be replaced with empty string => ''
    :return: Return a new *.qgs file without selected properties
    """

    dirname = os.path.dirname(qgs_file)
    name, extention = os.path.splitext(os.path.basename(qgs_file))
    new_path = os.path.join(dirname, name + '_NO_CREDENTIALS' + extention)

    with open(qgs_file, 'r', encoding='utf-8') as input_qgs, \
            open(new_path, 'w', encoding='utf-8') as output_qgs:

        for line in input_qgs.readlines():
            new_line = line
            for prop in properties:
                if prop in new_line:
                    new_list = []
                    first_part, second_part = new_line.split(f"{prop}='")
                    other_parts = second_part.split(f"'")
                    new_list.append(first_part + f"{prop}='")
                    new_list.extend(other_parts[1:])
                    new_line = "'".join(new_list)

            output_qgs.write(new_line)

    print(f'\nCredentials deleted, file saved:\n{new_path}')


if __name__ == '__main__':
    qgs_file_ = r"C:\QGIS_project.qgs"
    properties_ = ['password', 'user']
    delete_properties(qgs_file_, properties_)
5
  • Thanks for your comprehensive answer, but I'm not getting it right, unfortunately. It may be that I'm too stupid, but I'm relatively new to Python programming^^ I have inserted the code in the startup.py and adjusted the paths. Only unfortunately password and user are not removed.
    – joe_gerner
    Apr 6, 2022 at 12:17
  • @joe_gerner No, this code is standalone python script. It is not for startup.py. Change qgs_file_ variable to your QGIS project file path, save the code to text file with .py extention and run in the console (cmd.exe in OS Windows): python C:\script_location.py or "C:\Python39\python.exe" "C:\script_location.py" Apr 6, 2022 at 12:32
  • I followed your explanations save the file as python file and start the C:\Python38-32\python.exe and then run the script. But I always get the following error: "SyntaxError: unexpected character after line continuation character. >>>>C:\Users\Test\Desktop\delete.py File "<stdin>",line 1 C:\Users\Test\Dekstop\delete.py If I run the script in the cmd.exe I get no error, but nothing happens
    – joe_gerner
    Apr 6, 2022 at 13:29
  • @joe_gerner This picture may help you understand how to run the script: ibb.co/NZfM74t Apr 6, 2022 at 15:16
  • @joe_gerner stackoverflow.com/q/4621255/2829863 Apr 8, 2022 at 4:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.