Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "permissionsmanager.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// TnzBase includes
Toshihiro Shimizu 890ddd
#include "tenv.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// TnzCore includes
Toshihiro Shimizu 890ddd
#include "tsystem.h"
Toshihiro Shimizu 890ddd
#include "tstream.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=========================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace {
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
class User {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  std::string m_name;
Shinya Kitaoka 120a6e
  std::vector<std::string> m_svnUsernames;</std::string>
Shinya Kitaoka 120a6e
  std::vector<std::string> m_svnPasswords;</std::string>
Shinya Kitaoka 120a6e
  User(std::string name) : m_name(name) {}
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  void addSvnUsername(std::string username) {
Shinya Kitaoka 120a6e
    m_svnUsernames.push_back(username);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  void addSvnPassword(std::string password) {
Shinya Kitaoka 120a6e
    m_svnPasswords.push_back(password);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  std::string getSvnUsername(int index) {
Shinya Kitaoka 120a6e
    if (index < 0 || index >= (int)m_svnUsernames.size()) return "";
Shinya Kitaoka 120a6e
    return m_svnUsernames.at(index);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  std::string getSvnPassword(int index) {
Shinya Kitaoka 120a6e
    if (index < 0 || index >= (int)m_svnPasswords.size()) return "";
Shinya Kitaoka 120a6e
    return m_svnPasswords.at(index);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
}  // namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
class PermissionsManager::Imp {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  std::map<std::string, *="" user=""> m_users;</std::string,>
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // utente corrente
Shinya Kitaoka 120a6e
  User *m_user;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  Imp() : m_user(0) {
Shinya Kitaoka 120a6e
    loadPermissions();
Shinya Kitaoka 120a6e
    m_user              = findUser(TSystem::getUserName().toStdString(), false);
Shinya Kitaoka 120a6e
    if (!m_user) m_user = findUser("guest", false);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  ~Imp() {
Shinya Kitaoka 120a6e
    for (std::map<std::string, *="" user="">::iterator u = m_users.begin();</std::string,>
Shinya Kitaoka 120a6e
         u != m_users.end(); ++u)
Shinya Kitaoka 120a6e
      delete u->second;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  User *findUser(std::string userName, bool create = true) {
Shinya Kitaoka 120a6e
    std::map<std::string, *="" user="">::iterator i = m_users.find(userName);</std::string,>
Shinya Kitaoka 120a6e
    if (i != m_users.end()) return i->second;
Shinya Kitaoka 120a6e
    if (!create) return 0;
Shinya Kitaoka 120a6e
    User *user        = new User(userName);
Shinya Kitaoka 120a6e
    m_users[userName] = user;
Shinya Kitaoka 120a6e
    return user;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  std::string getSVNUserName(int index) {
Shinya Kitaoka 120a6e
    User *user      = findUser(TSystem::getUserName().toStdString(), false);
Shinya Kitaoka 120a6e
    if (!user) user = findUser("guest", false);
Shinya Kitaoka 120a6e
    if (!user) return "";
Shinya Kitaoka 120a6e
    return user->getSvnUsername(index);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  std::string getSVNPassword(int index) {
Shinya Kitaoka 120a6e
    User *user      = findUser(TSystem::getUserName().toStdString(), false);
Shinya Kitaoka 120a6e
    if (!user) user = findUser("guest", false);
Shinya Kitaoka 120a6e
    if (!user) return "";
Shinya Kitaoka 120a6e
    return user->getSvnPassword(index);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TFilePath getPermissionFile() {
Shinya Kitaoka 120a6e
    return TEnv::getConfigDir() + "permissions.xml";
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  void loadPermissions();
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void PermissionsManager::Imp::loadPermissions() {
Shinya Kitaoka 120a6e
  TFilePath fp = getPermissionFile();
Shinya Kitaoka 120a6e
  TIStream is(fp);
Shinya Kitaoka 120a6e
  if (!is) return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  std::string tagName;
Shinya Kitaoka 120a6e
  if (!is.matchTag(tagName) || tagName != "permissions") return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  while (is.matchTag(tagName))
Shinya Kitaoka 120a6e
    if (tagName == "users") {
Shinya Kitaoka 120a6e
      while (is.matchTag(tagName)) {
Shinya Kitaoka 120a6e
        if (tagName != "user") return;
Shinya Kitaoka 120a6e
        std::string userName;
Shinya Kitaoka 120a6e
        is.getTagParam("name", userName);
Shinya Kitaoka 120a6e
        if (userName == "") return;
Shinya Kitaoka 120a6e
        User *user = findUser(userName);
Shinya Kitaoka 120a6e
        while (is.matchTag(tagName)) {
Shinya Kitaoka 120a6e
          if (tagName == "roles") {
Shinya Kitaoka 120a6e
            //  <roles> is no longer used</roles>
Shinya Kitaoka 120a6e
            is.skipCurrentTag();
Shinya Kitaoka 120a6e
          } else if (tagName == "svn") {
Shinya Kitaoka 120a6e
            std::string name;
Shinya Kitaoka 120a6e
            is.getTagParam("name", name);
Shinya Kitaoka 120a6e
            std::string password;
Shinya Kitaoka 120a6e
            is.getTagParam("password", password);
Shinya Kitaoka 120a6e
            user->addSvnUsername(name);
Shinya Kitaoka 120a6e
            user->addSvnPassword(password);
Shinya Kitaoka 120a6e
          } else
Shinya Kitaoka 120a6e
            return;
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
        if (!is.matchEndTag()) return;  // 
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
      if (!is.matchEndTag()) return;  // 
Shinya Kitaoka 120a6e
    } else if (tagName == "roles") {
Shinya Kitaoka 120a6e
      //  <roles> is no longer used</roles>
Shinya Kitaoka 120a6e
      is.skipCurrentTag();
Shinya Kitaoka 120a6e
    } else
Shinya Kitaoka 120a6e
      return;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=========================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
PermissionsManager::PermissionsManager() : m_imp(new Imp()) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
PermissionsManager::~PermissionsManager() {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
PermissionsManager *PermissionsManager::instance() {
Shinya Kitaoka 120a6e
  static PermissionsManager _instance;
Shinya Kitaoka 120a6e
  return &_instance;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
std::string PermissionsManager::getSVNUserName(int index) const {
Shinya Kitaoka 120a6e
  return m_imp->getSVNUserName(index);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
std::string PermissionsManager::getSVNPassword(int index) const {
Shinya Kitaoka 120a6e
  return m_imp->getSVNPassword(index);
Toshihiro Shimizu 890ddd
}