diff --git a/database/database.h b/database/database.h index c8e8bc1..3e7ee5e 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: @@ -32,6 +33,7 @@ class Database 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 80b12ea..94feb9f 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 2d74cb2..a9c5507 100644 --- a/database/permissionsdaosqlite.cpp +++ b/database/permissionsdaosqlite.cpp @@ -24,8 +24,20 @@ 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 42410f4..d0da690 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 cf74691..1b01321 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) { @@ -58,6 +59,12 @@ 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 4af14d1..6411daf 100644 --- a/database/sqlite.h +++ b/database/sqlite.h @@ -23,6 +23,7 @@ class Sqlite : public Database 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();