diff --git a/src/lib/api/service_helpers/AdminServiceUtils.cpp b/src/lib/api/service_helpers/AdminServiceUtils.cpp index 022542544..80001cbf4 100644 --- a/src/lib/api/service_helpers/AdminServiceUtils.cpp +++ b/src/lib/api/service_helpers/AdminServiceUtils.cpp @@ -886,30 +886,51 @@ std::string AdminServiceUtils::find_user_exchange_id (const bts::api::admin::Adm } return rv; } -std::pair -AdminServiceUtils::find_user_account_matches (const bts::api::admin::AdminData& admin_data, - const std::string& connection, const std::string& exchange_account, + +std::pair +AdminServiceUtils::find_user_account_matches (const AdminData& admin_data, + const std::string& connection, + const std::string& exchange_account, const std::string& exchange_user, api::Venue exchange){ - bts::api::admin::AdminServiceOperationResult find_account_result, find_user_result; - auto account_ptrs = accounts(admin_data, false); - auto user_ptrs = users(admin_data, false); - for(const bts::api::admin::BTSAccount* account_ptr : account_ptrs){ - if (account_has_connection(*account_ptr, connection)){ - if (boost::algorithm::iequals(exchange_account, - find_account_exchange_account(admin_data,*account_ptr,exchange,find_account_result)) && - find_account_result.is_success()){ - for(const bts::api::admin::BTSUser* user_ptr : user_ptrs){ - if (boost::algorithm::iequals(exchange_user, - find_user_exchange_id(admin_data,*user_ptr,exchange,find_user_result)) && - find_user_result.is_success()) - return std::pair(account_ptr,user_ptr); - } - } - } + AdminServiceOperationResult find_account_result, find_user_result; + // i. uses the exchange and exchange_user to reverse lookup the user(s) + auto user_ptrs = users(admin_data, false); + std::vector matching_users; + for (BTSUser const* u : user_ptrs){ + auto exchange_u = find_user_exchange_id(admin_data, *u, + exchange, find_user_result); + if (find_user_result.is_success() && boost::algorithm::iequals ( + exchange_u, + exchange_user)) + matching_users.push_back(u); } - return std::pair(nullptr, nullptr); + // ii. uses the exchange, connection and exchange_account to reverse lookup the account(s) + auto account_ptrs = accounts(admin_data, false); + std::vector matching_accounts; + for (BTSAccount const* a :account_ptrs) + if (account_has_connection(*a, connection)){ + auto exchange_a = find_account_exchange_account(admin_data, *a, + exchange, + find_account_result); + if (find_account_result.is_success() && boost::algorithm::iequals ( + exchange_a, + exchange_account)) + matching_accounts.push_back(a); + } + // iii. finds the first account from ii that the user from i is authorized to trade + for (BTSUser const* u : matching_users){ + std::vector user_auth_accounts; + find_user_trading_accounts (admin_data, u->id(), user_auth_accounts); + for (BTSAccount const* i : user_auth_accounts) + for (BTSAccount const* j : matching_accounts) + if(i->id() == j->id()) + return std::pair(i,u); + } + return std::pair(nullptr, nullptr); } + + // for a given user finds the account permissions (if permissions are // derived from the group, it traverses the group hierarcy to find the // correct permissions. diff --git a/src/lib/api/service_helpers/AdminServiceUtils.h b/src/lib/api/service_helpers/AdminServiceUtils.h index ebd941cf6..70d1e1e5b 100644 --- a/src/lib/api/service_helpers/AdminServiceUtils.h +++ b/src/lib/api/service_helpers/AdminServiceUtils.h @@ -207,6 +207,9 @@ public: const BTSUser& user, api::Venue exchange, AdminServiceOperationResult& out_result); + // i. uses the exchange and exchange_user to reverse lookup the user(s) + // ii. uses the exchange, connection and exchange_account to reverse lookup the account(s) + // iii. finds the first account from ii that a user from i is authorized to trade static std::pair find_user_account_matches (const AdminData& admin_data, const std::string& connection,