From cd3e6607659103a484e9bf8bef713c9455094cb4 Mon Sep 17 00:00:00 2001 From: Brett Date: Tue, 22 Aug 2023 21:57:26 -0400 Subject: [PATCH] finish logical expression evaluator --- .gitignore | 4 +- crow_test/data/db/users.sqlite | Bin 20480 -> 0 bytes crow_test/data/session/.expirations | 1 - crow_test/data/session/aubTl45vzPn5feHw.json | 1 - include/crowsite/config.h | 6 +- src/crowsite/requests/jellyfin.cpp | 2 +- src/crowsite/site/cache.cpp | 79 +++++++++++++++---- src/main.cpp | 35 +++++++- 8 files changed, 103 insertions(+), 25 deletions(-) delete mode 100644 crow_test/data/db/users.sqlite delete mode 100644 crow_test/data/session/.expirations delete mode 100644 crow_test/data/session/aubTl45vzPn5feHw.json diff --git a/.gitignore b/.gitignore index 0bd11af..a4ec9fd 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,6 @@ cmake-build-debug/ cmake-build-debug-address/ cmake-build-minsizerel/ cmake-build-release/ -cmake-build-relwithdebinfo/ \ No newline at end of file +cmake-build-relwithdebinfo/ +cmake-build-relwithdebaddr/ +crow_test/data/ diff --git a/crow_test/data/db/users.sqlite b/crow_test/data/db/users.sqlite deleted file mode 100644 index d4654215a225053983bf4fd6413b3ae888e959c4..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 20480 zcmeI&J#X4T7zc10NJuMzBXvN{bW0RaLa=!;&{x?BgQ!w<$k0ar)1A4;KH2yDh11BCBTZh+HH~scf?34e`Fcd5%jN z&7Li`tZ@$+np#IzANtGLEpF!X#ck`J)15zNx0u62YKzcry-^#px5{NTo7<$^CgYvO zC=h@E1Rwwb2tWV=5P$##AOL~q5}0xu&h6>jX@W9!h2#37_1UYbr`o;J?TUfOK1;k; z9CtwSiaMN-NsO=a;RB+N-f?VjpY@T9EOoR%kmXpbawM(ja#NcG=2u73*&tKU?}tM^ zUrdIg$~fK6a=BnEe8OMLRW(%|T#M3i*6cT~Zkt9vcBgCClj!YvYd1O8`JQmp8>KU4 zu69uq+POeA86Bm~^F(4=bvdi^Nk>>Q$Vs7)pXa5pKO(YCf1C|khj1v&hk|S{BJ$(top1;hOFL)66%@swfbE00bZa0SG_<0uX=z1Rwwb2)qb^ wO?qXa;LU9RpJu*ORz-mT1Rwwb2tWV=5P$##AOHafK;VT4F!t?N%l{Pk3Gc=-asU7T diff --git a/crow_test/data/session/.expirations b/crow_test/data/session/.expirations deleted file mode 100644 index 63655a4..0000000 --- a/crow_test/data/session/.expirations +++ /dev/null @@ -1 +0,0 @@ -aubTl45vzPn5feHw 1692660008 diff --git a/crow_test/data/session/aubTl45vzPn5feHw.json b/crow_test/data/session/aubTl45vzPn5feHw.json deleted file mode 100644 index d6599b7..0000000 --- a/crow_test/data/session/aubTl45vzPn5feHw.json +++ /dev/null @@ -1 +0,0 @@ -{"clientID":"50a21c33-66c4-5a0f-902f-9434632025e6","clientToken":"bsmbPoeuxWBE0/j1W9O/EOs85s79wIS1P5qYHovfhkIhNwl5SGkcpHww+DnbzwNCJ1y+Tb2hLpic3qtgbbtWvg=="} \ No newline at end of file diff --git a/include/crowsite/config.h b/include/crowsite/config.h index 786a174..9a31853 100644 --- a/include/crowsite/config.h +++ b/include/crowsite/config.h @@ -8,8 +8,10 @@ #include #include -#define CROW_STATIC_DIRECTORY "/home/brett/projects/cpp/crowsite/crow_test/static/" -#define SITE_FILES_PATH "/home/brett/projects/cpp/crowsite/crow_test" +//#define CROW_STATIC_DIRECTORY "/home/brett/projects/cpp/crowsite/crow_test/static/" +#define CROW_STATIC_DIRECTORY "/home/brett/Documents/code/c++/crowsite/crow_test/static/" +//#define SITE_FILES_PATH "/home/brett/projects/cpp/crowsite/crow_test" +#define SITE_FILES_PATH "/home/brett/Documents/code/c++/crowsite/crow_test" #define CROW_STATIC_ENDPOINT "/static/" #define MILES_SITE diff --git a/src/crowsite/requests/jellyfin.cpp b/src/crowsite/requests/jellyfin.cpp index 32bf465..952d13a 100644 --- a/src/crowsite/requests/jellyfin.cpp +++ b/src/crowsite/requests/jellyfin.cpp @@ -51,7 +51,7 @@ namespace cs::jellyfin std::string getUserData() { -#define url "https://media.tpgc.me/Users" + const auto url = "https://media.tpgc.me/Users"; cs::request request; request.setContentHeaderJson(); diff --git a/src/crowsite/site/cache.cpp b/src/crowsite/site/cache.cpp index 4e378c1..4334f51 100644 --- a/src/crowsite/site/cache.cpp +++ b/src/crowsite/site/cache.cpp @@ -119,6 +119,24 @@ namespace cs OPEN, // ( CLOSE // ) }; + + static std::string decodeName(TokenType type){ + switch (type){ + case TokenType::AND: + return "AND"; + case TokenType::OR: + return "OR"; + case TokenType::NOT: + return "NOT"; + case TokenType::IDENT: + return "IDENT"; + case TokenType::OPEN: + return "OPEN"; + case TokenType::CLOSE: + return "CLOSE"; + } + } + struct Token { TokenType type; @@ -164,12 +182,6 @@ namespace cs { return str[s_index++]; } - - public: - explicit LogicalEval(std::string str): str(std::move(str)) - { - processString(); - } void processString() { @@ -202,6 +214,7 @@ namespace cs break; default: std::string token; + token += c; while (hasNext() && !isSpecial(peek())) token += consume(); tokens.emplace_back(TokenType::IDENT, token); @@ -210,31 +223,63 @@ namespace cs } } - static inline bool isEmpty(const std::string& token, const RuntimeContext& context) + static inline bool isTrue(const RuntimeContext& context, const std::string& token) { - return !context.contains(token) || context.at(token).empty(); + BLT_DEBUG("isTrue for token '%s' contains? %s", token.c_str(), context.contains(token) ? "True" : "False"); + return context.contains(token) && !context.at(token).empty(); } // http://www.cs.unb.ca/~wdu/cs4613/a2ans.htm bool factor(const RuntimeContext& context) { if (!hasNextToken()) throw LexerSyntaxError("Processing boolean factor but no token was found!"); - - } - - bool term(const RuntimeContext& context) - { - + auto next = consumeToken(); + switch (next.type){ + case TokenType::IDENT: + if (!next.value.has_value()) + throw LexerSyntaxError("Token identifier does not have a value!"); + return isTrue(context, next.value.value()); + case TokenType::NOT: + return !factor(context); + case TokenType::OPEN: + { +// auto ret = ; +// if (consume() != ')') +// throw LexerSyntaxError("Found token '(', parsed expression, but not ')' was found!"); + return expr(context); + } + default: + throw LexerSyntaxError("Weird token found while parsing tokens, type: " + decodeName(next.type)); + } } bool expr(const RuntimeContext& context) { - + auto fac = factor(context); + if (!hasNextToken()) + return fac; + auto next = consumeToken(); + switch (next.type) { + case TokenType::AND: + return fac && expr(context); + case TokenType::OR: + return fac || expr(context); + case TokenType::CLOSE: + default: + //BLT_WARN("I've reached the default, help! %s", decodeName(next.type).c_str()); + return fac; + } + } + + public: + explicit LogicalEval(std::string str): str(std::move(str)) + { + processString(); } bool eval(const RuntimeContext& context) { - + return expr(context); } }; @@ -286,7 +331,7 @@ namespace cs auto searchField = lexer.str.substr(lexer.index); auto endTokenLoc = RuntimeLexer::findLastTagLocation(token, searchField); - auto + } else results += lexer.consume(); } diff --git a/src/main.cpp b/src/main.cpp index 84572c5..3d94a0f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -99,6 +99,36 @@ bool isUserAdmin(CrowApp& app, const crow::request& req) return cs::isUserAdmin(cs::getUserFromID(s_clientID)); } +void generateRuntimeContext(const site_params& params, cs::RuntimeContext& context){ + auto& session = params.app.get_context(params.req); + auto s_clientID = session.get("clientID", ""); + auto s_clientToken = session.get("clientToken", ""); + if (cs::isUserLoggedIn(s_clientID, s_clientToken)) + { + auto username = cs::getUserFromID(s_clientID); + auto perms = cs::getUserPermissions(username); + auto isAdmin = cs::isUserAdmin(username); + context["_logged_in"] = "True"; + context["_username"] = username; + if (isAdmin) + context["_admin"] = "True"; + if (perms & cs::PERM_READ_FILES) + context["_read_files"] = "True"; + if (perms & cs::PERM_WRITE_FILES) + context["_write_files"] = "True"; + if (perms & cs::PERM_CREATE_POSTS) + context["_create_posts"] = "True"; + if (perms & cs::PERM_CREATE_COMMENTS) + context["_create_comments"] = "True"; + if (perms & cs::PERM_CREATE_SHARES) + context["_create_shares"] = "True"; + if (perms & cs::PERM_EDIT_POSTS) + context["_edit_posts"] = "True"; + if (perms & cs::PERM_EDIT_COMMENTS) + context["_edit_comments"] = "True"; + } +} + crow::response handle_root_page(const site_params& params) { //auto page = crow::mustache::load("index.html"); // @@ -120,6 +150,7 @@ crow::response handle_root_page(const site_params& params) auto user_perms = cs::getUserPermissions(cs::getUserFromID(s_clientID)); crow::mustache::context ctx; + cs::RuntimeContext context; // pass perms in if (user_perms & cs::PERM_ADMIN) @@ -136,7 +167,7 @@ crow::response handle_root_page(const site_params& params) auto referer = params.req.url_params.get("referer"); if (referer) ctx["referer"] = referer; - auto page = crow::mustache::compile(params.engine.fetch(params.name)); + auto page = crow::mustache::compile(params.engine.fetch(params.name, context)); return page.render(ctx); } @@ -163,7 +194,7 @@ int main(int argc, const char** argv) blt::arg_parse parser; parser.addArgument(blt::arg_builder("--tests").setAction(blt::arg_action_t::STORE_TRUE).build()); parser.addArgument(blt::arg_builder({"--port", "-p"}).setDefault(8080).build()); - parser.addArgument(blt::arg_builder("token").build()); + parser.addArgument(blt::arg_builder("token").setRequired().build()); auto args = parser.parse_args(argc, argv); cs::jellyfin::setToken(blt::arg_parse::get(args["token"])); cs::jellyfin::processUserData();