Commit 063da678 authored by Micha Mueller's avatar Micha Mueller
Browse files

Configuration: move readRestAPIUsers() to globalconfiguration

parent 808d810f
......@@ -12,6 +12,8 @@
#include <boost/algorithm/string.hpp>
#include "logging.h"
class RESTHttpsServer;
// Wrapper class for plugin-specific settings
class pluginSettings_t {
public:
......@@ -114,6 +116,15 @@ public:
*/
bool readConfig();
/**
* @brief Reads user credentials from the config file and accordingly adds
* users to a RESTHttpsServer.
*
* @param server The Rest API server where to add the users.
* @return True on success, false otherwise.
*/
bool readRestAPIUsers(RESTHttpsServer* server);
// Global configuration members directly accessible from the class
bool validateConfig = false;
bool daemonize = false;
......
......@@ -3,6 +3,7 @@
//
#include "globalconfiguration.h"
#include "RESTHttpsServer.h"
GlobalConfiguration::GlobalConfiguration(const std::string& cfgFilePath, const std::string& cfgFileName) {
_cfgFileName = cfgFileName;
......@@ -94,3 +95,57 @@ bool GlobalConfiguration::readConfig() {
readAdditionalBlocks(cfg);
return true;
}
bool GlobalConfiguration::readRestAPIUsers(RESTHttpsServer* server) {
//open file
std::string globalConfig = _cfgFilePath;
globalConfig.append(_cfgFileName);
boost::property_tree::iptree cfg;
//parse to property_tree
try {
boost::property_tree::read_info(globalConfig, cfg);
} catch (boost::property_tree::info_parser_error& e) {
LOG(error) << "Error when reading users from " << _cfgFileName << ":" << e.what();
return false;
}
//read users
BOOST_FOREACH(boost::property_tree::iptree::value_type &global, cfg.get_child("restAPI")) {
if (boost::iequals(global.first, "user")) {
std::string username = global.second.data();
userAttributes_t attributes;
#ifdef DEBUG
LOG(info) << "Username: \"" << username << "\"";
#endif
BOOST_FOREACH(boost::property_tree::iptree::value_type &perm, global.second) {
if (boost::iequals(perm.first, "GET")) {
#ifdef DEBUG
LOG(info) << " Permission \"GET\"";
#endif
attributes.second[GET] = true;
} else if (boost::iequals(perm.first, "PUT")) {
#ifdef DEBUG
LOG(info) << " Permission \"PUT\"";
#endif
attributes.second[PUT] = true;
} else if (boost::iequals(perm.first, "password")) {
attributes.first = perm.second.data();
#ifdef DEBUG
LOG(info) << " Password: \"" << attributes.first << "\"";
#endif
} else {
#ifdef DEBUG
LOG(warning) << "Permission \"" << perm.first << "\" not recognized. Omitting";
#endif
}
}
if (server->addUser(username, attributes)) {
LOG(warning) << "User " << username << " already existed and was overwritten!";
}
} else {
// do nothing and continue search for the user section
}
}
return true;
}
......@@ -45,60 +45,6 @@ bool Configuration::readAdditionalValues(boost::property_tree::iptree::value_typ
return true;
}
bool Configuration::readRestAPIUsers(RestAPI* server) {
//open file
std::string globalConfig = _cfgFilePath;
globalConfig.append(_cfgFileName);
boost::property_tree::iptree cfg;
//parse to property_tree
try {
boost::property_tree::read_info(globalConfig, cfg);
} catch (boost::property_tree::info_parser_error& e) {
LOG(error) << "Error when reading users from " << _cfgFileName << ":" << e.what();
return false;
}
//read users
BOOST_FOREACH(boost::property_tree::iptree::value_type &global, cfg.get_child("restAPI")) {
if (boost::iequals(global.first, "user")) {
std::string username = global.second.data();
userAttributes_t attributes;
#ifdef DEBUG
LOG(info) << "Username: \"" << username << "\"";
#endif
BOOST_FOREACH(boost::property_tree::iptree::value_type &perm, global.second) {
if (boost::iequals(perm.first, "GET")) {
#ifdef DEBUG
LOG(info) << " Permission \"GET\"";
#endif
attributes.second[GET] = true;
} else if (boost::iequals(perm.first, "PUT")) {
#ifdef DEBUG
LOG(info) << " Permission \"PUT\"";
#endif
attributes.second[PUT] = true;
} else if (boost::iequals(perm.first, "password")) {
attributes.first = perm.second.data();
#ifdef DEBUG
LOG(info) << " Password: \"" << attributes.first << "\"";
#endif
} else {
#ifdef DEBUG
LOG(warning) << "Permission \"" << perm.first << "\" not recognized. Omitting";
#endif
}
}
if (server->addUser(username, attributes)) {
LOG(warning) << "User already existed and was overwritten!";
}
} else {
//
}
}
return true;
}
bool Configuration::readPlugins() {
std::string globalConfig = _cfgFilePath;
globalConfig.append(_cfgFileName);
......
......@@ -13,7 +13,6 @@
#include <boost/log/trivial.hpp>
#include "includes/PluginDefinitions.h"
#include "mqttchecker.h"
#include "RestAPI.h"
#define BROKERPORT 1883
#define BROKERHOST "127.0.0.1"
......@@ -34,14 +33,6 @@ public:
Configuration(const std::string& cfgFilePath, const std::string& cfgFileName) : GlobalConfiguration(cfgFilePath, cfgFileName) {}
virtual ~Configuration();
/**
* Reads and sets the users for the REST API server.
*
* @param server The Rest API server where to add the users
* @return true on success, false otherwise
*/
bool readRestAPIUsers(RestAPI* server);
/**
* Reads the plugin configuration section from global.conf (located at _cfgFilePath).
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment