Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame^] | 3 | #include <crow/app.h> |
| 4 | |
| 5 | #include <boost/algorithm/string.hpp> |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 6 | #include <dbus_singleton.hpp> |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 7 | #include <fstream> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame^] | 8 | #include <persistent_data_middleware.hpp> |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 9 | #include <streambuf> |
| 10 | #include <string> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame^] | 11 | #include <token_authorization_middleware.hpp> |
| 12 | namespace crow |
| 13 | { |
| 14 | namespace redfish |
| 15 | { |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 16 | |
Ed Tanous | 6c23301 | 2018-03-15 14:43:56 -0700 | [diff] [blame] | 17 | using ManagedObjectType = std::vector<std::pair< |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 18 | sdbusplus::message::object_path, |
| 19 | boost::container::flat_map< |
| 20 | std::string, boost::container::flat_map< |
| 21 | std::string, sdbusplus::message::variant<bool>>>>>; |
Ed Tanous | 6c23301 | 2018-03-15 14:43:56 -0700 | [diff] [blame] | 22 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame^] | 23 | template <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 Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 31 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame^] | 32 | 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 Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 84 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame^] | 85 | 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 Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 147 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame^] | 148 | } // namespace redfish |
| 149 | } // namespace crow |