Skip to content

Commit

Permalink
[auth system] Core data source URI integration
Browse files Browse the repository at this point in the history
  • Loading branch information
dakcarto committed Sep 21, 2015
1 parent e65aa99 commit 95214e9
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 19 deletions.
3 changes: 2 additions & 1 deletion python/core/qgsdataprovider.sip
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ class QgsDataProvider : QObject
/**
* Get the data source specification. This may be a path or database
* connection string
* @param expandAuthConfig Whether to expand any assigned authentication configuration
* @return data source specification
*/
virtual QString dataSourceUri() const;
virtual QString dataSourceUri( bool expandAuthConfig = true ) const;


/**
Expand Down
14 changes: 10 additions & 4 deletions python/core/qgsdatasourceuri.sip
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ class QgsDataSourceURI
QgsDataSourceURI( QString uri );

//! return connection part of URI
QString connectionInfo() const;
QString connectionInfo( bool expandAuthConfig = true ) const;

//! return complete uri
QString uri() const;
QString uri( bool expandAuthConfig = true ) const;

//! return complete encoded uri (generic mode)
QByteArray encodedUri() const;
Expand Down Expand Up @@ -59,14 +59,16 @@ class QgsDataSourceURI
const QString& aDatabase,
const QString& aUsername,
const QString& aPassword,
SSLmode sslmode = SSLprefer );
SSLmode sslmode = SSLprefer,
const QString& authConfigId = QString() );

//! Set all connection related members at once (for the service case)
void setConnection( const QString& aService,
const QString& aDatabase,
const QString& aUsername,
const QString& aPassword,
SSLmode sslmode = SSLprefer );
SSLmode sslmode = SSLprefer,
const QString& authConfigId = QString() );

//! Set database
void setDatabase( const QString &database );
Expand All @@ -78,6 +80,9 @@ class QgsDataSourceURI
const QString& aSql = QString(),
const QString& aKeyColumn = QString() );

//! set authentication configuration ID
void setAuthConfigId( const QString& authcfg );

//! set username
void setUsername( QString username );

Expand All @@ -87,6 +92,7 @@ class QgsDataSourceURI
//! Removes password element from uris
static QString removePassword( const QString& aUri );

QString authConfigId() const;
QString username() const;
QString schema() const;
QString table() const;
Expand Down
14 changes: 13 additions & 1 deletion src/app/qgisapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8204,10 +8204,22 @@ QgsVectorLayer* QgisApp::addVectorLayer( QString vectorLayerPath, QString baseNa
+ " with baseName of " + baseName
+ " and providerKey of " + providerKey );

// if the layer needs authentication, ensure the master password is set
bool authok = true;
QRegExp rx( "authcfg=([a-z]|[0-9]){7}" );
if ( rx.indexIn( vectorLayerPath ) != -1 )
{
authok = false;
if ( !QgsAuthGuiUtils::isDisabled( messageBar(), messageTimeout() ) )
{
authok = QgsAuthManager::instance()->setMasterPassword( true );
}
}

// create the layer
QgsVectorLayer *layer = new QgsVectorLayer( vectorLayerPath, baseName, providerKey, false );

if ( layer && layer->isValid() )
if ( authok && layer && layer->isValid() )
{
QStringList sublayers = layer->dataProvider()->subLayers();
QgsDebugMsg( QString( "got valid layer with %1 sublayers" ).arg( sublayers.count() ) );
Expand Down
17 changes: 15 additions & 2 deletions src/core/qgsdataprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <QStringList>

//#include "qgsdataitem.h"
#include "qgsdatasourceuri.h"
#include "qgserror.h"

typedef int dataCapabilities_t();
Expand Down Expand Up @@ -91,11 +92,23 @@ class CORE_EXPORT QgsDataProvider : public QObject
/**
* Get the data source specification. This may be a path or database
* connection string
* @param expandAuthConfig Whether to expand any assigned authentication configuration
* @return data source specification
* @note The default authentication configuration expansion is FALSE. This keeps credentials
* out of layer data source URIs and project files. Expansion should be specifically done
* only when needed within a provider
*/
virtual QString dataSourceUri() const
virtual QString dataSourceUri( bool expandAuthConfig = false ) const
{
return mDataSourceURI;
if ( expandAuthConfig )
{
QgsDataSourceURI uri( mDataSourceURI );
return uri.uri( expandAuthConfig );
}
else
{
return mDataSourceURI;
}
}


