| 15 | REGISTER_URLHANDLER("/v1/actions", ActionsHandler); |
| 16 | |
| 17 | bool ActionsHandler::HandleRequest( |
| 18 | const WaitGroup::Ptr& waitGroup, |
| 19 | const HttpApiRequest& request, |
| 20 | HttpApiResponse& response, |
| 21 | boost::asio::yield_context& yc |
| 22 | ) |
| 23 | { |
| 24 | namespace http = boost::beast::http; |
| 25 | auto url = request.Url(); |
| 26 | auto user = request.User(); |
| 27 | auto params = request.Params(); |
| 28 | |
| 29 | if (url->GetPath().size() != 3) |
| 30 | return false; |
| 31 | |
| 32 | if (request.method() != http::verb::post) |
| 33 | return false; |
| 34 | |
| 35 | String actionName = url->GetPath()[2]; |
| 36 | |
| 37 | ApiAction::Ptr action = ApiAction::GetByName(actionName); |
| 38 | |
| 39 | if (!action) { |
| 40 | HttpUtility::SendJsonError(response, params, 404, "Action '" + actionName + "' does not exist."); |
| 41 | return true; |
| 42 | } |
| 43 | |
| 44 | QueryDescription qd; |
| 45 | |
| 46 | const std::vector<String>& types = action->GetTypes(); |
| 47 | std::vector<Value> objs; |
| 48 | |
| 49 | String permission = "actions/" + actionName; |
| 50 | |
| 51 | if (!types.empty()) { |
| 52 | qd.Types = std::set<String>(types.begin(), types.end()); |
| 53 | qd.Permission = permission; |
| 54 | |
| 55 | try { |
| 56 | objs = FilterUtility::GetFilterTargets(qd, params, user); |
| 57 | } catch (const MissingPermissionError& ex) { |
| 58 | HttpUtility::SendJsonError(response, params, 403, ex.what()); |
| 59 | return true; |
| 60 | } catch (const std::exception& ex) { |
| 61 | HttpUtility::SendJsonError(response, params, 404, |
| 62 | "No objects found.", |
| 63 | DiagnosticInformation(ex)); |
| 64 | return true; |
| 65 | } |
| 66 | |
| 67 | if (objs.empty()) { |
| 68 | HttpUtility::SendJsonError(response, params, 404, |
| 69 | "No objects found."); |
| 70 | return true; |
| 71 | } |
| 72 | } else { |
| 73 | FilterUtility::CheckPermission(user, permission); |
| 74 | objs.emplace_back(nullptr); |