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/old_src/doLogin.cpp

115 lines
2.5 KiB
C++
Raw Permalink Normal View History

#include "bs_util.h"
2017-11-19 15:36:43 +08:00
using namespace std;
2017-11-20 11:43:49 +08:00
2017-11-19 15:36:43 +08:00
int main()
{
Request req;
Session se(req);
Response res;
json j;
auto jsonfail=[&](int errcode,const std::string& dtl="")
2017-11-19 15:36:43 +08:00
{
jsonfail_func(j,errcode,dtl);
2017-11-19 15:36:43 +08:00
};
2017-11-20 11:43:49 +08:00
do
2017-11-19 15:36:43 +08:00
{
2017-11-20 11:43:49 +08:00
if(!se.isReady())
{
jsonfail(err_session);
break;
}
if(!se.getUser().empty())
2017-11-20 11:43:49 +08:00
{
/// Logged in...
j["success"]=2;
2017-12-08 22:37:18 +08:00
j["next_url"]="/booksys/mainpage.html";
2017-11-20 11:43:49 +08:00
break;
}
if(req.requestMethod!="POST")
{
jsonfail(err_method_not_supported);
break;
}
postval(username);
postval(password);
startdb();
2017-11-20 11:43:49 +08:00
int count_val;
2017-11-20 11:43:49 +08:00
if(conn.exec(make_str("select count(username) from bs_user where username='",
username,
"' and password='",
password,
"'"),
SQLParseInt(count_val)
)<0)
2017-11-20 11:43:49 +08:00
{
jsonfail(err_sql,"Step 1");
break;
2017-11-19 15:36:43 +08:00
}
2017-11-20 11:43:49 +08:00
if(count_val!=1)
2017-11-20 11:43:49 +08:00
{
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,
"'"),
SQLParseInt(account_status))<0)
2017-11-20 11:43:49 +08:00
{
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;
}
/// Try to mark session logged in
int ret;
if((ret=se.setUser(username))<0)
{
jsonfail(err_session,make_str("Failed to set session, ret=",ret));
break;
}
if(account_status==2)
{
/// Need verify, redirect to account enable.
j["success"]=3;
j["next_url"]="/booksys/enable.html";
}
else
{
j["success"]=1;
2017-12-08 22:37:18 +08:00
j["next_url"]="/booksys/mainpage.html";
}
2017-11-19 15:36:43 +08:00
}
2017-11-20 11:43:49 +08:00
while(0);
2017-11-19 15:36:43 +08:00
se.writeToResponse(res);
2017-11-19 15:36:43 +08:00
res.content.append(j.dump());
return 0;
}