blob: 46b37d970d42ec3393b0f14ad11229e9f29ee172 [file] [log] [blame]
Borawski.Lukasz86e1b662018-01-19 14:22:14 +01001/*
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
18#include "crow.h"
19#include "privileges.hpp"
20#include "token_authorization_middleware.hpp"
21
22namespace redfish {
23
24/**
25 * @brief Abstract class used for implementing Redfish nodes.
26 *
27 */
28class Node {
29 public:
30 template <typename CrowApp, typename... Params>
31 Node(CrowApp& app, PrivilegeProvider& provider, std::string odataType,
32 std::string odataId, Params... params)
33 : odataType(odataType), odataId(odataId) {
34
35 // privileges for the node as defined in the privileges_registry.json
36 entityPrivileges = provider.getPrivileges(odataId, odataType);
37
38 app.route_dynamic(std::move(odataId))
39 .methods("GET"_method, "PATCH"_method, "POST"_method,
40 "DELETE"_method)([&](const crow::request& req,
41 crow::response& res, Params... params) {
42 std::vector<std::string> paramVec = {params...};
43 dispatchRequest(app, req, res, paramVec);
44 });
45 }
46
47 template <typename CrowApp>
48 void dispatchRequest(CrowApp& app, const crow::request& req,
49 crow::response& res,
50 const std::vector<std::string>& params) {
51 // drop requests without required privileges
52 auto ctx =
53 app.template get_context<crow::TokenAuthorization::Middleware>(req);
54
55 if (!entityPrivileges.isMethodAllowed(req.method, ctx.session->username)) {
56 res.code = static_cast<int>(HttpRespCode::METHOD_NOT_ALLOWED);
57 res.end();
58 return;
59 }
60
61 switch (req.method) {
62 case "GET"_method:
63 doGet(res, req, params);
64 break;
65
66 case "PATCH"_method:
67 doPatch(res, req, params);
68 break;
69
70 case "POST"_method:
71 doPost(res, req, params);
72 break;
73
74 case "DELETE"_method:
75 doDelete(res, req, params);
76 break;
77
78 default:
79 res.code = static_cast<int>(HttpRespCode::NOT_FOUND);
80 res.end();
81 }
82 return;
83 }
84
85 protected:
86 const std::string odataType;
87 const std::string odataId;
88
89 // Node is designed to be an abstract class, so doGet is pure virutal
90 virtual void doGet(crow::response& res, const crow::request& req,
91 const std::vector<std::string>& params) = 0;
92
93 virtual void doPatch(crow::response& res, const crow::request& req,
94 const std::vector<std::string>& params) {
95 res.code = static_cast<int>(HttpRespCode::METHOD_NOT_ALLOWED);
96 res.end();
97 }
98
99 virtual void doPost(crow::response& res, const crow::request& req,
100 const std::vector<std::string>& params) {
101 res.code = static_cast<int>(HttpRespCode::METHOD_NOT_ALLOWED);
102 res.end();
103 }
104
105 virtual void doDelete(crow::response& res, const crow::request& req,
106 const std::vector<std::string>& params) {
107 res.code = static_cast<int>(HttpRespCode::METHOD_NOT_ALLOWED);
108 res.end();
109 }
110
111 EntityPrivileges entityPrivileges;
112};
113
114} // namespace redfish
115