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
Expand Up @@ -50,9 +50,10 @@ class QgsDataProvider : QObject
/** /**
* Get the data source specification. This may be a path or database * Get the data source specification. This may be a path or database
* connection string * connection string
* @param expandAuthConfig Whether to expand any assigned authentication configuration
* @return data source specification * @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
Expand Up @@ -21,10 +21,10 @@ class QgsDataSourceURI
QgsDataSourceURI( QString uri ); QgsDataSourceURI( QString uri );


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


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


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


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


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


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

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


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


QString authConfigId() const;
QString username() const; QString username() const;
QString schema() const; QString schema() const;
QString table() const; QString table() const;
Expand Down
14 changes: 13 additions & 1 deletion src/app/qgisapp.cpp
Expand Up @@ -8204,10 +8204,22 @@ QgsVectorLayer* QgisApp::addVectorLayer( QString vectorLayerPath, QString baseNa
+ " with baseName of " + baseName + " with baseName of " + baseName
+ " and providerKey of " + providerKey ); + " 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 // create the layer
QgsVectorLayer *layer = new QgsVectorLayer( vectorLayerPath, baseName, providerKey, false ); QgsVectorLayer *layer = new QgsVectorLayer( vectorLayerPath, baseName, providerKey, false );


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


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


typedef int dataCapabilities_t(); 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 * Get the data source specification. This may be a path or database
* connection string * connection string
* @param expandAuthConfig Whether to expand any assigned authentication configuration
* @return data source specification * @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
Expand Up @@ -17,6 +17,7 @@
***************************************************************************/ ***************************************************************************/


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


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


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

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


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


Expand Down Expand Up @@ -493,12 +503,27 @@ QString QgsDataSourceURI::connectionInfo() const
connectionItems << "sslmode=prefer"; connectionItems << "sslmode=prefer";
#endif #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( " " ); return connectionItems.join( " " );
} }


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


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


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


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


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

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


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


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


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


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


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


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

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


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


QString authConfigId() const;
QString username() const; QString username() const;
QString schema() const; QString schema() const;
QString table() const; QString table() const;
Expand Down Expand Up @@ -178,6 +184,8 @@ class CORE_EXPORT QgsDataSourceURI
QString mGeometryColumn; QString mGeometryColumn;
//! SQL query or where clause used to limit features returned from the layer //! SQL query or where clause used to limit features returned from the layer
QString mSql; QString mSql;
//! authentication configuration ID
QString mAuthConfigId;
//! username //! username
QString mUsername; QString mUsername;
//! password //! password
Expand Down
9 changes: 9 additions & 0 deletions src/core/qgsmaplayer.cpp
Expand Up @@ -34,6 +34,7 @@
#include "qgscoordinatereferencesystem.h" #include "qgscoordinatereferencesystem.h"
#include "qgsdatasourceuri.h" #include "qgsdatasourceuri.h"
#include "qgslogger.h" #include "qgslogger.h"
#include "qgsauthmanager.h"
#include "qgsmaplayer.h" #include "qgsmaplayer.h"
#include "qgsmaplayerlegend.h" #include "qgsmaplayerlegend.h"
#include "qgsmaplayerstylemanager.h" #include "qgsmaplayerstylemanager.h"
Expand Down Expand Up @@ -182,6 +183,14 @@ bool QgsMapLayer::readLayerXML( const QDomElement& layerElement )
mne = mnl.toElement(); mne = mnl.toElement();
mDataSource = mne.text(); 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 // TODO: this should go to providers
// see also QgsProject::createEmbeddedLayer // see also QgsProject::createEmbeddedLayer
if ( provider == "spatialite" ) if ( provider == "spatialite" )
Expand Down
4 changes: 3 additions & 1 deletion src/providers/memory/qgsmemoryprovider.cpp
Expand Up @@ -175,8 +175,10 @@ QgsAbstractFeatureSource* QgsMemoryProvider::featureSource() const
return new QgsMemoryFeatureSource( this ); return new QgsMemoryFeatureSource( this );
} }


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

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

0 comments on commit 95214e9

Please sign in to comment.