diff --git a/database/database.h b/database/database.h index 040ae3e..67fc2a6 100644 --- a/database/database.h +++ b/database/database.h @@ -10,6 +10,7 @@ #include "sessiondao.h" #include "userdao.h" #include "categorydao.h" +#include "permissionsdao.h" class Database { private: @@ -26,6 +27,7 @@ private: virtual std::unique_ptr createSessionDao() const = 0; virtual std::unique_ptr createUserDao() const = 0; virtual std::unique_ptr createCategoryDao() const = 0; + virtual std::unique_ptr createPermissionsDao() const = 0; virtual ~Database() { } }; diff --git a/database/permissionsdao.h b/database/permissionsdao.h index 9b508bd..0fdc640 100644 --- a/database/permissionsdao.h +++ b/database/permissionsdao.h @@ -6,7 +6,7 @@ class PermissionsDao { public: PermissionsDao(); - virtual Permissions find(std::string pagename, std::string username) = 0; + virtual std::optional find(std::string pagename, std::string username) = 0; }; #endif // PERMISSIONSDAO_H diff --git a/database/permissionsdaosqlite.cpp b/database/permissionsdaosqlite.cpp index 1afcc83..a3bf537 100644 --- a/database/permissionsdaosqlite.cpp +++ b/database/permissionsdaosqlite.cpp @@ -25,8 +25,19 @@ PermissionsDaoSqlite::PermissionsDaoSqlite() } -Permissions PermissionsDaoSqlite::find(std::string pagename, std::string username) +std::optional PermissionsDaoSqlite::find(std::string pagename, std::string username) { - /* auto query = *db << "SELECT COALESCE( (SELECT permissions FROM permissions WHERE page = ? AND userid = ?), (SELECT permissions FROM user WHERE ID = ?))"; - exec*/ + auto query = *db << "SELECT permissions FROM permissions WHERE page = (SELECT id FROM page WHERE name = ?) AND userid = (SELECT id FROM user WHERE username = ?)"; + query << pagename << username; + int permissions = 0; + try + { + permissions = execInt(query); + } + catch(const sqlite::errors::no_rows &e) + { + return { }; + } + + return Permissions { permissions }; } diff --git a/database/permissionsdaosqlite.h b/database/permissionsdaosqlite.h index b7d38c1..ef06c76 100644 --- a/database/permissionsdaosqlite.h +++ b/database/permissionsdaosqlite.h @@ -8,7 +8,7 @@ class PermissionsDaoSqlite : public PermissionsDao, protected SqliteDao public: PermissionsDaoSqlite(); - Permissions find(std::string pagename, std::string username) override; + std::optional find(std::string pagename, std::string username) override; using SqliteDao::SqliteDao; }; diff --git a/database/sqlite.cpp b/database/sqlite.cpp index 937c388..39b3c8c 100644 --- a/database/sqlite.cpp +++ b/database/sqlite.cpp @@ -26,6 +26,7 @@ SOFTWARE. #include "userdaosqlite.h" #include "categorydaosqlite.h" #include "exceptions.h" +#include "permissionsdaosqlite.h" Sqlite::Sqlite(std::string path) : Database(path) { @@ -62,6 +63,13 @@ std::unique_ptr Sqlite::createCategoryDao() const { return create(); } + + +std::unique_ptr Sqlite::createPermissionsDao() const +{ + return create(); +} + void Sqlite::beginTransaction() { if(!inTransaction) diff --git a/database/sqlite.h b/database/sqlite.h index 97868b1..39b2f9a 100644 --- a/database/sqlite.h +++ b/database/sqlite.h @@ -22,6 +22,7 @@ public: std::unique_ptr createUserDao() const; std::unique_ptr createSessionDao() const; std::unique_ptr createCategoryDao() const; + std::unique_ptr createPermissionsDao() const; void beginTransaction(); void commitTransaction(); void rollbackTransaction();