#include "permissionservice.h" #include "service.h" #include 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;*/ return {}; } QSharedPointer PermissionService::forNameAndPlugin(const QString &name, const QString &pluginId) { qx::QxSqlQuery q; q.where("permissionName").isEqualTo(name) .and_("pluginId").isEqualTo(pluginId); QList> ret; auto err = qx::dao::fetch_by_query(q, ret); if (err.isValid()) { qDebug() << err.text(); // ToDo - log error } return ret.count() > 0 ? ret[0] : QSharedPointer(); } 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) { Service srvUser; auto admin = srvUser.all("login = '" + login + "'"); if (admin.count() == 1) { Service srvRole; for (auto& role : admin[0]->listRoles()) { srvRole.load(role); } return admin[0]; } return {}; } void PermissionService::checkForAdmin() { Service srvUser; auto admUser = srvUser.all("isAdmin = 1"); if (admUser.isEmpty()) { QSharedPointer admin(new User); admin->setLogin("admin"); admin->setName("Administrator"); admin->setIsAdmin(true); admin->setPassword(encryptPassword("admin")); admin->setActive(true); srvUser.save(admin); } } QString PermissionService::encryptPassword(const QString &plainPasswd) { return QString(QCryptographicHash::hash(plainPasswd.toUtf8(),QCryptographicHash::Sha256).toBase64()); }