X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/143aec2c09f1eafee27b02ad3f138149eb1c20e2..2574a28cbf000603bc31f61593dbf061ff56c1d5:/projects/stargazer/plugins/configuration/rpcconfig/users_methods.cpp?ds=sidebyside diff --git a/projects/stargazer/plugins/configuration/rpcconfig/users_methods.cpp b/projects/stargazer/plugins/configuration/rpcconfig/users_methods.cpp index fcd81a5e..71910620 100644 --- a/projects/stargazer/plugins/configuration/rpcconfig/users_methods.cpp +++ b/projects/stargazer/plugins/configuration/rpcconfig/users_methods.cpp @@ -1,16 +1,18 @@ -#include +#include "users_methods.h" +#include "rpcconfig.h" +#include "user_helper.h" #include "stg/users.h" #include "stg/admins.h" #include "stg/tariffs.h" -#include "stg/user_ips.h" -#include "stg/common.h" +#include "stg/tariff.h" +#include "stg/user.h" #include "stg/user_property.h" +#include "stg/common.h" -#include "users_methods.h" -#include "rpcconfig.h" -#include "user_helper.h" -#include "utils.h" +#include + +using UserPtr = STG::User*; //------------------------------------------------------------------------------ @@ -19,7 +21,6 @@ void METHOD_USER_GET::execute(xmlrpc_c::paramList const & paramList, { std::string cookie = paramList.getString(0); std::string login = paramList.getString(1); -std::string enc; paramList.verifyEnd(2); std::map structVal; @@ -32,7 +33,7 @@ if (config->GetAdminInfo(cookie, &adminInfo)) return; } -USER_PTR u; +UserPtr u; if (users->FindByName(login, &u)) { @@ -41,7 +42,7 @@ if (users->FindByName(login, &u)) return; } -USER_HELPER uhelper(u); +USER_HELPER uhelper(u, *users); if (!adminInfo.priviledges.userConf || !adminInfo.priviledges.userPasswd) { @@ -59,7 +60,6 @@ void METHOD_USER_ADD::execute(xmlrpc_c::paramList const & paramList, { std::string cookie = paramList.getString(0); std::string login = paramList.getString(1); -std::string enc; paramList.verifyEnd(2); ADMIN_INFO adminInfo; @@ -70,15 +70,15 @@ if (config->GetAdminInfo(cookie, &adminInfo)) return; } -ADMIN * admin = NULL; +STG::Admin * admin = NULL; -if (admins->FindAdmin(adminInfo.admin, &admin)) +if (admins->find(adminInfo.admin, &admin)) { *retvalPtr = xmlrpc_c::value_boolean(false); return; } -USER_PTR u; +UserPtr u; if (users->FindByName(login, &u)) { @@ -91,7 +91,7 @@ if (users->FindByName(login, &u)) *retvalPtr = xmlrpc_c::value_boolean(true); return; } - + *retvalPtr = xmlrpc_c::value_boolean(false); return; } @@ -103,7 +103,6 @@ void METHOD_USER_DEL::execute(xmlrpc_c::paramList const & paramList, { std::string cookie = paramList.getString(0); std::string login = paramList.getString(1); -std::string enc; paramList.verifyEnd(2); ADMIN_INFO adminInfo; @@ -114,15 +113,15 @@ if (config->GetAdminInfo(cookie, &adminInfo)) return; } -ADMIN * admin; +STG::Admin * admin; -if (admins->FindAdmin(adminInfo.admin, &admin)) +if (admins->find(adminInfo.admin, &admin)) { *retvalPtr = xmlrpc_c::value_boolean(false); return; } -USER_PTR u; +UserPtr u; if (!users->FindByName(login, &u)) { @@ -141,7 +140,6 @@ void METHOD_USERS_GET::execute(xmlrpc_c::paramList const & paramList, xmlrpc_c::value * const retvalPtr) { std::string cookie = paramList.getString(0); -std::string enc; paramList.verifyEnd(1); std::map structVal; @@ -158,7 +156,7 @@ if (config->GetAdminInfo(cookie, &adminInfo)) bool hidePassword = !adminInfo.priviledges.userConf || !adminInfo.priviledges.userPasswd; -USER_PTR u; +UserPtr u; int h = users->OpenSearch(); if (!h) @@ -177,7 +175,7 @@ while (1) xmlrpc_c::value info; - USER_HELPER uhelper(u); + USER_HELPER uhelper(u, *users); uhelper.GetUserInfo(&info, hidePassword); @@ -195,7 +193,6 @@ void METHOD_USER_CHG::execute(xmlrpc_c::paramList const & paramList, std::string cookie = paramList.getString(0); std::string login = paramList.getString(1); xmlrpc_c::value_struct info(paramList.getStruct(2)); -std::string enc; paramList.verifyEnd(3); ADMIN_INFO adminInfo; @@ -206,15 +203,15 @@ if (config->GetAdminInfo(cookie, &adminInfo)) return; } -ADMIN * admin; +STG::Admin * admin; -if (admins->FindAdmin(adminInfo.admin, &admin)) +if (admins->find(adminInfo.admin, &admin)) { *retvalPtr = xmlrpc_c::value_boolean(false); return; } -USER_PTR u; +UserPtr u; if (users->FindByName(login, &u)) { @@ -222,15 +219,15 @@ if (users->FindByName(login, &u)) return; } -USER_HELPER uhelper(u); +USER_HELPER uhelper(u, *users); if (!adminInfo.priviledges.userConf || !adminInfo.priviledges.userPasswd) { - uhelper.SetUserInfo(info, admin, login, *store, tariffs); + uhelper.SetUserInfo(info, *admin, login, *store, tariffs); } else { - uhelper.SetUserInfo(info, admin, login, *store, tariffs); + uhelper.SetUserInfo(info, *admin, login, *store, tariffs); } u->WriteConf(); @@ -248,7 +245,6 @@ std::string cookie = paramList.getString(0); std::string login = paramList.getString(1); double amount = paramList.getDouble(2); std::string comment = IconvString(paramList.getString(3), "UTF-8", "KOI8-R"); -std::string enc; paramList.verifyEnd(4); ADMIN_INFO adminInfo; @@ -259,15 +255,15 @@ if (config->GetAdminInfo(cookie, &adminInfo)) return; } -ADMIN * admin; +STG::Admin * admin; -if (admins->FindAdmin(adminInfo.admin, &admin)) +if (admins->find(adminInfo.admin, &admin)) { *retvalPtr = xmlrpc_c::value_boolean(false); return; } -USER_PTR u; +UserPtr u; if (users->FindByName(login, &u)) { @@ -275,10 +271,10 @@ if (users->FindByName(login, &u)) return; } -double cash = u->GetProperty().cash.Get(); +double cash = u->GetProperties().cash.Get(); cash += amount; -if (!u->GetProperty().cash.Set(cash, admin, login, store, comment)) +if (!u->GetProperties().cash.Set(cash, *admin, login, *store, comment)) { *retvalPtr = xmlrpc_c::value_boolean(false); return; @@ -298,7 +294,6 @@ std::string cookie = paramList.getString(0); std::string login = paramList.getString(1); double cash = paramList.getDouble(2); std::string comment = IconvString(paramList.getString(3), "UTF-8", "KOI8-R"); -std::string enc; paramList.verifyEnd(4); ADMIN_INFO adminInfo; @@ -309,15 +304,15 @@ if (config->GetAdminInfo(cookie, &adminInfo)) return; } -ADMIN * admin; +STG::Admin * admin; -if (admins->FindAdmin(adminInfo.admin, &admin)) +if (admins->find(adminInfo.admin, &admin)) { *retvalPtr = xmlrpc_c::value_boolean(false); return; } -USER_PTR u; +UserPtr u; if (users->FindByName(login, &u)) { @@ -325,7 +320,7 @@ if (users->FindByName(login, &u)) return; } -if (!u->GetProperty().cash.Set(cash, admin, login, store, comment)) +if (!u->GetProperties().cash.Set(cash, *admin, login, *store, comment)) { *retvalPtr = xmlrpc_c::value_boolean(false); return; @@ -346,7 +341,6 @@ std::string login = paramList.getString(1); std::string tariff = paramList.getString(2); bool delayed = paramList.getBoolean(3); std::string comment = IconvString(paramList.getString(4), "UTF-8", "KOI8-R"); -std::string enc; paramList.verifyEnd(5); ADMIN_INFO adminInfo; @@ -357,15 +351,15 @@ if (config->GetAdminInfo(cookie, &adminInfo)) return; } -ADMIN * admin; +STG::Admin * admin; -if (admins->FindAdmin(adminInfo.admin, &admin)) +if (admins->find(adminInfo.admin, &admin)) { *retvalPtr = xmlrpc_c::value_boolean(false); return; } -USER_PTR u; +UserPtr u; if (users->FindByName(login, &u)) { @@ -377,10 +371,7 @@ if (tariffs->FindByName(tariff)) { if (delayed) { - if (u->GetProperty().nextTariff.Set(tariff, - admin, - login, - store)) + if (u->GetProperties().nextTariff.Set(tariff, *admin, login, *store, comment)) { u->WriteConf(); *retvalPtr = xmlrpc_c::value_boolean(true); @@ -389,15 +380,25 @@ if (tariffs->FindByName(tariff)) } else { - if (u->GetProperty().tariffName.Set(tariff, - admin, - login, - store)) + const auto newTariff = tariffs->FindByName(tariff); + if (newTariff) { - u->ResetNextTariff(); - u->WriteConf(); - *retvalPtr = xmlrpc_c::value_boolean(true); - return; + const auto currentTariff = u->GetTariff(); + std::string message = currentTariff->TariffChangeIsAllowed(*newTariff, time(NULL)); + if (message.empty()) + { + if (u->GetProperties().tariffName.Set(tariff, *admin, login, *store, comment)) + { + u->ResetNextTariff(); + u->WriteConf(); + *retvalPtr = xmlrpc_c::value_boolean(true); + return; + } + } + else + { + STG::PluginLogger::get("conf_rpc")("Tariff change is prohibited for user %s. %s", u->GetLogin().c_str(), message.c_str()); + } } } } @@ -411,16 +412,15 @@ void METHOD_GET_ONLINE_IPS::execute(xmlrpc_c::paramList const & paramList, xmlrpc_c::value * const retvalPtr) { std::string cookie = paramList.getString(0); -std::vector subnetsStr = paramList.getArray(1); +typedef std::vector ValueVector; +ValueVector subnetsStr = paramList.getArray(1); paramList.verifyEnd(2); -std::vector subnets; - -std::vector::iterator it; +std::vector subnets; -for (it = subnetsStr.begin(); it != subnetsStr.end(); ++it) +for (ValueVector::const_iterator it(subnetsStr.begin()); it != subnetsStr.end(); ++it) { - IP_MASK ipm; + STG::IPMask ipm; if (ParseNet(xmlrpc_c::value_string(*it), ipm)) { printfd(__FILE__, "METHOD_GET_ONLINE_IPS::execute(): Failed to parse subnet ('%s')\n", std::string(xmlrpc_c::value_string(*it)).c_str()); @@ -441,9 +441,9 @@ if (config->GetAdminInfo(cookie, &adminInfo)) return; } -std::vector ips; +ValueVector ips; -USER_PTR u; +UserPtr u; int handle = users->OpenSearch(); if (!handle) @@ -464,8 +464,7 @@ while (1) { uint32_t ip = u->GetCurrIP(); - std::vector::iterator it; - for (it = subnets.begin(); it != subnets.end(); ++it) + for (std::vector::const_iterator it(subnets.begin()); it != subnets.end(); ++it) { if ((it->ip & it->mask) == (ip & it->mask)) { @@ -481,7 +480,7 @@ structVal["ips"] = xmlrpc_c::value_array(ips); *retvalPtr = xmlrpc_c::value_struct(structVal); } -bool METHOD_GET_ONLINE_IPS::ParseNet(const std::string & net, IP_MASK & ipm) const +bool METHOD_GET_ONLINE_IPS::ParseNet(const std::string & net, STG::IPMask & ipm) const { size_t pos = net.find_first_of('/'); @@ -518,3 +517,36 @@ ipm.mask = htonl(0xffFFffFF << (32 - ipm.mask)); return false; } + +void METHOD_GET_USER_AUTH_BY::execute(xmlrpc_c::paramList const & paramList, + xmlrpc_c::value * const retvalPtr) +{ +std::string cookie = paramList.getString(0); +std::string login = paramList.getString(1); +paramList.verifyEnd(2); + +std::map structVal; +ADMIN_INFO adminInfo; + +if (config->GetAdminInfo(cookie, &adminInfo)) + { + structVal["result"] = xmlrpc_c::value_boolean(false); + *retvalPtr = xmlrpc_c::value_struct(structVal); + return; + } + +UserPtr u; + +if (users->FindByName(login, &u)) + { + structVal["result"] = xmlrpc_c::value_boolean(false); + *retvalPtr = xmlrpc_c::value_struct(structVal); + return; + } + +std::vector list(u->GetAuthorizers()); +std::vector authList; +for (std::vector::const_iterator it = list.begin(); it != list.end(); ++it) + authList.push_back(xmlrpc_c::value_string(*it)); +*retvalPtr = xmlrpc_c::value_array(authList); +}