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> |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 21 | void 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 Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 25 | res.end(); |
| 26 | }); |
| 27 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 28 | BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/") |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 29 | .methods( |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 30 | "GET"_method)([&](const crow::Request& req, crow::Response& res) { |
| 31 | crow::connections::systemBus->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 | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 37 | res.jsonValue = { |
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 | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 48 | nlohmann::json memberArray = nlohmann::json::array(); |
| 49 | int userIndex = 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 | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 53 | std::size_t lastIndex = path.rfind("/"); |
| 54 | if (lastIndex == std::string::npos) { |
| 55 | lastIndex = 0; |
Ed Tanous | 6c23301 | 2018-03-15 14:43:56 -0700 | [diff] [blame] | 56 | } else { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 57 | lastIndex += 1; |
Ed Tanous | 6c23301 | 2018-03-15 14:43:56 -0700 | [diff] [blame] | 58 | } |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 59 | memberArray.push_back( |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 60 | {{"@odata.id", "/redfish/v1/AccountService/Accounts/" + |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 61 | path.substr(lastIndex)}}); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 62 | } |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 63 | res.jsonValue["Members"] = memberArray; |
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 | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 71 | BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Accounts/<str>/") |
| 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 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 75 | crow::connections::systemBus->async_method_call( |
| 76 | [&, accountName{std::move(account_name)} ]( |
Ed Tanous | 6c23301 | 2018-03-15 14:43:56 -0700 | [diff] [blame] | 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 | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 85 | std::size_t lastIndex = path.rfind("/"); |
| 86 | if (lastIndex == std::string::npos) { |
| 87 | lastIndex = 0; |
Ed Tanous | 6c23301 | 2018-03-15 14:43:56 -0700 | [diff] [blame] | 88 | } else { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 89 | lastIndex += 1; |
Ed Tanous | 6c23301 | 2018-03-15 14:43:56 -0700 | [diff] [blame] | 90 | } |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 91 | if (path.substr(lastIndex) == accountName) { |
| 92 | res.jsonValue = { |
Ed Tanous | 6c23301 | 2018-03-15 14:43:56 -0700 | [diff] [blame] | 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}, |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 103 | {"UserName", accountName}, |
Ed Tanous | 6c23301 | 2018-03-15 14:43:56 -0700 | [diff] [blame] | 104 | {"RoleId", "Administrator"}, |
| 105 | {"Links", |
| 106 | {{"Role", |
| 107 | {{"@odata.id", |
| 108 | "/redfish/v1/AccountService/Roles/" |
| 109 | "Administrator"}}}}}}; |
| 110 | break; |
| 111 | } |
| 112 | } |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 113 | if (res.jsonValue.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 |