Ed Tanous | 08bbe11 | 2023-04-06 13:10:02 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "async_resp.hpp" |
| 4 | #include "http_request.hpp" |
| 5 | #include "privileges.hpp" |
| 6 | #include "verb.hpp" |
| 7 | |
Ed Tanous | 003301a | 2024-04-16 09:59:19 -0700 | [diff] [blame] | 8 | #include <boost/asio/ip/tcp.hpp> |
| 9 | #include <boost/asio/ssl/stream.hpp> |
Ed Tanous | 08bbe11 | 2023-04-06 13:10:02 -0700 | [diff] [blame] | 10 | |
| 11 | #include <memory> |
| 12 | #include <string> |
| 13 | |
| 14 | namespace crow |
| 15 | { |
| 16 | class BaseRule |
| 17 | { |
| 18 | public: |
| 19 | explicit BaseRule(const std::string& thisRule) : rule(thisRule) {} |
| 20 | |
| 21 | virtual ~BaseRule() = default; |
| 22 | |
| 23 | BaseRule(const BaseRule&) = delete; |
| 24 | BaseRule(BaseRule&&) = delete; |
| 25 | BaseRule& operator=(const BaseRule&) = delete; |
| 26 | BaseRule& operator=(const BaseRule&&) = delete; |
| 27 | |
| 28 | virtual void validate() = 0; |
| 29 | std::unique_ptr<BaseRule> upgrade() |
| 30 | { |
| 31 | if (ruleToUpgrade) |
| 32 | { |
| 33 | return std::move(ruleToUpgrade); |
| 34 | } |
| 35 | return {}; |
| 36 | } |
| 37 | |
| 38 | virtual void handle(const Request& /*req*/, |
| 39 | const std::shared_ptr<bmcweb::AsyncResp>&, |
| 40 | const std::vector<std::string>&) = 0; |
Ed Tanous | 08bbe11 | 2023-04-06 13:10:02 -0700 | [diff] [blame] | 41 | virtual void |
| 42 | handleUpgrade(const Request& /*req*/, |
| 43 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 44 | boost::asio::ip::tcp::socket&& /*adaptor*/) |
| 45 | { |
| 46 | asyncResp->res.result(boost::beast::http::status::not_found); |
| 47 | } |
Ed Tanous | 8db8374 | 2024-04-13 09:11:15 -0700 | [diff] [blame] | 48 | |
Ed Tanous | 08bbe11 | 2023-04-06 13:10:02 -0700 | [diff] [blame] | 49 | virtual void handleUpgrade( |
| 50 | const Request& /*req*/, |
| 51 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
Ed Tanous | 003301a | 2024-04-16 09:59:19 -0700 | [diff] [blame] | 52 | boost::asio::ssl::stream<boost::asio::ip::tcp::socket>&& /*adaptor*/) |
Ed Tanous | 08bbe11 | 2023-04-06 13:10:02 -0700 | [diff] [blame] | 53 | { |
| 54 | asyncResp->res.result(boost::beast::http::status::not_found); |
| 55 | } |
Ed Tanous | 08bbe11 | 2023-04-06 13:10:02 -0700 | [diff] [blame] | 56 | |
| 57 | size_t getMethods() const |
| 58 | { |
| 59 | return methodsBitfield; |
| 60 | } |
| 61 | |
| 62 | bool checkPrivileges(const redfish::Privileges& userPrivileges) |
| 63 | { |
| 64 | // If there are no privileges assigned, assume no privileges |
| 65 | // required |
| 66 | if (privilegesSet.empty()) |
| 67 | { |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | for (const redfish::Privileges& requiredPrivileges : privilegesSet) |
| 72 | { |
| 73 | if (userPrivileges.isSupersetOf(requiredPrivileges)) |
| 74 | { |
| 75 | return true; |
| 76 | } |
| 77 | } |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | size_t methodsBitfield{1 << static_cast<size_t>(HttpVerb::Get)}; |
| 82 | static_assert(std::numeric_limits<decltype(methodsBitfield)>::digits > |
Ed Tanous | a3b9eb9 | 2024-06-03 08:39:37 -0700 | [diff] [blame] | 83 | static_cast<int>(HttpVerb::Max), |
Ed Tanous | 08bbe11 | 2023-04-06 13:10:02 -0700 | [diff] [blame] | 84 | "Not enough bits to store bitfield"); |
| 85 | |
Ed Tanous | a3b9eb9 | 2024-06-03 08:39:37 -0700 | [diff] [blame] | 86 | bool isNotFound = false; |
| 87 | bool isMethodNotAllowed = false; |
| 88 | bool isUpgrade = false; |
| 89 | |
Ed Tanous | 08bbe11 | 2023-04-06 13:10:02 -0700 | [diff] [blame] | 90 | std::vector<redfish::Privileges> privilegesSet; |
| 91 | |
| 92 | std::string rule; |
| 93 | |
| 94 | std::unique_ptr<BaseRule> ruleToUpgrade; |
| 95 | |
| 96 | friend class Router; |
| 97 | template <typename T> |
| 98 | friend struct RuleParameterTraits; |
| 99 | }; |
| 100 | |
| 101 | } // namespace crow |