mirror of
https://github.com/arcan1s/queued.git
synced 2025-04-24 15:37:19 +00:00
Pretty result usage, try to split Core class to several
This commit is contained in:
parent
bdb1dd0101
commit
2bd72344ba
2
sources/3rdparty/result
vendored
2
sources/3rdparty/result
vendored
@ -1 +1 @@
|
||||
Subproject commit e03dc8efb056f468b108c5034e19fec96e5b976b
|
||||
Subproject commit fafc01123d53370f7744596fadd4e77212bc55fa
|
@ -49,6 +49,7 @@ QueuedApplication::~QueuedApplication()
|
||||
|
||||
void QueuedApplication::init()
|
||||
{
|
||||
m_core->deinit();
|
||||
m_core->init(m_configuration["config"].toString());
|
||||
}
|
||||
|
||||
|
@ -28,8 +28,7 @@ QVariantHash QueuedTcpServerResponseHelperAuth::auth(const QVariantHash &_data)
|
||||
auto res = QueuedCoreAdaptor::auth(_data["user"].toString(),
|
||||
_data["password"].toString());
|
||||
|
||||
Result::match(
|
||||
res,
|
||||
res.match(
|
||||
[&output](const QString &val) {
|
||||
output = {{"code", 200}, {"token", val}};
|
||||
},
|
||||
@ -51,8 +50,8 @@ bool QueuedTcpServerResponseHelperAuth::tryAuth(const QString &_token)
|
||||
auto res = QueuedCoreAdaptor::auth(_token);
|
||||
|
||||
bool ret = true;
|
||||
Result::match(res, [&ret](const bool val) { ret = val; },
|
||||
[&ret](const QueuedError &err) { ret = false; });
|
||||
res.match([&ret](const bool val) { ret = val; },
|
||||
[&ret](const QueuedError &err) { ret = false; });
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
@ -27,13 +27,13 @@ QueuedTcpServerResponseHelperOption::getOption(const QString &_option)
|
||||
auto res = QueuedCoreAdaptor::getOption(_option);
|
||||
|
||||
QVariantHash output;
|
||||
Result::match(res,
|
||||
[&output](const QVariant &val) {
|
||||
output = {{"code", 200}, {"token", val}};
|
||||
},
|
||||
[&output](const QueuedError &) {
|
||||
output = {{"code", 404}, {"message", "Option not found"}};
|
||||
});
|
||||
res.match(
|
||||
[&output](const QVariant &val) {
|
||||
output = {{"code", 200}, {"token", val}};
|
||||
},
|
||||
[&output](const QueuedError &) {
|
||||
output = {{"code", 404}, {"message", "Option not found"}};
|
||||
});
|
||||
|
||||
return output;
|
||||
}
|
||||
@ -51,8 +51,7 @@ QVariantHash QueuedTcpServerResponseHelperOption::setOption(
|
||||
= QueuedCoreAdaptor::sendOptionEdit(_option, _value["value"], _token);
|
||||
|
||||
QVariantHash output;
|
||||
Result::match(
|
||||
res,
|
||||
res.match(
|
||||
[&output](const QVariant &) {
|
||||
output = {{"code", 200}};
|
||||
},
|
||||
|
@ -36,8 +36,7 @@ QVariantHash QueuedTcpServerResponseHelperPermissions::addPermission(
|
||||
= QueuedCoreAdaptor::sendUserPermissionAdd(_id, permission, _token);
|
||||
|
||||
QVariantHash output;
|
||||
Result::match(
|
||||
res,
|
||||
res.match(
|
||||
[&output](const QVariant &) {
|
||||
output = {{"code", 200}};
|
||||
},
|
||||
@ -66,8 +65,7 @@ QVariantHash QueuedTcpServerResponseHelperPermissions::removePermission(
|
||||
= QueuedCoreAdaptor::sendUserPermissionRemove(_id, permission, _token);
|
||||
|
||||
QVariantHash output;
|
||||
Result::match(
|
||||
res,
|
||||
res.match(
|
||||
[&output](const QVariant &) {
|
||||
output = {{"code", 200}};
|
||||
},
|
||||
|
@ -28,8 +28,7 @@ QueuedTcpServerResponseHelperPlugins::addPlugin(const QString &_name,
|
||||
auto res = QueuedCoreAdaptor::sendPluginAdd(_name, _token);
|
||||
|
||||
QVariantHash output;
|
||||
Result::match(
|
||||
res,
|
||||
res.match(
|
||||
[&output](const bool) {
|
||||
output = {{"code", 200}};
|
||||
},
|
||||
@ -47,8 +46,7 @@ QVariantHash QueuedTcpServerResponseHelperPlugins::listPlugins()
|
||||
= QueuedCoreAdaptor::getOption(QueuedConfig::QueuedSettings::Plugins);
|
||||
|
||||
QVariantHash output;
|
||||
Result::match(
|
||||
res,
|
||||
res.match(
|
||||
[&output](const QVariant &val) {
|
||||
output = {{"code", 200}, {"plugins", val.toStringList()}};
|
||||
},
|
||||
@ -69,8 +67,7 @@ QueuedTcpServerResponseHelperPlugins::removePlugin(const QString &_name,
|
||||
auto res = QueuedCoreAdaptor::sendPluginRemove(_name, _token);
|
||||
|
||||
QVariantHash output;
|
||||
Result::match(
|
||||
res,
|
||||
res.match(
|
||||
[&output](const bool) {
|
||||
output = {{"code", 200}};
|
||||
},
|
||||
|
@ -30,8 +30,7 @@ QVariantHash QueuedTcpServerResponseHelperTask::addOrEditTask(
|
||||
if (_id > 0) {
|
||||
// edit existing task
|
||||
auto res = QueuedCoreAdaptor::sendTaskEdit(_id, defs, _token);
|
||||
Result::match(
|
||||
res,
|
||||
res.match(
|
||||
[&output](const bool val) {
|
||||
output = {{"code", val ? 200 : 500}};
|
||||
},
|
||||
@ -41,8 +40,7 @@ QVariantHash QueuedTcpServerResponseHelperTask::addOrEditTask(
|
||||
} else {
|
||||
// add new task
|
||||
auto res = QueuedCoreAdaptor::sendTaskAdd(defs, _token);
|
||||
Result::match(
|
||||
res,
|
||||
res.match(
|
||||
[&output](const long long val) {
|
||||
output = {{"code", val ? 200 : 500}, {"id", val}};
|
||||
},
|
||||
@ -98,16 +96,14 @@ QueuedTcpServerResponseHelperTask::getTask(const long long _id,
|
||||
QVariantHash output = {{"code", 200}};
|
||||
if (property.isEmpty()) {
|
||||
auto res = QueuedCoreAdaptor::getTask(_id);
|
||||
Result::match(
|
||||
res,
|
||||
res.match(
|
||||
[&output](const QVariantHash &val) { output["properties"] = val; },
|
||||
[&output](const QueuedError &err) {
|
||||
output = {{"code", 500}, {"message", err.message().c_str()}};
|
||||
});
|
||||
} else {
|
||||
auto res = QueuedCoreAdaptor::getTask(_id, property);
|
||||
Result::match(
|
||||
res,
|
||||
res.match(
|
||||
[&output, &property](const QVariant &val) {
|
||||
output["properties"] = {{property, val}};
|
||||
},
|
||||
@ -136,8 +132,7 @@ QueuedTcpServerResponseHelperTask::getTasks(const QVariantHash &_data,
|
||||
// some conversion magic
|
||||
QVariantList outputReport;
|
||||
auto res = QueuedCoreAdaptor::getTasks(userId, start, stop, _token);
|
||||
Result::match(
|
||||
res,
|
||||
res.match(
|
||||
[&output, &outputReport](const QList<QVariantHash> &val) {
|
||||
for (auto &user : val)
|
||||
outputReport += user;
|
||||
@ -160,8 +155,7 @@ QueuedTcpServerResponseHelperTask::startOrStopTask(const long long _id,
|
||||
auto res = QueuedCoreAdaptor::getTask(_id);
|
||||
|
||||
QVariantHash output;
|
||||
Result::match(
|
||||
res,
|
||||
res.match(
|
||||
[&output, &_id, &_token](const QVariantHash &val) {
|
||||
if (val["startTime"].toString().isEmpty()
|
||||
|| !val["endTime"].toString().isEmpty())
|
||||
@ -185,8 +179,7 @@ QVariantHash QueuedTcpServerResponseHelperTask::startTask(const long long _id,
|
||||
auto res = QueuedCoreAdaptor::sendTaskStart(_id, _token);
|
||||
|
||||
QVariantHash output;
|
||||
Result::match(
|
||||
res,
|
||||
res.match(
|
||||
[&output](const bool val) {
|
||||
output = {{"code", val ? 200 : 500}};
|
||||
},
|
||||
@ -206,8 +199,7 @@ QVariantHash QueuedTcpServerResponseHelperTask::stopTask(const long long _id,
|
||||
auto res = QueuedCoreAdaptor::sendTaskStop(_id, _token);
|
||||
|
||||
QVariantHash output;
|
||||
Result::match(
|
||||
res,
|
||||
res.match(
|
||||
[&output](const bool val) {
|
||||
output = {{"code", val ? 200 : 500}};
|
||||
},
|
||||
|
@ -27,8 +27,8 @@ QVariantHash QueuedTcpServerResponseHelperUser::addOrEditUser(
|
||||
// try define if user exists first
|
||||
auto userIdRes = QueuedCoreAdaptor::getUserId(_user);
|
||||
long long userId = -1;
|
||||
Result::match(userIdRes, [&userId](const long long val) { userId = val; },
|
||||
[&userId](const QueuedError &) {});
|
||||
userIdRes.match([&userId](const long long val) { userId = val; },
|
||||
[&userId](const QueuedError &) {});
|
||||
|
||||
auto defs = getDefinitions(_data);
|
||||
defs.name = _user;
|
||||
@ -37,8 +37,7 @@ QVariantHash QueuedTcpServerResponseHelperUser::addOrEditUser(
|
||||
if (userId > 0) {
|
||||
// edit existing user
|
||||
auto res = QueuedCoreAdaptor::sendUserEdit(userId, defs, _token);
|
||||
Result::match(
|
||||
res,
|
||||
res.match(
|
||||
[&output](const bool val) {
|
||||
output = {{"code", val ? 200 : 500}};
|
||||
},
|
||||
@ -48,8 +47,7 @@ QVariantHash QueuedTcpServerResponseHelperUser::addOrEditUser(
|
||||
} else {
|
||||
// add new user
|
||||
auto res = QueuedCoreAdaptor::sendUserAdd(defs, _token);
|
||||
Result::match(
|
||||
res,
|
||||
res.match(
|
||||
[&output](const long long val) {
|
||||
output = {{"code", val ? 200 : 500}, {"id", val}};
|
||||
},
|
||||
@ -69,10 +67,10 @@ QueuedTcpServerResponseHelperUser::getDefinitions(const QVariantHash &_data)
|
||||
|
||||
QueuedUser::QueuedUserDefinitions defs;
|
||||
defs.email = _data["email"].toString();
|
||||
auto res = QueuedCoreAdaptor::sendPasswordHash(_data["password"].toString());
|
||||
Result::match(
|
||||
res, [&defs](const QString &val) { defs.password = val; },
|
||||
[](const QueuedError &) {});
|
||||
auto res
|
||||
= QueuedCoreAdaptor::sendPasswordHash(_data["password"].toString());
|
||||
res.match([&defs](const QString &val) { defs.password = val; },
|
||||
[](const QueuedError &) {});
|
||||
defs.permissions = _data["permissions"].toUInt();
|
||||
// limits
|
||||
QueuedLimits::Limits limits;
|
||||
@ -102,8 +100,7 @@ QueuedTcpServerResponseHelperUser::getReport(const QVariantHash &_data,
|
||||
// some conversion magic
|
||||
QVariantList outputReport;
|
||||
auto res = QueuedCoreAdaptor::getPerformance(start, stop, _token);
|
||||
Result::match(
|
||||
res,
|
||||
res.match(
|
||||
[&output, &outputReport](const QList<QVariantHash> &val) {
|
||||
for (auto &user : val)
|
||||
outputReport += user;
|
||||
@ -125,8 +122,8 @@ QueuedTcpServerResponseHelperUser::getUser(const QString &_user,
|
||||
|
||||
auto userIdRes = QueuedCoreAdaptor::getUserId(_user);
|
||||
long long userId = -1;
|
||||
Result::match(userIdRes, [&userId](const long long val) { userId = val; },
|
||||
[](const QueuedError &) {});
|
||||
userIdRes.match([&userId](const long long val) { userId = val; },
|
||||
[](const QueuedError &) {});
|
||||
if (userId == -1)
|
||||
return {{"code", 500}};
|
||||
|
||||
@ -135,16 +132,14 @@ QueuedTcpServerResponseHelperUser::getUser(const QString &_user,
|
||||
QVariantHash output = {{"code", 200}};
|
||||
if (property.isEmpty()) {
|
||||
auto res = QueuedCoreAdaptor::getUser(userId);
|
||||
Result::match(
|
||||
res,
|
||||
res.match(
|
||||
[&output](const QVariantHash &val) { output["properties"] = val; },
|
||||
[&output](const QueuedError &err) {
|
||||
output = {{"code", 500}, {"message", err.message().c_str()}};
|
||||
});
|
||||
} else {
|
||||
auto res = QueuedCoreAdaptor::getUser(userId, property);
|
||||
Result::match(
|
||||
res,
|
||||
res.match(
|
||||
[&output, &property](const QVariant &val) {
|
||||
output["properties"] = {{property, val}};
|
||||
},
|
||||
@ -172,8 +167,7 @@ QueuedTcpServerResponseHelperUser::getUsers(const QVariantHash &_data,
|
||||
// some conversion magic
|
||||
QVariantList outputReport;
|
||||
auto res = QueuedCoreAdaptor::getUsers(lastLogin, permission, _token);
|
||||
Result::match(
|
||||
res,
|
||||
res.match(
|
||||
[&output, &outputReport](const QList<QVariantHash> &val) {
|
||||
for (auto &user : val)
|
||||
outputReport += user;
|
||||
|
@ -29,20 +29,11 @@
|
||||
#include "QueuedEnums.h"
|
||||
#include "QueuedLimits.h"
|
||||
#include "QueuedResult.h"
|
||||
#include "QueuedStaticConfig.h"
|
||||
|
||||
|
||||
class QueuedAdvancedSettings;
|
||||
class QueuedDatabase;
|
||||
class QueuedDatabaseManager;
|
||||
class QueuedPluginManager;
|
||||
class QueuedCorePrivate;
|
||||
class QueuedProcess;
|
||||
class QueuedProcessManager;
|
||||
class QueuedReportManager;
|
||||
class QueuedSettings;
|
||||
class QueuedTokenManager;
|
||||
class QueuedUser;
|
||||
class QueuedUserManager;
|
||||
|
||||
/**
|
||||
* @brief aggregator of queued classes
|
||||
@ -314,222 +305,16 @@ public:
|
||||
*/
|
||||
void init(const QString &_configuration);
|
||||
|
||||
private slots:
|
||||
/**
|
||||
* @brief notify clients about settings update
|
||||
* @param _id
|
||||
* updated key id
|
||||
* @param _key
|
||||
* updated key
|
||||
* @param _value
|
||||
* new value
|
||||
*/
|
||||
void updateSettings(const QueuedConfig::QueuedSettings _id,
|
||||
const QString &_key, const QVariant &_value);
|
||||
/**
|
||||
* @brief update process time
|
||||
* @param _id
|
||||
* task id
|
||||
* @param _startTime
|
||||
* task start time or empty
|
||||
* @param _endTime
|
||||
* task end time or empty
|
||||
*/
|
||||
void updateTaskTime(const long long _id, const QDateTime &_startTime,
|
||||
const QDateTime &_endTime);
|
||||
/**
|
||||
* @brief update user login time
|
||||
* @param _id
|
||||
* user ID
|
||||
* @param _time
|
||||
* user login time
|
||||
*/
|
||||
void updateUserLoginTime(const long long _id, const QDateTime &_time);
|
||||
|
||||
private:
|
||||
/**
|
||||
* @brief pointer to advanced settings object
|
||||
* @brief pointer to private implementation
|
||||
*/
|
||||
QueuedAdvancedSettings *m_advancedSettings = nullptr;
|
||||
/**
|
||||
* @brief pointer to database object
|
||||
*/
|
||||
QueuedDatabase *m_database = nullptr;
|
||||
/**
|
||||
* @brief pointer to database manager object
|
||||
*/
|
||||
QueuedDatabaseManager *m_databaseManager = nullptr;
|
||||
/**
|
||||
* @brief pointer to plugin manager
|
||||
*/
|
||||
QueuedPluginManager *m_plugins = nullptr;
|
||||
/**
|
||||
* @brief pointer to process manager
|
||||
*/
|
||||
QueuedProcessManager *m_processes = nullptr;
|
||||
/**
|
||||
* @brief pointer to report manager
|
||||
*/
|
||||
QueuedReportManager *m_reports = nullptr;
|
||||
/**
|
||||
* @brief pointer to settings object
|
||||
*/
|
||||
QueuedSettings *m_settings = nullptr;
|
||||
/**
|
||||
* @brief pointer to user manager
|
||||
*/
|
||||
QueuedUserManager *m_users = nullptr;
|
||||
// additional properties
|
||||
/**
|
||||
* @brief connection list
|
||||
*/
|
||||
QList<QMetaObject::Connection> m_connections;
|
||||
/**
|
||||
* @brief drop non-admin fields from database payload
|
||||
* @param _table
|
||||
* table name
|
||||
* @param _payload
|
||||
* initial database payload
|
||||
* @return payload with dropped keys
|
||||
*/
|
||||
QVariantHash dropAdminFields(const QString &_table,
|
||||
const QVariantHash &_payload);
|
||||
QueuedCorePrivate *m_impl = nullptr;
|
||||
/**
|
||||
* @brief init DBus interface
|
||||
* @throw QueuedDBusException
|
||||
*/
|
||||
void initDBus();
|
||||
/**
|
||||
* @brief method allows to init class if it was not created
|
||||
* @tparam T
|
||||
* class name
|
||||
* @tparam Args
|
||||
* class constructor arguments
|
||||
* @param _dest
|
||||
* pointer to destination
|
||||
* @param _args
|
||||
* class constructor arguments
|
||||
*/
|
||||
template <class T, typename... Args> T *initObject(T *_dest, Args... _args)
|
||||
{
|
||||
return _dest ? _dest : new T(this, _args...);
|
||||
};
|
||||
/**
|
||||
* @brief init plugins
|
||||
*/
|
||||
void initPlugins();
|
||||
/**
|
||||
* @brief init processes
|
||||
*/
|
||||
void initProcesses();
|
||||
/**
|
||||
* @brief init settings and database
|
||||
* @param _configuration
|
||||
* path to configuration file
|
||||
* @throw QueuedDatabaseException
|
||||
*/
|
||||
void initSettings(const QString &_configuration);
|
||||
/**
|
||||
* @brief init users
|
||||
*/
|
||||
void initUsers();
|
||||
// private interfaces
|
||||
/**
|
||||
* @brief add new task
|
||||
* @param _command
|
||||
* command line
|
||||
* @param _arguments
|
||||
* command arguments
|
||||
* @param _workingDirectory
|
||||
* working directory
|
||||
* @param _userId
|
||||
* task owner user ID
|
||||
* @param _limits
|
||||
* task defined limits
|
||||
* @return task ID or -1 if no task added
|
||||
*/
|
||||
QueuedResult<long long> addTaskPrivate(const QString &_command,
|
||||
const QStringList &_arguments,
|
||||
const QString &_workingDirectory,
|
||||
const long long _userId,
|
||||
const QueuedLimits::Limits &_limits);
|
||||
/**
|
||||
* @brief add new user
|
||||
* @param _name
|
||||
* user name
|
||||
* @param _email
|
||||
* user email
|
||||
* @param _password
|
||||
* user password
|
||||
* @param _permissions
|
||||
* user permissions
|
||||
* @param _limits
|
||||
* user limits
|
||||
* @return user ID or -1 if no user found
|
||||
*/
|
||||
QueuedResult<long long> addUserPrivate(const QString &_name,
|
||||
const QString &_email,
|
||||
const QString &_password,
|
||||
const uint _permissions,
|
||||
const QueuedLimits::Limits &_limits);
|
||||
/**
|
||||
* @brief edit advanced settings
|
||||
* @param _key
|
||||
* advanced settings key
|
||||
* @param _value
|
||||
* advanced settings value
|
||||
* @return true on successful option edition
|
||||
*/
|
||||
QueuedResult<bool> editOptionPrivate(const QString &_key,
|
||||
const QVariant &_value);
|
||||
/**
|
||||
* @brief edit plugin list
|
||||
* @param _plugin
|
||||
* plugin name
|
||||
* @param add
|
||||
* true if it requires add plugin
|
||||
* @return true on successful action
|
||||
*/
|
||||
QueuedResult<bool> editPluginPrivate(const QString &_plugin,
|
||||
const bool _add);
|
||||
/**
|
||||
* @brief edit task
|
||||
* @param _id
|
||||
* task ID to edit
|
||||
* @param _taskData
|
||||
* task data to edit
|
||||
* @remark _taskData should contain only fields defined in schema, any other
|
||||
* fields will be ignored. No need to pass all properties here
|
||||
* @return true on successful task edition
|
||||
*/
|
||||
QueuedResult<bool> editTaskPrivate(const long long _id,
|
||||
const QVariantHash &_taskData);
|
||||
/**
|
||||
* @brief edit user
|
||||
* @param _id
|
||||
* user ID to edit
|
||||
* @param _userData
|
||||
* user data to edit
|
||||
* @remark _userData should contain only fields defined in schema, any other
|
||||
* fields will be ignored. No need to pass all properties here
|
||||
* @return true on successful user edition
|
||||
*/
|
||||
QueuedResult<bool> editUserPrivate(const long long _id,
|
||||
const QVariantHash &_userData);
|
||||
/**
|
||||
* @brief edit user permissions
|
||||
* @param _id
|
||||
* user ID to edit
|
||||
* @param _permission
|
||||
* permission to add or remove
|
||||
* @param _add
|
||||
* indicates whether it should be added or removed
|
||||
* @return true on successful user permission edition
|
||||
*/
|
||||
QueuedResult<bool>
|
||||
editUserPermissionPrivate(const long long _id,
|
||||
const QueuedEnums::Permission &_permission,
|
||||
const bool _add);
|
||||
};
|
||||
|
||||
|
||||
|
@ -94,14 +94,14 @@ public:
|
||||
|
||||
/**
|
||||
* @brief QueuedProcess class constructor
|
||||
* @param parent
|
||||
* @param _parent
|
||||
* pointer to parent item
|
||||
* @param definitions
|
||||
* definitions of process
|
||||
* @param index
|
||||
* index of process
|
||||
*/
|
||||
explicit QueuedProcess(QObject *parent,
|
||||
explicit QueuedProcess(QObject *_parent,
|
||||
const QueuedProcessDefinitions &definitions,
|
||||
const long long index);
|
||||
/**
|
||||
|
@ -43,12 +43,12 @@ class QueuedReportManager : public QObject
|
||||
public:
|
||||
/**
|
||||
* @brief QueuedReportManager class constructor
|
||||
* @param parent
|
||||
* @param _parent
|
||||
* pointer to parent item
|
||||
* @param database
|
||||
* @param _database
|
||||
* pointer to database object
|
||||
*/
|
||||
explicit QueuedReportManager(QObject *parent, QueuedDatabase *database);
|
||||
explicit QueuedReportManager(QObject *_parent, QueuedDatabase *_database);
|
||||
/**
|
||||
* @brief QueuedReportManager class destructor
|
||||
*/
|
||||
|
@ -54,10 +54,10 @@ public:
|
||||
|
||||
/**
|
||||
* @brief QueuedTokenManager class constructor
|
||||
* @param parent
|
||||
* @param _parent
|
||||
* pointer to parent item
|
||||
*/
|
||||
explicit QueuedTokenManager(QObject *parent);
|
||||
explicit QueuedTokenManager(QObject *_parent);
|
||||
/**
|
||||
* @brief QueuedTokenManager class destructor
|
||||
*/
|
||||
|
@ -71,14 +71,14 @@ public:
|
||||
|
||||
/**
|
||||
* @brief QueuedUser class constructor
|
||||
* @param parent
|
||||
* @param _parent
|
||||
* pointer to parent item
|
||||
* @param definitions
|
||||
* definitions of user
|
||||
* @param index
|
||||
* index of process
|
||||
*/
|
||||
explicit QueuedUser(QObject *parent,
|
||||
explicit QueuedUser(QObject *_parent,
|
||||
const QueuedUserDefinitions &definitions,
|
||||
const long long index);
|
||||
/**
|
||||
|
414
sources/queued/include/queued/private/QueuedCorePrivate.h
Normal file
414
sources/queued/include/queued/private/QueuedCorePrivate.h
Normal file
@ -0,0 +1,414 @@
|
||||
/*
|
||||
* Copyright (c) 2017 Queued team
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
*
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*/
|
||||
/**
|
||||
* @file QueuedCorePrivate.h
|
||||
* Header of Queued library
|
||||
* @author Queued team
|
||||
* @copyright MIT
|
||||
* @bug https://github.com/arcan1s/queued/issues
|
||||
*/
|
||||
|
||||
|
||||
#ifndef QUEUEDCOREPRIVATE_H
|
||||
#define QUEUEDCOREPRIVATE_H
|
||||
|
||||
#include <QObject>
|
||||
|
||||
#include "queued/QueuedEnums.h"
|
||||
#include "queued/QueuedLimits.h"
|
||||
#include "queued/QueuedResult.h"
|
||||
#include "queued/QueuedStaticConfig.h"
|
||||
|
||||
|
||||
class QueuedAdvancedSettings;
|
||||
class QueuedCorePrivateHelper;
|
||||
class QueuedDatabase;
|
||||
class QueuedDatabaseManager;
|
||||
class QueuedPluginManager;
|
||||
class QueuedProcess;
|
||||
class QueuedProcessManager;
|
||||
class QueuedReportManager;
|
||||
class QueuedSettings;
|
||||
class QueuedUser;
|
||||
class QueuedUserManager;
|
||||
|
||||
/**
|
||||
* @brief private aggregator of queued classes
|
||||
*/
|
||||
class QueuedCorePrivate : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief QueuedCorePrivate class constructor
|
||||
* @param _parent
|
||||
* pointer to parent item
|
||||
*/
|
||||
explicit QueuedCorePrivate(QObject *_parent);
|
||||
/**
|
||||
* @brief QueuedCorePrivate class destructor
|
||||
*/
|
||||
virtual ~QueuedCorePrivate();
|
||||
/**
|
||||
* @brief add plugin to autoload and load it now
|
||||
* @param _plugin
|
||||
* plugin name
|
||||
* @param _token
|
||||
* user auth token
|
||||
* @return true on successfully addition
|
||||
*/
|
||||
QueuedResult<bool> addPlugin(const QString &_plugin, const QString &_token);
|
||||
/**
|
||||
* @brief add new task
|
||||
* @param _command
|
||||
* command line
|
||||
* @param _arguments
|
||||
* command arguments
|
||||
* @param _workingDirectory
|
||||
* working directory
|
||||
* @param _userId
|
||||
* task owner user ID
|
||||
* @param _limits
|
||||
* task defined limits
|
||||
* @param _token
|
||||
* user auth token
|
||||
* @return task ID or -1 if no task added
|
||||
*/
|
||||
QueuedResult<long long>
|
||||
addTask(const QString &_command, const QStringList &_arguments,
|
||||
const QString &_workingDirectory, const long long _userId,
|
||||
const QueuedLimits::Limits &_limits, const QString &_token);
|
||||
/**
|
||||
* @brief add new user
|
||||
* @param _name
|
||||
* user name
|
||||
* @param _email
|
||||
* user email
|
||||
* @param _password
|
||||
* user password
|
||||
* @param _permissions
|
||||
* user permissions
|
||||
* @param _limits
|
||||
* user limits
|
||||
* @param _token
|
||||
* user auth token
|
||||
* @return user ID or -1 if no user created
|
||||
*/
|
||||
QueuedResult<long long> addUser(const QString &_name, const QString &_email,
|
||||
const QString &_password,
|
||||
const uint _permissions,
|
||||
const QueuedLimits::Limits &_limits,
|
||||
const QString &_token);
|
||||
/**
|
||||
* @brief try to authorize by given token
|
||||
* @param _token
|
||||
* token ID
|
||||
* @return true if token is valid
|
||||
*/
|
||||
QueuedResult<bool> authorization(const QString &_token);
|
||||
/**
|
||||
* @brief authorize and create new token for user
|
||||
* @param _name
|
||||
* user name
|
||||
* @param _password
|
||||
* user password
|
||||
* @return token. It will be empty if authorization error occurs
|
||||
*/
|
||||
QueuedResult<QString> authorization(const QString &_name,
|
||||
const QString &_password);
|
||||
/**
|
||||
* @brief edit advanced settings
|
||||
* @param _key
|
||||
* advanced settings key
|
||||
* @param _value
|
||||
* advanced settings value
|
||||
* @param _token
|
||||
* user auth token
|
||||
* @return true on successful option edition
|
||||
*/
|
||||
QueuedResult<bool> editOption(const QString &_key, const QVariant &_value,
|
||||
const QString &_token);
|
||||
/**
|
||||
* @brief edit task
|
||||
* @param _id
|
||||
* task ID to edit
|
||||
* @param _taskData
|
||||
* task data to edit
|
||||
* @param _token
|
||||
* user auth token
|
||||
* @remark _taskData should contain only fields defined in schema, any other
|
||||
* fields will be ignored. No need to pass all properties here
|
||||
* @return true on successful task edition
|
||||
*/
|
||||
QueuedResult<bool> editTask(const long long _id,
|
||||
const QVariantHash &_taskData,
|
||||
const QString &_token);
|
||||
/**
|
||||
* @brief edit user
|
||||
* @param _id
|
||||
* user ID to edit
|
||||
* @param _userData
|
||||
* user data to edit
|
||||
* @param _token
|
||||
* user auth token
|
||||
* @remark _userData should contain only fields defined in schema, any other
|
||||
* fields will be ignored. No need to pass all properties here
|
||||
* @return true on successful user edition
|
||||
*/
|
||||
QueuedResult<bool> editUser(const long long _id,
|
||||
const QVariantHash &_userData,
|
||||
const QString &_token);
|
||||
/**
|
||||
* @brief edit user permissions
|
||||
* @param _id
|
||||
* user ID to edit
|
||||
* @param _permission
|
||||
* permission to add or remove
|
||||
* @param _add
|
||||
* indicates whether it should be added or removed
|
||||
* @param _token
|
||||
* user auth token
|
||||
* @return true on successful user permission edition
|
||||
*/
|
||||
QueuedResult<bool>
|
||||
editUserPermission(const long long _id,
|
||||
const QueuedEnums::Permission &_permission,
|
||||
const bool _add, const QString &_token);
|
||||
/**
|
||||
* @brief hash password
|
||||
* @param _password
|
||||
* user password as plain text
|
||||
* @return hashed password with applied salt
|
||||
*/
|
||||
QueuedResult<QString> hashFromPassword(const QString &_password);
|
||||
/**
|
||||
* @brief get value from advanced settings
|
||||
* @param _key
|
||||
* key string
|
||||
* @return option value or empty QVariant
|
||||
*/
|
||||
QueuedResult<QVariant> option(const QString &_key);
|
||||
/**
|
||||
* @brief usage report
|
||||
* @param _from
|
||||
* start report date
|
||||
* @param _to
|
||||
* stop report date
|
||||
* @param _token
|
||||
* user auth token
|
||||
* @return performance table
|
||||
*/
|
||||
QueuedResult<QList<QVariantHash>>
|
||||
performanceReport(const QDateTime &_from, const QDateTime &_to,
|
||||
const QString &_token) const;
|
||||
/**
|
||||
* @brief get plugin settings
|
||||
* @param _plugin
|
||||
* plugin name
|
||||
* @return hash of plugin settings
|
||||
*/
|
||||
QVariantHash pluginSettings(const QString &_plugin);
|
||||
/**
|
||||
* @brief remove plugin from autoload and unload it now
|
||||
* @param _plugin
|
||||
* plugin name
|
||||
* @param _token
|
||||
* user auth token
|
||||
* @return true on successful plugin removal
|
||||
*/
|
||||
QueuedResult<bool> removePlugin(const QString &_plugin,
|
||||
const QString &_token);
|
||||
/**
|
||||
* @brief force start task
|
||||
* @param _id
|
||||
* task ID
|
||||
* @param _token
|
||||
* user auth token
|
||||
* @return true on successful task start
|
||||
*/
|
||||
QueuedResult<bool> startTask(const long long _id, const QString &_token);
|
||||
/**
|
||||
* @brief force stop task
|
||||
* @param _id
|
||||
* task ID
|
||||
* @param _token
|
||||
* user auth token
|
||||
* @return true on successful task stop
|
||||
*/
|
||||
QueuedResult<bool> stopTask(const long long _id, const QString &_token);
|
||||
/**
|
||||
* @brief get task by ID
|
||||
* @param _id
|
||||
* task ID
|
||||
* @return task object or nullptr if no task found
|
||||
*/
|
||||
const QueuedProcess *task(const long long _id) const;
|
||||
/**
|
||||
* list of tasks which match criteria
|
||||
* @param _user
|
||||
* task user ID filter
|
||||
* @param _from
|
||||
* minimal start time
|
||||
* @param _to
|
||||
* maximal end time
|
||||
* @param _token
|
||||
* user auth token
|
||||
* @return list of tasks in database format
|
||||
*/
|
||||
QueuedResult<QList<QVariantHash>> taskReport(const long long _user,
|
||||
const QDateTime &_from,
|
||||
const QDateTime &_to,
|
||||
const QString &_token) const;
|
||||
/**
|
||||
* @brief get user by ID
|
||||
* @param _id
|
||||
* user ID
|
||||
* @return user object or nullptr if no user found
|
||||
*/
|
||||
const QueuedUser *user(const long long _id) const;
|
||||
/**
|
||||
* @brief get user by name
|
||||
* @param _name
|
||||
* user name
|
||||
* @return user object or nullptr if no user found
|
||||
*/
|
||||
const QueuedUser *user(const QString &_name) const;
|
||||
/**
|
||||
* list of users which match criteria
|
||||
* @param _lastLogged
|
||||
* last logged minimal date
|
||||
* @param _permission
|
||||
* user permission filter
|
||||
* @param _token
|
||||
* user auth token
|
||||
* @return list of users in database format
|
||||
*/
|
||||
QueuedResult<QList<QVariantHash>>
|
||||
userReport(const QDateTime &_lastLogged,
|
||||
const QueuedEnums::Permission _permission,
|
||||
const QString &_token) const;
|
||||
// control methods
|
||||
/**
|
||||
* @brief deinit subclasses
|
||||
*/
|
||||
void deinit();
|
||||
/**
|
||||
* @brief init subclasses
|
||||
* @param _configuration
|
||||
* path to configuration file
|
||||
* @throw QueuedDatabaseException
|
||||
* @throw QueuedDBusException
|
||||
*/
|
||||
void init(const QString &_configuration);
|
||||
|
||||
private slots:
|
||||
/**
|
||||
* @brief notify clients about settings update
|
||||
* @param _id
|
||||
* updated key id
|
||||
* @param _key
|
||||
* updated key
|
||||
* @param _value
|
||||
* new value
|
||||
*/
|
||||
void updateSettings(const QueuedConfig::QueuedSettings _id,
|
||||
const QString &_key, const QVariant &_value);
|
||||
/**
|
||||
* @brief update process time
|
||||
* @param _id
|
||||
* task id
|
||||
* @param _startTime
|
||||
* task start time or empty
|
||||
* @param _endTime
|
||||
* task end time or empty
|
||||
*/
|
||||
void updateTaskTime(const long long _id, const QDateTime &_startTime,
|
||||
const QDateTime &_endTime);
|
||||
/**
|
||||
* @brief update user login time
|
||||
* @param _id
|
||||
* user ID
|
||||
* @param _time
|
||||
* user login time
|
||||
*/
|
||||
void updateUserLoginTime(const long long _id, const QDateTime &_time);
|
||||
|
||||
private:
|
||||
/**
|
||||
* @brief private helper pointer
|
||||
*/
|
||||
friend class QueuedCorePrivateHelper;
|
||||
QueuedCorePrivateHelper *m_helper = nullptr;
|
||||
/**
|
||||
* @brief pointer to advanced settings object
|
||||
*/
|
||||
QueuedAdvancedSettings *m_advancedSettings = nullptr;
|
||||
/**
|
||||
* @brief pointer to database object
|
||||
*/
|
||||
QueuedDatabase *m_database = nullptr;
|
||||
/**
|
||||
* @brief pointer to database manager object
|
||||
*/
|
||||
QueuedDatabaseManager *m_databaseManager = nullptr;
|
||||
/**
|
||||
* @brief pointer to plugin manager
|
||||
*/
|
||||
QueuedPluginManager *m_plugins = nullptr;
|
||||
/**
|
||||
* @brief pointer to process manager
|
||||
*/
|
||||
QueuedProcessManager *m_processes = nullptr;
|
||||
/**
|
||||
* @brief pointer to report manager
|
||||
*/
|
||||
QueuedReportManager *m_reports = nullptr;
|
||||
/**
|
||||
* @brief pointer to settings object
|
||||
*/
|
||||
QueuedSettings *m_settings = nullptr;
|
||||
/**
|
||||
* @brief pointer to user manager
|
||||
*/
|
||||
QueuedUserManager *m_users = nullptr;
|
||||
// additional properties
|
||||
/**
|
||||
* @brief connection list
|
||||
*/
|
||||
QList<QMetaObject::Connection> m_connections;
|
||||
/**
|
||||
* @brief init plugins
|
||||
*/
|
||||
void initPlugins();
|
||||
/**
|
||||
* @brief init processes
|
||||
*/
|
||||
void initProcesses();
|
||||
/**
|
||||
* @brief init settings and database
|
||||
* @param _configuration
|
||||
* path to configuration file
|
||||
* @throw QueuedDatabaseException
|
||||
*/
|
||||
void initSettings(const QString &_configuration);
|
||||
/**
|
||||
* @brief init users
|
||||
*/
|
||||
void initUsers();
|
||||
};
|
||||
|
||||
|
||||
#endif /* QUEUEDCOREPRIVATE_H */
|
217
sources/queued/include/queued/private/QueuedCorePrivateHelper.h
Normal file
217
sources/queued/include/queued/private/QueuedCorePrivateHelper.h
Normal file
@ -0,0 +1,217 @@
|
||||
/*
|
||||
* Copyright (c) 2017 Queued team
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
*
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*/
|
||||
/**
|
||||
* @file QueuedCorePrivateHelper.h
|
||||
* Header of Queued library
|
||||
* @author Queued team
|
||||
* @copyright MIT
|
||||
* @bug https://github.com/arcan1s/queued/issues
|
||||
*/
|
||||
|
||||
|
||||
#ifndef QUEUEDCOREPRIVATEHELPER_H
|
||||
#define QUEUEDCOREPRIVATEHELPER_H
|
||||
|
||||
#include <QObject>
|
||||
|
||||
#include "queued/QueuedEnums.h"
|
||||
#include "queued/QueuedLimits.h"
|
||||
#include "queued/QueuedResult.h"
|
||||
|
||||
|
||||
class QueuedAdvancedSettings;
|
||||
class QueuedCorePrivate;
|
||||
class QueuedDatabase;
|
||||
class QueuedPluginManager;
|
||||
class QueuedProcessManager;
|
||||
class QueuedUserManager;
|
||||
|
||||
/**
|
||||
* @brief helper for private core class
|
||||
*/
|
||||
class QueuedCorePrivateHelper : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief QueuedCorePrivateHelper class constructor
|
||||
* @param _parent
|
||||
* pointer to parent item
|
||||
*/
|
||||
explicit QueuedCorePrivateHelper(QObject *_parent);
|
||||
/**
|
||||
* @brief QueuedCorePrivateHelper class destructor
|
||||
*/
|
||||
virtual ~QueuedCorePrivateHelper();
|
||||
|
||||
private:
|
||||
// interfaces
|
||||
friend class QueuedCorePrivate;
|
||||
QueuedCorePrivate *m_core = nullptr;
|
||||
/**
|
||||
* @brief pointer to advanced settings object
|
||||
* @return pointer to advanced settings object
|
||||
*/
|
||||
QueuedAdvancedSettings *advancedSettings();
|
||||
/**
|
||||
* @brief pointer to database object
|
||||
* @return pointer to database object
|
||||
*/
|
||||
QueuedDatabase *database();
|
||||
/**
|
||||
* @brief pointer to plugin manager
|
||||
* @return pointer to plugin manager
|
||||
*/
|
||||
QueuedPluginManager *plugins();
|
||||
/**
|
||||
* @brief pointer to process manager
|
||||
* @return pointer to process manager
|
||||
*/
|
||||
QueuedProcessManager *processes();
|
||||
/**
|
||||
* @brief pointer to user manager
|
||||
* @return pointer to user manager
|
||||
*/
|
||||
QueuedUserManager *users();
|
||||
|
||||
/**
|
||||
* @brief drop non-admin fields from database payload
|
||||
* @param _table
|
||||
* table name
|
||||
* @param _payload
|
||||
* initial database payload
|
||||
* @return payload with dropped keys
|
||||
*/
|
||||
QVariantHash dropAdminFields(const QString &_table,
|
||||
const QVariantHash &_payload);
|
||||
/**
|
||||
* @brief method allows to init class if it was not created
|
||||
* @tparam T
|
||||
* class name
|
||||
* @tparam Args
|
||||
* class constructor arguments
|
||||
* @param _parent
|
||||
* pointer to parent object of destination object
|
||||
* @param _dest
|
||||
* pointer to destination
|
||||
* @param _args
|
||||
* class constructor arguments
|
||||
*/
|
||||
template <class T, typename... Args> T *initObject(T *_dest, Args... _args)
|
||||
{
|
||||
return _dest ? _dest : new T(m_core, _args...);
|
||||
};
|
||||
// private interfaces
|
||||
/**
|
||||
* @brief add new task
|
||||
* @param _command
|
||||
* command line
|
||||
* @param _arguments
|
||||
* command arguments
|
||||
* @param _workingDirectory
|
||||
* working directory
|
||||
* @param _userId
|
||||
* task owner user ID
|
||||
* @param _limits
|
||||
* task defined limits
|
||||
* @return task ID or -1 if no task added
|
||||
*/
|
||||
QueuedResult<long long> addTaskPrivate(const QString &_command,
|
||||
const QStringList &_arguments,
|
||||
const QString &_workingDirectory,
|
||||
const long long _userId,
|
||||
const QueuedLimits::Limits &_limits);
|
||||
/**
|
||||
* @brief add new user
|
||||
* @param _name
|
||||
* user name
|
||||
* @param _email
|
||||
* user email
|
||||
* @param _password
|
||||
* user password
|
||||
* @param _permissions
|
||||
* user permissions
|
||||
* @param _limits
|
||||
* user limits
|
||||
* @return user ID or -1 if no user found
|
||||
*/
|
||||
QueuedResult<long long> addUserPrivate(const QString &_name,
|
||||
const QString &_email,
|
||||
const QString &_password,
|
||||
const uint _permissions,
|
||||
const QueuedLimits::Limits &_limits);
|
||||
/**
|
||||
* @brief edit advanced settings
|
||||
* @param _key
|
||||
* advanced settings key
|
||||
* @param _value
|
||||
* advanced settings value
|
||||
* @return true on successful option edition
|
||||
*/
|
||||
QueuedResult<bool> editOptionPrivate(const QString &_key,
|
||||
const QVariant &_value);
|
||||
/**
|
||||
* @brief edit plugin list
|
||||
* @param _plugin
|
||||
* plugin name
|
||||
* @param add
|
||||
* true if it requires add plugin
|
||||
* @return true on successful action
|
||||
*/
|
||||
QueuedResult<bool> editPluginPrivate(const QString &_plugin,
|
||||
const bool _add);
|
||||
/**
|
||||
* @brief edit task
|
||||
* @param _id
|
||||
* task ID to edit
|
||||
* @param _taskData
|
||||
* task data to edit
|
||||
* @remark _taskData should contain only fields defined in schema, any other
|
||||
* fields will be ignored. No need to pass all properties here
|
||||
* @return true on successful task edition
|
||||
*/
|
||||
QueuedResult<bool> editTaskPrivate(const long long _id,
|
||||
const QVariantHash &_taskData);
|
||||
/**
|
||||
* @brief edit user
|
||||
* @param _id
|
||||
* user ID to edit
|
||||
* @param _userData
|
||||
* user data to edit
|
||||
* @remark _userData should contain only fields defined in schema, any other
|
||||
* fields will be ignored. No need to pass all properties here
|
||||
* @return true on successful user edition
|
||||
*/
|
||||
QueuedResult<bool> editUserPrivate(const long long _id,
|
||||
const QVariantHash &_userData);
|
||||
/**
|
||||
* @brief edit user permissions
|
||||
* @param _id
|
||||
* user ID to edit
|
||||
* @param _permission
|
||||
* permission to add or remove
|
||||
* @param _add
|
||||
* indicates whether it should be added or removed
|
||||
* @return true on successful user permission edition
|
||||
*/
|
||||
QueuedResult<bool>
|
||||
editUserPermissionPrivate(const long long _id,
|
||||
const QueuedEnums::Permission &_permission,
|
||||
const bool _add);
|
||||
};
|
||||
|
||||
|
||||
#endif /* QUEUEDCOREPRIVATEHELPER_H */
|
File diff suppressed because it is too large
Load Diff
@ -75,9 +75,9 @@ QueuedResult<QString>
|
||||
QueuedCoreAdaptor::sendPasswordHash(const QString &_password)
|
||||
{
|
||||
QVariantList args = {_password};
|
||||
return sendRequest<QString>(QueuedConfig::DBUS_SERVICE,
|
||||
QueuedConfig::DBUS_OBJECT_PATH,
|
||||
QueuedConfig::DBUS_SERVICE, "Auth", args);
|
||||
return sendRequest<QString>(
|
||||
QueuedConfig::DBUS_SERVICE, QueuedConfig::DBUS_OBJECT_PATH,
|
||||
QueuedConfig::DBUS_SERVICE, "PasswordHash", args);
|
||||
}
|
||||
|
||||
|
||||
@ -348,11 +348,11 @@ QueuedResult<QVariantHash> QueuedCoreAdaptor::getTask(const long long _id)
|
||||
auto res = getTask(_id, "");
|
||||
|
||||
QueuedResult<QVariantHash> output;
|
||||
Result::match(res,
|
||||
[&output](const QVariant &val) {
|
||||
output = toResult<QVariantHash>(val);
|
||||
},
|
||||
[&output](const QueuedError &err) { output = err; });
|
||||
res.match(
|
||||
[&output](const QVariant &val) {
|
||||
output = toResult<QVariantHash>(val);
|
||||
},
|
||||
[&output](const QueuedError &err) { output = err; });
|
||||
|
||||
return output;
|
||||
}
|
||||
@ -400,11 +400,11 @@ QueuedResult<QVariantHash> QueuedCoreAdaptor::getUser(const long long _id)
|
||||
auto res = getUser(_id, "");
|
||||
|
||||
QueuedResult<QVariantHash> output;
|
||||
Result::match(res,
|
||||
[&output](const QVariant &val) {
|
||||
output = toResult<QVariantHash>(val);
|
||||
},
|
||||
[&output](const QueuedError &err) { output = err; });
|
||||
res.match(
|
||||
[&output](const QVariant &val) {
|
||||
output = toResult<QVariantHash>(val);
|
||||
},
|
||||
[&output](const QueuedError &err) { output = err; });
|
||||
|
||||
return output;
|
||||
}
|
||||
|
585
sources/queued/src/QueuedCorePrivate.cpp
Normal file
585
sources/queued/src/QueuedCorePrivate.cpp
Normal file
@ -0,0 +1,585 @@
|
||||
/*
|
||||
* Copyright (c) 2017 Queued team
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
*
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*/
|
||||
/**
|
||||
* @file QueuedCorePrivate.cpp
|
||||
* Source code of queued library
|
||||
* @author Queued team
|
||||
* @copyright MIT
|
||||
* @bug https://github.com/arcan1s/queued/issues
|
||||
*/
|
||||
|
||||
|
||||
#include <queued/Queued.h>
|
||||
#include <queued/private/QueuedCorePrivate.h>
|
||||
|
||||
#include "queued/QueuedDatabaseSchema.h"
|
||||
|
||||
#include <queued/private/QueuedCorePrivateHelper.h>
|
||||
|
||||
|
||||
/**
|
||||
* @class QueuedCorePrivate
|
||||
*/
|
||||
/**
|
||||
* @fn addPlugin
|
||||
*/
|
||||
QueuedResult<bool> QueuedCorePrivate::addPlugin(const QString &_plugin,
|
||||
const QString &_token)
|
||||
{
|
||||
qCDebug(LOG_LIB) << "Add plugin" << _plugin;
|
||||
|
||||
bool isAdmin = m_users->authorize(_token, QueuedEnums::Permission::Admin);
|
||||
if (!isAdmin) {
|
||||
qCInfo(LOG_LIB) << "User" << _token << "not allowed to add plugin";
|
||||
return QueuedError("Not allowed",
|
||||
QueuedEnums::ReturnStatus::InsufficientPermissions);
|
||||
}
|
||||
|
||||
return m_helper->editPluginPrivate(_plugin, true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @addTask
|
||||
*/
|
||||
QueuedResult<long long> QueuedCorePrivate::addTask(
|
||||
const QString &_command, const QStringList &_arguments,
|
||||
const QString &_workingDirectory, const long long _userId,
|
||||
const QueuedLimits::Limits &_limits, const QString &_token)
|
||||
{
|
||||
qCDebug(LOG_LIB) << "Add task" << _command << "with arguments" << _arguments
|
||||
<< "from user" << _userId;
|
||||
|
||||
auto authUser = m_users->user(_token, true);
|
||||
if (!authUser) {
|
||||
qCWarning(LOG_LIB) << "Could not find auth user" << _token;
|
||||
return QueuedError("Invalid token",
|
||||
QueuedEnums::ReturnStatus::InvalidToken);
|
||||
}
|
||||
long long userAuthId = authUser->index();
|
||||
long long actualUserId = (_userId == -1) ? userAuthId : _userId;
|
||||
|
||||
// check permissions
|
||||
bool isAdmin = m_users->authorize(_token, QueuedEnums::Permission::Admin);
|
||||
bool isUser = m_users->authorize(_token, QueuedEnums::Permission::Job);
|
||||
if (userAuthId == actualUserId) {
|
||||
// it means that user places task as own one
|
||||
if (!isUser) {
|
||||
qCInfo(LOG_LIB) << "User" << _token << "not allowed to add task";
|
||||
return QueuedError(
|
||||
"Not allowed",
|
||||
QueuedEnums::ReturnStatus::InsufficientPermissions);
|
||||
}
|
||||
} else {
|
||||
// user tries to place task as another one
|
||||
if (!isAdmin) {
|
||||
qCInfo(LOG_LIB) << "User" << _token << "not allowed to add task";
|
||||
return QueuedError(
|
||||
"Not allowed",
|
||||
QueuedEnums::ReturnStatus::InsufficientPermissions);
|
||||
}
|
||||
}
|
||||
|
||||
return m_helper->addTaskPrivate(_command, _arguments, _workingDirectory,
|
||||
_userId, _limits);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @fn addUser
|
||||
*/
|
||||
QueuedResult<long long>
|
||||
QueuedCorePrivate::addUser(const QString &_name, const QString &_email,
|
||||
const QString &_password, const uint _permissions,
|
||||
const QueuedLimits::Limits &_limits,
|
||||
const QString &_token)
|
||||
{
|
||||
qCDebug(LOG_LIB) << "Add user" << _name << "with email" << _email
|
||||
<< "and permissions" << _permissions;
|
||||
|
||||
// check permissions
|
||||
bool isAdmin = m_users->authorize(_token, QueuedEnums::Permission::Admin);
|
||||
if (!isAdmin) {
|
||||
qCInfo(LOG_LIB) << "User" << _token << "not allowed to add user";
|
||||
return QueuedError("Not allowed",
|
||||
QueuedEnums::ReturnStatus::InsufficientPermissions);
|
||||
}
|
||||
|
||||
// check if already exists
|
||||
auto userObj = user(_name);
|
||||
if (userObj) {
|
||||
qCWarning(LOG_LIB) << "User" << _name << "already exists";
|
||||
return QueuedError("User already exists",
|
||||
QueuedEnums::ReturnStatus::InvalidArgument);
|
||||
}
|
||||
|
||||
return m_helper->addUserPrivate(_name, _email, _password, _permissions,
|
||||
_limits);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @fn authorization
|
||||
*/
|
||||
QueuedResult<bool> QueuedCorePrivate::authorization(const QString &_token)
|
||||
{
|
||||
bool status = false;
|
||||
m_users->checkToken(_token, &status);
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @fn authorization
|
||||
*/
|
||||
QueuedResult<QString> QueuedCorePrivate::authorization(const QString &_name,
|
||||
const QString &_password)
|
||||
{
|
||||
qCDebug(LOG_LIB) << "Authorize user" << _name;
|
||||
|
||||
QString token = m_users->authorize(_name, _password);
|
||||
if (token.isEmpty()) {
|
||||
return QueuedError("Invalid username or password",
|
||||
QueuedEnums::ReturnStatus::InvalidPassword);
|
||||
} else {
|
||||
QVariantHash payload
|
||||
= {{"token", token},
|
||||
{"user", _name},
|
||||
{"validUntil",
|
||||
m_users->checkToken(token).toString(Qt::ISODateWithMs)}};
|
||||
m_database->add(QueuedDB::TOKENS_TABLE, payload);
|
||||
return token;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @fn editOption
|
||||
*/
|
||||
QueuedResult<bool> QueuedCorePrivate::editOption(const QString &_key,
|
||||
const QVariant &_value,
|
||||
const QString &_token)
|
||||
{
|
||||
qCDebug(LOG_LIB) << "Set key" << _key << "to" << _value;
|
||||
|
||||
// check permissions
|
||||
bool isAdmin = m_users->authorize(_token, QueuedEnums::Permission::Admin);
|
||||
if (!isAdmin) {
|
||||
qCInfo(LOG_LIB) << "User" << _token << "not allowed to edit options";
|
||||
return QueuedError("Not allowed",
|
||||
QueuedEnums::ReturnStatus::InsufficientPermissions);
|
||||
}
|
||||
|
||||
return m_helper->editOptionPrivate(_key, _value);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @fn editTask
|
||||
*/
|
||||
QueuedResult<bool> QueuedCorePrivate::editTask(const long long _id,
|
||||
const QVariantHash &_taskData,
|
||||
const QString &_token)
|
||||
{
|
||||
qCDebug(LOG_LIB) << "Edit task with ID" << _id;
|
||||
|
||||
auto task = m_processes->process(_id);
|
||||
if (!task) {
|
||||
qCWarning(LOG_LIB) << "Could not find task with ID" << _id;
|
||||
return QueuedError("Task does not exist",
|
||||
QueuedEnums::ReturnStatus::InvalidArgument);
|
||||
}
|
||||
|
||||
// check permissions
|
||||
auto authUser = m_users->user(_token, true);
|
||||
if (!authUser) {
|
||||
qCWarning(LOG_LIB) << "Could not find auth user" << _token;
|
||||
return QueuedError("Invalid token",
|
||||
QueuedEnums::ReturnStatus::InvalidToken);
|
||||
}
|
||||
long long userAuthId = authUser->index();
|
||||
bool isAdmin = m_users->authorize(_token, QueuedEnums::Permission::Admin);
|
||||
bool isUser = m_users->authorize(_token, QueuedEnums::Permission::Job);
|
||||
if (userAuthId == task->user()) {
|
||||
// it means that user edits own task
|
||||
if (!isUser) {
|
||||
qCInfo(LOG_LIB) << "User" << _token << "not allowed to edit task";
|
||||
return QueuedError(
|
||||
"Not allowed",
|
||||
QueuedEnums::ReturnStatus::InsufficientPermissions);
|
||||
}
|
||||
} else {
|
||||
// user tries to edit random task
|
||||
if (!isAdmin) {
|
||||
qCInfo(LOG_LIB) << "User" << _token << "not allowed to edit task";
|
||||
return QueuedError(
|
||||
"Not allowed",
|
||||
QueuedEnums::ReturnStatus::InsufficientPermissions);
|
||||
}
|
||||
}
|
||||
// only admin can edit run/stopped task
|
||||
if (!task->startTime().isNull()) {
|
||||
if (!isAdmin) {
|
||||
qCInfo(LOG_LIB)
|
||||
<< "User" << _token << "not allowed to edit run/exited task";
|
||||
return QueuedError(
|
||||
"Not allowed",
|
||||
QueuedEnums::ReturnStatus::InsufficientPermissions);
|
||||
}
|
||||
}
|
||||
|
||||
// drop admin fields
|
||||
QVariantHash payload
|
||||
= isAdmin ? _taskData
|
||||
: m_helper->dropAdminFields(QueuedDB::TASKS_TABLE, _taskData);
|
||||
|
||||
return m_helper->editTaskPrivate(_id, payload);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @fn editUser
|
||||
*/
|
||||
QueuedResult<bool> QueuedCorePrivate::editUser(const long long _id,
|
||||
const QVariantHash &_userData,
|
||||
const QString &_token)
|
||||
{
|
||||
qCDebug(LOG_LIB) << "Edit user with ID" << _id;
|
||||
|
||||
auto userObj = user(_id);
|
||||
if (!userObj) {
|
||||
qCWarning(LOG_LIB) << "Could not find user with ID" << _id;
|
||||
return QueuedError("User does not exist",
|
||||
QueuedEnums::ReturnStatus::InvalidArgument);
|
||||
}
|
||||
|
||||
// check permissions
|
||||
auto authUser = m_users->user(_token, true);
|
||||
if (!authUser) {
|
||||
qCWarning(LOG_LIB) << "Could not find auth user" << _token;
|
||||
return QueuedError("Invalid token",
|
||||
QueuedEnums::ReturnStatus::InvalidToken);
|
||||
}
|
||||
long long userAuthId = authUser->index();
|
||||
bool isAdmin = m_users->authorize(_token, QueuedEnums::Permission::Admin);
|
||||
if (userAuthId != _id) {
|
||||
if (!isAdmin) {
|
||||
qCInfo(LOG_LIB) << "User" << _token << "not allowed to edit user";
|
||||
return QueuedError(
|
||||
"Not allowed",
|
||||
QueuedEnums::ReturnStatus::InsufficientPermissions);
|
||||
}
|
||||
}
|
||||
|
||||
// drop admin fields
|
||||
QVariantHash payload
|
||||
= isAdmin ? _userData
|
||||
: m_helper->dropAdminFields(QueuedDB::USERS_TABLE, _userData);
|
||||
|
||||
return m_helper->editUserPrivate(_id, payload);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @fn editUserPermission
|
||||
*/
|
||||
QueuedResult<bool> QueuedCorePrivate::editUserPermission(
|
||||
const long long _id, const QueuedEnums::Permission &_permission,
|
||||
const bool _add, const QString &_token)
|
||||
{
|
||||
qCDebug(LOG_LIB) << "Edit permissions" << static_cast<int>(_permission)
|
||||
<< "for user" << _id << "add" << _add;
|
||||
|
||||
// check permissions
|
||||
bool isAdmin = m_users->authorize(_token, QueuedEnums::Permission::Admin);
|
||||
if (!isAdmin) {
|
||||
qCInfo(LOG_LIB) << "User" << _token
|
||||
<< "not allowed to edit permissions";
|
||||
return QueuedError("Not allowed",
|
||||
QueuedEnums::ReturnStatus::InsufficientPermissions);
|
||||
}
|
||||
|
||||
return m_helper->editUserPermissionPrivate(_id, _permission, _add);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @fn hashFromPassword
|
||||
*/
|
||||
QueuedResult<QString>
|
||||
QueuedCorePrivate::hashFromPassword(const QString &_password)
|
||||
{
|
||||
return _password.isEmpty() ? ""
|
||||
: QueuedUser::hashFromPassword(
|
||||
_password, m_settings->admin().salt);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @fn option
|
||||
*/
|
||||
QueuedResult<QVariant> QueuedCorePrivate::option(const QString &_key)
|
||||
{
|
||||
qCDebug(LOG_LIB) << "Look for option" << _key;
|
||||
|
||||
return m_advancedSettings->get(_key);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @fn performanceReport
|
||||
*/
|
||||
QueuedResult<QList<QVariantHash>> QueuedCorePrivate::performanceReport(
|
||||
const QDateTime &_from, const QDateTime &_to, const QString &_token) const
|
||||
{
|
||||
qCDebug(LOG_LIB) << "Get performance report for" << _from << _to;
|
||||
|
||||
// check permissions
|
||||
auto authUser = m_users->user(_token, true);
|
||||
if (!authUser) {
|
||||
qCWarning(LOG_LIB) << "Could not find auth user" << _token;
|
||||
return QueuedError("Invalid token",
|
||||
QueuedEnums::ReturnStatus::InvalidToken);
|
||||
}
|
||||
long long userAuthId = authUser->index();
|
||||
bool isAdmin = m_users->authorize(_token, QueuedEnums::Permission::Reports);
|
||||
|
||||
if (isAdmin) {
|
||||
return m_reports->performance(static_cast<QueuedCore *>(parent()),
|
||||
_from, _to);
|
||||
} else {
|
||||
auto data = m_reports->performance(static_cast<QueuedCore *>(parent()),
|
||||
_from, _to);
|
||||
QList<QVariantHash> output;
|
||||
for (auto &userData : data) {
|
||||
if (userData["_id"].toLongLong() != userAuthId)
|
||||
continue;
|
||||
output.append(userData);
|
||||
}
|
||||
return output;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @fn pluginSettings
|
||||
*/
|
||||
QVariantHash QueuedCorePrivate::pluginSettings(const QString &_plugin)
|
||||
{
|
||||
qCDebug(LOG_LIB) << "Get plugin settings for" << _plugin;
|
||||
|
||||
auto dbSettings
|
||||
= m_database->get(QueuedDB::SETTINGS_TABLE,
|
||||
QString("WHERE key LIKE 'Plugin.%1.%'").arg(_plugin));
|
||||
QVariantHash settings;
|
||||
std::for_each(dbSettings.cbegin(), dbSettings.cend(),
|
||||
[&settings](const QVariantHash &value) {
|
||||
settings[value["key"].toString()] = value["value"];
|
||||
});
|
||||
|
||||
return settings;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @fn removePlugin
|
||||
*/
|
||||
QueuedResult<bool> QueuedCorePrivate::removePlugin(const QString &_plugin,
|
||||
const QString &_token)
|
||||
{
|
||||
qCDebug(LOG_LIB) << "Remove plugin" << _plugin;
|
||||
|
||||
bool isAdmin = m_users->authorize(_token, QueuedEnums::Permission::Admin);
|
||||
if (!isAdmin) {
|
||||
qCInfo(LOG_LIB) << "User" << _token << "not allowed to remove plugin";
|
||||
return QueuedError("Not allowed",
|
||||
QueuedEnums::ReturnStatus::InsufficientPermissions);
|
||||
}
|
||||
|
||||
return m_helper->editPluginPrivate(_plugin, false);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @fn startTask
|
||||
*/
|
||||
QueuedResult<bool> QueuedCorePrivate::startTask(const long long _id,
|
||||
const QString &_token)
|
||||
{
|
||||
qCDebug(LOG_LIB) << "Force start task with ID" << _id;
|
||||
|
||||
// check permissions
|
||||
auto authUser = m_users->user(_token, true);
|
||||
if (!authUser) {
|
||||
qCWarning(LOG_LIB) << "Could not find auth user" << _token;
|
||||
return QueuedError("Invalid token",
|
||||
QueuedEnums::ReturnStatus::InvalidToken);
|
||||
}
|
||||
long long userAuthId = authUser->index();
|
||||
bool isAdmin = m_users->authorize(_token, QueuedEnums::Permission::Admin);
|
||||
if (userAuthId != _id) {
|
||||
if (!isAdmin) {
|
||||
qCInfo(LOG_LIB) << "User" << _token << "not allowed to start tasks";
|
||||
return QueuedError(
|
||||
"Not allowed",
|
||||
QueuedEnums::ReturnStatus::InsufficientPermissions);
|
||||
}
|
||||
}
|
||||
|
||||
m_processes->start(_id);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @fn stopTask
|
||||
*/
|
||||
QueuedResult<bool> QueuedCorePrivate::stopTask(const long long _id,
|
||||
const QString &_token)
|
||||
{
|
||||
qCDebug(LOG_LIB) << "Force stop task with ID" << _id;
|
||||
|
||||
auto task = m_processes->process(_id);
|
||||
if (!task) {
|
||||
qCWarning(LOG_LIB) << "Could not find task with ID" << _id;
|
||||
return QueuedError("Task does not exist",
|
||||
QueuedEnums::ReturnStatus::InvalidArgument);
|
||||
}
|
||||
|
||||
// check permissions
|
||||
auto authUser = m_users->user(_token, true);
|
||||
if (!authUser) {
|
||||
qCWarning(LOG_LIB) << "Could not find auth user" << _token;
|
||||
return QueuedError("Invalid token",
|
||||
QueuedEnums::ReturnStatus::InvalidToken);
|
||||
}
|
||||
long long userAuthId = authUser->index();
|
||||
bool isAdmin = m_users->authorize(_token, QueuedEnums::Permission::Admin);
|
||||
bool isUser = m_users->authorize(_token, QueuedEnums::Permission::Job);
|
||||
if (userAuthId == task->user()) {
|
||||
// it means that user edits own task
|
||||
if (!isUser) {
|
||||
qCInfo(LOG_LIB) << "User" << _token << "not allowed to stop task";
|
||||
return QueuedError(
|
||||
"Not allowed",
|
||||
QueuedEnums::ReturnStatus::InsufficientPermissions);
|
||||
}
|
||||
} else {
|
||||
// user tries to edit random task
|
||||
if (!isAdmin) {
|
||||
qCInfo(LOG_LIB) << "User" << _token << "not allowed to stop task";
|
||||
return QueuedError(
|
||||
"Not allowed",
|
||||
QueuedEnums::ReturnStatus::InsufficientPermissions);
|
||||
}
|
||||
}
|
||||
|
||||
m_processes->stop(_id);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @fn task
|
||||
*/
|
||||
const QueuedProcess *QueuedCorePrivate::task(const long long _id) const
|
||||
{
|
||||
qCDebug(LOG_LIB) << "Get task by ID" << _id;
|
||||
|
||||
return m_processes->process(_id);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @fn taskReport
|
||||
*/
|
||||
QueuedResult<QList<QVariantHash>>
|
||||
QueuedCorePrivate::taskReport(const long long _user, const QDateTime &_from,
|
||||
const QDateTime &_to, const QString &_token) const
|
||||
{
|
||||
qCDebug(LOG_LIB) << "Get tasks table by" << _user << _from << _to;
|
||||
|
||||
// check permissions
|
||||
auto authUser = m_users->user(_token, true);
|
||||
if (!authUser) {
|
||||
qCWarning(LOG_LIB) << "Could not find auth user" << _token;
|
||||
return QueuedError("Invalid token",
|
||||
QueuedEnums::ReturnStatus::InvalidToken);
|
||||
}
|
||||
long long userAuthId = authUser->index();
|
||||
bool isAdmin = m_users->authorize(_token, QueuedEnums::Permission::Reports);
|
||||
long long effectiveUserId = _user;
|
||||
if (_user == -1) {
|
||||
effectiveUserId = userAuthId;
|
||||
} else if (userAuthId != _user) {
|
||||
if (!isAdmin) {
|
||||
qCInfo(LOG_LIB)
|
||||
<< "User" << _token << "not allowed to get task report";
|
||||
return QueuedError(
|
||||
"Not allowed",
|
||||
QueuedEnums::ReturnStatus::InsufficientPermissions);
|
||||
}
|
||||
}
|
||||
|
||||
return m_reports->tasks(effectiveUserId, _from, _to);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @fn user
|
||||
*/
|
||||
const QueuedUser *QueuedCorePrivate::user(const long long _id) const
|
||||
{
|
||||
qCDebug(LOG_LIB) << "Get user by ID" << _id;
|
||||
|
||||
return m_users->user(_id);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @fn user
|
||||
*/
|
||||
const QueuedUser *QueuedCorePrivate::user(const QString &_name) const
|
||||
{
|
||||
qCDebug(LOG_LIB) << "Get user by name" << _name;
|
||||
|
||||
return m_users->user(_name, false);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @fn userReport
|
||||
*/
|
||||
QueuedResult<QList<QVariantHash>>
|
||||
QueuedCorePrivate::userReport(const QDateTime &_lastLogged,
|
||||
const QueuedEnums::Permission _permission,
|
||||
const QString &_token) const
|
||||
{
|
||||
qCDebug(LOG_LIB) << "Get users table by" << _lastLogged
|
||||
<< static_cast<int>(_permission);
|
||||
|
||||
// check permissions
|
||||
bool isAdmin = m_users->authorize(_token, QueuedEnums::Permission::Reports);
|
||||
if (!isAdmin) {
|
||||
qCInfo(LOG_LIB) << "User" << _token << "not allowed to get user report";
|
||||
return QueuedError("Not allowed",
|
||||
QueuedEnums::ReturnStatus::InsufficientPermissions);
|
||||
}
|
||||
|
||||
return m_reports->users(_lastLogged, _permission);
|
||||
}
|
401
sources/queued/src/QueuedCorePrivateHelper.cpp
Normal file
401
sources/queued/src/QueuedCorePrivateHelper.cpp
Normal file
@ -0,0 +1,401 @@
|
||||
/*
|
||||
* Copyright (c) 2017 Queued team
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
*
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*/
|
||||
/**
|
||||
* @file QueuedCorePrivateHelper.cpp
|
||||
* Source code of queued library
|
||||
* @author Queued team
|
||||
* @copyright MIT
|
||||
* @bug https://github.com/arcan1s/queued/issues
|
||||
*/
|
||||
|
||||
|
||||
#include <queued/Queued.h>
|
||||
#include <queued/private/QueuedCorePrivateHelper.h>
|
||||
|
||||
#include "queued/QueuedDatabaseSchema.h"
|
||||
|
||||
#include <queued/private/QueuedCorePrivate.h>
|
||||
|
||||
|
||||
/**
|
||||
* @class QueuedCorePrivateHelper
|
||||
*/
|
||||
/**
|
||||
* @fn QueuedCorePrivateHelper
|
||||
*/
|
||||
QueuedCorePrivateHelper::QueuedCorePrivateHelper(QObject *_parent)
|
||||
: QObject(_parent)
|
||||
{
|
||||
qCDebug(LOG_LIB) << __PRETTY_FUNCTION__;
|
||||
|
||||
m_core = static_cast<QueuedCorePrivate *>(parent());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @fn ~QueuedCorePrivateHelper
|
||||
*/
|
||||
QueuedCorePrivateHelper::~QueuedCorePrivateHelper()
|
||||
{
|
||||
qCDebug(LOG_LIB) << __PRETTY_FUNCTION__;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @fn advancedSettings
|
||||
*/
|
||||
QueuedAdvancedSettings *QueuedCorePrivateHelper::advancedSettings()
|
||||
{
|
||||
return m_core->m_advancedSettings;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @fn database
|
||||
*/
|
||||
QueuedDatabase *QueuedCorePrivateHelper::database()
|
||||
{
|
||||
return m_core->m_database;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @fn plugins
|
||||
*/
|
||||
QueuedPluginManager *QueuedCorePrivateHelper::plugins()
|
||||
{
|
||||
return m_core->m_plugins;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @fn processes
|
||||
*/
|
||||
QueuedProcessManager *QueuedCorePrivateHelper::processes()
|
||||
{
|
||||
return m_core->m_processes;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @fn users
|
||||
*/
|
||||
QueuedUserManager *QueuedCorePrivateHelper::users()
|
||||
{
|
||||
return m_core->m_users;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @fn dropAdminFields
|
||||
*/
|
||||
QVariantHash
|
||||
QueuedCorePrivateHelper::dropAdminFields(const QString &_table,
|
||||
const QVariantHash &_payload)
|
||||
{
|
||||
qCDebug(LOG_LIB) << "Drop admin fields from" << _payload << "in table"
|
||||
<< _table;
|
||||
|
||||
QVariantHash payload;
|
||||
for (auto &key : _payload.keys()) {
|
||||
if (QueuedDB::DBSchema[_table][key].adminField)
|
||||
continue;
|
||||
payload[key] = _payload[key];
|
||||
}
|
||||
|
||||
return payload;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @addTaskPrivate
|
||||
*/
|
||||
QueuedResult<long long> QueuedCorePrivateHelper::addTaskPrivate(
|
||||
const QString &_command, const QStringList &_arguments,
|
||||
const QString &_workingDirectory, const long long _userId,
|
||||
const QueuedLimits::Limits &_limits)
|
||||
{
|
||||
qCDebug(LOG_LIB) << "Add task" << _command << "with arguments" << _arguments
|
||||
<< "from user" << _userId;
|
||||
|
||||
// add to database
|
||||
auto ids = users()->ids(_userId);
|
||||
auto userObj = m_core->user(_userId);
|
||||
if (!userObj) {
|
||||
qCWarning(LOG_LIB) << "Could not find task user" << _userId;
|
||||
return QueuedError("Invalid token",
|
||||
QueuedEnums::ReturnStatus::InvalidToken);
|
||||
}
|
||||
auto taskLimits = QueuedLimits::minimalLimits(
|
||||
_limits, userObj->nativeLimits(),
|
||||
QueuedLimits::Limits(
|
||||
advancedSettings()
|
||||
->get(QueuedConfig::QueuedSettings::DefaultLimits)
|
||||
.toString()));
|
||||
QVariantHash properties = {{"user", _userId},
|
||||
{"command", _command},
|
||||
{"commandArguments", _arguments},
|
||||
{"workDirectory", _workingDirectory},
|
||||
{"nice", 0},
|
||||
{"uid", ids.first},
|
||||
{"gid", ids.second},
|
||||
{"limits", taskLimits.toString()}};
|
||||
auto id = database()->add(QueuedDB::TASKS_TABLE, properties);
|
||||
if (id == -1) {
|
||||
qCWarning(LOG_LIB) << "Could not add task" << _command;
|
||||
return QueuedError("", QueuedEnums::ReturnStatus::Error);
|
||||
}
|
||||
|
||||
// add to child object
|
||||
processes()->add(properties, id);
|
||||
// notify plugins
|
||||
if (plugins())
|
||||
emit(plugins()->interface()->onAddTask(id));
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @fn addUserPrivate
|
||||
*/
|
||||
QueuedResult<long long> QueuedCorePrivateHelper::addUserPrivate(
|
||||
const QString &_name, const QString &_email, const QString &_password,
|
||||
const uint _permissions, const QueuedLimits::Limits &_limits)
|
||||
{
|
||||
qCDebug(LOG_LIB) << "Add user" << _name << "with email" << _email
|
||||
<< "and permissions" << _permissions;
|
||||
// add to database
|
||||
QVariantHash properties = {{"name", _name},
|
||||
{"password", _password},
|
||||
{"email", _email},
|
||||
{"permissions", _permissions},
|
||||
{"limits", _limits.toString()}};
|
||||
auto id = database()->add(QueuedDB::USERS_TABLE, properties);
|
||||
if (id == -1) {
|
||||
qCWarning(LOG_LIB) << "Could not add user" << _name;
|
||||
return QueuedError("", QueuedEnums::ReturnStatus::Error);
|
||||
}
|
||||
|
||||
// add to child object
|
||||
users()->add(properties, id);
|
||||
// notify plugins
|
||||
if (plugins())
|
||||
emit(plugins()->interface()->onAddUser(id));
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @fn editOptionPrivate
|
||||
*/
|
||||
QueuedResult<bool>
|
||||
QueuedCorePrivateHelper::editOptionPrivate(const QString &_key,
|
||||
const QVariant &_value)
|
||||
{
|
||||
qCDebug(LOG_LIB) << "Set key" << _key << "to" << _value;
|
||||
|
||||
// add to database
|
||||
long long id = advancedSettings()->id(_key);
|
||||
QVariantHash payload = {{"key", _key}, {"value", _value}};
|
||||
|
||||
bool status;
|
||||
if (id == -1) {
|
||||
id = database()->add(QueuedDB::SETTINGS_TABLE, payload);
|
||||
qCInfo(LOG_LIB) << "Added new key with ID" << id;
|
||||
status = (id != -1);
|
||||
} else {
|
||||
status = database()->modify(QueuedDB::SETTINGS_TABLE, id, payload);
|
||||
qCInfo(LOG_LIB) << "Value for" << _key
|
||||
<< "has been modified with status" << status;
|
||||
}
|
||||
|
||||
// add to child object
|
||||
if (status) {
|
||||
advancedSettings()->set(_key, _value);
|
||||
// notify plugins if required
|
||||
if (plugins()) {
|
||||
auto tryPluginOption = plugins()->convertOptionName(_key);
|
||||
if ((!tryPluginOption.first.isEmpty())
|
||||
&& (!tryPluginOption.second.isEmpty()))
|
||||
plugins()->optionChanged(_key, _value);
|
||||
// notify plugins
|
||||
emit(plugins()->interface()->onEditOption(_key, _value));
|
||||
}
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @fn editPluginPrivate
|
||||
*/
|
||||
QueuedResult<bool>
|
||||
QueuedCorePrivateHelper::editPluginPrivate(const QString &_plugin,
|
||||
const bool _add)
|
||||
{
|
||||
qCDebug(LOG_LIB) << "Edit plugin" << _plugin << "add" << _add;
|
||||
|
||||
QStringList pluginList = advancedSettings()
|
||||
->get(QueuedConfig::QueuedSettings::Plugins)
|
||||
.toString()
|
||||
.split('\n');
|
||||
|
||||
QueuedResult<bool> r;
|
||||
if (_add && !pluginList.contains(_plugin)) {
|
||||
if (plugins()->loadPlugin(_plugin, m_core->pluginSettings(_plugin))) {
|
||||
pluginList.append(_plugin);
|
||||
r = true;
|
||||
}
|
||||
} else if (!_add && pluginList.contains(_plugin)) {
|
||||
if (plugins()->unloadPlugin(_plugin)) {
|
||||
pluginList.removeAll(_plugin);
|
||||
r = true;
|
||||
}
|
||||
} else {
|
||||
qCDebug(LOG_LIB) << "Plugin" << _plugin
|
||||
<< "not loaded or already loaded";
|
||||
r = QueuedError("Plugin is not loaded or already loaded",
|
||||
QueuedEnums::ReturnStatus::Error);
|
||||
}
|
||||
|
||||
if (r.type() == Result::Content::Value) {
|
||||
editOptionPrivate(advancedSettings()->internalId(
|
||||
QueuedConfig::QueuedSettings::Plugins),
|
||||
pluginList.join('\n'));
|
||||
// notify plugins
|
||||
if (plugins()) {
|
||||
if (_add)
|
||||
emit(plugins()->interface()->onAddPlugin(_plugin));
|
||||
else
|
||||
emit(plugins()->interface()->onRemovePlugin(_plugin));
|
||||
}
|
||||
}
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @fn editTaskPrivate
|
||||
*/
|
||||
QueuedResult<bool>
|
||||
QueuedCorePrivateHelper::editTaskPrivate(const long long _id,
|
||||
const QVariantHash &_taskData)
|
||||
{
|
||||
qCDebug(LOG_LIB) << "Edit task with ID" << _id;
|
||||
|
||||
auto task = processes()->process(_id);
|
||||
if (!task) {
|
||||
qCWarning(LOG_LIB) << "Could not find task with ID" << _id;
|
||||
return QueuedError("Task does not exist",
|
||||
QueuedEnums::ReturnStatus::InvalidArgument);
|
||||
}
|
||||
|
||||
// modify record in database first
|
||||
bool status = database()->modify(QueuedDB::TASKS_TABLE, _id, _taskData);
|
||||
if (!status) {
|
||||
qCWarning(LOG_LIB) << "Could not modify task record" << _id
|
||||
<< "in database, do not edit it in memory";
|
||||
return QueuedError("", QueuedEnums::ReturnStatus::Error);
|
||||
}
|
||||
|
||||
// modify values stored in memory
|
||||
for (auto &property : _taskData.keys())
|
||||
task->setProperty(qPrintable(property), _taskData[property]);
|
||||
// notify plugins
|
||||
if (plugins())
|
||||
emit(plugins()->interface()->onEditTask(_id, _taskData));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @fn editUserPrivate
|
||||
*/
|
||||
QueuedResult<bool>
|
||||
QueuedCorePrivateHelper::editUserPrivate(const long long _id,
|
||||
const QVariantHash &_userData)
|
||||
{
|
||||
qCDebug(LOG_LIB) << "Edit user with ID" << _id;
|
||||
|
||||
auto userObj = users()->user(_id);
|
||||
if (!userObj) {
|
||||
qCWarning(LOG_LIB) << "Could not find user with ID" << _id;
|
||||
return QueuedError("User does not exist",
|
||||
QueuedEnums::ReturnStatus::InvalidArgument);
|
||||
};
|
||||
|
||||
// modify record in database first
|
||||
bool status = database()->modify(QueuedDB::USERS_TABLE, _id, _userData);
|
||||
if (!status) {
|
||||
qCWarning(LOG_LIB) << "Could not modify user record" << _id
|
||||
<< "in database, do not edit it in memory";
|
||||
return QueuedError("", QueuedEnums::ReturnStatus::Error);
|
||||
}
|
||||
|
||||
// modify values stored in memory
|
||||
for (auto &property : _userData.keys())
|
||||
userObj->setProperty(qPrintable(property), _userData[property]);
|
||||
// notify plugins
|
||||
if (plugins())
|
||||
emit(plugins()->interface()->onEditUser(_id, _userData));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @fn editUserPermissionPrivate
|
||||
*/
|
||||
QueuedResult<bool> QueuedCorePrivateHelper::editUserPermissionPrivate(
|
||||
const long long _id, const QueuedEnums::Permission &_permission,
|
||||
const bool _add)
|
||||
{
|
||||
qCDebug(LOG_LIB) << "Edit permissions" << static_cast<int>(_permission)
|
||||
<< "for user" << _id << "add" << _add;
|
||||
|
||||
auto userObj = users()->user(_id);
|
||||
if (!userObj) {
|
||||
qCWarning(LOG_LIB) << "Could not find user with ID" << _id;
|
||||
return QueuedError("User does not exist",
|
||||
QueuedEnums::ReturnStatus::InvalidArgument);
|
||||
}
|
||||
|
||||
// edit runtime permissions to get value
|
||||
auto perms = _add ? userObj->addPermission(_permission)
|
||||
: userObj->removePermission(_permission);
|
||||
auto permissions = static_cast<uint>(perms);
|
||||
qCInfo(LOG_LIB) << "New user permissions" << perms;
|
||||
|
||||
// modify in database now
|
||||
QVariantHash payload = {{"permissions", permissions}};
|
||||
bool status = database()->modify(QueuedDB::USERS_TABLE, _id, payload);
|
||||
if (!status) {
|
||||
qCWarning(LOG_LIB) << "Could not modify user record" << _id
|
||||
<< "in database, do not edit it in memory";
|
||||
// rollback in-memory values
|
||||
if (_add)
|
||||
userObj->removePermission(_permission);
|
||||
else
|
||||
userObj->addPermission(_permission);
|
||||
return QueuedError("", QueuedEnums::ReturnStatus::Error);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
218
sources/queued/src/QueuedCorePrivateInitializator.cpp
Normal file
218
sources/queued/src/QueuedCorePrivateInitializator.cpp
Normal file
@ -0,0 +1,218 @@
|
||||
/*
|
||||
* Copyright (c) 2017 Queued team
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
*
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*/
|
||||
/**
|
||||
* @file QueuedCorePrivateInitializator.cpp
|
||||
* Source code of queued library
|
||||
* @author Queued team
|
||||
* @copyright MIT
|
||||
* @bug https://github.com/arcan1s/queued/issues
|
||||
*/
|
||||
|
||||
|
||||
#include <queued/Queued.h>
|
||||
#include <queued/private/QueuedCorePrivate.h>
|
||||
|
||||
#include "queued/QueuedDatabaseSchema.h"
|
||||
|
||||
#include <queued/private/QueuedCorePrivateHelper.h>
|
||||
|
||||
|
||||
/**
|
||||
* @class QueuedCorePrivate
|
||||
*/
|
||||
/**
|
||||
* @fn QueuedCorePrivate
|
||||
*/
|
||||
QueuedCorePrivate::QueuedCorePrivate(QObject *_parent)
|
||||
: QObject(_parent)
|
||||
{
|
||||
qCDebug(LOG_LIB) << __PRETTY_FUNCTION__;
|
||||
|
||||
m_helper = new QueuedCorePrivateHelper(this);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @fn ~QueuedCorePrivate
|
||||
*/
|
||||
QueuedCorePrivate::~QueuedCorePrivate()
|
||||
{
|
||||
qCDebug(LOG_LIB) << __PRETTY_FUNCTION__;
|
||||
|
||||
deinit();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @fn deinit
|
||||
*/
|
||||
void QueuedCorePrivate::deinit()
|
||||
{
|
||||
// clear connections first
|
||||
for (auto &connection : m_connections)
|
||||
disconnect(connection);
|
||||
m_connections.clear();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @fn init
|
||||
*/
|
||||
void QueuedCorePrivate::init(const QString &_configuration)
|
||||
{
|
||||
qCDebug(LOG_LIB) << "Load configuration from" << _configuration;
|
||||
|
||||
// deinit objects if any
|
||||
deinit();
|
||||
|
||||
// init parts
|
||||
initSettings(_configuration);
|
||||
initUsers();
|
||||
initPlugins();
|
||||
initProcesses();
|
||||
|
||||
// settings update notifier
|
||||
m_connections
|
||||
+= connect(m_advancedSettings,
|
||||
SIGNAL(valueUpdated(const QueuedConfig::QueuedSettings,
|
||||
const QString &, const QVariant &)),
|
||||
this,
|
||||
SLOT(updateSettings(const QueuedConfig::QueuedSettings,
|
||||
const QString &, const QVariant &)));
|
||||
|
||||
// run!
|
||||
m_processes->start();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @fn initPlugins
|
||||
*/
|
||||
void QueuedCorePrivate::initPlugins()
|
||||
{
|
||||
QStringList pluginList
|
||||
= m_advancedSettings->get(QueuedConfig::QueuedSettings::Plugins)
|
||||
.toString()
|
||||
.split('\n');
|
||||
QString token = m_users->authorize(m_settings->admin().name);
|
||||
|
||||
m_plugins = m_helper->initObject(m_plugins, token);
|
||||
for (auto &plugin : pluginList)
|
||||
m_plugins->loadPlugin(plugin, pluginSettings(plugin));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @fn initProcesses
|
||||
*/
|
||||
void QueuedCorePrivate::initProcesses()
|
||||
{
|
||||
// init processes
|
||||
auto onExitAction = static_cast<QueuedEnums::ExitAction>(
|
||||
m_advancedSettings->get(QueuedConfig::QueuedSettings::OnExitAction)
|
||||
.toInt());
|
||||
auto processLine
|
||||
= m_advancedSettings
|
||||
->get(QueuedConfig::QueuedSettings::ProcessCommandLine)
|
||||
.toString();
|
||||
|
||||
m_processes = m_helper->initObject(m_processes);
|
||||
m_processes->setProcessLine(processLine);
|
||||
m_processes->setExitAction(onExitAction);
|
||||
auto dbProcesses
|
||||
= m_database->get(QueuedDB::TASKS_TABLE, "WHERE endTime IS NULL");
|
||||
m_processes->loadProcesses(dbProcesses);
|
||||
|
||||
m_connections
|
||||
+= connect(m_processes, &QueuedProcessManager::taskStartTimeReceived,
|
||||
[this](const long long _index, const QDateTime &_time) {
|
||||
return updateTaskTime(_index, _time, QDateTime());
|
||||
});
|
||||
m_connections
|
||||
+= connect(m_processes, &QueuedProcessManager::taskStopTimeReceived,
|
||||
[this](const long long _index, const QDateTime &_time) {
|
||||
return updateTaskTime(_index, QDateTime(), _time);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @fn initSettings
|
||||
*/
|
||||
void QueuedCorePrivate::initSettings(const QString &_configuration)
|
||||
{
|
||||
// read configuration first
|
||||
m_settings = m_helper->initObject(m_settings, _configuration);
|
||||
m_settings->readConfiguration();
|
||||
// init database now
|
||||
auto dbSetup = m_settings->db();
|
||||
m_database = m_helper->initObject(m_database, dbSetup.path, dbSetup.driver);
|
||||
m_database->close();
|
||||
bool status = m_database->open(dbSetup.hostname, dbSetup.port,
|
||||
dbSetup.username, dbSetup.password);
|
||||
if (!status) {
|
||||
QString message = "Could not open database";
|
||||
qCCritical(LOG_LIB) << message;
|
||||
throw QueuedDatabaseException(message);
|
||||
}
|
||||
|
||||
// create administrator if required
|
||||
auto dbAdmin = m_settings->admin();
|
||||
m_database->createAdministrator(dbAdmin.name, dbAdmin.password);
|
||||
|
||||
// and load advanced settings
|
||||
m_advancedSettings = m_helper->initObject(m_advancedSettings);
|
||||
m_advancedSettings->set(m_database->get(QueuedDB::SETTINGS_TABLE));
|
||||
if (!m_advancedSettings->checkDatabaseVersion()) {
|
||||
qCInfo(LOG_LIB) << "Bump database version to"
|
||||
<< QueuedConfig::DATABASE_VERSION;
|
||||
m_helper->editOptionPrivate(
|
||||
m_advancedSettings->internalId(
|
||||
QueuedConfig::QueuedSettings::DatabaseVersion),
|
||||
QueuedConfig::DATABASE_VERSION);
|
||||
}
|
||||
|
||||
// report manager
|
||||
m_reports = m_helper->initObject(m_reports, m_database);
|
||||
// database manager
|
||||
m_databaseManager = m_helper->initObject(m_databaseManager, m_database);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @fn initUsers
|
||||
*/
|
||||
void QueuedCorePrivate::initUsers()
|
||||
{
|
||||
// load users and tokens
|
||||
auto expiry
|
||||
= m_advancedSettings->get(QueuedConfig::QueuedSettings::TokenExpiration)
|
||||
.toLongLong();
|
||||
|
||||
m_users = m_helper->initObject(m_users);
|
||||
m_users->setSalt(m_settings->admin().salt);
|
||||
m_users->setTokenExpiration(expiry);
|
||||
QString now = QDateTime::currentDateTimeUtc().toString(Qt::ISODateWithMs);
|
||||
auto dbTokens = m_database->get(
|
||||
QueuedDB::TOKENS_TABLE,
|
||||
QString("WHERE datetime(validUntil) > datetime('%1')").arg(now));
|
||||
m_users->loadTokens(dbTokens);
|
||||
auto dbUsers = m_database->get(QueuedDB::USERS_TABLE);
|
||||
m_users->loadUsers(dbUsers);
|
||||
|
||||
m_connections += connect(
|
||||
m_users, SIGNAL(userLoggedIn(const long long, const QDateTime &)), this,
|
||||
SLOT(updateUserLoginTime(const long long, const QDateTime &)));
|
||||
}
|
130
sources/queued/src/QueuedCorePrivateSlots.cpp
Normal file
130
sources/queued/src/QueuedCorePrivateSlots.cpp
Normal file
@ -0,0 +1,130 @@
|
||||
/*
|
||||
* Copyright (c) 2017 Queued team
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
*
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*/
|
||||
/**
|
||||
* @file QueuedCorePrivate.cpp
|
||||
* Source code of queued library
|
||||
* @author Queued team
|
||||
* @copyright MIT
|
||||
* @bug https://github.com/arcan1s/queued/issues
|
||||
*/
|
||||
|
||||
|
||||
#include <queued/Queued.h>
|
||||
#include <queued/private/QueuedCorePrivate.h>
|
||||
|
||||
#include "queued/QueuedDatabaseSchema.h"
|
||||
|
||||
#include <queued/private/QueuedCorePrivateHelper.h>
|
||||
|
||||
|
||||
/**
|
||||
* @class QueuedCorePrivate
|
||||
*/
|
||||
/**
|
||||
* @fn updateSettings
|
||||
*/
|
||||
void QueuedCorePrivate::updateSettings(const QueuedConfig::QueuedSettings _id,
|
||||
const QString &_key,
|
||||
const QVariant &_value)
|
||||
{
|
||||
qCDebug(LOG_LIB) << "Received update for" << static_cast<int>(_id) << _key
|
||||
<< "with value" << _value;
|
||||
|
||||
// FIXME probably there is a better way to change settings
|
||||
switch (_id) {
|
||||
case QueuedConfig::QueuedSettings::Invalid:
|
||||
// check if it is plugin settings
|
||||
if (_key.startsWith("Plugin."))
|
||||
m_plugins->optionChanged(_key, _value);
|
||||
// do nothing otherwise
|
||||
break;
|
||||
case QueuedConfig::QueuedSettings::DatabaseInterval:
|
||||
m_databaseManager->setInterval(_value.toLongLong());
|
||||
break;
|
||||
case QueuedConfig::QueuedSettings::DatabaseVersion:
|
||||
break;
|
||||
case QueuedConfig::QueuedSettings::DefaultLimits:
|
||||
break;
|
||||
case QueuedConfig::QueuedSettings::KeepTasks:
|
||||
m_databaseManager->setKeepTasks(_value.toLongLong());
|
||||
break;
|
||||
case QueuedConfig::QueuedSettings::KeepUsers:
|
||||
m_databaseManager->setKeepUsers(_value.toLongLong());
|
||||
break;
|
||||
case QueuedConfig::QueuedSettings::OnExitAction:
|
||||
m_processes->setExitAction(
|
||||
static_cast<QueuedEnums::ExitAction>(_value.toInt()));
|
||||
break;
|
||||
case QueuedConfig::QueuedSettings::Plugins:
|
||||
// do nothing here
|
||||
break;
|
||||
case QueuedConfig::QueuedSettings::ProcessCommandLine:
|
||||
m_processes->setProcessLine(_value.toString());
|
||||
break;
|
||||
case QueuedConfig::QueuedSettings::ServerAddress:
|
||||
case QueuedConfig::QueuedSettings::ServerMaxConnections:
|
||||
case QueuedConfig::QueuedSettings::ServerPort:
|
||||
case QueuedConfig::QueuedSettings::ServerTimeout:
|
||||
// do nothing here
|
||||
break;
|
||||
case QueuedConfig::QueuedSettings::TokenExpiration:
|
||||
m_users->setTokenExpiration(_value.toLongLong());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @fn updateTaskTime
|
||||
*/
|
||||
void QueuedCorePrivate::updateTaskTime(const long long _id,
|
||||
const QDateTime &_startTime,
|
||||
const QDateTime &_endTime)
|
||||
{
|
||||
qCDebug(LOG_LIB) << "Update task" << _id << "time to" << _startTime
|
||||
<< _endTime;
|
||||
|
||||
QVariantHash record;
|
||||
if (_startTime.isValid()) {
|
||||
record["startTime"] = _startTime.toString(Qt::ISODateWithMs);
|
||||
if (m_plugins)
|
||||
emit(m_plugins->interface()->onStartTask(_id));
|
||||
}
|
||||
if (_endTime.isValid()) {
|
||||
record["endTime"] = _endTime.toString(Qt::ISODateWithMs);
|
||||
if (m_plugins)
|
||||
emit(m_plugins->interface()->onStopTask(_id));
|
||||
}
|
||||
|
||||
bool status = m_database->modify(QueuedDB::TASKS_TABLE, _id, record);
|
||||
if (!status)
|
||||
qCWarning(LOG_LIB) << "Could not modify task record" << _id;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @fn updateUserLoginTime
|
||||
*/
|
||||
void QueuedCorePrivate::updateUserLoginTime(const long long _id,
|
||||
const QDateTime &_time)
|
||||
{
|
||||
qCDebug(LOG_LIB) << "Update user" << _id << "with login time" << _time;
|
||||
|
||||
QVariantHash record = {{"lastLogin", _time.toString(Qt::ISODateWithMs)}};
|
||||
|
||||
bool status = m_database->modify(QueuedDB::USERS_TABLE, _id, record);
|
||||
if (!status)
|
||||
qCWarning(LOG_LIB) << "Could not modify user record" << _id;
|
||||
}
|
@ -30,10 +30,10 @@
|
||||
/**
|
||||
* @fn QueuedDatabaseManager
|
||||
*/
|
||||
QueuedDatabaseManager::QueuedDatabaseManager(QObject *parent,
|
||||
QueuedDatabase *database)
|
||||
: QObject(parent)
|
||||
, m_database(database)
|
||||
QueuedDatabaseManager::QueuedDatabaseManager(QObject *_parent,
|
||||
QueuedDatabase *_database)
|
||||
: QObject(_parent)
|
||||
, m_database(_database)
|
||||
{
|
||||
qCDebug(LOG_LIB) << __PRETTY_FUNCTION__;
|
||||
|
||||
|
@ -33,10 +33,10 @@
|
||||
/**
|
||||
* @fn QueuedProcess
|
||||
*/
|
||||
QueuedProcess::QueuedProcess(QObject *parent,
|
||||
QueuedProcess::QueuedProcess(QObject *_parent,
|
||||
const QueuedProcessDefinitions &definitions,
|
||||
const long long index)
|
||||
: QProcess(parent)
|
||||
: QProcess(_parent)
|
||||
, m_definitions(definitions)
|
||||
, m_index(index)
|
||||
{
|
||||
|
@ -32,10 +32,10 @@
|
||||
/**
|
||||
* @fn QueuedReportManager
|
||||
*/
|
||||
QueuedReportManager::QueuedReportManager(QObject *parent,
|
||||
QueuedDatabase *database)
|
||||
: QObject(parent)
|
||||
, m_database(database)
|
||||
QueuedReportManager::QueuedReportManager(QObject *_parent,
|
||||
QueuedDatabase *_database)
|
||||
: QObject(_parent)
|
||||
, m_database(_database)
|
||||
{
|
||||
qCDebug(LOG_LIB) << __PRETTY_FUNCTION__;
|
||||
}
|
||||
|
@ -32,8 +32,8 @@
|
||||
/**
|
||||
* @fn QueuedTokenManager
|
||||
*/
|
||||
QueuedTokenManager::QueuedTokenManager(QObject *parent)
|
||||
: QObject(parent)
|
||||
QueuedTokenManager::QueuedTokenManager(QObject *_parent)
|
||||
: QObject(_parent)
|
||||
{
|
||||
qCDebug(LOG_LIB) << __PRETTY_FUNCTION__;
|
||||
}
|
||||
|
@ -36,10 +36,10 @@ extern "C" {
|
||||
/**
|
||||
* @fn QueuedUser
|
||||
*/
|
||||
QueuedUser::QueuedUser(QObject *parent,
|
||||
QueuedUser::QueuedUser(QObject *_parent,
|
||||
const QueuedUserDefinitions &definitions,
|
||||
const long long index)
|
||||
: QObject(parent)
|
||||
: QObject(_parent)
|
||||
, m_definitions(definitions)
|
||||
, m_index(index)
|
||||
{
|
||||
|
@ -31,15 +31,15 @@ QueuedctlCommon::QueuedctlResult QueuedctlAuth::auth(const QString &_user,
|
||||
QueuedctlCommon::QueuedctlResult output;
|
||||
|
||||
auto res = QueuedCoreAdaptor::auth(_user, QueuedctlUser::getPassword());
|
||||
Result::match(res,
|
||||
[&output, &_user, &_cache](const QString &val) {
|
||||
setToken(val, _user, _cache);
|
||||
output.status = true;
|
||||
},
|
||||
[&output](const QueuedError &err) {
|
||||
output.status = false;
|
||||
output.output = err.message().c_str();
|
||||
});
|
||||
res.match(
|
||||
[&output, &_user, &_cache](const QString &val) {
|
||||
setToken(val, _user, _cache);
|
||||
output.status = true;
|
||||
},
|
||||
[&output](const QueuedError &err) {
|
||||
output.status = false;
|
||||
output.output = err.message().c_str();
|
||||
});
|
||||
|
||||
return output;
|
||||
}
|
||||
|
@ -236,11 +236,10 @@ QueuedctlCommon::process(QCommandLineParser &_parser, const QString &_cache,
|
||||
case QueuedctlArgument::PermissionAdd: {
|
||||
auto userIdRes = QueuedCoreAdaptor::getUserId(args.at(1));
|
||||
long long userId = -1;
|
||||
Result::match(userIdRes,
|
||||
[&userId](const long long val) { userId = val; },
|
||||
[&result](const QueuedError &err) {
|
||||
result.output = err.message().c_str();
|
||||
});
|
||||
userIdRes.match([&userId](const long long val) { userId = val; },
|
||||
[&result](const QueuedError &err) {
|
||||
result.output = err.message().c_str();
|
||||
});
|
||||
if (userId == -1)
|
||||
break;
|
||||
QString token = QueuedctlAuth::getToken(_cache, _user);
|
||||
@ -250,11 +249,10 @@ QueuedctlCommon::process(QCommandLineParser &_parser, const QString &_cache,
|
||||
case QueuedctlArgument::PermissionRemove: {
|
||||
auto userIdRes = QueuedCoreAdaptor::getUserId(args.at(1));
|
||||
long long userId = -1;
|
||||
Result::match(userIdRes,
|
||||
[&userId](const long long val) { userId = val; },
|
||||
[&result](const QueuedError &err) {
|
||||
result.output = err.message().c_str();
|
||||
});
|
||||
userIdRes.match([&userId](const long long val) { userId = val; },
|
||||
[&result](const QueuedError &err) {
|
||||
result.output = err.message().c_str();
|
||||
});
|
||||
if (userId == -1)
|
||||
break;
|
||||
QString token = QueuedctlAuth::getToken(_cache, _user);
|
||||
@ -283,14 +281,14 @@ QueuedctlCommon::process(QCommandLineParser &_parser, const QString &_cache,
|
||||
}
|
||||
case QueuedctlArgument::Status: {
|
||||
auto res = QueuedCoreAdaptor::getStatus();
|
||||
Result::match(res,
|
||||
[&result](const QueuedStatusMap &val) {
|
||||
result.status = true;
|
||||
result.output = hashHashToString(val);
|
||||
},
|
||||
[&result](const QueuedError &err) {
|
||||
result.output = err.message().c_str();
|
||||
});
|
||||
res.match(
|
||||
[&result](const QueuedStatusMap &val) {
|
||||
result.status = true;
|
||||
result.output = hashHashToString(val);
|
||||
},
|
||||
[&result](const QueuedError &err) {
|
||||
result.output = err.message().c_str();
|
||||
});
|
||||
break;
|
||||
}
|
||||
case QueuedctlArgument::TaskAdd: {
|
||||
@ -334,11 +332,10 @@ QueuedctlCommon::process(QCommandLineParser &_parser, const QString &_cache,
|
||||
case QueuedctlArgument::UserGet: {
|
||||
auto userIdRes = QueuedCoreAdaptor::getUserId(args.at(1));
|
||||
long long userId = -1;
|
||||
Result::match(userIdRes,
|
||||
[&userId](const long long val) { userId = val; },
|
||||
[&result](const QueuedError &err) {
|
||||
result.output = err.message().c_str();
|
||||
});
|
||||
userIdRes.match([&userId](const long long val) { userId = val; },
|
||||
[&result](const QueuedError &err) {
|
||||
result.output = err.message().c_str();
|
||||
});
|
||||
if (userId == -1)
|
||||
break;
|
||||
result = QueuedctlUser::getUser(userId, args.at(2));
|
||||
@ -352,11 +349,10 @@ QueuedctlCommon::process(QCommandLineParser &_parser, const QString &_cache,
|
||||
case QueuedctlArgument::UserSet: {
|
||||
auto userIdRes = QueuedCoreAdaptor::getUserId(args.at(1));
|
||||
long long userId = -1;
|
||||
Result::match(userIdRes,
|
||||
[&userId](const long long val) { userId = val; },
|
||||
[&result](const QueuedError &err) {
|
||||
result.output = err.message().c_str();
|
||||
});
|
||||
userIdRes.match([&userId](const long long val) { userId = val; },
|
||||
[&result](const QueuedError &err) {
|
||||
result.output = err.message().c_str();
|
||||
});
|
||||
if (userId == -1)
|
||||
break;
|
||||
QString token = QueuedctlAuth::getToken(_cache, _user);
|
||||
|
@ -28,10 +28,10 @@ QueuedctlOption::editOption(const QString &_option, const QVariant &_value,
|
||||
|
||||
QueuedctlCommon::QueuedctlResult output;
|
||||
auto res = QueuedCoreAdaptor::sendOptionEdit(_option, _value, _token);
|
||||
Result::match(res, [&output](const bool val) { output.status = val; },
|
||||
[&output](const QueuedError &err) {
|
||||
output.output = err.message().c_str();
|
||||
});
|
||||
res.match([&output](const bool val) { output.status = val; },
|
||||
[&output](const QueuedError &err) {
|
||||
output.output = err.message().c_str();
|
||||
});
|
||||
|
||||
return output;
|
||||
}
|
||||
@ -44,14 +44,14 @@ QueuedctlOption::getOption(const QString &_option)
|
||||
|
||||
QueuedctlCommon::QueuedctlResult output;
|
||||
auto res = QueuedCoreAdaptor::getOption(_option);
|
||||
Result::match(res,
|
||||
[&output](const QVariant &val) {
|
||||
output.status = val.isValid();
|
||||
output.output = val.toString();
|
||||
},
|
||||
[&output](const QueuedError &err) {
|
||||
output.output = err.message().c_str();
|
||||
});
|
||||
res.match(
|
||||
[&output](const QVariant &val) {
|
||||
output.status = val.isValid();
|
||||
output.output = val.toString();
|
||||
},
|
||||
[&output](const QueuedError &err) {
|
||||
output.output = err.message().c_str();
|
||||
});
|
||||
|
||||
return output;
|
||||
}
|
||||
|
@ -33,10 +33,10 @@ QueuedctlCommon::QueuedctlResult QueuedctlPermissions::addPermission(
|
||||
} else {
|
||||
auto res
|
||||
= QueuedCoreAdaptor::sendUserPermissionAdd(_id, permission, _token);
|
||||
Result::match(res, [&output](const bool val) { output.status = val; },
|
||||
[&output](const QueuedError &err) {
|
||||
output.output = err.message().c_str();
|
||||
});
|
||||
res.match([&output](const bool val) { output.status = val; },
|
||||
[&output](const QueuedError &err) {
|
||||
output.output = err.message().c_str();
|
||||
});
|
||||
}
|
||||
|
||||
return output;
|
||||
@ -56,10 +56,10 @@ QueuedctlCommon::QueuedctlResult QueuedctlPermissions::removePermission(
|
||||
} else {
|
||||
auto res = QueuedCoreAdaptor::sendUserPermissionRemove(_id, permission,
|
||||
_token);
|
||||
Result::match(res, [&output](const bool val) { output.status = val; },
|
||||
[&output](const QueuedError &err) {
|
||||
output.output = err.message().c_str();
|
||||
});
|
||||
res.match([&output](const bool val) { output.status = val; },
|
||||
[&output](const QueuedError &err) {
|
||||
output.output = err.message().c_str();
|
||||
});
|
||||
}
|
||||
|
||||
return output;
|
||||
|
@ -28,10 +28,10 @@ QueuedctlPlugins::addPlugin(const QString &_plugin, const QString &_token)
|
||||
auto res = QueuedCoreAdaptor::sendPluginAdd(_plugin, _token);
|
||||
|
||||
QueuedctlCommon::QueuedctlResult output;
|
||||
Result::match(res, [&output](const bool val) { output.status = val; },
|
||||
[&output](const QueuedError &err) {
|
||||
output.output = err.message().c_str();
|
||||
});
|
||||
res.match([&output](const bool val) { output.status = val; },
|
||||
[&output](const QueuedError &err) {
|
||||
output.output = err.message().c_str();
|
||||
});
|
||||
|
||||
return output;
|
||||
}
|
||||
@ -43,14 +43,14 @@ QueuedctlCommon::QueuedctlResult QueuedctlPlugins::listPlugins()
|
||||
= QueuedCoreAdaptor::getOption(QueuedConfig::QueuedSettings::Plugins);
|
||||
|
||||
QueuedctlCommon::QueuedctlResult output;
|
||||
Result::match(res,
|
||||
[&output](const QVariant &val) {
|
||||
output.status = val.isValid();
|
||||
output.output = val.toString();
|
||||
},
|
||||
[&output](const QueuedError &err) {
|
||||
output.output = err.message().c_str();
|
||||
});
|
||||
res.match(
|
||||
[&output](const QVariant &val) {
|
||||
output.status = val.isValid();
|
||||
output.output = val.toString();
|
||||
},
|
||||
[&output](const QueuedError &err) {
|
||||
output.output = err.message().c_str();
|
||||
});
|
||||
|
||||
return output;
|
||||
}
|
||||
@ -64,10 +64,10 @@ QueuedctlPlugins::removePlugin(const QString &_plugin, const QString &_token)
|
||||
auto res = QueuedCoreAdaptor::sendPluginRemove(_plugin, _token);
|
||||
|
||||
QueuedctlCommon::QueuedctlResult output;
|
||||
Result::match(res, [&output](const bool val) { output.status = val; },
|
||||
[&output](const QueuedError &err) {
|
||||
output.output = err.message().c_str();
|
||||
});
|
||||
res.match([&output](const bool val) { output.status = val; },
|
||||
[&output](const QueuedError &err) {
|
||||
output.output = err.message().c_str();
|
||||
});
|
||||
|
||||
return output;
|
||||
}
|
||||
|
@ -35,14 +35,14 @@ QueuedctlCommon::QueuedctlResult QueuedctlTask::addTask(
|
||||
auto res = QueuedCoreAdaptor::sendTaskAdd(_definitions, _token);
|
||||
|
||||
QueuedctlCommon::QueuedctlResult output;
|
||||
Result::match(res,
|
||||
[&output](const long long val) {
|
||||
output.status = (val > 0);
|
||||
output.output = QString::number(val);
|
||||
},
|
||||
[&output](const QueuedError &err) {
|
||||
output.output = err.message().c_str();
|
||||
});
|
||||
res.match(
|
||||
[&output](const long long val) {
|
||||
output.status = (val > 0);
|
||||
output.output = QString::number(val);
|
||||
},
|
||||
[&output](const QueuedError &err) {
|
||||
output.output = err.message().c_str();
|
||||
});
|
||||
|
||||
return output;
|
||||
}
|
||||
@ -69,8 +69,7 @@ QueuedctlTask::getDefinitions(const QCommandLineParser &_parser,
|
||||
definitions.user = 0;
|
||||
} else {
|
||||
auto res = QueuedCoreAdaptor::getUserId(_parser.value("task-user"));
|
||||
Result::match(
|
||||
res,
|
||||
res.match(
|
||||
[&definitions](const long long val) { definitions.user = val; },
|
||||
[&definitions](const QueuedError &) { definitions.user = 0; });
|
||||
}
|
||||
@ -114,24 +113,24 @@ QueuedctlTask::getTask(const long long _id, const QString &_property)
|
||||
|
||||
if (_property.isEmpty()) {
|
||||
auto res = QueuedCoreAdaptor::getTask(_id);
|
||||
Result::match(res,
|
||||
[&output](const QVariantHash &val) {
|
||||
output.status = true;
|
||||
output.output = QueuedctlCommon::hashToString(val);
|
||||
},
|
||||
[&output](const QueuedError &err) {
|
||||
output.output = err.message().c_str();
|
||||
});
|
||||
res.match(
|
||||
[&output](const QVariantHash &val) {
|
||||
output.status = true;
|
||||
output.output = QueuedctlCommon::hashToString(val);
|
||||
},
|
||||
[&output](const QueuedError &err) {
|
||||
output.output = err.message().c_str();
|
||||
});
|
||||
} else {
|
||||
auto res = QueuedCoreAdaptor::getTask(_id, _property);
|
||||
Result::match(res,
|
||||
[&output](const QVariant &val) {
|
||||
output.status = val.isValid();
|
||||
output.output = val.toString();
|
||||
},
|
||||
[&output](const QueuedError &err) {
|
||||
output.output = err.message().c_str();
|
||||
});
|
||||
res.match(
|
||||
[&output](const QVariant &val) {
|
||||
output.status = val.isValid();
|
||||
output.output = val.toString();
|
||||
},
|
||||
[&output](const QueuedError &err) {
|
||||
output.output = err.message().c_str();
|
||||
});
|
||||
}
|
||||
|
||||
return output;
|
||||
@ -145,8 +144,8 @@ QueuedctlTask::getTasks(const QCommandLineParser &_parser,
|
||||
long long userId = -1;
|
||||
if (!_parser.value("task-user").isEmpty()) {
|
||||
auto res = QueuedCoreAdaptor::getUserId(_parser.value("task-user"));
|
||||
Result::match(res, [&userId](const long long val) { userId = val; },
|
||||
[&userId](const QueuedError &) {});
|
||||
res.match([&userId](const long long val) { userId = val; },
|
||||
[&userId](const QueuedError &) {});
|
||||
}
|
||||
QDateTime stop
|
||||
= QDateTime::fromString(_parser.value("stop"), Qt::ISODateWithMs);
|
||||
@ -156,14 +155,14 @@ QueuedctlTask::getTasks(const QCommandLineParser &_parser,
|
||||
QueuedctlCommon::QueuedctlResult output;
|
||||
|
||||
auto res = QueuedCoreAdaptor::getTasks(userId, start, stop, _token);
|
||||
Result::match(res,
|
||||
[&output](const QList<QVariantHash> &val) {
|
||||
output.status = true;
|
||||
output.output = QueuedctlCommon::hashListToString(val);
|
||||
},
|
||||
[&output](const QueuedError &err) {
|
||||
output.output = err.message().c_str();
|
||||
});
|
||||
res.match(
|
||||
[&output](const QList<QVariantHash> &val) {
|
||||
output.status = true;
|
||||
output.output = QueuedctlCommon::hashListToString(val);
|
||||
},
|
||||
[&output](const QueuedError &err) {
|
||||
output.output = err.message().c_str();
|
||||
});
|
||||
|
||||
return output;
|
||||
}
|
||||
@ -310,10 +309,10 @@ QueuedctlCommon::QueuedctlResult QueuedctlTask::setTask(
|
||||
auto res = QueuedCoreAdaptor::sendTaskEdit(_id, _definitions, _token);
|
||||
|
||||
QueuedctlCommon::QueuedctlResult output;
|
||||
Result::match(res, [&output](const bool val) { output.status = val; },
|
||||
[&output](const QueuedError &err) {
|
||||
output.output = err.message().c_str();
|
||||
});
|
||||
res.match([&output](const bool val) { output.status = val; },
|
||||
[&output](const QueuedError &err) {
|
||||
output.output = err.message().c_str();
|
||||
});
|
||||
|
||||
return output;
|
||||
}
|
||||
@ -327,10 +326,10 @@ QueuedctlCommon::QueuedctlResult QueuedctlTask::startTask(const long long _id,
|
||||
auto res = QueuedCoreAdaptor::sendTaskStart(_id, _token);
|
||||
|
||||
QueuedctlCommon::QueuedctlResult output;
|
||||
Result::match(res, [&output](const bool val) { output.status = val; },
|
||||
[&output](const QueuedError &err) {
|
||||
output.output = err.message().c_str();
|
||||
});
|
||||
res.match([&output](const bool val) { output.status = val; },
|
||||
[&output](const QueuedError &err) {
|
||||
output.output = err.message().c_str();
|
||||
});
|
||||
|
||||
return output;
|
||||
}
|
||||
@ -344,10 +343,10 @@ QueuedctlCommon::QueuedctlResult QueuedctlTask::stopTask(const long long _id,
|
||||
auto res = QueuedCoreAdaptor::sendTaskStart(_id, _token);
|
||||
|
||||
QueuedctlCommon::QueuedctlResult output;
|
||||
Result::match(res, [&output](const bool val) { output.status = val; },
|
||||
[&output](const QueuedError &err) {
|
||||
output.output = err.message().c_str();
|
||||
});
|
||||
res.match([&output](const bool val) { output.status = val; },
|
||||
[&output](const QueuedError &err) {
|
||||
output.output = err.message().c_str();
|
||||
});
|
||||
|
||||
return output;
|
||||
}
|
||||
|
@ -37,14 +37,14 @@ QueuedctlUser::addUser(const QueuedUser::QueuedUserDefinitions &_definitions,
|
||||
auto res = QueuedCoreAdaptor::sendUserAdd(_definitions, _token);
|
||||
|
||||
QueuedctlCommon::QueuedctlResult output;
|
||||
Result::match(res,
|
||||
[&output](const long long val) {
|
||||
output.status = (val > 0);
|
||||
output.output = QString::number(val);
|
||||
},
|
||||
[&output](const QueuedError &err) {
|
||||
output.output = err.message().c_str();
|
||||
});
|
||||
res.match(
|
||||
[&output](const long long val) {
|
||||
output.status = (val > 0);
|
||||
output.output = QString::number(val);
|
||||
},
|
||||
[&output](const QueuedError &err) {
|
||||
output.output = err.message().c_str();
|
||||
});
|
||||
|
||||
return output;
|
||||
}
|
||||
@ -64,14 +64,14 @@ QueuedctlUser::getReport(const QCommandLineParser &_parser,
|
||||
auto res = QueuedCoreAdaptor::getPerformance(start, stop, _token);
|
||||
|
||||
QueuedctlCommon::QueuedctlResult output;
|
||||
Result::match(res,
|
||||
[&output](const QList<QVariantHash> &val) {
|
||||
output.status = true;
|
||||
output.output = QueuedctlCommon::hashListToString(val);
|
||||
},
|
||||
[&output](const QueuedError &err) {
|
||||
output.output = err.message().c_str();
|
||||
});
|
||||
res.match(
|
||||
[&output](const QList<QVariantHash> &val) {
|
||||
output.status = true;
|
||||
output.output = QueuedctlCommon::hashListToString(val);
|
||||
},
|
||||
[&output](const QueuedError &err) {
|
||||
output.output = err.message().c_str();
|
||||
});
|
||||
|
||||
return output;
|
||||
}
|
||||
@ -88,12 +88,12 @@ QueuedctlUser::getDefinitions(const QCommandLineParser &_parser,
|
||||
|
||||
// define password first
|
||||
definitions.password = _parser.isSet("stdin-password")
|
||||
? getPassword()
|
||||
: _parser.value("password");
|
||||
? getPassword()
|
||||
: _parser.value("password");
|
||||
auto res = QueuedCoreAdaptor::sendPasswordHash(definitions.password);
|
||||
Result::match(
|
||||
res, [&definitions](const QString &val) { definitions.password = val; },
|
||||
[](const QueuedError &) {});
|
||||
res.match(
|
||||
[&definitions](const QString &val) { definitions.password = val; },
|
||||
[&definitions](const QueuedError &) { definitions.password = ""; });
|
||||
|
||||
definitions.email = _parser.value("email");
|
||||
// limits now
|
||||
@ -144,24 +144,24 @@ QueuedctlUser::getUser(const long long _id, const QString &_property)
|
||||
|
||||
if (_property.isEmpty()) {
|
||||
auto res = QueuedCoreAdaptor::getUser(_id);
|
||||
Result::match(res,
|
||||
[&output](const QVariantHash &val) {
|
||||
output.status = true;
|
||||
output.output = QueuedctlCommon::hashToString(val);
|
||||
},
|
||||
[&output](const QueuedError &err) {
|
||||
output.output = err.message().c_str();
|
||||
});
|
||||
res.match(
|
||||
[&output](const QVariantHash &val) {
|
||||
output.status = true;
|
||||
output.output = QueuedctlCommon::hashToString(val);
|
||||
},
|
||||
[&output](const QueuedError &err) {
|
||||
output.output = err.message().c_str();
|
||||
});
|
||||
} else {
|
||||
auto res = QueuedCoreAdaptor::getUser(_id, _property);
|
||||
Result::match(res,
|
||||
[&output](const QVariant &val) {
|
||||
output.status = val.isValid();
|
||||
output.output = val.toString();
|
||||
},
|
||||
[&output](const QueuedError &err) {
|
||||
output.output = err.message().c_str();
|
||||
});
|
||||
res.match(
|
||||
[&output](const QVariant &val) {
|
||||
output.status = val.isValid();
|
||||
output.output = val.toString();
|
||||
},
|
||||
[&output](const QueuedError &err) {
|
||||
output.output = err.message().c_str();
|
||||
});
|
||||
}
|
||||
|
||||
return output;
|
||||
@ -182,14 +182,14 @@ QueuedctlUser::getUsers(const QCommandLineParser &_parser,
|
||||
auto res = QueuedCoreAdaptor::getUsers(lastLogin, permission, _token);
|
||||
|
||||
QueuedctlCommon::QueuedctlResult output;
|
||||
Result::match(res,
|
||||
[&output](const QList<QVariantHash> &val) {
|
||||
output.status = true;
|
||||
output.output = QueuedctlCommon::hashListToString(val);
|
||||
},
|
||||
[&output](const QueuedError &err) {
|
||||
output.output = err.message().c_str();
|
||||
});
|
||||
res.match(
|
||||
[&output](const QList<QVariantHash> &val) {
|
||||
output.status = true;
|
||||
output.output = QueuedctlCommon::hashListToString(val);
|
||||
},
|
||||
[&output](const QueuedError &err) {
|
||||
output.output = err.message().c_str();
|
||||
});
|
||||
|
||||
return output;
|
||||
}
|
||||
@ -326,10 +326,10 @@ QueuedctlUser::setUser(const long long _id,
|
||||
auto res = QueuedCoreAdaptor::sendUserEdit(_id, _definitions, _token);
|
||||
|
||||
QueuedctlCommon::QueuedctlResult output;
|
||||
Result::match(res, [&output](const bool val) { output.status = val; },
|
||||
[&output](const QueuedError &err) {
|
||||
output.output = err.message().c_str();
|
||||
});
|
||||
res.match([&output](const bool val) { output.status = val; },
|
||||
[&output](const QueuedError &err) {
|
||||
output.output = err.message().c_str();
|
||||
});
|
||||
|
||||
return output;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user