From: Maxim Mamontov Date: Fri, 26 Jul 2013 17:25:54 +0000 (+0300) Subject: Added ability to list authorizers for user. X-Git-Tag: 2.409~356 X-Git-Url: https://git.stg.codes/stg.git/commitdiff_plain/c4456f2cae7261f7263e3708ff995bade7da722f?ds=sidebyside;hp=40079f3d50a559ea36705b72b2fe2d190a6cc354 Added ability to list authorizers for user. --- diff --git a/projects/sgconf/common_sg.cpp b/projects/sgconf/common_sg.cpp index 3d015160..bb4f5967 100644 --- a/projects/sgconf/common_sg.cpp +++ b/projects/sgconf/common_sg.cpp @@ -52,6 +52,12 @@ struct GetUserCbData void * data; bool * result; }; +//----------------------------------------------------------------------------- +struct AuthByCbData +{ + void * data; + bool * result; +}; //--------------------------------------------------------------------------- struct HelpParams { @@ -122,6 +128,9 @@ printf("sgconf set -s -p -a -w -u -- printf("To get userdata<0...9> use:\n"); printf("sgconf get -s -p -a -w -u --ud0 [--ud1 ...]\n\n"); +printf("To get user's authorizers list use:\n"); +printf("sgconf get -s -p -a -w -u --authorized-by\n"); + printf("To send message use:\n"); printf("sgconf set -s -p -a -w -u -m \n\n"); @@ -406,6 +415,22 @@ for (unsigned i = 0; i < sizeof(strReqParams) / sizeof(StringReqParams); i++) cout << strReqParams[i].name << "=" << str << endl; } } +*result = true; +} +//----------------------------------------------------------------------------- +void RecvAuthByData(const std::vector & list, void * d) +{ +AuthByCbData * abcbd; +abcbd = (AuthByCbData *)d; + +bool * result = abcbd->result; + +REQUEST * req = (REQUEST *)abcbd->data; + +for (std::vector::const_iterator it = list.begin(); it != list.end(); ++it) + cout << *it << "\n"; +cout << endl; + *result = true; } //----------------------------------------------------------------------------- @@ -497,3 +522,42 @@ else return 0; } //----------------------------------------------------------------------------- +int ProcessAuthBy(const std::string &server, + int port, + const std::string &admLogin, + const std::string &admPasswd, + const std::string &login, + void * data) +{ +SERVCONF sc; + +bool result = false; + +sc.SetServer(server.c_str()); // õÓÔÁÎÁ×ÌÉ×ÁÅÍ ÉÍÑ ÓÅÒ×ÅÒÁ Ó ËÏÔÏÒÇÏ ÚÁÂÉÒÁÔØ ÉÎÆÕ +sc.SetPort(port); // ÁÄÍÉÎÓËÉÊ ÐÏÒÔ ÓÅÒ×ÅÒÁÐÏÒÔ +sc.SetAdmLogin(admLogin.c_str()); // ÷ÙÓÔÁ×ÌÑÅÍ ÌÏÇÉÎ É ÐÁÒÏÌØ ÁÄÍÉÎÁ +sc.SetAdmPassword(admPasswd.c_str()); + +// TODO Good variable name :) +AuthByCbData abcbd; + +abcbd.data = data; +abcbd.result = &result; + +sc.SetGetUserAuthByRecvCb(RecvAuthByData, &abcbd); +sc.GetUserAuthBy(login.c_str()); + +if (result) + { + printf("Ok\n"); + return 0; + } +else + { + printf("Error\n"); + return -1; + } + +return 0; +} +//----------------------------------------------------------------------------- diff --git a/projects/sgconf/common_sg.h b/projects/sgconf/common_sg.h index a654748a..a5cbffad 100644 --- a/projects/sgconf/common_sg.h +++ b/projects/sgconf/common_sg.h @@ -52,6 +52,13 @@ int ProcessGetUser(const std::string &server, const std::string &login, void * data); +int ProcessAuthBy(const std::string &server, + int port, + const std::string &admLogin, + const std::string &admPasswd, + const std::string &login, + void * data); + int ProcessSetUser(const std::string &server, int port, const std::string &admLogin, diff --git a/projects/sgconf/main.cpp b/projects/sgconf/main.cpp index 2aaacbc0..ce5a2072 100644 --- a/projects/sgconf/main.cpp +++ b/projects/sgconf/main.cpp @@ -108,7 +108,8 @@ struct option long_options_get[] = { {"email", 0, 0, 'L'}, //emaiL {"phone", 0, 0, 'P'}, //phone {"group", 0, 0, 'G'}, //Group -{"ip", 0, 0, 'I'}, //IP-address of user +{"ip", 0, 0, 'I'}, //IP-address of user +{"authorized-by",0, 0, 800}, //always online {0, 0, 0, 0}}; @@ -846,6 +847,9 @@ while (1) req.ud[c - 700] = " "; break; + case 800: + req.authBy = true; + case '?': case ':': //printf ("Unknown option \n"); @@ -873,7 +877,10 @@ if (missedOptionArg || !CheckParametersGet(&req)) exit(PARAMETER_PARSING_ERR_CODE); } -return ProcessGetUser(req.server, req.port, req.admLogin, req.admPasswd, req.login, &req); +if (req.authBy) + return ProcessAuthBy(req.server, req.port, req.admLogin, req.admPasswd, req.login, &req); +else + return ProcessGetUser(req.server, req.port, req.admLogin, req.admPasswd, req.login, &req); } //----------------------------------------------------------------------------- int mainSet(int argc, char **argv) diff --git a/projects/sgconf/request.h b/projects/sgconf/request.h index daddec1d..61222127 100644 --- a/projects/sgconf/request.h +++ b/projects/sgconf/request.h @@ -49,7 +49,8 @@ struct REQUEST REQUEST() : chgTariff(false), createUser(false), - deleteUser(false) + deleteUser(false), + authBy(false) { for (int i = 0; i < DIR_NUM; i++) { @@ -75,6 +76,7 @@ RESETABLE setCash; string message; bool createUser; bool deleteUser; +bool authBy; RESETABLE usrMsg; RESETABLE credit; diff --git a/projects/stargazer/plugins/configuration/sgconfig/Makefile b/projects/stargazer/plugins/configuration/sgconfig/Makefile index b6f4938a..83e267d1 100644 --- a/projects/stargazer/plugins/configuration/sgconfig/Makefile +++ b/projects/stargazer/plugins/configuration/sgconfig/Makefile @@ -11,7 +11,8 @@ SRCS = ./stgconfig.cpp \ ./configproto.cpp \ ./parser.cpp \ ./parser_tariff.cpp \ - ./parser_admin.cpp + ./parser_admin.cpp \ + ./parser_auth_by.cpp LIBS += -lexpat \ $(LIB_THREAD) diff --git a/projects/stargazer/plugins/configuration/sgconfig/configproto.cpp b/projects/stargazer/plugins/configuration/sgconfig/configproto.cpp index 22facc61..015c2460 100644 --- a/projects/stargazer/plugins/configuration/sgconfig/configproto.cpp +++ b/projects/stargazer/plugins/configuration/sgconfig/configproto.cpp @@ -107,6 +107,7 @@ CONFIGPROTO::CONFIGPROTO(PLUGIN_LOGGER & l) parserDelUser(), parserCheckUser(), parserSendMessage(), + parserAuthBy(), parserGetAdmins(), parserAddAdmin(), parserDelAdmin(), @@ -130,6 +131,7 @@ dataParser.push_back(&parserAddUser); dataParser.push_back(&parserDelUser); dataParser.push_back(&parserCheckUser); dataParser.push_back(&parserSendMessage); +dataParser.push_back(&parserAuthBy); dataParser.push_back(&parserGetTariffs); dataParser.push_back(&parserAddTariff); diff --git a/projects/stargazer/plugins/configuration/sgconfig/configproto.h b/projects/stargazer/plugins/configuration/sgconfig/configproto.h index b8b01f02..ccb8a4d7 100644 --- a/projects/stargazer/plugins/configuration/sgconfig/configproto.h +++ b/projects/stargazer/plugins/configuration/sgconfig/configproto.h @@ -41,6 +41,8 @@ #include "stg/logger.h" #include "parser.h" +#include "parser_auth_by.h" + #define STG_HEADER "SG04" #define OK_HEADER "OKHD" #define ERR_HEADER "ERHD" @@ -107,6 +109,7 @@ private: PARSER_DEL_USER parserDelUser; PARSER_CHECK_USER parserCheckUser; PARSER_SEND_MESSAGE parserSendMessage; + PARSER_AUTH_BY parserAuthBy; PARSER_GET_ADMINS parserGetAdmins; PARSER_ADD_ADMIN parserAddAdmin; diff --git a/projects/stargazer/plugins/configuration/sgconfig/parser.cpp b/projects/stargazer/plugins/configuration/sgconfig/parser.cpp index 8e11a157..b6c49172 100644 --- a/projects/stargazer/plugins/configuration/sgconfig/parser.cpp +++ b/projects/stargazer/plugins/configuration/sgconfig/parser.cpp @@ -286,6 +286,13 @@ answerList->push_back(s); strprintf(&s, "", u->GetProperty().creditExpire.Get()); answerList->push_back(s); +s = ""; +std::vector list(u->GetAuthorizers()); +for (std::vector::const_iterator it = list.begin(); it != list.end(); ++it) + s += ""; +s += ""; +answerList->push_back(s); + strprintf(&s, ""); answerList->push_back(s); } diff --git a/stglibs/srvconf.lib/include/stg/servconf.h b/stglibs/srvconf.lib/include/stg/servconf.h index 67c2f248..dbbe2001 100644 --- a/stglibs/srvconf.lib/include/stg/servconf.h +++ b/stglibs/srvconf.lib/include/stg/servconf.h @@ -30,6 +30,7 @@ #include #include +#include #include #include "stg/os_int.h" @@ -101,6 +102,7 @@ typedef void(*RecvServerInfoDataCb_t)(SERVERINFO * si, void * data); typedef int(*RecvChgUserCb_t)(const char * asnwer, void * data); typedef int(*RecvCheckUserCb_t)(const char * answer, void * data); typedef int(*RecvSendMessageCb_t)(const char * answer, void * data); +typedef void(*RecvAuthByDataCb_t)(const std::vector & list, void * data); //----------------------------------------------------------------------------- struct ADMINDATA { @@ -224,6 +226,23 @@ private: bool error; }; //----------------------------------------------------------------------------- +class PARSER_AUTH_BY: public PARSER +{ +public: + PARSER_AUTH_BY(); + int ParseStart(const char *el, const char **attr); + void ParseEnd(const char *el); + void ParseServerInfo(const char *el, const char **attr); + bool GetError(); + void SetRecvCb(RecvAuthByDataCb_t, void * data); +private: + RecvAuthByDataCb_t RecvAuthByDataCb; + void * authByDataCb; + int depth; + bool error; + std::vector list; +}; +//----------------------------------------------------------------------------- class SERVCONF { public: @@ -235,6 +254,7 @@ public: void SetAdmPassword(const char * password); void SetUserDataRecvCb(RecvUserDataCb_t, void * data); + void SetGetUserAuthByRecvCb(RecvAuthByDataCb_t, void * data); void SetServerInfoRecvCb(RecvServerInfoDataCb_t, void * data); void SetChgUserCb(RecvChgUserCb_t, void * data); void SetCheckUserCb(RecvCheckUserCb_t, void * data); @@ -244,6 +264,7 @@ public: int GetUsers(); int GetUser(const char * login); int ChgUser(const char * request); + int GetUserAuthBy(const char * login); // TODO: Remove this shit! int MsgUser(const char * request); int SendMessage(const char * login, const char * message, int prio); @@ -260,6 +281,7 @@ private: PARSER_GET_USERS parserGetUsers; PARSER_GET_USER parserGetUser; + PARSER_AUTH_BY parserAuthBy; PARSER_GET_SERVER_INFO parserServerInfo; PARSER_CHG_USER parserChgUser; PARSER_CHECK_USER parserCheckUser; @@ -274,12 +296,14 @@ private: RecvUserDataCb_t RecvUserDataCb; RecvUserDataCb_t RecvGetUserDataCb; + RecvAuthByDataCb_t RecvAuthByCb; RecvServerInfoDataCb_t RecvServerInfoDataCb; RecvChgUserCb_t RecvChgUserCb; RecvCheckUserCb_t RecvCheckUserCb; RecvSendMessageCb_t RecvSendMessageCb; void * getUserDataDataCb; + void * getUserAuthByDataCb; void * getUsersDataDataCb; void * getServerInfoDataCb; void * chgUserDataCb; diff --git a/stglibs/srvconf.lib/parser.cpp b/stglibs/srvconf.lib/parser.cpp index a87266eb..62537359 100644 --- a/stglibs/srvconf.lib/parser.cpp +++ b/stglibs/srvconf.lib/parser.cpp @@ -822,6 +822,58 @@ if (strcmp(attr[0], "value") == 0) //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- +PARSER_AUTH_BY::PARSER_AUTH_BY() + : RecvAuthByDataCb(NULL), + authByDataCb(NULL), + depth(0), + error(false), + list() +{ +} +//----------------------------------------------------------------------------- +int PARSER_AUTH_BY::ParseStart(const char *el, const char **attr) +{ +depth++; +if (depth == 1) + { + if (strcasecmp(el, "AuthorizedBy") != 0) + { + list.erase(list.begin(), list.end()); + //printf("%s\n", el); + } + } +else + { + if (depth == 2) + { + if (strcasecmp(el, "Auth") == 0) + { + if (attr && attr[0]) + list.push_back(attr[0]); + return 0; + } + } + } +return 0; +} +//----------------------------------------------------------------------------- +void PARSER_AUTH_BY::ParseEnd(const char *) +{ +depth--; +if (depth == 0) + { + RecvAuthByDataCb(list, authByDataCb); + } +} +//----------------------------------------------------------------------------- +void PARSER_AUTH_BY::SetRecvCb(RecvAuthByDataCb_t f, void * data) +{ +RecvAuthByDataCb = f; +authByDataCb = data; +} +//----------------------------------------------------------------------------- +//----------------------------------------------------------------------------- +//----------------------------------------------------------------------------- PARSER_CHG_USER::PARSER_CHG_USER() : RecvChgUserCb(NULL), chgUserCbData(NULL), diff --git a/stglibs/srvconf.lib/servconf.cpp b/stglibs/srvconf.lib/servconf.cpp index ab386cf7..0ca3028c 100644 --- a/stglibs/srvconf.lib/servconf.cpp +++ b/stglibs/srvconf.lib/servconf.cpp @@ -101,6 +101,7 @@ SERVCONF::SERVCONF() RecvCheckUserCb(NULL), RecvSendMessageCb(NULL), getUserDataDataCb(NULL), + getUserAuthByDataCb(NULL), getUsersDataDataCb(NULL), getServerInfoDataCb(NULL), chgUserDataCb(NULL), @@ -142,6 +143,37 @@ currParser = &parserGetUser; nt.Reset(); nt.SetRxCallback(this, AnsRecv); +if ((ret = nt.Connect()) != st_ok) + { + errorMsg = nt.GetError(); + return ret; + } +if ((ret = nt.Transact(request)) != st_ok) + { + errorMsg = nt.GetError(); + return ret; + } +if ((ret = nt.Disconnect()) != st_ok) + { + errorMsg = nt.GetError(); + return ret; + } + +return st_ok; +} +//----------------------------------------------------------------------------- +int SERVCONF::GetUserAuthBy(const char * l) +{ +char request[255]; +snprintf(request, 255, "", l); +int ret; + +currParser = &parserAuthBy; +((PARSER_AUTH_BY*)currParser)->SetRecvCb(RecvAuthByCb, getUserAuthByDataCb); + +nt.Reset(); +nt.SetRxCallback(this, AnsRecv); + if ((ret = nt.Connect()) != st_ok) { errorMsg = nt.GetError(); @@ -368,6 +400,12 @@ RecvGetUserDataCb = f; //GET_USER getUserDataDataCb = data; } //----------------------------------------------------------------------------- +void SERVCONF::SetGetUserAuthByRecvCb(RecvAuthByDataCb_t f, void * data) +{ +RecvAuthByCb = f; +getUserAuthByDataCb = data; +} +//----------------------------------------------------------------------------- void SERVCONF::SetServerInfoRecvCb(RecvServerInfoDataCb_t f, void * data) { RecvServerInfoDataCb = f;