Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 3 | #include <dbus_singleton.hpp> |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 4 | #include <persistent_data_middleware.hpp> |
| 5 | #include <token_authorization_middleware.hpp> |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 6 | #include <fstream> |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 7 | #include <streambuf> |
| 8 | #include <string> |
Ed Tanous | 2a866f8 | 2017-10-25 17:46:24 -0700 | [diff] [blame] | 9 | #include <crow/app.h> |
| 10 | #include <boost/algorithm/string.hpp> |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 11 | namespace crow { |
| 12 | namespace redfish { |
| 13 | |
Ed Tanous | 6c23301 | 2018-03-15 14:43:56 -0700 | [diff] [blame] | 14 | using ManagedObjectType = std::vector<std::pair< |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 15 | sdbusplus::message::object_path, |
| 16 | boost::container::flat_map< |
| 17 | std::string, boost::container::flat_map< |
| 18 | std::string, sdbusplus::message::variant<bool>>>>>; |
Ed Tanous | 6c23301 | 2018-03-15 14:43:56 -0700 | [diff] [blame] | 19 | |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 20 | template <typename... Middlewares> |
| 21 | void request_routes(Crow<Middlewares...>& app) { |
| 22 | CROW_ROUTE(app, "/redfish/") |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 23 | .methods("GET"_method)([](const crow::request& req, crow::response& res) { |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 24 | res.json_value = {{"v1", "/redfish/v1/"}}; |
| 25 | res.end(); |
| 26 | }); |
| 27 | |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 28 | CROW_ROUTE(app, "/redfish/v1/AccountService/Accounts/") |
| 29 | .methods( |
| 30 | "GET"_method)([&](const crow::request& req, crow::response& res) { |
Ed Tanous | 6c23301 | 2018-03-15 14:43:56 -0700 | [diff] [blame] | 31 | crow::connections::system_bus->async_method_call( |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 32 | [&](const boost::system::error_code ec, |
Ed Tanous | 6c23301 | 2018-03-15 14:43:56 -0700 | [diff] [blame] | 33 | const ManagedObjectType& users) { |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 34 | if (ec) { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 35 | res.result(boost::beast::http::status::internal_server_error); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 36 | } else { |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 37 | res.json_value = { |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 38 | {"@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 Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 48 | nlohmann::json member_array = nlohmann::json::array(); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 49 | int user_index = 0; |
Ed Tanous | 6c23301 | 2018-03-15 14:43:56 -0700 | [diff] [blame] | 50 | for (auto& user : users) { |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 51 | const std::string& path = |
Ed Tanous | daf36e2 | 2018-04-20 16:01:36 -0700 | [diff] [blame] | 52 | static_cast<const std::string&>(user.first); |
Ed Tanous | 6c23301 | 2018-03-15 14:43:56 -0700 | [diff] [blame] | 53 | std::size_t last_index = path.rfind("/"); |
| 54 | if (last_index == std::string::npos) { |
| 55 | last_index = 0; |
| 56 | } else { |
| 57 | last_index += 1; |
| 58 | } |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 59 | member_array.push_back( |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 60 | {{"@odata.id", "/redfish/v1/AccountService/Accounts/" + |
Ed Tanous | 6c23301 | 2018-03-15 14:43:56 -0700 | [diff] [blame] | 61 | path.substr(last_index)}}); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 62 | } |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 63 | res.json_value["Members"] = member_array; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 64 | } |
| 65 | res.end(); |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 66 | }, |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 67 | "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user", |
| 68 | "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 69 | }); |
| 70 | |
Ed Tanous | 6c23301 | 2018-03-15 14:43:56 -0700 | [diff] [blame] | 71 | CROW_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/") |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 72 | .methods("GET"_method)([](const crow::request& req, crow::response& res, |
Ed Tanous | 6c23301 | 2018-03-15 14:43:56 -0700 | [diff] [blame] | 73 | const std::string& account_name) { |
| 74 | |
| 75 | crow::connections::system_bus->async_method_call( |
| 76 | [&, account_name{std::move(account_name)} ]( |
| 77 | const boost::system::error_code ec, |
| 78 | const ManagedObjectType& users) { |
| 79 | if (ec) { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 80 | res.result(boost::beast::http::status::internal_server_error); |
Ed Tanous | 6c23301 | 2018-03-15 14:43:56 -0700 | [diff] [blame] | 81 | } else { |
| 82 | for (auto& user : users) { |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 83 | const std::string& path = |
Ed Tanous | daf36e2 | 2018-04-20 16:01:36 -0700 | [diff] [blame] | 84 | static_cast<const std::string&>(user.first); |
Ed Tanous | 6c23301 | 2018-03-15 14:43:56 -0700 | [diff] [blame] | 85 | std::size_t last_index = path.rfind("/"); |
| 86 | if (last_index == std::string::npos) { |
| 87 | last_index = 0; |
| 88 | } else { |
| 89 | last_index += 1; |
| 90 | } |
| 91 | if (path.substr(last_index) == account_name) { |
| 92 | res.json_value = { |
| 93 | {"@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}, |
| 103 | {"UserName", account_name}, |
| 104 | {"RoleId", "Administrator"}, |
| 105 | {"Links", |
| 106 | {{"Role", |
| 107 | {{"@odata.id", |
| 108 | "/redfish/v1/AccountService/Roles/" |
| 109 | "Administrator"}}}}}}; |
| 110 | break; |
| 111 | } |
| 112 | } |
| 113 | if (res.json_value.is_null()) { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 114 | res.result(boost::beast::http::status::not_found); |
Ed Tanous | 6c23301 | 2018-03-15 14:43:56 -0700 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | res.end(); |
| 118 | }, |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 119 | "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user", |
| 120 | "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 121 | }); |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 122 | } |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 123 | } // namespace redfish |
| 124 | } // namespace crow |