This repository has been archived on 2021-11-25. You can view files and clone it, but cannot push or open issues or pull requests.
DBHomework/bs_util.cpp

84 lines
1.8 KiB
C++

#include "bs_util.h"
using namespace std;
void jsonfail_func(json& j,int errcode,const std::string& detail)
{
j["success"]=0;
j["errcode"]=errcode;
j["errmsg"]=string(GetErrMsg(errcode))+" : "+detail;
}
int getPermissionLevel(const std::string& Username,Response& res)
{
DBInfo db;
MySQLConn conn;
json j;
bool failed=false;
auto jsonfail=[&](int errcode,const std::string& detail="")
{
jsonfail_func(j,errcode,detail);
failed=true;
};
if( db.readConfig()<0 )
{
jsonfail(err_config);
}
else if(db.connectProxy(conn)<0)
{
jsonfail(err_connect);
}
else
{
int permission_level;
int ret=conn.exec(make_str("select permission_level from bs_user where username='",
Username,
"'"),
[&](MySQLResult& res)
{
res.stepRow([&](char** val,unsigned long* len)
{
permission_level=ParseInt(val[0]);
});
});
if(ret==1)
{
jsonfail(err_data,"User Not Found");
}
else if(ret<0)
{
jsonfail(err_sql,"Step 1");
}
else
{
return permission_level;
}
}
if(failed)
{
res.content=j.dump();
exit(0);
/// Never reaches here.
return -1;
}
else
{
return -1;
}
}
std::function<void(MySQLResult&)> SQLParseInt(int& nval)
{
return
[&](MySQLResult& res)
{
res.stepRow([&](char** val,unsigned long* len)
{
nval=ParseInt(val[0]);
});
};
}