Expand Down
44 changes: 39 additions & 5 deletions src/core/qgsdatasourceuri.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
***************************************************************************/

#include "qgsdatasourceuri.h"
#include "qgsauthmanager.h"
#include "qgslogger.h"
#include "qgswkbtypes.h"

Expand Down Expand Up @@ -148,6 +149,10 @@ QgsDataSourceURI::QgsDataSourceURI( QString uri )
{
mService = pval;
}
else if ( pname == "authcfg" )
{
mAuthConfigId = pval;
}
else if ( pname == "user" )
{
mUsername = pval;
Expand Down Expand Up @@ -253,6 +258,11 @@ QString QgsDataSourceURI::removePassword( const QString& aUri )
return safeName;
}

QString QgsDataSourceURI::authConfigId() const
{
return mAuthConfigId;
}

QString QgsDataSourceURI::username() const
{
return mUsername;
Expand Down Expand Up @@ -448,7 +458,7 @@ QString QgsDataSourceURI::getValue( const QString &uri, int &i )
return pval;
}

QString QgsDataSourceURI::connectionInfo() const
QString QgsDataSourceURI::connectionInfo( bool expandAuthConfig ) const
{
QStringList connectionItems;

Expand Down Expand Up @@ -493,12 +503,27 @@ QString QgsDataSourceURI::connectionInfo() const
connectionItems << "sslmode=prefer";
#endif

if ( !mAuthConfigId.isEmpty() )
{
if ( expandAuthConfig )
{
if ( !QgsAuthManager::instance()->updateDataSourceUriItems( connectionItems, mAuthConfigId ) )
{
QgsDebugMsg( QString( "Data source URI FAILED to update via loading configuration ID '%1'" ).arg( mAuthConfigId ) );
}
}
else
{
connectionItems << "authcfg=" + mAuthConfigId;
}
}

return connectionItems.join( " " );
}

QString QgsDataSourceURI::uri() const
QString QgsDataSourceURI::uri( bool expandAuthConfig ) const
{
QString theUri = connectionInfo();
QString theUri = connectionInfo( expandAuthConfig );

if ( !mKeyColumn.isEmpty() )
{
Expand Down Expand Up @@ -595,27 +620,31 @@ void QgsDataSourceURI::setConnection( const QString &host,
const QString &database,
const QString &username,
const QString &password,
SSLmode sslmode )
SSLmode sslmode,
const QString &authConfigId )
{
mHost = host;
mDatabase = database;
mPort = port;
mUsername = username;
mPassword = password;
mSSLmode = sslmode;
mAuthConfigId = authConfigId;
}

void QgsDataSourceURI::setConnection( const QString &service,
const QString &database,
const QString &username,
const QString &password,
SSLmode sslmode )
SSLmode sslmode,
const QString &authConfigId )
{
mService = service;
mDatabase = database;
mUsername = username;
mPassword = password;
mSSLmode = sslmode;
mAuthConfigId = authConfigId;
}

