permissionsdao: no silent fallback to user-permissions if we find none for user

This commit is contained in:
Albert S. 2019-05-03 16:00:36 +02:00
parent 7d1cae24a9
commit 2ee515945d
6 changed files with 27 additions and 5 deletions

View File

@ -10,6 +10,7 @@
#include "sessiondao.h" #include "sessiondao.h"
#include "userdao.h" #include "userdao.h"
#include "categorydao.h" #include "categorydao.h"
#include "permissionsdao.h"
class Database class Database
{ {
private: private:
@ -26,6 +27,7 @@ private:
virtual std::unique_ptr<SessionDao> createSessionDao() const = 0; virtual std::unique_ptr<SessionDao> createSessionDao() const = 0;
virtual std::unique_ptr<UserDao> createUserDao() const = 0; virtual std::unique_ptr<UserDao> createUserDao() const = 0;
virtual std::unique_ptr<CategoryDao> createCategoryDao() const = 0; virtual std::unique_ptr<CategoryDao> createCategoryDao() const = 0;
virtual std::unique_ptr<PermissionsDao> createPermissionsDao() const = 0;
virtual ~Database() { } virtual ~Database() { }
}; };

View File

@ -6,7 +6,7 @@ class PermissionsDao
{ {
public: public:
PermissionsDao(); PermissionsDao();
virtual Permissions find(std::string pagename, std::string username) = 0; virtual std::optional<Permissions> find(std::string pagename, std::string username) = 0;
}; };
#endif // PERMISSIONSDAO_H #endif // PERMISSIONSDAO_H

View File

@ -25,8 +25,19 @@ PermissionsDaoSqlite::PermissionsDaoSqlite()
} }
Permissions PermissionsDaoSqlite::find(std::string pagename, std::string username) std::optional<Permissions> 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 = ?))"; auto query = *db << "SELECT permissions FROM permissions WHERE page = (SELECT id FROM page WHERE name = ?) AND userid = (SELECT id FROM user WHERE username = ?)";
exec*/ query << pagename << username;
int permissions = 0;
try
{
permissions = execInt(query);
}
catch(const sqlite::errors::no_rows &e)
{
return { };
}
return Permissions { permissions };
} }

View File

@ -8,7 +8,7 @@ class PermissionsDaoSqlite : public PermissionsDao, protected SqliteDao
public: public:
PermissionsDaoSqlite(); PermissionsDaoSqlite();
Permissions find(std::string pagename, std::string username) override; std::optional<Permissions> find(std::string pagename, std::string username) override;
using SqliteDao::SqliteDao; using SqliteDao::SqliteDao;
}; };

View File

@ -26,6 +26,7 @@ SOFTWARE.
#include "userdaosqlite.h" #include "userdaosqlite.h"
#include "categorydaosqlite.h" #include "categorydaosqlite.h"
#include "exceptions.h" #include "exceptions.h"
#include "permissionsdaosqlite.h"
Sqlite::Sqlite(std::string path) : Database(path) Sqlite::Sqlite(std::string path) : Database(path)
{ {
@ -62,6 +63,13 @@ std::unique_ptr<CategoryDao> Sqlite::createCategoryDao() const
{ {
return create<CategoryDaoSqlite>(); return create<CategoryDaoSqlite>();
} }
std::unique_ptr<PermissionsDao> Sqlite::createPermissionsDao() const
{
return create<PermissionsDaoSqlite>();
}
void Sqlite::beginTransaction() void Sqlite::beginTransaction()
{ {
if(!inTransaction) if(!inTransaction)

View File

@ -22,6 +22,7 @@ public:
std::unique_ptr<UserDao> createUserDao() const; std::unique_ptr<UserDao> createUserDao() const;
std::unique_ptr<SessionDao> createSessionDao() const; std::unique_ptr<SessionDao> createSessionDao() const;
std::unique_ptr<CategoryDao> createCategoryDao() const; std::unique_ptr<CategoryDao> createCategoryDao() const;
std::unique_ptr<PermissionsDao> createPermissionsDao() const;
void beginTransaction(); void beginTransaction();
void commitTransaction(); void commitTransaction();
void rollbackTransaction(); void rollbackTransaction();