PageRenderTime 30ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/libtomahawk/aclsystem.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 152 lines | 110 code | 21 blank | 21 comment | 15 complexity | b3b4bf68f340c37ba8b769adacf17eb3 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, GPL-3.0, GPL-2.0
  1. /* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2. *
  3. * Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
  4. *
  5. * Tomahawk is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * Tomahawk is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "aclsystem.h"
  19. #include <QMutexLocker>
  20. #include <QVariant>
  21. #include "tomahawksettings.h"
  22. #include "utils/logger.h"
  23. ACLSystem* ACLSystem::s_instance = 0;
  24. ACLSystem*
  25. ACLSystem::instance()
  26. {
  27. if( !s_instance )
  28. new ACLSystem();
  29. return s_instance;
  30. }
  31. ACLSystem::ACLSystem( QObject* parent )
  32. : QObject( parent ),
  33. m_saveTimer( this )
  34. {
  35. s_instance = this;
  36. //qRegisterMetaType< QHash< QString, QHash< QString, ACL > > >("ACLSystem::ACLCacheHash");
  37. QStringList savedEntries = TomahawkSettings::instance()->aclEntries();
  38. if( !savedEntries.empty() && savedEntries.size() % 3 == 0 )
  39. {
  40. int index = 0;
  41. while( index < savedEntries.length() )
  42. {
  43. if( !m_cache.contains( savedEntries.at( index ) ) )
  44. m_cache[savedEntries.at( index ) ] = QHash< QString, ACL >();
  45. m_cache[savedEntries.at( index )][savedEntries.at( index + 1 )] = (ACL)(savedEntries.at( index + 2 ).toInt() );
  46. index += 3;
  47. }
  48. }
  49. m_saveTimer.setSingleShot( false );
  50. m_saveTimer.setInterval( 60000 );
  51. connect( &m_saveTimer, SIGNAL( timeout() ), this, SLOT( saveTimerFired() ) );
  52. m_saveTimer.start();
  53. }
  54. ACLSystem::~ACLSystem()
  55. {
  56. m_saveTimer.stop();
  57. saveTimerFired();
  58. }
  59. ACLSystem::ACL
  60. ACLSystem::isAuthorizedUser( const QString& dbid )
  61. {
  62. // qDebug() << Q_FUNC_INFO;
  63. QMutexLocker locker( &m_cacheMutex );
  64. qDebug() << "Current cache keys =" << m_cache.keys();
  65. // qDebug() << "Looking up dbid";
  66. if( !m_cache.contains( dbid ) )
  67. return ACLSystem::NotFound;
  68. else
  69. {
  70. QHash< QString, ACL > peerHash = m_cache[dbid];
  71. if( peerHash.contains( "global" ) )
  72. return peerHash["global"];
  73. return ACLSystem::NotFound;
  74. }
  75. }
  76. void
  77. ACLSystem::authorizeUser( const QString& dbid, ACLSystem::ACL globalType )
  78. {
  79. // qDebug() << Q_FUNC_INFO;
  80. if( globalType == ACLSystem::NotFound )
  81. return;
  82. QMutexLocker locker( &m_cacheMutex );
  83. QHash< QString, ACL > peerHash;
  84. if( m_cache.contains( dbid ) )
  85. peerHash = m_cache[dbid];
  86. peerHash["global"] = globalType;
  87. m_cache[dbid] = peerHash;
  88. }
  89. ACLSystem::ACL
  90. ACLSystem::isAuthorizedPath( const QString& dbid, const QString& path )
  91. {
  92. QMutexLocker locker( &m_cacheMutex );
  93. if( !m_cache.contains( dbid ) )
  94. return ACLSystem::NotFound;
  95. QHash< QString, ACL > peerHash = m_cache[dbid];
  96. if( !peerHash.contains( path ) )
  97. {
  98. if( peerHash.contains( "global" ) )
  99. return peerHash["global"];
  100. else
  101. return ACLSystem::Deny;
  102. }
  103. return peerHash[path];
  104. }
  105. void
  106. ACLSystem::authorizePath( const QString& dbid, const QString& path, ACLSystem::ACL type )
  107. {
  108. TomahawkSettings *s = TomahawkSettings::instance();
  109. if( !s->scannerPaths().contains( path ) )
  110. {
  111. qDebug() << "path selected is not in our scanner path!";
  112. return;
  113. }
  114. QMutexLocker locker( &m_cacheMutex );
  115. QHash< QString, ACLSystem::ACL > peerHash;
  116. if ( m_cache.contains( dbid ) )
  117. peerHash = m_cache[dbid];
  118. peerHash[path] = type;
  119. m_cache[dbid] = peerHash;
  120. }
  121. void
  122. ACLSystem::saveTimerFired()
  123. {
  124. QStringList saveCache;
  125. foreach( QString dbid, m_cache.keys() )
  126. {
  127. foreach( QString path, m_cache[dbid].keys() )
  128. saveCache << dbid << path << QString::number( (int)(m_cache[dbid][path]) );
  129. }
  130. TomahawkSettings::instance()->setAclEntries( saveCache );
  131. }