#include "parser_admins.h"
#include "stg/admins.h"
-#include "stg/common.h"
+#include "stg/admin.h"
+#include "stg/admin_conf.h"
#include <strings.h> // strcasecmp
using STG::PARSER::DEL_ADMIN;
using STG::PARSER::CHG_ADMIN;
+const char * GET_ADMINS::tag = "GetAdmins";
+const char * ADD_ADMIN::tag = "AddAdmin";
+const char * DEL_ADMIN::tag = "DelAdmin";
+const char * CHG_ADMIN::tag = "ChgAdmin";
+
void GET_ADMINS::CreateAnswer()
{
- const PRIV * priv = m_currAdmin.GetPriv();
- if (!priv->adminChg)
+ const auto& priv = m_currAdmin.priv();
+ if (!priv.adminChg)
{
m_answer = "<Error Result=\"Error. Access denied.\"/>";
return;
}
- m_answer.clear();
-
- m_answer += GetOpenTag();
- ADMIN_CONF ac;
- int h = m_admins.OpenSearch();
-
- while (m_admins.SearchNext(h, &ac) == 0)
- {
- unsigned int p = (ac.priv.userStat << 0) +
- (ac.priv.userConf << 2) +
- (ac.priv.userCash << 4) +
- (ac.priv.userPasswd << 6) +
- (ac.priv.userAddDel << 8) +
- (ac.priv.adminChg << 10) +
- (ac.priv.tariffChg << 12);
- m_answer += "<admin login=\"" + ac.login + "\" priv=\"" + x2str(p) + "\"/>";
- }
- m_admins.CloseSearch(h);
- m_answer += GetCloseTag();
+ m_answer = "<Admins>";
+ m_admins.fmap([this](const Admin& admin)
+ {
+ const unsigned int p = (admin.priv().userStat << 0) +
+ (admin.priv().userConf << 2) +
+ (admin.priv().userCash << 4) +
+ (admin.priv().userPasswd << 6) +
+ (admin.priv().userAddDel << 8) +
+ (admin.priv().adminChg << 10) +
+ (admin.priv().tariffChg << 12);
+ m_answer += "<admin login=\"" + admin.login() + "\" priv=\"" + std::to_string(p) + "\"/>";
+ });
+ m_answer += "</Admins>";
}
int DEL_ADMIN::Start(void *, const char * el, const char ** attr)
{
if (strcasecmp(el, m_tag.c_str()) == 0)
{
- admin = attr[1];
+ m_admin = attr[1];
return 0;
}
return -1;
void DEL_ADMIN::CreateAnswer()
{
- if (m_admins.Del(admin, &m_currAdmin) == 0)
+ if (m_admins.del(m_admin, m_currAdmin) == 0)
m_answer = "<" + m_tag + " Result=\"Ok\"/>";
else
- m_answer = "<" + m_tag + " Result=\"Error. " + m_admins.GetStrError() + "\"/>";
+ m_answer = "<" + m_tag + " Result=\"Error. " + m_admins.strError() + "\"/>";
}
int ADD_ADMIN::Start(void *, const char *el, const char **attr)
{
if (strcasecmp(el, m_tag.c_str()) == 0)
{
- admin = attr[1];
+ m_admin = attr[1];
return 0;
}
return -1;
void ADD_ADMIN::CreateAnswer()
{
- if (m_admins.Add(admin, &m_currAdmin) == 0)
+ if (m_admins.add(m_admin, m_currAdmin) == 0)
m_answer = "<" + m_tag + " Result=\"Ok\"/>";
else
- m_answer = "<" + m_tag + " Result=\"Error. " + m_admins.GetStrError() + "\"/>";
+ m_answer = "<" + m_tag + " Result=\"Error. " + m_admins.strError() + "\"/>";
}
int CHG_ADMIN::Start(void *, const char * el, const char ** attr)
{
if (!login.empty())
{
- ADMIN * origAdmin = NULL;
+ Admin * origAdmin = NULL;
- if (m_admins.Find(login, &origAdmin))
+ if (m_admins.find(login, &origAdmin))
{
m_answer = "<" + m_tag + " Result = \"Admin '" + login + "' is not found.\"/>";
return;
}
- ADMIN_CONF conf(origAdmin->GetConf());
+ AdminConf conf(origAdmin->conf());
- if (!password.empty())
- conf.password = password.data();
+ if (password)
+ conf.password = password.value();
- if (!privAsString.empty())
+ if (privAsString)
{
int p = 0;
- if (str2x(privAsString.data().c_str(), p) < 0)
+ if (str2x(privAsString.value().c_str(), p) < 0)
{
m_answer = "<" + m_tag + " Result = \"Incorrect parameter Priv.\"/>";
return;
}
- conf.priv.FromInt(p);
+ conf.priv = Priv(p);
}
- if (m_admins.Change(conf, &m_currAdmin) != 0)
- m_answer = "<" + m_tag + " Result = \"" + m_admins.GetStrError() + "\"/>";
+ if (m_admins.change(conf, m_currAdmin) != 0)
+ m_answer = "<" + m_tag + " Result = \"" + m_admins.strError() + "\"/>";
else
m_answer = "<" + m_tag + " Result = \"Ok\"/>";
}