diff --git a/HTTPWrapper b/HTTPWrapper index 7c5558e..af77d51 160000 --- a/HTTPWrapper +++ b/HTTPWrapper @@ -1 +1 @@ -Subproject commit 7c5558ea3fe2240b61b18849ce68535a89de95c2 +Subproject commit af77d51fd8071cd598c94ab8a49a632f5ccdca8d diff --git a/MySQLTransaction.cpp b/MySQLTransaction.cpp deleted file mode 100644 index 2a43ba1..0000000 --- a/MySQLTransaction.cpp +++ /dev/null @@ -1,58 +0,0 @@ -#include "MySQLTransaction.h" - -Transaction::Transaction(MySQLConn& MySQLConnection) : conn(MySQLConnection) -{ - _isReady=false; - _operated=false; - if(conn.exec("start transaction",nullptr)<0) - { - /// SQL execute error. - } - else - { - _isReady=true; - } -} - -Transaction::~Transaction() -{ - if(!_operated) - { - rollback(); - } -} - -int Transaction::commit() -{ - if(_operated) return -2; - - if(conn.exec("commit",nullptr)<0) - { - return -1; - } - else - { - _operated=true; - return conn.getAffectedRows(); - } -} - -int Transaction::rollback() -{ - if(_operated) return -2; - - if(conn.exec("rollback",nullptr)<0) - { - return -1; - } - else - { - _operated=true; - return conn.getAffectedRows(); - } -} - -bool Transaction::isReady() const -{ - return _isReady; -} diff --git a/MySQLTransaction.h b/MySQLTransaction.h deleted file mode 100644 index b962ff5..0000000 --- a/MySQLTransaction.h +++ /dev/null @@ -1,26 +0,0 @@ -#include "MySQLWrapper.h" - -class Transaction -{ -public: - /// Start Transaction on construct. - Transaction(MySQLConn& MySQLConnection); - - /// Rollback transaction on destruct. If commit() or rollback() is called, this function will not execute any sql. - ~Transaction(); - - /// NOTE: A Transaction can only call commit() or rollback() once, until it calls start commit again. - /// Commit transaction. Equals to SQL: COMMIT - /// ReturnValue: -1 SQL Error. -2 Operated >=0 Affected Rows - int commit(); - /// Rollback transaction. Equals to SQL: ROLLBACK - /// ReturnValue: -1 SQL Error. -2 Operated >=0 Affected Rows - int rollback(); - - /// Check if a Transaction object is ready. If it is ready, then a transaction has started. - bool isReady() const; -private: - MySQLConn& conn; - bool _isReady; - bool _operated; -}; diff --git a/MySQLWrapper b/MySQLWrapper index 5de3e0c..621ef36 160000 --- a/MySQLWrapper +++ b/MySQLWrapper @@ -1 +1 @@ -Subproject commit 5de3e0c2954ad4051e5b794453015457b7f0b459 +Subproject commit 621ef36565bd0d262437e879a833ae3a5c59cd0d diff --git a/bs_util.cpp b/bs_util.cpp new file mode 100644 index 0000000..4a0d459 --- /dev/null +++ b/bs_util.cpp @@ -0,0 +1,83 @@ +#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 SQLParseInt(int& nval) +{ + return + [&](MySQLResult& res) + { + res.stepRow([&](char** val,unsigned long* len) + { + nval=ParseInt(val[0]); + }); + }; +} diff --git a/bs_util.h b/bs_util.h new file mode 100644 index 0000000..aa08a9d --- /dev/null +++ b/bs_util.h @@ -0,0 +1,22 @@ +#pragma once +#include "Request.h" +#include "Response.h" +#include "Session.h" +#include "json.hpp" +#include "jsonfail.h" +#include "Util.h" +using json=nlohmann::json; + +/// General Json Failure Function. +void jsonfail_func(json& j,int errcode,const std::string& detail=""); + +/// Get Permission of User +/// If failed to get level, error message will be written to Response in JSON style. Then exit(0) is called. +/// If success, returns permission_level. +/// Returns -1 if control flow reaches unknown branch. +int getPermissionLevel(const std::string& Username,Response& res); + +std::function SQLParseInt(int& nval); + +#define postval(NAME) if(req.post[#NAME].empty()){jsonfail(err_missing_parameter);break;}string NAME=req.post[#NAME] +#define startdb() DBInfo db;if(db.readConfig()<0){jsonfail(err_config);break;} MySQLConn conn;if(db.connectProxy(conn)<0){jsonfail(err_connect);break;} diff --git a/addbook.cpp b/src/addbook.cpp similarity index 100% rename from addbook.cpp rename to src/addbook.cpp diff --git a/addbookobject.cpp b/src/addbookobject.cpp similarity index 99% rename from addbookobject.cpp rename to src/addbookobject.cpp index 595dee9..8ca46b9 100644 --- a/addbookobject.cpp +++ b/src/addbookobject.cpp @@ -2,6 +2,7 @@ #include "Util.h" #include "jsonfail.h" #include "json.hpp" +#include "MySQLTransaction.h" using namespace std; using json = nlohmann::json; diff --git a/addbooktype.cpp b/src/addbooktype.cpp similarity index 100% rename from addbooktype.cpp rename to src/addbooktype.cpp diff --git a/allowuser.cpp b/src/allowuser.cpp similarity index 100% rename from allowuser.cpp rename to src/allowuser.cpp diff --git a/blockuser.cpp b/src/blockuser.cpp similarity index 100% rename from blockuser.cpp rename to src/blockuser.cpp diff --git a/editbook.cpp b/src/editbook.cpp similarity index 100% rename from editbook.cpp rename to src/editbook.cpp diff --git a/editbooktype.cpp b/src/editbooktype.cpp similarity index 100% rename from editbooktype.cpp rename to src/editbooktype.cpp diff --git a/enableuser.cpp b/src/enableuser.cpp similarity index 100% rename from enableuser.cpp rename to src/enableuser.cpp diff --git a/explore.cpp b/src/explore.cpp similarity index 100% rename from explore.cpp rename to src/explore.cpp diff --git a/installer.cpp b/src/installer.cpp similarity index 100% rename from installer.cpp rename to src/installer.cpp diff --git a/join.cpp b/src/join.cpp similarity index 100% rename from join.cpp rename to src/join.cpp diff --git a/login.cpp b/src/login.cpp similarity index 50% rename from login.cpp rename to src/login.cpp index 56510d9..4171f69 100644 --- a/login.cpp +++ b/src/login.cpp @@ -1,11 +1,7 @@ -#include "Session.h" -#include "Util.h" -#include "json.hpp" -#include "jsonfail.h" +#include "bs_util.h" +#include +#include using namespace std; -using json=nlohmann::json; - -#define postval(NAME) string NAME=req.post[#NAME] int main() { @@ -14,11 +10,9 @@ int main() Response res; json j; - auto jsonfail=[&](int errcode,const string& detail="") + auto jsonfail=[&](int errcode,const std::string& dtl="") { - j["success"]=0; - j["errcode"]=errcode; - j["errmsg"]=string(GetErrMsg(errcode))+" : "+detail; + jsonfail_func(j,errcode,dtl); }; do @@ -29,7 +23,7 @@ int main() break; } - if(!se.isNew()||!se.getUser().empty()) + if(!se.getUser().empty()) { /// Logged in... j["success"]=2; @@ -42,53 +36,26 @@ int main() jsonfail(err_method_not_supported); break; } - if(req.post["username"].empty() || - req.post["password"].empty() ) - { - jsonfail(err_missing_parameter); - break; - } postval(username); postval(password); - DBInfo db; - MySQLConn conn; - if(db.readConfig()<0) - { - jsonfail(err_config); - break; - } + startdb(); - if(db.connectProxy(conn)<0) - { - jsonfail(err_connect); - break; - } - - /// Check if password is correct - bool authpass=false; + int count_val; 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) - { - if(string(val[0])=="1") - { - authpass=true; - } - }); - })<0) + username, + "' and password='", + password, + "'"), + SQLParseInt(count_val) + )<0) { jsonfail(err_sql,"Step 1"); break; } - if(!authpass) + if(count_val!=1) { jsonfail(err_data,"Auth Failed"); break; @@ -99,13 +66,7 @@ int main() 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) + SQLParseInt(account_status))<0) { jsonfail(err_sql,"Step 2"); break; @@ -123,12 +84,6 @@ int main() 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 int ret; @@ -138,12 +93,24 @@ int main() break; } - j["success"]=1; + if(account_status==2) + { + /// Need verify, redirect to account enable. + j["success"]=3; + j["next_url"]="/booksys/enable.html"; + } + else + { + j["success"]=1; + j["next_url"]="/booksys/dashboard.html"; + } } while(0); + se.writeToResponse(res); res.content.append(j.dump()); - res.show(); return 0; } + + diff --git a/logout.cpp b/src/logout.cpp similarity index 100% rename from logout.cpp rename to src/logout.cpp diff --git a/removebook.cpp b/src/removebook.cpp similarity index 100% rename from removebook.cpp rename to src/removebook.cpp diff --git a/removebooktype.cpp b/src/removebooktype.cpp similarity index 100% rename from removebooktype.cpp rename to src/removebooktype.cpp diff --git a/search.cpp b/src/search.cpp similarity index 100% rename from search.cpp rename to src/search.cpp diff --git a/web/booksys/img/booksys_icon.png b/web/booksys/img/booksys_icon.png new file mode 100644 index 0000000..dd12e56 Binary files /dev/null and b/web/booksys/img/booksys_icon.png differ diff --git a/web/booksys/img/default_user.png b/web/booksys/img/default_user.png new file mode 100644 index 0000000..98bb652 Binary files /dev/null and b/web/booksys/img/default_user.png differ diff --git a/web/booksys/img/notice_icon.png b/web/booksys/img/notice_icon.png new file mode 100644 index 0000000..0193f07 Binary files /dev/null and b/web/booksys/img/notice_icon.png differ diff --git a/web/booksys/install-failed.html b/web/booksys/install-failed.html deleted file mode 100644 index 0f43046..0000000 --- a/web/booksys/install-failed.html +++ /dev/null @@ -1,14 +0,0 @@ - - - - 安装失败 - - - - -

