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> |
| 23 | |
Borawski.Lukasz | b6df6dc | 2018-01-24 10:20:45 +0100 | [diff] [blame] | 24 | #include "crow.h" |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 25 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 26 | namespace redfish |
| 27 | { |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 28 | |
| 29 | /** |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 30 | * AsyncResp |
| 31 | * Gathers data needed for response processing after async calls are done |
| 32 | */ |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 33 | class AsyncResp |
| 34 | { |
| 35 | public: |
| 36 | AsyncResp(crow::Response& response) : res(response) |
| 37 | { |
| 38 | } |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 39 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 40 | ~AsyncResp() |
| 41 | { |
| 42 | res.end(); |
| 43 | } |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 44 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 45 | crow::Response& res; |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | /** |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 49 | * @brief Abstract class used for implementing Redfish nodes. |
| 50 | * |
| 51 | */ |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 52 | class Node |
| 53 | { |
| 54 | public: |
| 55 | template <typename... Params> |
| 56 | Node(CrowApp& app, std::string&& entityUrl, Params... params) |
| 57 | { |
| 58 | app.routeDynamic(entityUrl.c_str()) |
| 59 | .methods("GET"_method, "PATCH"_method, "POST"_method, |
| 60 | "DELETE"_method)([&](const crow::Request& req, |
| 61 | crow::Response& res, |
| 62 | Params... params) { |
| 63 | std::vector<std::string> paramVec = {params...}; |
| 64 | dispatchRequest(app, req, res, paramVec); |
| 65 | }); |
Borawski.Lukasz | c1a46bd | 2018-02-08 13:31:59 +0100 | [diff] [blame] | 66 | } |
Ed Tanous | cbbfa96 | 2018-03-13 16:46:28 -0700 | [diff] [blame] | 67 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 68 | virtual ~Node() = default; |
Borawski.Lukasz | c1a46bd | 2018-02-08 13:31:59 +0100 | [diff] [blame] | 69 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 70 | OperationMap entityPrivileges; |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 71 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 72 | protected: |
| 73 | // Node is designed to be an abstract class, so doGet is pure virtual |
| 74 | virtual void doGet(crow::Response& res, const crow::Request& req, |
| 75 | const std::vector<std::string>& params) |
| 76 | { |
| 77 | res.result(boost::beast::http::status::method_not_allowed); |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 78 | res.end(); |
| 79 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 80 | |
| 81 | virtual void doPatch(crow::Response& res, const crow::Request& req, |
| 82 | const std::vector<std::string>& params) |
| 83 | { |
| 84 | res.result(boost::beast::http::status::method_not_allowed); |
| 85 | res.end(); |
| 86 | } |
| 87 | |
| 88 | virtual void doPost(crow::Response& res, const crow::Request& req, |
| 89 | const std::vector<std::string>& params) |
| 90 | { |
| 91 | res.result(boost::beast::http::status::method_not_allowed); |
| 92 | res.end(); |
| 93 | } |
| 94 | |
| 95 | virtual void doDelete(crow::Response& res, const crow::Request& req, |
| 96 | const std::vector<std::string>& params) |
| 97 | { |
| 98 | res.result(boost::beast::http::status::method_not_allowed); |
| 99 | res.end(); |
| 100 | } |
| 101 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 102 | private: |
| 103 | void dispatchRequest(CrowApp& app, const crow::Request& req, |
| 104 | crow::Response& res, |
| 105 | const std::vector<std::string>& params) |
| 106 | { |
| 107 | auto ctx = |
| 108 | app.template getContext<crow::token_authorization::Middleware>(req); |
| 109 | |
| 110 | if (!isMethodAllowedForUser(req.method(), entityPrivileges, |
| 111 | ctx.session->username)) |
| 112 | { |
| 113 | res.result(boost::beast::http::status::method_not_allowed); |
| 114 | res.end(); |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | switch (req.method()) |
| 119 | { |
| 120 | case "GET"_method: |
| 121 | doGet(res, req, params); |
| 122 | break; |
| 123 | |
| 124 | case "PATCH"_method: |
| 125 | doPatch(res, req, params); |
| 126 | break; |
| 127 | |
| 128 | case "POST"_method: |
| 129 | doPost(res, req, params); |
| 130 | break; |
| 131 | |
| 132 | case "DELETE"_method: |
| 133 | doDelete(res, req, params); |
| 134 | break; |
| 135 | |
| 136 | default: |
| 137 | res.result(boost::beast::http::status::not_found); |
| 138 | res.end(); |
| 139 | } |
| 140 | return; |
| 141 | } |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 142 | }; |
| 143 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 144 | } // namespace redfish |