Update DBHomework
This commit is contained in:
parent
7ecb062364
commit
1031ce7258
|
@ -198,10 +198,8 @@
|
|||
账户或密码错误(包括账户错误)
|
||||
|
||||
返回:
|
||||
当登陆成功时返回一个apikey用于调用以上的API. 此key在一段时间内有效.
|
||||
当登陆成功时绑定User到Session.
|
||||
|
||||
http://booksys.com/api/v1/logout POST
|
||||
POST:
|
||||
apikey
|
||||
http://booksys.com/api/v1/logout GET/POST
|
||||
|
||||
提示:退出登陆. 会立刻注销此apikey
|
||||
提示:退出登陆. 会立刻注销Session.
|
|
@ -24,7 +24,9 @@ const char* GetErrMsg(int errcode)
|
|||
return "Data Error";
|
||||
case err_session:
|
||||
return "Session Error";
|
||||
default:
|
||||
case err_general:
|
||||
return "General error";
|
||||
default:
|
||||
return "Unknown error";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,5 +10,6 @@
|
|||
#define err_permission_denied 8
|
||||
#define err_data 9
|
||||
#define err_session 10
|
||||
#define err_general 11
|
||||
|
||||
const char* GetErrMsg(int errcode);
|
||||
|
|
127
login.cpp
127
login.cpp
|
@ -1,11 +1,12 @@
|
|||
#include "Request.h"
|
||||
#include "Response.h"
|
||||
#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;
|
||||
|
@ -13,40 +14,65 @@ int main()
|
|||
Response res;
|
||||
json j;
|
||||
|
||||
auto jsonfail=[&](int errcode,const std::string& errmsg)
|
||||
auto jsonfail=[&](int errcode,const string& detail="")
|
||||
{
|
||||
j["success"]=0;
|
||||
j["errcode"]=errcode;
|
||||
j["errmsg"]=errmsg;
|
||||
j["errmsg"]=string(GetErrMsg(errcode))+" : "+detail;
|
||||
};
|
||||
|
||||
if(req.requestMethod=="POST")
|
||||
do
|
||||
{
|
||||
if(!se.isReady())
|
||||
{
|
||||
jsonfail(err_session);
|
||||
break;
|
||||
}
|
||||
|
||||
if(!se.isNew()||!se.getUser().empty())
|
||||
{
|
||||
/// Logged in...
|
||||
j["success"]=2;
|
||||
j["next_url"]="/booksys/dashboard.html";
|
||||
break;
|
||||
}
|
||||
|
||||
if(req.requestMethod!="POST")
|
||||
{
|
||||
jsonfail(err_method_not_supported);
|
||||
break;
|
||||
}
|
||||
if(req.post["username"].empty() ||
|
||||
req.post["password"].empty() )
|
||||
{
|
||||
jsonfail(2,"Missing Parameter");
|
||||
jsonfail(err_missing_parameter);
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
postval(username);
|
||||
postval(password);
|
||||
|
||||
DBInfo db;
|
||||
MySQLConn conn;
|
||||
if(db.readConfig()<0)
|
||||
{
|
||||
jsonfail(3,"Failed to read configure");
|
||||
jsonfail(err_config);
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
string uname=req.post["username"];
|
||||
string upass=req.post["password"];
|
||||
MySQLConn conn;
|
||||
|
||||
if(db.connectProxy(conn)<0)
|
||||
{
|
||||
jsonfail(4,"Failed to connect DB");
|
||||
jsonfail(err_connect);
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
/// Check if password is correct
|
||||
bool authpass=false;
|
||||
if(conn.exec(make_str("select count(username) from bs_user where username='",uname,"' and password='",upass,"'"),
|
||||
if(conn.exec(make_str("select count(username) from bs_user where username='",
|
||||
username,
|
||||
"' and password='",
|
||||
password,
|
||||
"'"),
|
||||
[&](MySQLResult& res)
|
||||
{
|
||||
res.stepRow([&](char** val,unsigned long* len)
|
||||
|
@ -58,28 +84,63 @@ int main()
|
|||
});
|
||||
})<0)
|
||||
{
|
||||
jsonfail(5,"Failed to execute SQL.");
|
||||
jsonfail(err_sql,"Step 1");
|
||||
break;
|
||||
}
|
||||
else
|
||||
|
||||
if(!authpass)
|
||||
{
|
||||
jsonfail(err_data,"Auth Failed");
|
||||
break;
|
||||
}
|
||||
|
||||
/// Check if the user is allowed to login
|
||||
int account_status;
|
||||
if(conn.exec(make_str("select account_status from bs_user where username='",
|
||||
username,
|
||||
"'"),
|
||||
[&](MySQLResult& res)
|
||||
{
|
||||
res.stepRow([&](char** val,unsigned long* len)
|
||||
{
|
||||
account_status=ParseInt(val[0]);
|
||||
});
|
||||
})<0)
|
||||
{
|
||||
jsonfail(err_sql,"Step 2");
|
||||
break;
|
||||
}
|
||||
|
||||
if(account_status==0)
|
||||
{
|
||||
/// Act as auth failed (username or password wrong) when account is hidden.
|
||||
jsonfail(err_data,"Auth Failed");
|
||||
break;
|
||||
}
|
||||
else if(account_status==1)
|
||||
{
|
||||
/// Banned
|
||||
jsonfail(err_data,"Your account is banned.");
|
||||
break;
|
||||
}
|
||||
else if(account_status==2)
|
||||
{
|
||||
/// Need verify, redirect to account enable.
|
||||
jsonfail(err_data,"Need verify first.");
|
||||
j["next_url"]="/booksys/enable.html";
|
||||
}
|
||||
|
||||
/// Try to mark session logged in
|
||||
if(se.setUser(uname)<0)
|
||||
int ret;
|
||||
if((ret=se.setUser(username))<0)
|
||||
{
|
||||
jsonfail(6,"Failed to set session.");
|
||||
jsonfail(err_session,make_str("Failed to set session, ret=",ret));
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
j["success"]=1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
jsonfail(1,"Request Method Not Supported");
|
||||
}
|
||||
while(0);
|
||||
|
||||
res.content.append(j.dump());
|
||||
res.show();
|
||||
|
|
51
logout.cpp
Normal file
51
logout.cpp
Normal file
|
@ -0,0 +1,51 @@
|
|||
#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.isNew()||se.getUser().empty())
|
||||
{
|
||||
jsonfail(err_need_login);
|
||||
break;
|
||||
}
|
||||
|
||||
int ret;
|
||||
if((ret=se.setUser(""))<0)
|
||||
{
|
||||
jsonfail(err_session,make_str("Failed to logout. Session Module returns ",ret));
|
||||
break;
|
||||
}
|
||||
|
||||
j["success"]=1;
|
||||
}while(0);
|
||||
|
||||
res.content.append(j.dump());
|
||||
res.show();
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user