Refactor GetSingleObjectByNameUsingPermissions

This commit is contained in:
Mattia Codato 2020-07-07 15:45:58 +02:00
parent c304b59990
commit e8c8a7d9dd
2 changed files with 11 additions and 11 deletions

View File

@ -556,11 +556,11 @@ Dictionary::Ptr ApiActions::GenerateTicket(const ConfigObject::Ptr&,
+ cn + "'.", additional); + cn + "'.", additional);
} }
Value ApiActions::GetSingleObjectByNameUsingPermissions(String type, String value, ApiUser::Ptr user) Value ApiActions::GetSingleObjectByNameUsingPermissions(const String& type, const String& objectName, const ApiUser::Ptr& user)
{ {
Dictionary::Ptr queryParams = new Dictionary(); Dictionary::Ptr queryParams = new Dictionary();
queryParams->Set("type", type); queryParams->Set("type", type);
queryParams->Set(type.ToLower(), value); queryParams->Set(type.ToLower(), objectName);
QueryDescription qd; QueryDescription qd;
qd.Types.insert(type); qd.Types.insert(type);
@ -568,9 +568,9 @@ Value ApiActions::GetSingleObjectByNameUsingPermissions(String type, String valu
std::vector<Value> objs; std::vector<Value> objs;
try { try {
objs = FilterUtility::GetFilterTargets(qd, queryParams, ActionsHandler::AuthenticatedApiUser); objs = FilterUtility::GetFilterTargets(qd, queryParams, user);
} catch (const std::exception& ex) { } catch (const std::exception& ex) {
Log(LogCritical, "ApiActions") << DiagnosticInformation(ex); Log(LogWarning, "ApiActions") << DiagnosticInformation(ex);
return nullptr; return nullptr;
} }
@ -642,7 +642,7 @@ Dictionary::Ptr ApiActions::ExecuteCommand(const ConfigObject::Ptr& object,
BOOST_THROW_EXCEPTION(std::invalid_argument("Can't find API user.")); BOOST_THROW_EXCEPTION(std::invalid_argument("Can't find API user."));
/* Get endpoint */ /* Get endpoint */
Endpoint::Ptr endpointPtr = ApiActions::GetSingleObjectByNameUsingPermissions(Endpoint::GetTypeName(), resolved_endpoint, ActionsHandler::AuthenticatedApiUser); Endpoint::Ptr endpointPtr = GetSingleObjectByNameUsingPermissions(Endpoint::GetTypeName(), resolved_endpoint, ActionsHandler::AuthenticatedApiUser);
if (!endpointPtr) if (!endpointPtr)
return ApiActions::CreateResult(404, "Can't find a valid endpoint for '" + resolved_endpoint + "'."); return ApiActions::CreateResult(404, "Can't find a valid endpoint for '" + resolved_endpoint + "'.");
@ -677,19 +677,19 @@ Dictionary::Ptr ApiActions::ExecuteCommand(const ConfigObject::Ptr& object,
}); });
if (command_type == "CheckCommand") { if (command_type == "CheckCommand") {
CheckCommand::Ptr cmd = ApiActions::GetSingleObjectByNameUsingPermissions(CheckCommand::GetTypeName(), resolved_command, ActionsHandler::AuthenticatedApiUser); CheckCommand::Ptr cmd = GetSingleObjectByNameUsingPermissions(CheckCommand::GetTypeName(), resolved_command, ActionsHandler::AuthenticatedApiUser);
if (!cmd) if (!cmd)
return ApiActions::CreateResult(404, "Can't find a valid " + command_type + " for '" + resolved_command + "'."); return ApiActions::CreateResult(404, "Can't find a valid " + command_type + " for '" + resolved_command + "'.");
else else
cmd->Execute(checkable, cr, execMacros, false); cmd->Execute(checkable, cr, execMacros, false);
} else if (command_type == "EventCommand") { } else if (command_type == "EventCommand") {
EventCommand::Ptr cmd = ApiActions::GetSingleObjectByNameUsingPermissions(EventCommand::GetTypeName(), resolved_command, ActionsHandler::AuthenticatedApiUser); EventCommand::Ptr cmd = GetSingleObjectByNameUsingPermissions(EventCommand::GetTypeName(), resolved_command, ActionsHandler::AuthenticatedApiUser);
if (!cmd) if (!cmd)
return ApiActions::CreateResult(404, "Can't find a valid " + command_type + " for '" + resolved_command + "'."); return ApiActions::CreateResult(404, "Can't find a valid " + command_type + " for '" + resolved_command + "'.");
else else
cmd->Execute(checkable, execMacros, false); cmd->Execute(checkable, execMacros, false);
} else if (command_type == "NotificationCommand") { } else if (command_type == "NotificationCommand") {
NotificationCommand::Ptr cmd = ApiActions::GetSingleObjectByNameUsingPermissions(NotificationCommand::GetTypeName(), resolved_command, ActionsHandler::AuthenticatedApiUser); NotificationCommand::Ptr cmd = GetSingleObjectByNameUsingPermissions(NotificationCommand::GetTypeName(), resolved_command, ActionsHandler::AuthenticatedApiUser);
if (!cmd) if (!cmd)
return ApiActions::CreateResult(404, "Can't find a valid " + command_type + " for '" + resolved_command + "'."); return ApiActions::CreateResult(404, "Can't find a valid " + command_type + " for '" + resolved_command + "'.");
else { else {
@ -704,7 +704,7 @@ Dictionary::Ptr ApiActions::ExecuteCommand(const ConfigObject::Ptr& object,
MacroProcessor::EscapeCallback(), nullptr, false MacroProcessor::EscapeCallback(), nullptr, false
); );
User::Ptr user = ApiActions::GetSingleObjectByNameUsingPermissions(User::GetTypeName(), resolved_command, ActionsHandler::AuthenticatedApiUser); User::Ptr user = GetSingleObjectByNameUsingPermissions(User::GetTypeName(), resolved_user, ActionsHandler::AuthenticatedApiUser);
if (!user) if (!user)
return ApiActions::CreateResult(404, "Can't find a valid user for '" + resolved_user + "'."); return ApiActions::CreateResult(404, "Can't find a valid user for '" + resolved_user + "'.");
@ -719,7 +719,7 @@ Dictionary::Ptr ApiActions::ExecuteCommand(const ConfigObject::Ptr& object,
MacroProcessor::EscapeCallback(), nullptr, false MacroProcessor::EscapeCallback(), nullptr, false
); );
Notification::Ptr notification = ApiActions::GetSingleObjectByNameUsingPermissions(Notification::GetTypeName(), resolved_command, ActionsHandler::AuthenticatedApiUser); Notification::Ptr notification = GetSingleObjectByNameUsingPermissions(Notification::GetTypeName(), resolved_notification, ActionsHandler::AuthenticatedApiUser);
if (!notification) if (!notification)
return ApiActions::CreateResult(404, "Can't find a valid notification for '" + resolved_notification + "'."); return ApiActions::CreateResult(404, "Can't find a valid notification for '" + resolved_notification + "'.");

View File

@ -34,7 +34,7 @@ public:
private: private:
static Dictionary::Ptr CreateResult(int code, const String& status, const Dictionary::Ptr& additional = nullptr); static Dictionary::Ptr CreateResult(int code, const String& status, const Dictionary::Ptr& additional = nullptr);
static Value GetSingleObjectByNameUsingPermissions(String type, String value, ApiUser::Ptr user); static Value GetSingleObjectByNameUsingPermissions(const String& type, const String& value, const ApiUser::Ptr& user);
}; };
} }