blob: b81aa5474a91d30d42a7056d0d1cf517f61b7857 [file] [log] [blame]
Ed Tanous911ac312017-08-15 09:37:42 -07001#pragma once
2
Ed Tanousaa2e59c2018-04-12 12:17:20 -07003#include <dbus_singleton.hpp>
Ed Tanousba9f9a62017-10-11 16:40:35 -07004#include <persistent_data_middleware.hpp>
5#include <token_authorization_middleware.hpp>
Ed Tanous911ac312017-08-15 09:37:42 -07006#include <fstream>
Ed Tanousba9f9a62017-10-11 16:40:35 -07007#include <streambuf>
8#include <string>
Ed Tanous2a866f82017-10-25 17:46:24 -07009#include <crow/app.h>
10#include <boost/algorithm/string.hpp>
Ed Tanous3dac7492017-08-02 13:46:20 -070011namespace crow {
12namespace redfish {
13
Ed Tanous6c233012018-03-15 14:43:56 -070014using ManagedObjectType = std::vector<std::pair<
Ed Tanousaa2e59c2018-04-12 12:17:20 -070015 sdbusplus::message::object_path,
16 boost::container::flat_map<
17 std::string, boost::container::flat_map<
18 std::string, sdbusplus::message::variant<bool>>>>>;
Ed Tanous6c233012018-03-15 14:43:56 -070019
Ed Tanousba9f9a62017-10-11 16:40:35 -070020template <typename... Middlewares>
Ed Tanous55c7b7a2018-05-22 15:27:24 -070021void requestRoutes(Crow<Middlewares...>& app) {
22 BMCWEB_ROUTE(app, "/redfish/")
23 .methods("GET"_method)([](const crow::Request& req, crow::Response& res) {
24 res.jsonValue = {{"v1", "/redfish/v1/"}};
Ed Tanousba9f9a62017-10-11 16:40:35 -070025 res.end();
26 });
27
Ed Tanous55c7b7a2018-05-22 15:27:24 -070028 BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
Ed Tanousba9f9a62017-10-11 16:40:35 -070029 .methods(
Ed Tanous55c7b7a2018-05-22 15:27:24 -070030 "GET"_method)([&](const crow::Request& req, crow::Response& res) {
31 crow::connections::systemBus->async_method_call(
Ed Tanous911ac312017-08-15 09:37:42 -070032 [&](const boost::system::error_code ec,
Ed Tanous6c233012018-03-15 14:43:56 -070033 const ManagedObjectType& users) {
Ed Tanous911ac312017-08-15 09:37:42 -070034 if (ec) {
Ed Tanouse0d918b2018-03-27 17:41:04 -070035 res.result(boost::beast::http::status::internal_server_error);
Ed Tanous911ac312017-08-15 09:37:42 -070036 } else {
Ed Tanous55c7b7a2018-05-22 15:27:24 -070037 res.jsonValue = {
Ed Tanous911ac312017-08-15 09:37:42 -070038 {"@odata.context",
39 "/redfish/v1/"
40 "$metadata#ManagerAccountCollection."
41 "ManagerAccountCollection"},
42 {"@odata.id", "/redfish/v1/AccountService/Accounts"},
43 {"@odata.type",
44 "#ManagerAccountCollection.ManagerAccountCollection"},
45 {"Name", "Accounts Collection"},
46 {"Description", "BMC User Accounts"},
47 {"Members@odata.count", users.size()}};
Ed Tanous55c7b7a2018-05-22 15:27:24 -070048 nlohmann::json memberArray = nlohmann::json::array();
49 int userIndex = 0;
Ed Tanous6c233012018-03-15 14:43:56 -070050 for (auto& user : users) {
Ed Tanousaa2e59c2018-04-12 12:17:20 -070051 const std::string& path =
Ed Tanousdaf36e22018-04-20 16:01:36 -070052 static_cast<const std::string&>(user.first);
Ed Tanous55c7b7a2018-05-22 15:27:24 -070053 std::size_t lastIndex = path.rfind("/");
54 if (lastIndex == std::string::npos) {
55 lastIndex = 0;
Ed Tanous6c233012018-03-15 14:43:56 -070056 } else {
Ed Tanous55c7b7a2018-05-22 15:27:24 -070057 lastIndex += 1;
Ed Tanous6c233012018-03-15 14:43:56 -070058 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -070059 memberArray.push_back(
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +010060 {{"@odata.id", "/redfish/v1/AccountService/Accounts/" +
Ed Tanous55c7b7a2018-05-22 15:27:24 -070061 path.substr(lastIndex)}});
Ed Tanous911ac312017-08-15 09:37:42 -070062 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -070063 res.jsonValue["Members"] = memberArray;
Ed Tanous911ac312017-08-15 09:37:42 -070064 }
65 res.end();
Ed Tanousba9f9a62017-10-11 16:40:35 -070066 },
Ed Tanousaa2e59c2018-04-12 12:17:20 -070067 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
68 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous3dac7492017-08-02 13:46:20 -070069 });
70
Ed Tanous55c7b7a2018-05-22 15:27:24 -070071 BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/")
72 .methods("GET"_method)([](const crow::Request& req, crow::Response& res,
Ed Tanous6c233012018-03-15 14:43:56 -070073 const std::string& account_name) {
74
Ed Tanous55c7b7a2018-05-22 15:27:24 -070075 crow::connections::systemBus->async_method_call(
76 [&, accountName{std::move(account_name)} ](
Ed Tanous6c233012018-03-15 14:43:56 -070077 const boost::system::error_code ec,
78 const ManagedObjectType& users) {
79 if (ec) {
Ed Tanouse0d918b2018-03-27 17:41:04 -070080 res.result(boost::beast::http::status::internal_server_error);
Ed Tanous6c233012018-03-15 14:43:56 -070081 } else {
82 for (auto& user : users) {
Ed Tanousaa2e59c2018-04-12 12:17:20 -070083 const std::string& path =
Ed Tanousdaf36e22018-04-20 16:01:36 -070084 static_cast<const std::string&>(user.first);
Ed Tanous55c7b7a2018-05-22 15:27:24 -070085 std::size_t lastIndex = path.rfind("/");
86 if (lastIndex == std::string::npos) {
87 lastIndex = 0;
Ed Tanous6c233012018-03-15 14:43:56 -070088 } else {
Ed Tanous55c7b7a2018-05-22 15:27:24 -070089 lastIndex += 1;
Ed Tanous6c233012018-03-15 14:43:56 -070090 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -070091 if (path.substr(lastIndex) == accountName) {
92 res.jsonValue = {
Ed Tanous6c233012018-03-15 14:43:56 -070093 {"@odata.context",
94 "/redfish/v1/$metadata#ManagerAccount.ManagerAccount"},
95 {"@odata.id", "/redfish/v1/AccountService/Accounts/1"},
96 {"@odata.type",
97 "#ManagerAccount.v1_0_3.ManagerAccount"},
98 {"Id", "1"},
99 {"Name", "User Account"},
100 {"Description", "User Account"},
101 {"Enabled", false},
102 {"Password", nullptr},
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700103 {"UserName", accountName},
Ed Tanous6c233012018-03-15 14:43:56 -0700104 {"RoleId", "Administrator"},
105 {"Links",
106 {{"Role",
107 {{"@odata.id",
108 "/redfish/v1/AccountService/Roles/"
109 "Administrator"}}}}}};
110 break;
111 }
112 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700113 if (res.jsonValue.is_null()) {
Ed Tanouse0d918b2018-03-27 17:41:04 -0700114 res.result(boost::beast::http::status::not_found);
Ed Tanous6c233012018-03-15 14:43:56 -0700115 }
116 }
117 res.end();
118 },
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700119 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
120 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanousba9f9a62017-10-11 16:40:35 -0700121 });
Ed Tanous3dac7492017-08-02 13:46:20 -0700122}
Ed Tanous911ac312017-08-15 09:37:42 -0700123} // namespace redfish
124} // namespace crow