#12091 Show dialog with cancel button when requesting authorization tokens

This commit is contained in:
Magne Sjaastad 2025-02-03 11:42:17 +01:00
parent 08c207e556
commit 938222f878
6 changed files with 124 additions and 10 deletions

View File

@ -25,11 +25,15 @@
#include <QDateTime>
#include <QDesktopServices>
#include <QDialog>
#include <QEventLoop>
#include <QLabel>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QPushButton>
#include <QTimer>
#include <QUrlQuery>
#include <QVBoxLayout>
#include <QtNetworkAuth/QOAuth2AuthorizationCodeFlow>
//--------------------------------------------------------------------------------------------------
@ -240,6 +244,25 @@ void RiaCloudConnector::setTokenDataFilePath( const QString& filePath )
m_tokenDataFilePath = filePath;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaCloudConnector::forceNewTokens()
{
if ( m_authCodeFlow )
{
m_authCodeFlow->grant();
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RiaCloudConnector::isGranted() const
{
return m_authCodeFlow->status() == QAbstractOAuth::Status::Granted;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@ -276,6 +299,55 @@ QString RiaCloudConnector::requestTokenBlocking()
return m_authCodeFlow->token();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaCloudConnector::requestTokenWithCancelButton()
{
QDialog dialog;
dialog.setWindowTitle( "Requesting Token" );
dialog.setModal( true );
QVBoxLayout* layout = new QVBoxLayout( &dialog );
QLabel* label = new QLabel( "Requesting token. Please wait..." );
layout->addWidget( label );
QPushButton* cancelButton = new QPushButton( "Cancel" );
layout->addWidget( cancelButton );
QTimer timer;
timer.setSingleShot( true );
QEventLoop loop;
connect( this, &RiaCloudConnector::tokenReady, &loop, &QEventLoop::quit );
connect( &timer, &QTimer::timeout, &loop, &QEventLoop::quit );
connect( cancelButton,
&QPushButton::clicked,
[&]()
{
RiaLogging::info( "Token request canceled by user." );
timer.stop();
loop.quit();
} );
connect( &dialog,
&QDialog::rejected,
[&]()
{
timer.stop();
loop.quit();
} );
requestToken();
timer.start( RiaCloudDefines::requestTokenTimeoutMillis() );
dialog.show();
loop.exec();
dialog.close();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------

View File

@ -49,6 +49,10 @@ public:
QString server() const;
QString requestTokenBlocking();
void requestTokenWithCancelButton();
void forceNewTokens();
bool isGranted() const;
public slots:
void requestToken();

View File

@ -108,7 +108,11 @@ void RiuWellImportWizard::downloadWellPaths( const QString& wellboreId )
void RiuWellImportWizard::resetAuthenticationCount()
{
m_firstTimeRequestingAuthentication = true;
m_osduConnector->requestToken();
if ( !m_osduConnector->isGranted() )
{
m_osduConnector->requestTokenWithCancelButton();
}
}
//--------------------------------------------------------------------------------------------------
@ -187,6 +191,8 @@ AuthenticationPage::AuthenticationPage( RiaOsduConnector* osduConnector, QWidget
setLayout( layout );
connect( osduConnector, SIGNAL( tokenReady( const QString& ) ), this, SLOT( accessOk() ) );
if ( osduConnector->isGranted() ) accessOk();
}
//--------------------------------------------------------------------------------------------------

View File

@ -1,4 +1,4 @@
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2024 Equinor ASA
//
@ -42,6 +42,9 @@ RimCloudDataSourceCollection::RimCloudDataSourceCollection()
{
CAF_PDM_InitObject( "Cloud Data" + RiaDefines::betaFeaturePostfix(), ":/Cloud.svg" );
CAF_PDM_InitFieldNoDefault( &m_authenticate, "Authenticate", "" );
caf::PdmUiPushButtonEditor::configureEditorLabelLeft( &m_authenticate );
CAF_PDM_InitFieldNoDefault( &m_sumoFieldName, "SumoFieldId", "Field Id" );
CAF_PDM_InitFieldNoDefault( &m_sumoCaseId, "SumoCaseId", "Case Id" );
m_sumoCaseId.uiCapability()->setUiEditorTypeName( caf::PdmUiTreeSelectionEditor::uiEditorTypeName() );
@ -102,6 +105,13 @@ void RimCloudDataSourceCollection::fieldChangedByUi( const caf::PdmFieldHandle*
{
if ( !m_sumoConnector ) return;
if ( changedField == &m_authenticate )
{
m_sumoConnector->requestTokenWithCancelButton();
m_authenticate = false;
}
if ( changedField == &m_sumoFieldName )
{
m_sumoCaseId = "";
@ -132,7 +142,7 @@ void RimCloudDataSourceCollection::fieldChangedByUi( const caf::PdmFieldHandle*
//--------------------------------------------------------------------------------------------------
QList<caf::PdmOptionItemInfo> RimCloudDataSourceCollection::calculateValueOptions( const caf::PdmFieldHandle* fieldNeedingOptions )
{
if ( !m_sumoConnector ) return {};
if ( !m_sumoConnector || !m_sumoConnector->isGranted() ) return {};
QList<caf::PdmOptionItemInfo> options;
if ( fieldNeedingOptions == &m_sumoFieldName )
@ -185,13 +195,26 @@ QList<caf::PdmOptionItemInfo> RimCloudDataSourceCollection::calculateValueOption
//--------------------------------------------------------------------------------------------------
void RimCloudDataSourceCollection::defineUiOrdering( QString uiConfigName, caf::PdmUiOrdering& uiOrdering )
{
caf::PdmUiOrdering::LayoutOptions layout = { .newRow = true, .totalColumnSpan = 3, .leftLabelColumnSpan = 1 };
uiOrdering.add( &m_sumoFieldName, layout );
uiOrdering.add( &m_sumoCaseId, layout );
uiOrdering.add( &m_sumoEnsembleNames, layout );
auto authGroup = uiOrdering.addNewGroup( "Authentication" );
authGroup->add( &m_authenticate );
uiOrdering.add( &m_addDataSources, layout );
uiOrdering.add( &m_addEnsembles, layout );
bool isGranted = m_sumoConnector && m_sumoConnector->isGranted();
QString text = "Authentication Status: ";
text += isGranted ? "<font color='#228B22'>✔ Granted</font>" : "<font color='#FFA500'>❌ Not Granted</font>";
m_authenticate.uiCapability()->setUiName( text );
if ( isGranted )
{
caf::PdmUiOrdering::LayoutOptions layout = { .newRow = true, .totalColumnSpan = 3, .leftLabelColumnSpan = 1 };
uiOrdering.add( &m_sumoFieldName, layout );
uiOrdering.add( &m_sumoCaseId, layout );
uiOrdering.add( &m_sumoEnsembleNames, layout );
uiOrdering.add( &m_addDataSources, layout );
uiOrdering.add( &m_addEnsembles, layout );
}
uiOrdering.skipRemainingFields();
}
//--------------------------------------------------------------------------------------------------
@ -201,6 +224,14 @@ void RimCloudDataSourceCollection::defineEditorAttribute( const caf::PdmFieldHan
QString uiConfigName,
caf::PdmUiEditorAttribute* attribute )
{
if ( field == &m_authenticate )
{
if ( auto attrib = dynamic_cast<caf::PdmUiPushButtonEditorAttribute*>( attribute ) )
{
attrib->m_buttonText = "Authenticate";
}
}
if ( field == &m_addDataSources )
{
if ( auto attrib = dynamic_cast<caf::PdmUiPushButtonEditorAttribute*>( attribute ) )

View File

@ -57,6 +57,7 @@ private:
void addEnsembles();
private:
caf::PdmField<bool> m_authenticate;
caf::PdmField<QString> m_sumoFieldName;
caf::PdmField<QString> m_sumoCaseId;
caf::PdmField<std::vector<QString>> m_sumoEnsembleNames;

View File

@ -185,7 +185,7 @@ bool RimWellPathCollection::loadDataAndUpdate()
{
if ( auto osduConnector = RiaApplication::instance()->makeOsduConnector() )
{
osduConnector->requestTokenBlocking();
osduConnector->requestTokenWithCancelButton();
}
}