出现了一些问题导致安装无法进行:

-

错误代码: {ErrCode}

-

错误信息: {ErrMsg}

-

返回

- - \ No newline at end of file diff --git a/web/booksys/install-success.html b/web/booksys/install-success.html index 9f27d6c..13eda95 100644 --- a/web/booksys/install-success.html +++ b/web/booksys/install-success.html @@ -3,11 +3,24 @@ 安装成功 + + + + +

你已经安装好了BookSystem.

现在开始使用吧!

-

前往主页

+ + + \ No newline at end of file diff --git a/web/booksys/install.html b/web/booksys/install.html index a6d1e98..2cebf4b 100644 --- a/web/booksys/install.html +++ b/web/booksys/install.html @@ -57,14 +57,11 @@ obj.html('

'+obj.html()+'

'); } - $(".error").hide(); - $(".error").text("No Error"); + $(".error").fadeTo(0,0).text("No Error"); createError($(".error")); - $(".error").fadeTo(0,0); - $(".error").show(); $("#submit").click(function(){ - $("#submit").hide(); + $("#submit").hide(""); var bar=$("#progressbar").progressbar({ value:false }); @@ -88,17 +85,20 @@ console.log("Install Failure"); $(".error").text("Install Failure."); createError($(".error")); - $(".error").fadeTo(0,1).show().delay(1500).fadeTo(3000,0); + $(".error").fadeTo(0,1).delay(500).fadeTo(500,0,function(){ + $("#submit").show(""); + }); } },"json") .fail(function(err){ console.log("Failed to post"); $(".error").text("Failed to Install"); createError($(".error")); - $(".error").fadeTo(0,1).show().delay(1500).fadeTo(3000,0); + $(".error").fadeTo(0,1).delay(500).fadeTo(500,0,function(){ + $("#submit").show(""); + }); }) .always(function(){ - $("#submit").show(); bar.hide(); }); }); diff --git a/web/booksys/login.html b/web/booksys/login.html new file mode 100644 index 0000000..72b32ab --- /dev/null +++ b/web/booksys/login.html @@ -0,0 +1,105 @@ + + + + BookSystem 登录 + + + + + + + + + + + + + + \ No newline at end of file diff --git a/web/booksys/logout.html b/web/booksys/logout.html new file mode 100644 index 0000000..e69de29 diff --git a/web/booksys/mainpage.html b/web/booksys/mainpage.html new file mode 100644 index 0000000..f30fd45 --- /dev/null +++ b/web/booksys/mainpage.html @@ -0,0 +1,27 @@ + + + + BookSystem 主页 + + + + + + + + + + + +
+ BookSystem + +
+ + \ No newline at end of file