xref: /openbmc/bmcweb/http/routing/baserule.hpp (revision a3b9eb98)
108bbe119SEd Tanous #pragma once
208bbe119SEd Tanous 
308bbe119SEd Tanous #include "async_resp.hpp"
408bbe119SEd Tanous #include "http_request.hpp"
508bbe119SEd Tanous #include "privileges.hpp"
608bbe119SEd Tanous #include "verb.hpp"
708bbe119SEd Tanous 
8003301a2SEd Tanous #include <boost/asio/ip/tcp.hpp>
9003301a2SEd Tanous #include <boost/asio/ssl/stream.hpp>
1008bbe119SEd Tanous 
1108bbe119SEd Tanous #include <memory>
1208bbe119SEd Tanous #include <string>
1308bbe119SEd Tanous 
1408bbe119SEd Tanous namespace crow
1508bbe119SEd Tanous {
1608bbe119SEd Tanous class BaseRule
1708bbe119SEd Tanous {
1808bbe119SEd Tanous   public:
BaseRule(const std::string & thisRule)1908bbe119SEd Tanous     explicit BaseRule(const std::string& thisRule) : rule(thisRule) {}
2008bbe119SEd Tanous 
2108bbe119SEd Tanous     virtual ~BaseRule() = default;
2208bbe119SEd Tanous 
2308bbe119SEd Tanous     BaseRule(const BaseRule&) = delete;
2408bbe119SEd Tanous     BaseRule(BaseRule&&) = delete;
2508bbe119SEd Tanous     BaseRule& operator=(const BaseRule&) = delete;
2608bbe119SEd Tanous     BaseRule& operator=(const BaseRule&&) = delete;
2708bbe119SEd Tanous 
2808bbe119SEd Tanous     virtual void validate() = 0;
upgrade()2908bbe119SEd Tanous     std::unique_ptr<BaseRule> upgrade()
3008bbe119SEd Tanous     {
3108bbe119SEd Tanous         if (ruleToUpgrade)
3208bbe119SEd Tanous         {
3308bbe119SEd Tanous             return std::move(ruleToUpgrade);
3408bbe119SEd Tanous         }
3508bbe119SEd Tanous         return {};
3608bbe119SEd Tanous     }
3708bbe119SEd Tanous 
3808bbe119SEd Tanous     virtual void handle(const Request& /*req*/,
3908bbe119SEd Tanous                         const std::shared_ptr<bmcweb::AsyncResp>&,
4008bbe119SEd Tanous                         const std::vector<std::string>&) = 0;
4108bbe119SEd Tanous     virtual void
handleUpgrade(const Request &,const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,boost::asio::ip::tcp::socket &&)4208bbe119SEd Tanous         handleUpgrade(const Request& /*req*/,
4308bbe119SEd Tanous                       const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4408bbe119SEd Tanous                       boost::asio::ip::tcp::socket&& /*adaptor*/)
4508bbe119SEd Tanous     {
4608bbe119SEd Tanous         asyncResp->res.result(boost::beast::http::status::not_found);
4708bbe119SEd Tanous     }
488db83747SEd Tanous 
handleUpgrade(const Request &,const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,boost::asio::ssl::stream<boost::asio::ip::tcp::socket> &&)4908bbe119SEd Tanous     virtual void handleUpgrade(
5008bbe119SEd Tanous         const Request& /*req*/,
5108bbe119SEd Tanous         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
52003301a2SEd Tanous         boost::asio::ssl::stream<boost::asio::ip::tcp::socket>&& /*adaptor*/)
5308bbe119SEd Tanous     {
5408bbe119SEd Tanous         asyncResp->res.result(boost::beast::http::status::not_found);
5508bbe119SEd Tanous     }
5608bbe119SEd Tanous 
getMethods() const5708bbe119SEd Tanous     size_t getMethods() const
5808bbe119SEd Tanous     {
5908bbe119SEd Tanous         return methodsBitfield;
6008bbe119SEd Tanous     }
6108bbe119SEd Tanous 
checkPrivileges(const redfish::Privileges & userPrivileges)6208bbe119SEd Tanous     bool checkPrivileges(const redfish::Privileges& userPrivileges)
6308bbe119SEd Tanous     {
6408bbe119SEd Tanous         // If there are no privileges assigned, assume no privileges
6508bbe119SEd Tanous         // required
6608bbe119SEd Tanous         if (privilegesSet.empty())
6708bbe119SEd Tanous         {
6808bbe119SEd Tanous             return true;
6908bbe119SEd Tanous         }
7008bbe119SEd Tanous 
7108bbe119SEd Tanous         for (const redfish::Privileges& requiredPrivileges : privilegesSet)
7208bbe119SEd Tanous         {
7308bbe119SEd Tanous             if (userPrivileges.isSupersetOf(requiredPrivileges))
7408bbe119SEd Tanous             {
7508bbe119SEd Tanous                 return true;
7608bbe119SEd Tanous             }
7708bbe119SEd Tanous         }
7808bbe119SEd Tanous         return false;
7908bbe119SEd Tanous     }
8008bbe119SEd Tanous 
8108bbe119SEd Tanous     size_t methodsBitfield{1 << static_cast<size_t>(HttpVerb::Get)};
8208bbe119SEd Tanous     static_assert(std::numeric_limits<decltype(methodsBitfield)>::digits >
83*a3b9eb98SEd Tanous                       static_cast<int>(HttpVerb::Max),
8408bbe119SEd Tanous                   "Not enough bits to store bitfield");
8508bbe119SEd Tanous 
86*a3b9eb98SEd Tanous     bool isNotFound = false;
87*a3b9eb98SEd Tanous     bool isMethodNotAllowed = false;
88*a3b9eb98SEd Tanous     bool isUpgrade = false;
89*a3b9eb98SEd Tanous 
9008bbe119SEd Tanous     std::vector<redfish::Privileges> privilegesSet;
9108bbe119SEd Tanous 
9208bbe119SEd Tanous     std::string rule;
9308bbe119SEd Tanous 
9408bbe119SEd Tanous     std::unique_ptr<BaseRule> ruleToUpgrade;
9508bbe119SEd Tanous 
9608bbe119SEd Tanous     friend class Router;
9708bbe119SEd Tanous     template <typename T>
9808bbe119SEd Tanous     friend struct RuleParameterTraits;
9908bbe119SEd Tanous };
10008bbe119SEd Tanous 
10108bbe119SEd Tanous } // namespace crow
102