#include "core-odb.hxx" #include "permissionservice.h" #include typedef odb::query permQuery; typedef odb::result permResult; PermissionService::PermissionService() { } PermissionService::~PermissionService() { } QList > PermissionService::forPlugin(const QString &pluginId) { Transaction tr; odb::database *db = Context::instance().db(); permQuery q(permQuery::pluginId == pluginId); permResult result = db->query(q); QList > ret; for (permResult::iterator it = result.begin(); it != result.end(); it++) { ret.append(it.load()); } tr.commit(); return ret; } QSharedPointer PermissionService::forNameAndPlugin(const QString &name, const QString &pluginId) { Transaction tr; odb::database *db = Context::instance().db(); permQuery q(permQuery::pluginId == pluginId && permQuery::permissionName == name); QSharedPointer p = db->query_one(q); tr.commit(); return p; } bool PermissionService::checkLogin(const QString &login, const QString &password) { QSharedPointer user = loadUser(login); if (user.data()) { return user->password() == encryptPassword(password) && user->active(); } return false; } QSharedPointer PermissionService::loadUser(const QString &login) { odb::database *db = Context::instance().db(); Transaction tr; return db->query_one("login = " + odb::query::_ref(login)); } void PermissionService::checkForAdmin() { odb::database *db = Context::instance().db(); Transaction tr; odb::query q(odb::query::isAdmin == true); odb::result r = db->query(q); if (r.empty()) { QSharedPointer admin(new User); admin->setLogin("admin"); admin->setName("Administrator"); admin->setIsAdmin(true); admin->setPassword(encryptPassword("admin")); admin->setActive(true); db->persist(admin); } tr.commit(); } QString PermissionService::encryptPassword(const QString &plainPasswd) { return QString(QCryptographicHash::hash(plainPasswd.toUtf8(),QCryptographicHash::Sha256).toBase64()); }