#include <string>
#include <utility>
+#include <vector>
namespace parser {
class ActionSchema {
public:
explicit ActionSchema(std::string pkgid, std::string name,
- std::string category, std::string json_str)
+ std::string category, std::vector<std::string> privileges,
+ std::string json_str)
: pkgid_(std::move(pkgid)), name_(std::move(name)),
- category_(std::move(category)), json_str_(std::move(json_str)) {}
+ category_(std::move(category)), privielges_(std::move(privileges)),
+ json_str_(std::move(json_str)) {}
ActionSchema() = default;
~ActionSchema() = default;
std::string GetPkgId() const { return pkgid_; }
std::string GetName() const { return name_; }
std::string GetCategory() const { return category_; }
+ std::vector<std::string> GetPrivileges() const { return privielges_; }
std::string GetJsonStr() const { return json_str_; }
private:
std::string pkgid_;
std::string name_;
std::string category_;
+ std::vector<std::string> privielges_;
std::string json_str_;
};
#include <json/json.h>
+#include <algorithm>
#include <fstream>
#include <string>
+#include <vector>
#include "common/utils/logging.hh"
#include "pkgmgr_plugin_parser/action_schema.hh"
// TODO
std::string name = root["name"].asString();
std::string category = root["category"].asString();
+ const auto priv_array = root["required-privileges"];
+ std::vector<std::string> privs;
+ privs.reserve(priv_array.size());
+ std::transform(priv_array.begin(), priv_array.end(),
+ std::back_inserter(privs),
+ [](const auto& priv) { return priv.asString(); });
std::string json_str = JsonToString(root);
LOG(DEBUG) << "Parased action for pkgid[ " << pkgid << " ] : " << name;
LOG(DEBUG) << json_str;
- return ActionSchema(pkgid, name, category, json_str);
+ return ActionSchema(pkgid, name, category, privs, json_str);
}
} // namespace parser
" category TEXT,\n"
" json_str TEXT,\n"
" PRIMARY KEY (pkgid, action_name))";
+constexpr char kCreatePrivilegeTableQuery[] =
+ "CREATE TABLE IF NOT EXISTS privilege (\n"
+ " pkgid TEXT,\n"
+ " action_name TEXT,\n"
+ " privilege TEXT,\n"
+ " PRIMARY KEY (pkgid, action_name, privilege)\n"
+ " FOREIGN KEY (pkgid, action_name) REFERENCES action (pkgid, action_name) ON DELETE CASCADE)";
constexpr char kInsertQuery[] =
"INSERT INTO action (pkgid, action_name, category, json_str) "
"VALUES (?, ?, ?, ?)";
"DELETE FROM action WHERE pkgid = ?";
constexpr char kSelectQuery[] =
"SELECT json_str FROM action WHERE pkgid = ?";
+constexpr char kInsertPrivQuery[] =
+ "INSERT INTO privilege (pkgid, action_name, privilege) VALUES (?, ?, ?)";
} // namespace
SqliteDb::SqliteDb()
: conn_(kDbPath, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE) {
+ conn_.OneStepExec({ "PRAGMA foreign_keys=ON" });
if (!static_cast<bool>(conn_.Exec({ kCreateActionTableQuery })))
LOG(ERROR) << "Failed to create action table";
+ if (!static_cast<bool>(conn_.Exec({ kCreatePrivilegeTableQuery })))
+ LOG(ERROR) << "Failed to create privilege table";
}
bool SqliteDb::Install(const std::string& pkgid, const std::vector<ActionSchema>& schemas) {
<< ", error: " << static_cast<const char*>(result);
return false;
}
+
+ if (!InsertPrivileges(pkgid, schema.GetName(), schema.GetPrivileges()))
+ return false;
}
} catch (const tizen_base::DbException& e) {
LOG(ERROR) << "Exception occurred: " << e.what();
<< ", error: " << static_cast<const char*>(result);
return false;
}
+
+ if (!InsertPrivileges(pkgid, schema.GetName(), schema.GetPrivileges()))
+ return false;
}
} catch (const tizen_base::DbException& e) {
LOG(ERROR) << "Exception occurred: " << e.what();
return true;
}
+bool SqliteDb::InsertPrivileges(const std::string& pkgid,
+ const std::string& action_name,
+ const std::vector<std::string>& privileges) {
+ try {
+ auto sql = tizen_base::Database::Sql(kInsertPrivQuery);
+ for (const auto& priv : privileges) {
+ sql.Reset()
+ .Bind(pkgid)
+ .Bind(action_name)
+ .Bind(priv);
+ auto result = conn_.Exec(sql);
+ if (!result) {
+ LOG(ERROR) << "Failed to insert privileges: " << action_name
+ << ", error: " << static_cast<const char*>(result);
+ return false;
+ }
+ }
+ } catch (const tizen_base::DbException& e) {
+ LOG(ERROR) << "Exception occurred: " << e.what();
+ return false;
+ }
+ return true;
+}
+
} // namespace parser