263 lines
11 KiB
C++
263 lines
11 KiB
C++
#include "AdminServiceServer.h"
|
|
|
|
#include "server/sparkd/persistence/SqliteStore.h"
|
|
#include "lib/logging/logging.h"
|
|
#include "lib/utils/PBUtils.h"
|
|
#include "AdminServiceUtils.h"
|
|
|
|
#include <boost/tokenizer.hpp>
|
|
#include <fmt/format.h>
|
|
#include <map>
|
|
#include <set>
|
|
#include <utility>
|
|
|
|
namespace bts {
|
|
namespace admin{
|
|
|
|
AdminServiceServer::AdminServiceServer(bts::api::ServiceContext& context)
|
|
:
|
|
bts::api::admin::AdminServiceBase (context),
|
|
_table_name("users"),
|
|
_unique_key("1"),
|
|
_default_group_name("DEFAULT"),
|
|
_state(),
|
|
_store(new bts::persistence::SqliteStore("admin_data"))
|
|
{
|
|
LOG(Gen, Info, "AdminServiceServer created");
|
|
std::map<std::string,std::map<std::string,std::string>> all_data = _store->retrieve_all();
|
|
auto table = all_data.find(_table_name);
|
|
bool data_found = false;
|
|
if (table != all_data.end())
|
|
{
|
|
auto row = table->second.find(_unique_key);
|
|
if (row != table->second.end())
|
|
{
|
|
std::string& ser_data = row->second;
|
|
if (_state.ParseFromString(ser_data))
|
|
{
|
|
data_found = true;
|
|
_update_and_publish();
|
|
}
|
|
}
|
|
}
|
|
if (!data_found)
|
|
{
|
|
bts::api::admin::BTSUserGroup default_group;
|
|
default_group.set_id(1);
|
|
default_group.set_name(_default_group_name);
|
|
AdminServiceUtils::create_group(&_state, default_group);
|
|
_update_and_publish();
|
|
}
|
|
}
|
|
|
|
AdminServiceServer::~AdminServiceServer() {}
|
|
|
|
void AdminServiceServer::create_account(const api::admin::AccountCreateAPIArgument& acct_create_arg,
|
|
const api::Response<::bts::api::admin::PersistRequestResult>& reply)
|
|
{
|
|
bts::api::admin::PersistRequestResult result; result.set_is_success(true);
|
|
if (AdminServiceUtils::find_account(_state, acct_create_arg.account_name())==nullptr){
|
|
bts::api::admin::BTSAccount* new_account = _state.add_accounts();
|
|
int new_account_id = AdminServiceUtils::find_curr_acct_id(_state)+1;
|
|
new_account->set_id(new_account_id);
|
|
if(acct_create_arg.has_parent_account_id())
|
|
new_account->mutable_parent_account_id()->set_id(acct_create_arg.parent_account_id().id());
|
|
new_account->set_name(acct_create_arg.account_name());
|
|
new_account->set_description(acct_create_arg.description());
|
|
new_account->set_is_active(false);
|
|
_update_and_publish();
|
|
}
|
|
else{
|
|
result.set_is_success(false);
|
|
result.set_error_text("Account with this name already exists");
|
|
}
|
|
reply(result);
|
|
}
|
|
|
|
void AdminServiceServer::set_account_clip_limits(const api::admin::AccountSecurityClipLimitsAPIArgument& arg,
|
|
const api::Response<::bts::api::admin::PersistRequestResult>& reply)
|
|
{
|
|
bts::api::admin::PersistRequestResult result; result.set_is_success(true);
|
|
bts::api::admin::BTSAccount* account = AdminServiceUtils::find_account(_state, arg.account_id());
|
|
if (account != nullptr)
|
|
account->mutable_security_clip_limits()->CopyFrom(arg.complete_data());
|
|
else{
|
|
result.set_is_success(false);
|
|
result.set_error_text("Account does not exist");
|
|
}
|
|
reply(result);
|
|
}
|
|
|
|
|
|
void AdminServiceServer::set_account_product_position_limits(const api::admin::AccountProductPositionLimitsAPIArgument& arg,
|
|
const api::Response<bts::api::admin::PersistRequestResult>& reply)
|
|
{
|
|
bts::api::admin::PersistRequestResult result; result.set_is_success(true);
|
|
bts::api::admin::BTSAccount* account = AdminServiceUtils::find_account(_state, arg.account_id());
|
|
if (account != nullptr)
|
|
account->mutable_product_position_limits()->CopyFrom(arg.complete_data());
|
|
else{
|
|
result.set_is_success(false);
|
|
result.set_error_text("Account does not exist");
|
|
}
|
|
reply(result);
|
|
}
|
|
|
|
void AdminServiceServer::set_account_security_position_limits(const api::admin::AccountSecurityPositionLimitsAPIArgument& arg,
|
|
const api::Response<bts::api::admin::PersistRequestResult>& reply)
|
|
{
|
|
bts::api::admin::PersistRequestResult result; result.set_is_success(true);
|
|
bts::api::admin::BTSAccount* account = AdminServiceUtils::find_account(_state, arg.account_id());
|
|
if (account != nullptr)
|
|
account->mutable_security_position_limits()->CopyFrom(arg.complete_data());
|
|
else{
|
|
result.set_is_success(false);
|
|
result.set_error_text("Account does not exist");
|
|
}
|
|
reply(result);
|
|
}
|
|
|
|
void AdminServiceServer::set_account_margin_limits(const api::admin::AccountMarginLimitAPIArgument& arg,
|
|
const api::Response<bts::api::admin::PersistRequestResult>& reply)
|
|
{
|
|
bts::api::admin::PersistRequestResult result; result.set_is_success(true);
|
|
bts::api::admin::BTSAccount* account = AdminServiceUtils::find_account(_state, arg.account_id());
|
|
if(account != nullptr){
|
|
account->mutable_margin_limit()->CopyFrom(arg.complete_data());
|
|
}
|
|
else{
|
|
result.set_is_success(false);
|
|
result.set_error_text("Could not find account");
|
|
}
|
|
reply(result);
|
|
}
|
|
|
|
void AdminServiceServer::set_account_connections(const api::admin::AccountConnectionsAPIArgument& arg,
|
|
const api::Response<bts::api::admin::PersistRequestResult>& reply)
|
|
{
|
|
bts::api::admin::PersistRequestResult result; result.set_is_success(true);
|
|
bts::api::admin::BTSAccount* account = AdminServiceUtils::find_account(_state, arg.account_id());
|
|
if(account != nullptr){
|
|
account->mutable_connections()->CopyFrom(arg.complete_data());
|
|
}
|
|
else{
|
|
result.set_is_success(false);
|
|
result.set_error_text("Could not find account");
|
|
}
|
|
reply(result);
|
|
}
|
|
|
|
void AdminServiceServer::set_account_exchange_data(const api::admin::AccountExchangeDataAPIArgument& account_exchange_data,
|
|
const api::Response<::bts::api::admin::PersistRequestResult>& reply)
|
|
{
|
|
bts::api::admin::PersistRequestResult result; result.set_is_success(true);
|
|
bts::api::admin::BTSAccount* account = AdminServiceUtils::find_account(_state, account_exchange_data.account_id());
|
|
if(account != nullptr)
|
|
account->mutable_exchange_account_data()->CopyFrom(account_exchange_data.complete_data());
|
|
else{
|
|
result.set_is_success(false);
|
|
result.set_error_text("Could not find account");
|
|
}
|
|
reply(result);
|
|
}
|
|
|
|
void AdminServiceServer::create_user(const api::admin::UserCreateAPIArgument& new_user,
|
|
const api::Response<::bts::api::admin::PersistRequestResult>& reply){
|
|
bts::api::admin::PersistRequestResult result; result.set_is_success(true);
|
|
if (AdminServiceUtils::find_user (_state, new_user.name())){
|
|
bts::api::admin::BTSUser* new_user_ptr = _state.add_users();
|
|
int new_id = AdminServiceUtils::find_curr_user_id(_state) + 1;
|
|
new_user_ptr->set_id(new_id);
|
|
new_user_ptr->set_parent_group_id(new_user.parent_group_id());
|
|
new_user_ptr->set_name(new_user.name());
|
|
new_user_ptr->set_password (new_user.password());
|
|
new_user_ptr->set_is_active(false);
|
|
new_user_ptr->mutable_license_entitlements()->CopyFrom(new_user.license_entitlements());
|
|
_update_and_publish();
|
|
}
|
|
else{
|
|
result.set_is_success(false);
|
|
result.set_error_text("User by this name already exists");
|
|
}
|
|
reply(result);
|
|
}
|
|
|
|
void AdminServiceServer::change_user_password(const bts::api::admin::UserPasswordAPIArgument& arg,
|
|
const api::Response<::bts::api::admin::PersistRequestResult>& reply){
|
|
bts::api::admin::PersistRequestResult result; result.set_is_success(true);
|
|
bts::api::admin::BTSUser* user = AdminServiceUtils::find_user(_state, arg.user_id());
|
|
if (user != nullptr)
|
|
user->set_password(arg.password());
|
|
else{
|
|
result.set_is_success(false);
|
|
result.set_error_text("Could not find user");
|
|
}
|
|
reply(result);
|
|
}
|
|
|
|
void AdminServiceServer::set_user_account_association(const api::admin::UserAccountAssociationAPIArgument& arg,
|
|
const api::Response<::bts::api::admin::PersistRequestResult>& reply)
|
|
{
|
|
bts::api::admin::PersistRequestResult result; result.set_is_success(true);
|
|
bts::api::admin::BTSUser* user = AdminServiceUtils::find_user(_state, arg.user_id());
|
|
if (user != nullptr){
|
|
if (arg.has_complete_data())
|
|
AdminServiceUtils::set_user_accounts(user, arg.complete_data());
|
|
else
|
|
AdminServiceUtils::clear_user_accounts(user);
|
|
}
|
|
else{
|
|
result.set_is_success(false);
|
|
result.set_error_text("Could not find user");
|
|
}
|
|
reply(result);
|
|
}
|
|
|
|
void AdminServiceServer::set_user_exchange_routing_data(const api::admin::ExchangeUserDataAPIArgument& user_exchange_routing_data,
|
|
const api::Response<::bts::api::admin::PersistRequestResult>& reply){
|
|
bts::api::admin::BTSUser* user = AdminServiceUtils::find_user(_state, user_exchange_routing_data.user_id());
|
|
bts::api::admin::PersistRequestResult result; result.set_is_success(true);
|
|
if (user != nullptr){
|
|
AdminServiceUtils::set_user_exchange_data(user, user_exchange_routing_data.complete_data());
|
|
}
|
|
else{
|
|
result.set_is_success(false);
|
|
result.set_error_text("Could not find user");
|
|
}
|
|
reply(result);
|
|
}
|
|
|
|
void AdminServiceServer::set_user_md_entitlement_data(const bts::api::admin::ExchangeUserMarketDataEntitlementDataAPIArgument& user_md_entitlement_data,
|
|
const bts::api::Response<::bts::api::admin::PersistRequestResult>& reply){
|
|
bts::api::admin::BTSUser* user = AdminServiceUtils::find_user(_state, user_md_entitlement_data.user_id());
|
|
bts::api::admin::PersistRequestResult result; result.set_is_success(true);
|
|
if (user != nullptr){
|
|
AdminServiceUtils::set_user_md_entitlement_data(user, user_md_entitlement_data.complete_data());
|
|
}
|
|
else{
|
|
result.set_is_success(false);
|
|
result.set_error_text("Could not find user");
|
|
}
|
|
reply(result);
|
|
}
|
|
|
|
void AdminServiceServer::get_user_model(const bts::api::VoidMessage&,
|
|
const api::Response<::bts::api::admin::AdminData>& reply)
|
|
{
|
|
reply(_state);
|
|
}
|
|
|
|
void AdminServiceServer::_update_and_publish()
|
|
{
|
|
std::string ser_state;
|
|
if (_state.SerializeToString(&ser_state))
|
|
{
|
|
LOG(Gen, Info, "Could not save user data");
|
|
_store->update(_table_name, _unique_key, ser_state);
|
|
// TODO:publish it
|
|
}
|
|
else
|
|
LOG(Gen, Error, "Could not save user data");
|
|
}
|
|
|
|
}} |