summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/server/db/db.sql2
-rw-r--r--src/server/db/include/privilege_db.h20
-rw-r--r--src/server/db/privilege_db.cpp28
3 files changed, 41 insertions, 9 deletions
diff --git a/src/server/db/db.sql b/src/server/db/db.sql
index cd721629..680c2edf 100644
--- a/src/server/db/db.sql
+++ b/src/server/db/db.sql
@@ -94,7 +94,7 @@ DROP TRIGGER IF EXISTS app_pkg_view_delete_trigger;
CREATE TRIGGER app_pkg_view_delete_trigger
INSTEAD OF DELETE ON app_pkg_view
BEGIN
- DELETE FROM app WHERE app_id=OLD.app_id AND pkg_id=OLD.pkg_id;
+ DELETE FROM app WHERE app_id=OLD.app_id;
DELETE FROM pkg WHERE pkg_id NOT IN (SELECT DISTINCT pkg_id from app);
END;
diff --git a/src/server/db/include/privilege_db.h b/src/server/db/include/privilege_db.h
index 4b7aa257..77ab4c2f 100644
--- a/src/server/db/include/privilege_db.h
+++ b/src/server/db/include/privilege_db.h
@@ -47,6 +47,7 @@ enum class QueryType {
EAddAppPrivileges,
ERemoveAppPrivileges,
EPkgIdExists,
+ EGetPkgId,
};
class PrivilegeDb {
@@ -59,10 +60,11 @@ private:
const std::map<QueryType, const char * const > Queries = {
{ QueryType::EGetPkgPrivileges, "SELECT privilege_name FROM app_privilege_view WHERE pkg_name=?"},
{ QueryType::EAddApplication, "INSERT INTO app_pkg_view (app_name, pkg_name) VALUES (?, ?)" },
- { QueryType::ERemoveApplication, "DELETE FROM app_pkg_view WHERE app_name=? AND pkg_name=?" },
+ { QueryType::ERemoveApplication, "DELETE FROM app_pkg_view WHERE app_name=?" },
{ QueryType::EAddAppPrivileges, "INSERT INTO app_privilege_view (app_name, privilege_name) VALUES (?, ?)" },
{ QueryType::ERemoveAppPrivileges, "DELETE FROM app_privilege_view WHERE app_name=?" },
- { QueryType::EPkgIdExists, "SELECT * FROM pkg WHERE name=?" }
+ { QueryType::EPkgIdExists, "SELECT * FROM pkg WHERE name=?" },
+ { QueryType::EGetPkgId, " SELECT pkg_name FROM app_pkg_view WHERE app_name = ?" },
};
/**
@@ -114,6 +116,16 @@ public:
void RollbackTransaction(void);
/**
+ * Return package id associated with a given application id
+ *
+ * @param appId - application identifier
+ * @param[out] pkgId - return application's pkgId
+ * @return true is application exists, false otherwise
+ * @exception DB::SqlConnection::Exception::InternalError on internal error
+ */
+ bool GetAppPkgId(const std::string &appId, std::string &pkgId);
+
+ /**
* Retrieve list of privileges assigned to a pkgId
*
* @param pkgId - package identifier
@@ -138,12 +150,10 @@ public:
* Remove an application from the database
*
* @param appId - application identifier
- * @param pkgId - package identifier
* @param[out] pkgIdIsNoMore - return info if pkgId is in the database
* @exception DB::SqlConnection::Exception::InternalError on internal error
*/
- void RemoveApplication(const std::string &appId, const std::string &pkgId,
- bool &pkgIdIsNoMore);
+ void RemoveApplication(const std::string &appId, bool &pkgIdIsNoMore);
/**
* Remove privileges assigned to application
diff --git a/src/server/db/privilege_db.cpp b/src/server/db/privilege_db.cpp
index d0dcd6ff..f407c6be 100644
--- a/src/server/db/privilege_db.cpp
+++ b/src/server/db/privilege_db.cpp
@@ -111,6 +111,23 @@ bool PrivilegeDb::PkgIdExists(const std::string &pkgId)
});
}
+bool PrivilegeDb::GetAppPkgId(const std::string &appId, std::string &pkgId)
+{
+ return try_catch<bool>([&] {
+ DB::SqlConnection::DataCommandAutoPtr command =
+ mSqlConnection->PrepareDataCommand(Queries.at(QueryType::EGetPkgId));
+ command->BindString(1, appId.c_str());
+
+ if (!command->Step()) {
+ // No application with such appId
+ return false;
+ }
+
+ pkgId = command->GetColumnString(0);
+ return true;
+ });
+}
+
void PrivilegeDb::AddApplication(const std::string &appId,
const std::string &pkgId, bool &pkgIdIsNew)
{
@@ -135,15 +152,20 @@ void PrivilegeDb::AddApplication(const std::string &appId,
}
void PrivilegeDb::RemoveApplication(const std::string &appId,
- const std::string &pkgId, bool &pkgIdIsNoMore)
+ bool &pkgIdIsNoMore)
{
try_catch<void>([&] {
+ std::string pkgId;
+ if (!GetAppPkgId(appId, pkgId)) {
+ pkgIdIsNoMore = false;
+ return;
+ }
+
DB::SqlConnection::DataCommandAutoPtr command =
mSqlConnection->PrepareDataCommand(
Queries.at(QueryType::ERemoveApplication));
command->BindString(1, appId.c_str());
- command->BindString(2, pkgId.c_str());
if (command->Step()) {
LogPedantic("Unexpected SQLITE_ROW answer to query: " <<
@@ -151,7 +173,7 @@ void PrivilegeDb::RemoveApplication(const std::string &appId,
};
command->Reset();
- LogPedantic( "Removed appId: " << appId << ", pkgId: " << pkgId);
+ LogPedantic( "Removed appId: " << appId);
pkgIdIsNoMore = !(this->PkgIdExists(pkgId));
});