summaryrefslogtreecommitdiff
path: root/include/redfish_v1.hpp
blob: b81aa5474a91d30d42a7056d0d1cf517f61b7857 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#pragma once

#include <dbus_singleton.hpp>
#include <persistent_data_middleware.hpp>
#include <token_authorization_middleware.hpp>
#include <fstream>
#include <streambuf>
#include <string>
#include <crow/app.h>
#include <boost/algorithm/string.hpp>
namespace crow {
namespace redfish {

using ManagedObjectType = std::vector<std::pair<
    sdbusplus::message::object_path,
    boost::container::flat_map<
        std::string, boost::container::flat_map<
                         std::string, sdbusplus::message::variant<bool>>>>>;

template <typename... Middlewares>
void requestRoutes(Crow<Middlewares...>& app) {
  BMCWEB_ROUTE(app, "/redfish/")
      .methods("GET"_method)([](const crow::Request& req, crow::Response& res) {
        res.jsonValue = {{"v1", "/redfish/v1/"}};
        res.end();
      });

  BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
      .methods(
          "GET"_method)([&](const crow::Request& req, crow::Response& res) {
        crow::connections::systemBus->async_method_call(
            [&](const boost::system::error_code ec,
                const ManagedObjectType& users) {
              if (ec) {
                res.result(boost::beast::http::status::internal_server_error);
              } else {
                res.jsonValue = {
                    {"@odata.context",
                     "/redfish/v1/"
                     "$metadata#ManagerAccountCollection."
                     "ManagerAccountCollection"},
                    {"@odata.id", "/redfish/v1/AccountService/Accounts"},
                    {"@odata.type",
                     "#ManagerAccountCollection.ManagerAccountCollection"},
                    {"Name", "Accounts Collection"},
                    {"Description", "BMC User Accounts"},
                    {"Members@odata.count", users.size()}};
                nlohmann::json memberArray = nlohmann::json::array();
                int userIndex = 0;
                for (auto& user : users) {
                  const std::string& path =
                      static_cast<const std::string&>(user.first);
                  std::size_t lastIndex = path.rfind("/");
                  if (lastIndex == std::string::npos) {
                    lastIndex = 0;
                  } else {
                    lastIndex += 1;
                  }
                  memberArray.push_back(
                      {{"@odata.id", "/redfish/v1/AccountService/Accounts/" +
                                         path.substr(lastIndex)}});
                }
                res.jsonValue["Members"] = memberArray;
              }
              res.end();
            },
            "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
            "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
      });

  BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
      .methods("GET"_method)([](const crow::Request& req, crow::Response& res,
                                const std::string& account_name) {

        crow::connections::systemBus->async_method_call(
            [&, accountName{std::move(account_name)} ](
                const boost::system::error_code ec,
                const ManagedObjectType& users) {
              if (ec) {
                res.result(boost::beast::http::status::internal_server_error);
              } else {
                for (auto& user : users) {
                  const std::string& path =
                      static_cast<const std::string&>(user.first);
                  std::size_t lastIndex = path.rfind("/");
                  if (lastIndex == std::string::npos) {
                    lastIndex = 0;
                  } else {
                    lastIndex += 1;
                  }
                  if (path.substr(lastIndex) == accountName) {
                    res.jsonValue = {
                        {"@odata.context",
                         "/redfish/v1/$metadata#ManagerAccount.ManagerAccount"},
                        {"@odata.id", "/redfish/v1/AccountService/Accounts/1"},
                        {"@odata.type",
                         "#ManagerAccount.v1_0_3.ManagerAccount"},
                        {"Id", "1"},
                        {"Name", "User Account"},
                        {"Description", "User Account"},
                        {"Enabled", false},
                        {"Password", nullptr},
                        {"UserName", accountName},
                        {"RoleId", "Administrator"},
                        {"Links",
                         {{"Role",
                           {{"@odata.id",
                             "/redfish/v1/AccountService/Roles/"
                             "Administrator"}}}}}};
                    break;
                  }
                }
                if (res.jsonValue.is_null()) {
                  res.result(boost::beast::http::status::not_found);
                }
              }
              res.end();
            },
            "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
            "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
      });
}
}  // namespace redfish
}  // namespace crow