# Standard import json # PyQt from qgis.core import QgsAuthMethodConfig, QgsDataSourceUri from qgis.PyQt.QtCore import QByteArray, QJsonDocument, QObject, QUrl, pyqtSignal from qgis.PyQt.QtNetwork import QNetworkReply, QNetworkRequest from qgis.PyQt.QtWidgets import QAction, QMessageBox, QDialog from qgis.PyQt.QtCore import Qt from qgis.PyQt.QtWidgets import QApplication # from PyQt6.QtCore import Qt # from PyQt6.QtWidgets import QApplication # Project from gn_tools.__about__ import __login_url__ class GetLogin(QDialog): finished_login = pyqtSignal() """ Create a filter, specific to GeoNature ZH API and fetch all the features corresponding. """ def __init__( self, parent=None, network_manager=None, params=None ): super().__init__() print("enfin !") self.domain_url = parent.domain_url self.network_manager = network_manager self.auth = parent.dlg.mAuthConfigSelect self.is_connect = parent.dlg.is_connect self.params = params print(self.auth.configId()) # self.zhs = None # self.json_filter = {} # self.total_zh = 0 # PyQt5 - Version QApplication.setOverrideCursor(Qt.CursorShape(3)) # PyQt6 - Version # QApplication.setOverrideCursor(Qt.CursorShape.WaitCursor) self._pending_downloads = 0 self.check_password() @property def pending_downloads(self): return self._pending_downloads def handle_finished(self, reply): self._pending_downloads -= 1 QApplication.restoreOverrideCursor() # If reply is an error or reply is a redirection, login is not accepted print(reply.attribute( QNetworkRequest.Attribute(0) )) print('Login : ',reply.error()) self.network_manager.cookieStatus = reply.attribute( QNetworkRequest.Attribute(0) ) if ( reply.error() != QNetworkReply.NetworkError(0) or self.network_manager.cookieStatus != 200 ): # Error code 299 means the logins are invalid # Redirection not equal to 200 means a redirection happened if reply.error() == 0: self.is_connect.setText("Aie !") msg = QMessageBox() msg.warning( None, self.tr("Warning"), self.tr( "Houston, we've got a problem ... \n\n L'utilisateur ou le mot de passe est invalide !" ), # noqa: E501 ) elif reply.error() == 3: msg = QMessageBox() msg.warning( None, self.tr("Warning"), self.tr( "GéoNature's URL not found, add it to the plugin settings" ), # noqa: E501 ) self.close() self.parent.iface.showOptionsDialog( currentPage="mOptionsPage{}".format(__title__) ) self.parent.create_project_from_gn() else: print(f"code: {reply.error()} message: {reply.errorString()}") else: self.cookies = self.network_manager.cookieJar() self.is_connect.setText("Good Job !") QApplication.setOverrideCursor(Qt.CursorShape(0)) # If the authenfication configuration already exists, # modify it with the new values # self.manager_config.setConfigMap( # { # "password": self.usr, # "username": self.pwd, # } # ) # self.manager.storeAuthenticationConfig( # self.manager_config, True # ) # delete sensitive informations... even if it's in the database now... self.network_manager.accepted = True # Add the cookie created with the login to the request self.network_manager.setCookieJar(self.cookies) # self.network_manager.setCookiesFromUrl(self.cookies,self.domain_url) self.close() if self.pending_downloads == 0: self.finished_login.emit() return self.network_manager def get_param_connect(self): if self.params: self.usr = self.params["usr"] self.pwd = self.params["pwd"] return True elif self.auth.configId(): authCfg = self.auth.configId() quri = QgsDataSourceUri() quri.setParam("authcfg", authCfg) quri.setParam("url", self.domain_url) self.usr = quri.uri().split("user='")[1].split("' ",1)[0] self.pwd = quri.uri().split("password='")[1].split("' ",1)[0] return True else : return False def check_password(self): # Test if the user wrote something if self.auth.configId() or self.params: is_connect = self.get_param_connect() url = QUrl(self.domain_url + __login_url__) print(self.usr) data_json = { "login": ( self.usr ), "password": ( self.pwd ), } print(url) request = QNetworkRequest(url) request.setHeader(QNetworkRequest.KnownHeaders(0), "application/json") document = QJsonDocument(data_json) print(document.toJson()) reply = self.network_manager.post(request, document.toJson()) reply.finished.connect(lambda: self.handle_finished(reply)) self._pending_downloads += 1 else: msg = QMessageBox() msg.warning( None, self.tr("Warning"), self.tr( "GéoNature's Auth not found, add it to the plugin settings" ), # noqa: E501 )