Make authenticatedApiUser a static property of ActionsHandler

This commit is contained in:
Mattia Codato 2020-07-03 11:00:40 +02:00
parent 27a6fd6b40
commit 08c6e9bc42
3 changed files with 9 additions and 10 deletions

View File

@ -689,12 +689,11 @@ Dictionary::Ptr ApiActions::ExecuteCommand(const ConfigObject::Ptr& object,
return ApiActions::CreateResult(404, "Can't find a valid notification for '" + resolved_notification + "'.");
/* Get author */
if (!authenticatedApiUser)
return ApiActions::CreateResult(401, "Can't find API user");
if (!ActionsHandler::authenticatedApiUser)
BOOST_THROW_EXCEPTION(std::invalid_argument("Can't find API user."));
String author = authenticatedApiUser->GetName();
cmd->Execute(notification, user, cr, NotificationType::NotificationCustom, author, "", execMacros, false);
cmd->Execute(notification, user, cr, NotificationType::NotificationCustom,
ActionsHandler::authenticatedApiUser->GetName(), "", execMacros, false);
}
}

View File

@ -11,7 +11,7 @@
using namespace icinga;
thread_local ApiUser::Ptr authenticatedApiUser;
thread_local ApiUser::Ptr ActionsHandler::authenticatedApiUser;
REGISTER_URLHANDLER("/v1/actions", ActionsHandler);
@ -74,9 +74,9 @@ bool ActionsHandler::HandleRequest(
bool verbose = false;
authenticatedApiUser = user;
ActionsHandler::authenticatedApiUser = user;
Defer a ([&]() {
authenticatedApiUser = nullptr;
ActionsHandler::authenticatedApiUser = nullptr;
});
if (params)

View File

@ -5,8 +5,6 @@
#include "remote/httphandler.hpp"
extern thread_local icinga::ApiUser::Ptr authenticatedApiUser;
namespace icinga
{
@ -15,6 +13,8 @@ class ActionsHandler final : public HttpHandler
public:
DECLARE_PTR_TYPEDEFS(ActionsHandler);
static thread_local ApiUser::Ptr authenticatedApiUser;
bool HandleRequest(
AsioTlsStream& stream,
const ApiUser::Ptr& user,