diff --git a/enableuser.cpp b/enableuser.cpp new file mode 100644 index 0000000..8b95d2d --- /dev/null +++ b/enableuser.cpp @@ -0,0 +1,155 @@ +#include "Session.h" +#include "Util.h" +#include "json.hpp" +#include "jsonfail.h" +using namespace std; +using json=nlohmann::json; + +#define postval(NAME) string NAME=req.post[#NAME] + +int main() +{ + Request req; + Session se(req); + Response res; + json j; + + auto jsonfail=[&](int errcode,const string& detail="") + { + j["success"]=0; + j["errcode"]=errcode; + j["errmsg"]=string(GetErrMsg(errcode))+" : "+detail; + }; + + do + { + if(!se.isReady()) + { + jsonfail(err_session); + break; + } + + if(!se.getUser().empty()) + { + /// Logged in. Redirect user to dashboard. + j["success"]=2; + j["next_url"]="/booksys/dashboard.html"; + break; + } + + if(req.requestMethod!="POST") + { + jsonfail(err_method_not_supported); + break; + } + + if(req.post["account"].empty()|| + req.post["realname"].empty() || + req.post["realid"].empty() || + req.post["realphone"].empty()) + { + jsonfail(err_missing_parameter); + break; + } + + postval(account); + postval(realname); + postval(realid); + postval(realphone); + + /// Connect DB + DBInfo db; + MySQLConn conn; + if(db.readConfig()<0) + { + jsonfail(err_config); + break; + } + + if(db.connectProxy(conn)<0) + { + jsonfail(err_connect); + break; + } + + /// Check if user exist + int count_val; + if(conn.exec(make_str("select count(username) from bs_user where username='", + account, + "'"), + [&](MySQLResult& res) + { + res.stepRow([&](char** val,unsigned long* len) + { + count_val=ParseInt(val[0]); + }); + })<0) + { + jsonfail(err_sql,"Step 1"); + break; + } + + if(count_val!=1) + { + jsonfail(err_data,"user not exist"); + break; + } + + /// Check if user need enable + int permission_level; + int account_status; + if(conn.exec(make_str("select permission_level,account_status from bs_user where username='", + account, + "'"), + [&](MySQLResult& res) + { + res.stepRow([&](char** val,unsigned long* len) + { + permission_level=ParseInt(val[0]); + account_status=ParseInt(val[1]); + }); + })<0) + { + jsonfail(err_sql,"Step 2"); + break; + } + + if(permission_level!=3||account_status!=2) + { + /// Does not need enable + j["success"]=2; + j["errmsg"]="Enable is not needed"; + break; + } + + /// Do Update + if(conn.exec(make_str("insert into bs_reader values ('", + account, + "','", + realname, + "','", + realid, + "','", + realphone, + "',1,0,", /// LV, EXP + "0,0,0,", /// Borrow Limit, Borrow Used, Borrow Time Limit (This should be auto generated by Trigger) + "0,0)" /// Bonus, Balance + ),nullptr)<0) + { + jsonfail(err_sql,"Step 3"); + break; + } + + if(conn.getAffectedRows()<1) /// FIXME: I'm not sure if trigger will affect more than 1 rows. + { + jsonfail(err_sql_logic,"Affected rows less than 1"); + break; + } + + j["successs"]=1; + }while(0); + + res.content.append(j.dump()); + res.show(); + return 0; +}