void QgsDataSourceURI::setDataSource( const QString &schema,
Expand All @@ -631,6 +660,11 @@ void QgsDataSourceURI::setDataSource( const QString &schema,
mKeyColumn = keyColumn;
}

void QgsDataSourceURI::setAuthConfigId( const QString &authcfg )
{
mAuthConfigId = authcfg;
}

void QgsDataSourceURI::setDatabase( const QString &database )
{
mDatabase = database;
Expand Down
16 changes: 12 additions & 4 deletions src/core/qgsdatasourceuri.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ class CORE_EXPORT QgsDataSourceURI
QgsDataSourceURI( const QByteArray & uri );

//! return connection part of URI
QString connectionInfo() const;
QString connectionInfo( bool expandAuthConfig = true ) const;

//! return complete uri
QString uri() const;
QString uri( bool expandAuthConfig = true ) const;

//! return complete encoded uri (generic mode)
QByteArray encodedUri() const;
Expand Down Expand Up @@ -89,14 +89,16 @@ class CORE_EXPORT QgsDataSourceURI
const QString& aDatabase,
const QString& aUsername,
const QString& aPassword,
SSLmode sslmode = SSLprefer );
SSLmode sslmode = SSLprefer,
const QString& authConfigId = QString() );

//! Set all connection related members at once (for the service case)
void setConnection( const QString& aService,
const QString& aDatabase,
const QString& aUsername,
const QString& aPassword,
SSLmode sslmode = SSLprefer );
SSLmode sslmode = SSLprefer,
const QString& authConfigId = QString() );

//! Set database
void setDatabase( const QString &database );
Expand All @@ -108,6 +110,9 @@ class CORE_EXPORT QgsDataSourceURI
const QString& aSql = QString(),
const QString& aKeyColumn = QString() );

//! set authentication configuration ID
void setAuthConfigId( const QString& authcfg );

//! set username
void setUsername( QString username );

Expand All @@ -117,6 +122,7 @@ class CORE_EXPORT QgsDataSourceURI
//! Removes password element from uris
static QString removePassword( const QString& aUri );

QString authConfigId() const;
QString username() const;
QString schema() const;
QString table() const;
Expand Down Expand Up @@ -178,6 +184,8 @@ class CORE_EXPORT QgsDataSourceURI
QString mGeometryColumn;
//! SQL query or where clause used to limit features returned from the layer
QString mSql;
//! authentication configuration ID
QString mAuthConfigId;
//! username
QString mUsername;
//! password
Expand Down
9 changes: 9 additions & 0 deletions src/core/qgsmaplayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "qgscoordinatereferencesystem.h"
#include "qgsdatasourceuri.h"
#include "qgslogger.h"
#include "qgsauthmanager.h"
#include "qgsmaplayer.h"
#include "qgsmaplayerlegend.h"
#include "qgsmaplayerstylemanager.h"
Expand Down Expand Up @@ -182,6 +183,14 @@ bool QgsMapLayer::readLayerXML( const QDomElement& layerElement )
mne = mnl.toElement();
mDataSource = mne.text();

// if the layer needs authentication, ensure the master password is set
QRegExp rx( "authcfg=([a-z]|[0-9]){7}" );
if (( rx.indexIn( mDataSource ) != -1 )
&& !QgsAuthManager::instance()->setMasterPassword( true ) )
{
return false;
}

// TODO: this should go to providers
// see also QgsProject::createEmbeddedLayer
if ( provider == "spatialite" )
Expand Down
4 changes: 3 additions & 1 deletion src/providers/memory/qgsmemoryprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,10 @@ QgsAbstractFeatureSource* QgsMemoryProvider::featureSource() const
return new QgsMemoryFeatureSource( this );
}

QString QgsMemoryProvider::dataSourceUri() const
QString QgsMemoryProvider::dataSourceUri( bool expandAuthConfig ) const
{
Q_UNUSED( expandAuthConfig )

QUrl uri( "memory" );
QString geometry;
switch ( mWkbType )
Expand Down
2 changes: 1 addition & 1 deletion src/providers/memory/qgsmemoryprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class QgsMemoryProvider : public QgsVectorDataProvider
* Returns the permanent storage type for this layer as a friendly name.
*/

virtual QString dataSourceUri() const override;
virtual QString dataSourceUri( bool expandAuthConfig = true ) const override;

/**
* Returns the permanent storage type for this layer as a friendly name.
Expand Down

0 comments on commit 95214e9

Please sign in to comment.