blob: 13e18380ad2e3e698052478409c85b7a2184f981 [file] [log] [blame]
Ed Tanous911ac312017-08-15 09:37:42 -07001#pragma once
2
Ed Tanous1abe55e2018-09-05 08:30:59 -07003#include <crow/app.h>
4
5#include <boost/algorithm/string.hpp>
Ed Tanousaa2e59c2018-04-12 12:17:20 -07006#include <dbus_singleton.hpp>
Ed Tanous911ac312017-08-15 09:37:42 -07007#include <fstream>
Ed Tanous1abe55e2018-09-05 08:30:59 -07008#include <persistent_data_middleware.hpp>
Ed Tanousba9f9a62017-10-11 16:40:35 -07009#include <streambuf>
10#include <string>
Ed Tanous1abe55e2018-09-05 08:30:59 -070011#include <token_authorization_middleware.hpp>
12namespace crow
13{
14namespace redfish
15{
Ed Tanous3dac7492017-08-02 13:46:20 -070016
Ed Tanous6c233012018-03-15 14:43:56 -070017using ManagedObjectType = std::vector<std::pair<
Ed Tanousaa2e59c2018-04-12 12:17:20 -070018 sdbusplus::message::object_path,
19 boost::container::flat_map<
20 std::string, boost::container::flat_map<
21 std::string, sdbusplus::message::variant<bool>>>>>;
Ed Tanous6c233012018-03-15 14:43:56 -070022
Ed Tanous1abe55e2018-09-05 08:30:59 -070023template <typename... Middlewares> void requestRoutes(Crow<Middlewares...>& app)
24{
25 BMCWEB_ROUTE(app, "/redfish/")
26 .methods("GET"_method)(
27 [](const crow::Request& req, crow::Response& res) {
28 res.jsonValue = {{"v1", "/redfish/v1/"}};
29 res.end();
30 });
Ed Tanousba9f9a62017-10-11 16:40:35 -070031
Ed Tanous1abe55e2018-09-05 08:30:59 -070032 BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
33 .methods(
34 "GET"_method)([&](const crow::Request& req, crow::Response& res) {
35 crow::connections::systemBus->async_method_call(
36 [&](const boost::system::error_code ec,
37 const ManagedObjectType& users) {
38 if (ec)
39 {
40 res.result(
41 boost::beast::http::status::internal_server_error);
42 }
43 else
44 {
45 res.jsonValue = {
46 {"@odata.context",
47 "/redfish/v1/"
48 "$metadata#ManagerAccountCollection."
49 "ManagerAccountCollection"},
50 {"@odata.id",
51 "/redfish/v1/AccountService/Accounts"},
52 {"@odata.type", "#ManagerAccountCollection."
53 "ManagerAccountCollection"},
54 {"Name", "Accounts Collection"},
55 {"Description", "BMC User Accounts"},
56 {"Members@odata.count", users.size()}};
57 nlohmann::json memberArray = nlohmann::json::array();
58 int userIndex = 0;
59 for (auto& user : users)
60 {
61 const std::string& path =
62 static_cast<const std::string&>(user.first);
63 std::size_t lastIndex = path.rfind("/");
64 if (lastIndex == std::string::npos)
65 {
66 lastIndex = 0;
67 }
68 else
69 {
70 lastIndex += 1;
71 }
72 memberArray.push_back(
73 {{"@odata.id",
74 "/redfish/v1/AccountService/Accounts/" +
75 path.substr(lastIndex)}});
76 }
77 res.jsonValue["Members"] = memberArray;
78 }
79 res.end();
80 },
81 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
82 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
83 });
Ed Tanous3dac7492017-08-02 13:46:20 -070084
Ed Tanous1abe55e2018-09-05 08:30:59 -070085 BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
86 .methods("GET"_method)([](const crow::Request& req, crow::Response& res,
87 const std::string& account_name) {
88 crow::connections::systemBus->async_method_call(
89 [&, accountName{std::move(account_name)}](
90 const boost::system::error_code ec,
91 const ManagedObjectType& users) {
92 if (ec)
93 {
94 res.result(
95 boost::beast::http::status::internal_server_error);
96 }
97 else
98 {
99 for (auto& user : users)
100 {
101 const std::string& path =
102 static_cast<const std::string&>(user.first);
103 std::size_t lastIndex = path.rfind("/");
104 if (lastIndex == std::string::npos)
105 {
106 lastIndex = 0;
107 }
108 else
109 {
110 lastIndex += 1;
111 }
112 if (path.substr(lastIndex) == accountName)
113 {
114 res.jsonValue = {
115 {"@odata.context",
116 "/redfish/v1/"
117 "$metadata#ManagerAccount.ManagerAccount"},
118 {"@odata.id",
119 "/redfish/v1/AccountService/Accounts/1"},
120 {"@odata.type",
121 "#ManagerAccount.v1_0_3.ManagerAccount"},
122 {"Id", "1"},
123 {"Name", "User Account"},
124 {"Description", "User Account"},
125 {"Enabled", false},
126 {"Password", nullptr},
127 {"UserName", accountName},
128 {"RoleId", "Administrator"},
129 {"Links",
130 {{"Role",
131 {{"@odata.id",
132 "/redfish/v1/AccountService/Roles/"
133 "Administrator"}}}}}};
134 break;
135 }
136 }
137 if (res.jsonValue.is_null())
138 {
139 res.result(boost::beast::http::status::not_found);
140 }
141 }
142 res.end();
143 },
144 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
145 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
146 });
Ed Tanous3dac7492017-08-02 13:46:20 -0700147}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700148} // namespace redfish
149} // namespace crow