| 15 | REGISTER_URLHANDLER("/v1/objects", ModifyObjectHandler); |
| 16 | |
| 17 | bool ModifyObjectHandler::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 | |
| 26 | auto url = request.Url(); |
| 27 | auto user = request.User(); |
| 28 | auto params = request.Params(); |
| 29 | |
| 30 | if (url->GetPath().size() < 3 || url->GetPath().size() > 4) |
| 31 | return false; |
| 32 | |
| 33 | if (request.method() != http::verb::post) |
| 34 | return false; |
| 35 | |
| 36 | Type::Ptr type = FilterUtility::TypeFromPluralName(url->GetPath()[2]); |
| 37 | |
| 38 | if (!type) { |
| 39 | HttpUtility::SendJsonError(response, params, 400, "Invalid type specified."); |
| 40 | return true; |
| 41 | } |
| 42 | |
| 43 | QueryDescription qd; |
| 44 | qd.Types.insert(type->GetName()); |
| 45 | qd.Permission = "objects/modify/" + type->GetName(); |
| 46 | |
| 47 | params->Set("type", type->GetName()); |
| 48 | |
| 49 | if (url->GetPath().size() >= 4) { |
| 50 | String attr = type->GetName(); |
| 51 | boost::algorithm::to_lower(attr); |
| 52 | params->Set(attr, url->GetPath()[3]); |
| 53 | } |
| 54 | |
| 55 | std::vector<Value> objs; |
| 56 | |
| 57 | try { |
| 58 | objs = FilterUtility::GetFilterTargets(qd, params, user); |
| 59 | } catch (const MissingPermissionError& ex) { |
| 60 | HttpUtility::SendJsonError(response, params, 403, ex.what()); |
| 61 | return true; |
| 62 | } catch (const std::exception& ex) { |
| 63 | HttpUtility::SendJsonError(response, params, 404, |
| 64 | "No objects found.", |
| 65 | DiagnosticInformation(ex)); |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | Value attrsVal = params->Get("attrs"); |
| 70 | |
| 71 | if (attrsVal.GetReflectionType() != Dictionary::TypeInstance && attrsVal.GetType() != ValueEmpty) { |
| 72 | HttpUtility::SendJsonError(response, params, 400, |
| 73 | "Invalid type for 'attrs' attribute specified. Dictionary type is required." |
| 74 | "Or is this a POST query and you missed adding a 'X-HTTP-Method-Override: GET' header?"); |
nothing calls this directly
no test coverage detected