From 81816dd28c939160cd7e0117f5dbdb4b1bb7280c Mon Sep 17 00:00:00 2001 From: Albert S Date: Fri, 3 May 2019 15:56:52 +0200 Subject: [PATCH] userdaosqlite: query enabled + implemented save --- database/userdaosqlite.cpp | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/database/userdaosqlite.cpp b/database/userdaosqlite.cpp index 83e9fa4..b9a6fee 100644 --- a/database/userdaosqlite.cpp +++ b/database/userdaosqlite.cpp @@ -41,10 +41,10 @@ std::optional UserDaoSqlite::find(std::string username) try { User user; - auto stmt = *db << "SELECT username, password, salt, permissions FROM user WHERE username = ?" << username; + auto stmt = *db << "SELECT username, password, salt, permissions, enabled FROM user WHERE username = ?" << username; int perms = 0; - stmt >> std::tie(user.login, user.password, user.salt, perms); + stmt >> std::tie(user.login, user.password, user.salt, perms, user.enabled); user.permissions = Permissions { perms }; return std::move(user); @@ -64,10 +64,10 @@ std::optional UserDaoSqlite::find(int id) try { User user; - auto stmt = *db << "SELECT username, password, salt, permissions FROM user WHERE id = ?" << id; + auto stmt = *db << "SELECT username, password, salt, permissions, enabled FROM user WHERE id = ?" << id; int perms = 0; - stmt >> std::tie(user.login, user.password, user.salt, perms); + stmt >> std::tie(user.login, user.password, user.salt, perms, user.enabled); user.permissions = Permissions { perms }; return std::move(user); @@ -89,5 +89,15 @@ void UserDaoSqlite::deleteUser(std::string username) void UserDaoSqlite::save(const User &u) { + try + { + auto q = *db << "INSERT OR REPLACE INTO user(id, username, password, salt, permissions, enabled) VALUES((SELECT id FROM user WHERE username = ?), ?,?,?,?,?)"; + q << u.login << u.login << u.password << u.salt << u.permissions.getPermissions() << u.enabled; + q.execute(); + } + catch(sqlite::sqlite_exception &e) + { + throwFrom(e); + } }