You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
prodejna/core/permissionservice.cpp

91 lines
2.2 KiB
C++

#include "permissionservice.h"
#include "service.h"
#include <QCryptographicHash>
QList<QSharedPointer<Permission> > PermissionService::forPlugin(const QString &pluginId)
{
/*Transaction tr;
odb::database *db = Context::instance().db();
permQuery q(permQuery::pluginId == pluginId);
permResult result = db->query<Permission>(q);
QList<QSharedPointer<Permission> > ret;
for (permResult::iterator it = result.begin(); it != result.end(); it++)
{
ret.append(it.load());
}
tr.commit();
return ret;*/
return {};
}
QSharedPointer<Permission> PermissionService::forNameAndPlugin(const QString &name, const QString &pluginId)
{
qx::QxSqlQuery q;
q.where("permissionName").isEqualTo(name)
.and_("pluginId").isEqualTo(pluginId);
QList<QSharedPointer<Permission>> 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<Permission>();
}
bool PermissionService::checkLogin(const QString &login, const QString &password)
{
QSharedPointer<User> user = loadUser(login);
if (user.data())
{
return user->password() == encryptPassword(password) && user->active();
}
return false;
}
QSharedPointer<User> PermissionService::loadUser(const QString &login)
{
Service<User> srvUser;
auto admin = srvUser.all("login = '" + login + "'");
if (admin.count() == 1) {
Service<Role> srvRole;
for (auto& role : admin[0]->listRoles()) {
srvRole.load(role);
}
return admin[0];
}
return {};
}
void PermissionService::checkForAdmin()
{
Service<User> srvUser;
auto admUser = srvUser.all("isAdmin = 1");
if (admUser.isEmpty())
{
QSharedPointer<User> 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());
}