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