PermissionsDao: Add save()

This commit is contained in:
Albert S. 2021-10-05 21:51:31 +02:00
parent c4072a7e95
commit f002969cc1
3 changed files with 20 additions and 0 deletions

View File

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

View File

@ -41,3 +41,21 @@ std::optional<Permissions> PermissionsDaoSqlite::find(std::string pagename, std:
return Permissions{permissions}; return Permissions{permissions};
} }
void PermissionsDaoSqlite::save(std::string pagename, std::string username, Permissions perms)
{
try
{
auto query =
*db
<< "INSERT OR REPLACE INTO permissions (id, permissions, userid, page) VALUES((SELECT id FROM permissions "
"WHERE page = (SELECT id FROM page WHERE name = ?) AND userid = (SELECT id FROM user WHERE username = "
"?)), ?, (SELECT id FROM user WHERE username = ?), (SELECT id FROM page WHERE name = ?))";
query << pagename << username << perms.getPermissions() << username << pagename;
query.execute();
}
catch(const sqlite::errors::no_rows &e)
{
throwFrom(e);
}
}

View File

@ -9,6 +9,7 @@ class PermissionsDaoSqlite : public PermissionsDao, protected SqliteDao
PermissionsDaoSqlite(); PermissionsDaoSqlite();
std::optional<Permissions> find(std::string pagename, std::string username) override; std::optional<Permissions> find(std::string pagename, std::string username) override;
virtual void save(std::string pagename, std::string username, Permissions perms) override;
using SqliteDao::SqliteDao; using SqliteDao::SqliteDao;
}; };