Skip to content

Commit

Permalink
When loading a project with pending authentication requests, do not s…
Browse files Browse the repository at this point in the history
…et an active layer to avoid potential crash
  • Loading branch information
nirvn committed Oct 17, 2024
1 parent de18130 commit d908dd5
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 25 deletions.
27 changes: 27 additions & 0 deletions src/core/qfieldappauthrequesthandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@ void QFieldAppAuthRequestHandler::enterCredentials( const QString &realm, const
QgsCredentials::instance()->put( realm, username, password );
}

bool QFieldAppAuthRequestHandler::hasPendingAuthRequest() const
{
if ( mBrowserAuthenticationOngoing )
{
return true;
}

int pendingCount = mRealms.size();
for ( const RealmEntry &realm : mRealms )
{
if ( realm.canceled )
{
pendingCount--;
}
}
return pendingCount > 0;
}

QString QFieldAppAuthRequestHandler::getFirstUnhandledRealm() const
{
auto entry = std::find_if( mRealms.begin(), mRealms.end(), []( const RealmEntry &entry ) { return !entry.canceled; } );
Expand All @@ -59,6 +77,7 @@ bool QFieldAppAuthRequestHandler::handleLayerLogins()
if ( mRealms.at( i ).realm == realm )
{
mRealms.replace( i, RealmEntry( realm, true ) );
emit hasPendingAuthRequestChanged();
break;
}
}
Expand All @@ -71,6 +90,7 @@ bool QFieldAppAuthRequestHandler::handleLayerLogins()
if ( mRealms.at( i ).realm == realm )
{
mRealms.removeAt( i );
emit hasPendingAuthRequestChanged();
break;
}
}
Expand Down Expand Up @@ -116,6 +136,7 @@ void QFieldAppAuthRequestHandler::authNeeded( const QString &realm )

RealmEntry unhandledRealm( realm );
mRealms << unhandledRealm;
emit hasPendingAuthRequestChanged();
}

void QFieldAppAuthRequestHandler::handleAuthRequest( QNetworkReply *reply, QAuthenticator *auth )
Expand Down Expand Up @@ -176,17 +197,23 @@ void QFieldAppAuthRequestHandler::handleAuthRequest( QNetworkReply *reply, QAuth

void QFieldAppAuthRequestHandler::handleAuthRequestOpenBrowser( const QUrl &url )
{
mBrowserAuthenticationOngoing = true;
emit hasPendingAuthRequestChanged();
emit showLoginBrowser( url.toString() );
}

void QFieldAppAuthRequestHandler::handleAuthRequestCloseBrowser()
{
emit hideLoginBrowser();
mBrowserAuthenticationOngoing = false;
emit hasPendingAuthRequestChanged();
}

void QFieldAppAuthRequestHandler::abortAuthBrowser()
{
QgsNetworkAccessManager::instance()->abortAuthBrowser();
mBrowserAuthenticationOngoing = false;
emit hasPendingAuthRequestChanged();
}

QString QFieldAppAuthRequestHandler::getCredentialTitle( const QString &realm )
Expand Down
7 changes: 7 additions & 0 deletions src/core/qfieldappauthrequesthandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class QFieldAppAuthRequestHandler : public QObject, public QgsCredentials, publi
{
Q_OBJECT

Q_PROPERTY( bool hasPendingAuthRequest READ hasPendingAuthRequest NOTIFY hasPendingAuthRequestChanged )

public:
QFieldAppAuthRequestHandler();

Expand All @@ -58,12 +60,16 @@ class QFieldAppAuthRequestHandler : public QObject, public QgsCredentials, publi
//! abort an ongoing external browser authentication request
Q_INVOKABLE void abortAuthBrowser();

//! returns the number of pending authentication requests
bool hasPendingAuthRequest() const;

signals:
void showLoginDialog( const QString &realm, const QString &title );
void loginDialogClosed( const QString &realm, const bool canceled );
void reloadEverything();
void showLoginBrowser( const QString &url );
void hideLoginBrowser();
void hasPendingAuthRequestChanged();

protected:
bool request( const QString &realm, QString &username, QString &password, const QString &message = QString() ) override;
Expand Down Expand Up @@ -95,6 +101,7 @@ class QFieldAppAuthRequestHandler : public QObject, public QgsCredentials, publi
};

QList<RealmEntry> mRealms;
bool mBrowserAuthenticationOngoing = false;
};

#endif // QFIELDAPPAUTHREQUESTHANDLER_H
46 changes: 21 additions & 25 deletions src/qml/qgismobileapp.qml
Original file line number Diff line number Diff line change
Expand Up @@ -3342,6 +3342,11 @@ ApplicationWindow {
}

function onLoadProjectTriggered(path, name) {
messageLogModel.suppress({
"WFS": [""],
"WMS": [""],
"PostGIS": ["fe_sendauth: no password supplied"]
});
qfieldLocalDataPickerScreen.visible = false;
qfieldLocalDataPickerScreen.focus = false;
welcomeScreen.visible = false;
Expand All @@ -3360,6 +3365,17 @@ ApplicationWindow {
function onLoadProjectEnded(path, name) {
mapCanvasMap.unfreeze('projectload');
busyOverlay.state = "hidden";
dashBoard.layerTree.unfreeze(true);
if (qfieldAuthRequestHandler.hasPendingAuthRequest) {
qfieldAuthRequestHandler.handleLayerLogins();
} else {
// project in need of handling layer credentials
messageLogModel.unsuppress({
"WFS": [],
"WMS": [],
"PostGIS": []
});
}
projectInfo.filePath = path;
stateMachine.state = projectInfo.stateMode;
platformUtilities.setHandleVolumeKeys(qfieldSettings.digitizingVolumeKeys && stateMachine.state != 'browse');
Expand All @@ -3370,7 +3386,10 @@ ApplicationWindow {
activeLayer = defaultActiveLayer;
}
}
dashBoard.activeLayer = activeLayer;
if (!qfieldAuthRequestHandler.hasPendingAuthRequest) {
// only set active layer when not handling layer credentials
dashBoard.activeLayer = activeLayer;
}
drawingTemplateModel.projectFilePath = path;
mapCanvasBackground.color = mapCanvas.mapSettings.backgroundColor;
const titleDecorationConfiguration = projectInfo.getTitleDecorationConfiguration();
Expand Down Expand Up @@ -3447,7 +3466,7 @@ ApplicationWindow {
projectInfo.hasInsertRights = true;
projectInfo.hasEditRights = true;
}
if (stateMachine.state === "digitize") {
if (stateMachine.state === "digitize" && !qfieldAuthRequestHandler.hasPendingAuthRequest) {
dashBoard.ensureEditableLayerSelected();
}
var distanceString = iface.readProjectEntry("Measurement", "/DistanceUnits", "");
Expand Down Expand Up @@ -3536,29 +3555,6 @@ ApplicationWindow {
Item {
id: layerLogin

Connections {
target: iface
function onLoadProjectTriggered(path) {
messageLogModel.suppress({
"WFS": [""],
"WMS": [""],
"PostGIS": ["fe_sendauth: no password supplied"]
});
}

function onLoadProjectEnded() {
dashBoard.layerTree.unfreeze(true);
if (!qfieldAuthRequestHandler.handleLayerLogins()) {
//project loaded without more layer handling needed
messageLogModel.unsuppress({
"WFS": [],
"WMS": [],
"PostGIS": []
});
}
}
}

Connections {
target: qfieldAuthRequestHandler

Expand Down

1 comment on commit d908dd5

@qfield-fairy
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.