1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-13 01:24:11 +02:00

Refactor ACLSystem into ACLRegistry, clean it up a lot

This commit is contained in:
jeff
2012-03-18 15:08:09 -04:00
parent 819623300d
commit 0c4ac584e4
9 changed files with 310 additions and 228 deletions

View File

@@ -162,7 +162,7 @@ set( libSources
sourcelist.cpp
pipeline.cpp
aclsystem.cpp
aclregistry.cpp
artist.cpp
artistplaylistinterface.cpp
album.cpp

View File

@@ -0,0 +1,189 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
*
* Tomahawk is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Tomahawk is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
*/
#include "aclregistry.h"
#include <QMutexLocker>
#include <QVariant>
#include "tomahawksettings.h"
#include "utils/logger.h"
ACLRegistry* ACLRegistry::s_instance = 0;
ACLRegistry*
ACLRegistry::instance()
{
if( !s_instance )
new ACLRegistry();
return s_instance;
}
ACLRegistry::ACLRegistry( QObject* parent )
: QObject( parent )
{
s_instance = this;
//qRegisterMetaType< QHash< QString, QHash< QString, ACL > > >("ACLRegistry::ACLCacheHash");
m_cache = TomahawkSettings::instance()->aclEntries();
}
ACLRegistry::~ACLRegistry()
{
}
ACLRegistry::ACL
ACLRegistry::isAuthorizedPeer( const QString& dbid, ACLRegistry::ACL globalType )
{
// qDebug() << Q_FUNC_INFO;
QMutexLocker locker( &m_cacheMutex );
qDebug() << "Current cache keys =" << m_cache.keys();
// qDebug() << "Looking up dbid";
if( m_cache.contains( dbid ) )
{
QVariantHash peerHash = m_cache[ dbid ].toHash();
if( peerHash.contains( "global" ) )
return ACLRegistry::ACL( peerHash[ "global" ].toInt() );
if ( globalType == ACLRegistry::NotFound )
return globalType;
peerHash[ "global" ] = int( globalType );
m_cache[ dbid ] = peerHash;
save();
return globalType;
}
//not found
if ( globalType == ACLRegistry::NotFound )
return globalType;
QVariantHash peerHash;
peerHash[ "global" ] = int( globalType );
m_cache[ dbid ] = peerHash;
save();
return globalType;
}
void
ACLRegistry::registerPeer( const QString& dbid, ACLRegistry::ACL globalType, const QString &username )
{
// qDebug() << Q_FUNC_INFO;
if( globalType == ACLRegistry::NotFound )
return;
QMutexLocker locker( &m_cacheMutex );
QVariantHash peerHash;
if( m_cache.contains( dbid ) )
peerHash = m_cache[ dbid ].toHash();
peerHash[ "global" ] = int( globalType );
if ( !username.isEmpty() )
{
if ( peerHash.contains( "usernames" ) )
{
if ( !peerHash[ "usernames" ].toStringList().contains( username ) )
peerHash[ "usernames" ] = peerHash[ "usernames" ].toStringList() + QStringList( username );
}
else
peerHash[ "usernames" ] = QStringList( username );
}
m_cache[ dbid ] = peerHash;
save();
}
QPair< QString, ACLRegistry::ACL >
ACLRegistry::isAuthorizedUser( const QString &username, ACLRegistry::ACL globalType )
{
// qDebug() << Q_FUNC_INFO;
QMutexLocker locker( &m_cacheMutex );
qDebug() << "Current cache keys =" << m_cache.keys();
foreach ( QString dbid, m_cache.keys() )
{
// qDebug() << "Looking up dbid";
QVariantHash peerHash = m_cache[ dbid ].toHash();
if ( !peerHash.contains( "usernames" ) )
continue;
if ( !peerHash[ "usernames" ].toStringList().contains( username ) )
continue;
if ( globalType != ACLRegistry::NotFound )
{
peerHash[ "global" ] = int( globalType );
m_cache[ dbid ] = peerHash;
save();
return QPair< QString, ACLRegistry::ACL >( dbid, globalType );
}
return QPair< QString, ACLRegistry::ACL >( dbid, ACLRegistry::ACL( peerHash[ "global" ].toInt() ) );
}
return QPair< QString, ACLRegistry::ACL >( QString(), ACLRegistry::NotFound );
}
// ACLRegistry::ACL
// ACLRegistry::isAuthorizedPath( const QString& dbid, const QString& path )
// {
// QMutexLocker locker( &m_cacheMutex );
//
// if( !m_cache.contains( dbid ) )
// return ACLRegistry::NotFound;
//
// QHash< QString, ACL > peerHash = m_cache[dbid];
// if( !peerHash.contains( path ) )
// {
// if( peerHash.contains( "global" ) )
// return peerHash["global"];
// else
// return ACLRegistry::Deny;
// }
// return peerHash[path];
// }
//
// void
// ACLRegistry::authorizePath( const QString& dbid, const QString& path, ACLRegistry::ACL type )
// {
// TomahawkSettings *s = TomahawkSettings::instance();
// if( !s->scannerPaths().contains( path ) )
// {
// qDebug() << "path selected is not in our scanner path!";
// return;
// }
// QMutexLocker locker( &m_cacheMutex );
// QHash< QString, ACLRegistry::ACL > peerHash;
// if ( m_cache.contains( dbid ) )
// peerHash = m_cache[dbid];
// peerHash[path] = type;
// m_cache[dbid] = peerHash;
// }
void
ACLRegistry::save()
{
TomahawkSettings::instance()->setAclEntries( m_cache );
}

View File

@@ -0,0 +1,102 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
*
* Tomahawk is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Tomahawk is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef TOMAHAWK_ACLREGISTRY_H
#define TOMAHAWK_ACLREGISTRY_H
#include <QObject>
#include <QString>
#include <QHash>
#include <QTimer>
#include <QMutex>
#include <QVariant>
#include "dllmacro.h"
class DLLEXPORT ACLRegistry : public QObject
{
Q_OBJECT
public:
static ACLRegistry* instance();
enum ACL {
Allow = 0,
Deny = 1,
NotFound = 2
};
ACLRegistry( QObject *parent = 0 );
~ACLRegistry();
/**
* @brief Checks if peer is authorized; optionally, can authorize peer with given type if not found
*
* @param dbid DBID of peer
* @param globalType Global ACL to store if peer not found; if ACLRegistry::NotFound, does not store the peer Defaults to ACLRegistry::NotFound.
* @return ACLRegistry::ACL
**/
ACLRegistry::ACL isAuthorizedPeer( const QString &dbid, ACLRegistry::ACL globalType = ACLRegistry::NotFound );
/**
* @brief Registers the global ACL value for this peer
*
* @param dbid DBID of peer
* @param globalType Global ACL to use for this peer. ACLRegistry::NotFound is invalid and will return immediately.
* @param username If not empty, will store the given username along with the new ACL value. Defaults to QString().
* @return void
**/
void registerPeer( const QString &dbid, ACLRegistry::ACL globalType, const QString &username = QString() );
/**
* @brief Checks if peer is authorized, using the username. Optionally, can change authorization of the peer, but only if the peer is found.
*
* @param username Username for the peer
* @param globalType Global ACL to store if peer is found; if ACLRegistry::NotFound, does not change the ACL. Defaults to ACLRegistry::NotFound.
* @return QPair< QString, ACLRegistry::ACL >
**/
QPair< QString, ACLRegistry::ACL > isAuthorizedUser( const QString &username, ACLRegistry::ACL globalType = ACLRegistry::NotFound );
/**
* @brief Registers an alias for a known peer. If you do not know the DBID, you can retrieve it via isAuthorizedUser first.
*
* @param dbid DBID of peer
* @param username Username of the peer to be added to the entry
* @return void
**/
void registerAlias( const QString &dbid, QString username );
// ACLRegistry::ACL isAuthorizedPath( const QString &dbid, const QString &path );
// void authorizePath( const QString &dbid, const QString &path, ACLRegistry::ACL type );
private:
/**
* @brief Saves the cache.
*
* @return void
**/
void save();
QVariantHash m_cache;
QMutex m_cacheMutex;
static ACLRegistry* s_instance;
};
#endif // TOMAHAWK_ACLREGISTRY_H

View File

@@ -1,152 +0,0 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
*
* Tomahawk is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Tomahawk is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
*/
#include "aclsystem.h"
#include <QMutexLocker>
#include <QVariant>
#include "tomahawksettings.h"
#include "utils/logger.h"
ACLSystem* ACLSystem::s_instance = 0;
ACLSystem*
ACLSystem::instance()
{
if( !s_instance )
new ACLSystem();
return s_instance;
}
ACLSystem::ACLSystem( QObject* parent )
: QObject( parent ),
m_saveTimer( this )
{
s_instance = this;
//qRegisterMetaType< QHash< QString, QHash< QString, ACL > > >("ACLSystem::ACLCacheHash");
QStringList savedEntries = TomahawkSettings::instance()->aclEntries();
if( !savedEntries.empty() && savedEntries.size() % 3 == 0 )
{
int index = 0;
while( index < savedEntries.length() )
{
if( !m_cache.contains( savedEntries.at( index ) ) )
m_cache[savedEntries.at( index ) ] = QHash< QString, ACL >();
m_cache[savedEntries.at( index )][savedEntries.at( index + 1 )] = (ACL)(savedEntries.at( index + 2 ).toInt() );
index += 3;
}
}
m_saveTimer.setSingleShot( false );
m_saveTimer.setInterval( 60000 );
connect( &m_saveTimer, SIGNAL( timeout() ), this, SLOT( saveTimerFired() ) );
m_saveTimer.start();
}
ACLSystem::~ACLSystem()
{
m_saveTimer.stop();
saveTimerFired();
}
ACLSystem::ACL
ACLSystem::isAuthorizedUser( const QString& dbid )
{
// qDebug() << Q_FUNC_INFO;
QMutexLocker locker( &m_cacheMutex );
qDebug() << "Current cache keys =" << m_cache.keys();
// qDebug() << "Looking up dbid";
if( !m_cache.contains( dbid ) )
return ACLSystem::NotFound;
else
{
QHash< QString, ACL > peerHash = m_cache[dbid];
if( peerHash.contains( "global" ) )
return peerHash["global"];
return ACLSystem::NotFound;
}
}
void
ACLSystem::authorizeUser( const QString& dbid, ACLSystem::ACL globalType )
{
// qDebug() << Q_FUNC_INFO;
if( globalType == ACLSystem::NotFound )
return;
QMutexLocker locker( &m_cacheMutex );
QHash< QString, ACL > peerHash;
if( m_cache.contains( dbid ) )
peerHash = m_cache[dbid];
peerHash["global"] = globalType;
m_cache[dbid] = peerHash;
}
ACLSystem::ACL
ACLSystem::isAuthorizedPath( const QString& dbid, const QString& path )
{
QMutexLocker locker( &m_cacheMutex );
if( !m_cache.contains( dbid ) )
return ACLSystem::NotFound;
QHash< QString, ACL > peerHash = m_cache[dbid];
if( !peerHash.contains( path ) )
{
if( peerHash.contains( "global" ) )
return peerHash["global"];
else
return ACLSystem::Deny;
}
return peerHash[path];
}
void
ACLSystem::authorizePath( const QString& dbid, const QString& path, ACLSystem::ACL type )
{
TomahawkSettings *s = TomahawkSettings::instance();
if( !s->scannerPaths().contains( path ) )
{
qDebug() << "path selected is not in our scanner path!";
return;
}
QMutexLocker locker( &m_cacheMutex );
QHash< QString, ACLSystem::ACL > peerHash;
if ( m_cache.contains( dbid ) )
peerHash = m_cache[dbid];
peerHash[path] = type;
m_cache[dbid] = peerHash;
}
void
ACLSystem::saveTimerFired()
{
QStringList saveCache;
foreach( QString dbid, m_cache.keys() )
{
foreach( QString path, m_cache[dbid].keys() )
saveCache << dbid << path << QString::number( (int)(m_cache[dbid][path]) );
}
TomahawkSettings::instance()->setAclEntries( saveCache );
}

View File

@@ -1,64 +0,0 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
*
* Tomahawk is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Tomahawk is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef TOMAHAWK_ACLSYSTEM_H
#define TOMAHAWK_ACLSYSTEM_H
#include <QObject>
#include <QString>
#include <QHash>
#include <QTimer>
#include <QMutex>
#include "dllmacro.h"
class DLLEXPORT ACLSystem : public QObject
{
Q_OBJECT
public:
static ACLSystem* instance();
enum ACL {
Allow = 0,
Deny = 1,
NotFound = 2
};
ACLSystem( QObject *parent = 0 );
~ACLSystem();
ACL isAuthorizedUser( const QString &dbid );
void authorizeUser( const QString &dbid, ACL globalType );
ACL isAuthorizedPath( const QString &dbid, const QString &path );
void authorizePath( const QString &dbid, const QString &path, ACL type );
private slots:
void saveTimerFired();
private:
QHash< QString, QHash< QString, ACL > > m_cache;
QTimer m_saveTimer;
QMutex m_cacheMutex;
static ACLSystem* s_instance;
};
#endif // TOMAHAWK_ACLSYSTEM_H

View File

@@ -38,7 +38,7 @@
#include "portfwdthread.h"
#include "tomahawksettings.h"
#include <aclsystem.h>
#include "libtomahawk/aclregistry.h"
#include "utils/tomahawkutils.h"
#include "utils/logger.h"
@@ -64,7 +64,7 @@ Servent::Servent( QObject* parent )
s_instance = this;
m_lanHack = qApp->arguments().contains( "--lanhack" );
new ACLSystem( this );
ACLRegistry::instance();
setProxy( QNetworkProxy::NoProxy );
{
@@ -89,6 +89,7 @@ Servent::Servent( QObject* parent )
Servent::~Servent()
{
delete ACLRegistry::instance();
delete m_portfwd;
}
@@ -696,9 +697,11 @@ bool
Servent::checkACL( const Connection* conn, const QString &nodeid, bool showDialog ) const
{
Q_UNUSED( conn );
Q_UNUSED( nodeid );
Q_UNUSED( showDialog );
tDebug( LOGVERBOSE ) << "Checking ACLs";
/*
tDebug( LOGVERBOSE ) << "Checking ACLs";
ACLSystem* aclSystem = ACLSystem::instance();
ACLSystem::ACL peerStatus = aclSystem->isAuthorizedUser( nodeid );
if( peerStatus == ACLSystem::Deny )
@@ -706,7 +709,7 @@ Servent::checkACL( const Connection* conn, const QString &nodeid, bool showDialo
//FIXME: Actually enable it when it makes sense
//FIXME: needs refactoring because it depends on QtGui and the servent is part of libtomahawk-core
/*
return true;
if( peerStatus == ACLSystem::NotFound )
{

View File

@@ -291,7 +291,7 @@ TomahawkSettings::doUpgrade( int oldVersion, int newVersion )
beginGroup( "accounts/" + accountKey );
setValue( "enabled", enabledSip.contains( sipPlugin ) == true );
setValue( "autoconnect", true );
setValue( "acl", QVariantMap() );
setValue( "acl", QVariantHash() );
setValue( "types", QStringList() << "SipType" );
endGroup();
accounts << accountKey;
@@ -627,15 +627,19 @@ TomahawkSettings::setProxyDns( bool lookupViaProxy )
}
QStringList
QVariantHash
TomahawkSettings::aclEntries() const
{
return value( "acl/entries", QStringList() ).toStringList();
QVariant retVal = value( "acl/entries", QVariantHash() );
if ( retVal.isValid() && retVal.canConvert< QVariantHash >() )
return retVal.toHash();
return QVariantHash();
}
void
TomahawkSettings::setAclEntries( const QStringList &entries )
TomahawkSettings::setAclEntries( const QVariantHash &entries )
{
setValue( "acl/entries", entries );
}

View File

@@ -162,8 +162,8 @@ public:
void setProxyDns( bool lookupViaProxy );
/// ACL settings
QStringList aclEntries() const;
void setAclEntries( const QStringList &entries );
QVariantHash aclEntries() const;
void setAclEntries( const QVariantHash &entries );
/// XMPP Component Settings
QString xmppBotServer() const;

View File

@@ -314,7 +314,7 @@ NetworkProxyFactory::NetworkProxyFactory( const NetworkProxyFactory& other )
QList< QNetworkProxy >
NetworkProxyFactory::queryProxy( const QNetworkProxyQuery& query )
{
tDebug() << Q_FUNC_INFO << "query hostname is " << query.peerHostName();
//tDebug() << Q_FUNC_INFO << "query hostname is " << query.peerHostName();
QList< QNetworkProxy > proxies;
QString hostname = query.peerHostName();
s_noProxyHostsMutex.lock();