Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2018 Intel Corporation |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | */ |
| 16 | #pragma once |
| 17 | |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 18 | #include "privileges.hpp" |
| 19 | #include "token_authorization_middleware.hpp" |
Borawski.Lukasz | c1a46bd | 2018-02-08 13:31:59 +0100 | [diff] [blame] | 20 | #include "webserver_common.hpp" |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 21 | |
Ed Tanous | a0803ef | 2018-08-29 13:29:23 -0700 | [diff] [blame] | 22 | #include <error_messages.hpp> |
Ed Tanous | 770841b | 2019-02-27 10:25:45 -0800 | [diff] [blame] | 23 | #include <vector> |
Ed Tanous | a0803ef | 2018-08-29 13:29:23 -0700 | [diff] [blame] | 24 | |
Ed Tanous | 770841b | 2019-02-27 10:25:45 -0800 | [diff] [blame] | 25 | #include "crow/http_request.h" |
| 26 | #include "crow/http_response.h" |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 27 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 28 | namespace redfish |
| 29 | { |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 30 | |
| 31 | /** |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 32 | * AsyncResp |
| 33 | * Gathers data needed for response processing after async calls are done |
| 34 | */ |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 35 | class AsyncResp |
| 36 | { |
| 37 | public: |
| 38 | AsyncResp(crow::Response& response) : res(response) |
| 39 | { |
| 40 | } |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 41 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 42 | ~AsyncResp() |
| 43 | { |
| 44 | res.end(); |
| 45 | } |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 46 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 47 | crow::Response& res; |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | /** |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 51 | * @brief Abstract class used for implementing Redfish nodes. |
| 52 | * |
| 53 | */ |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 54 | class Node |
| 55 | { |
| 56 | public: |
| 57 | template <typename... Params> |
| 58 | Node(CrowApp& app, std::string&& entityUrl, Params... params) |
| 59 | { |
| 60 | app.routeDynamic(entityUrl.c_str()) |
| 61 | .methods("GET"_method, "PATCH"_method, "POST"_method, |
| 62 | "DELETE"_method)([&](const crow::Request& req, |
| 63 | crow::Response& res, |
| 64 | Params... params) { |
| 65 | std::vector<std::string> paramVec = {params...}; |
| 66 | dispatchRequest(app, req, res, paramVec); |
| 67 | }); |
Borawski.Lukasz | c1a46bd | 2018-02-08 13:31:59 +0100 | [diff] [blame] | 68 | } |
Ed Tanous | cbbfa96 | 2018-03-13 16:46:28 -0700 | [diff] [blame] | 69 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 70 | virtual ~Node() = default; |
Borawski.Lukasz | c1a46bd | 2018-02-08 13:31:59 +0100 | [diff] [blame] | 71 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 72 | OperationMap entityPrivileges; |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 73 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 74 | protected: |
| 75 | // Node is designed to be an abstract class, so doGet is pure virtual |
| 76 | virtual void doGet(crow::Response& res, const crow::Request& req, |
| 77 | const std::vector<std::string>& params) |
| 78 | { |
| 79 | res.result(boost::beast::http::status::method_not_allowed); |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 80 | res.end(); |
| 81 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 82 | |
| 83 | virtual void doPatch(crow::Response& res, const crow::Request& req, |
| 84 | const std::vector<std::string>& params) |
| 85 | { |
| 86 | res.result(boost::beast::http::status::method_not_allowed); |
| 87 | res.end(); |
| 88 | } |
| 89 | |
| 90 | virtual void doPost(crow::Response& res, const crow::Request& req, |
| 91 | const std::vector<std::string>& params) |
| 92 | { |
| 93 | res.result(boost::beast::http::status::method_not_allowed); |
| 94 | res.end(); |
| 95 | } |
| 96 | |
| 97 | virtual void doDelete(crow::Response& res, const crow::Request& req, |
| 98 | const std::vector<std::string>& params) |
| 99 | { |
| 100 | res.result(boost::beast::http::status::method_not_allowed); |
| 101 | res.end(); |
| 102 | } |
| 103 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 104 | private: |
| 105 | void dispatchRequest(CrowApp& app, const crow::Request& req, |
| 106 | crow::Response& res, |
| 107 | const std::vector<std::string>& params) |
| 108 | { |
| 109 | auto ctx = |
| 110 | app.template getContext<crow::token_authorization::Middleware>(req); |
| 111 | |
| 112 | if (!isMethodAllowedForUser(req.method(), entityPrivileges, |
| 113 | ctx.session->username)) |
| 114 | { |
| 115 | res.result(boost::beast::http::status::method_not_allowed); |
| 116 | res.end(); |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | switch (req.method()) |
| 121 | { |
| 122 | case "GET"_method: |
| 123 | doGet(res, req, params); |
| 124 | break; |
| 125 | |
| 126 | case "PATCH"_method: |
| 127 | doPatch(res, req, params); |
| 128 | break; |
| 129 | |
| 130 | case "POST"_method: |
| 131 | doPost(res, req, params); |
| 132 | break; |
| 133 | |
| 134 | case "DELETE"_method: |
| 135 | doDelete(res, req, params); |
| 136 | break; |
| 137 | |
| 138 | default: |
| 139 | res.result(boost::beast::http::status::not_found); |
| 140 | res.end(); |
| 141 | } |
| 142 | return; |
| 143 | } |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 144 | }; |
| 145 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 146 | } // namespace redfish |