Lewanczyk, Dawid | 4e49bd4 | 2018-01-25 11:30:19 +0100 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2018 Intel Corporation |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | */ |
| 16 | #pragma once |
| 17 | |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 18 | #include "app.hpp" |
| 19 | #include "dbus_utility.hpp" |
| 20 | #include "query.hpp" |
| 21 | #include "registries/privilege_registry.hpp" |
| 22 | |
Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame] | 23 | #include <boost/url/format.hpp> |
Ed Tanous | 20fa6a2 | 2024-05-20 18:02:58 -0700 | [diff] [blame] | 24 | #include <nlohmann/json.hpp> |
Jonathan Doman | 1e1e598 | 2021-06-11 09:36:17 -0700 | [diff] [blame] | 25 | #include <sdbusplus/asio/property.hpp> |
Lewanczyk, Dawid | 4e49bd4 | 2018-01-25 11:30:19 +0100 | [diff] [blame] | 26 | |
Ed Tanous | 20fa6a2 | 2024-05-20 18:02:58 -0700 | [diff] [blame] | 27 | #include <optional> |
| 28 | #include <string_view> |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 29 | #include <variant> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 30 | namespace redfish |
| 31 | { |
Lewanczyk, Dawid | 4e49bd4 | 2018-01-25 11:30:19 +0100 | [diff] [blame] | 32 | |
AppaRao Puli | 8fcb65b | 2018-12-27 14:11:55 +0530 | [diff] [blame] | 33 | inline std::string getRoleFromPrivileges(std::string_view priv) |
| 34 | { |
| 35 | if (priv == "priv-admin") |
| 36 | { |
| 37 | return "Administrator"; |
| 38 | } |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 39 | if (priv == "priv-user") |
AppaRao Puli | 8fcb65b | 2018-12-27 14:11:55 +0530 | [diff] [blame] | 40 | { |
AppaRao Puli | c80fee5 | 2019-10-16 14:49:36 +0530 | [diff] [blame] | 41 | return "ReadOnly"; |
AppaRao Puli | 8fcb65b | 2018-12-27 14:11:55 +0530 | [diff] [blame] | 42 | } |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 43 | if (priv == "priv-operator") |
AppaRao Puli | 8fcb65b | 2018-12-27 14:11:55 +0530 | [diff] [blame] | 44 | { |
| 45 | return "Operator"; |
| 46 | } |
| 47 | return ""; |
| 48 | } |
| 49 | |
Ed Tanous | 20fa6a2 | 2024-05-20 18:02:58 -0700 | [diff] [blame] | 50 | inline std::optional<nlohmann::json::array_t> |
| 51 | getAssignedPrivFromRole(std::string_view role) |
AppaRao Puli | 8fcb65b | 2018-12-27 14:11:55 +0530 | [diff] [blame] | 52 | { |
Ed Tanous | 20fa6a2 | 2024-05-20 18:02:58 -0700 | [diff] [blame] | 53 | nlohmann::json::array_t privArray; |
AppaRao Puli | 8fcb65b | 2018-12-27 14:11:55 +0530 | [diff] [blame] | 54 | if (role == "Administrator") |
| 55 | { |
Ed Tanous | 20fa6a2 | 2024-05-20 18:02:58 -0700 | [diff] [blame] | 56 | privArray.emplace_back("Login"); |
| 57 | privArray.emplace_back("ConfigureManager"); |
| 58 | privArray.emplace_back("ConfigureUsers"); |
| 59 | privArray.emplace_back("ConfigureSelf"); |
| 60 | privArray.emplace_back("ConfigureComponents"); |
AppaRao Puli | 8fcb65b | 2018-12-27 14:11:55 +0530 | [diff] [blame] | 61 | } |
| 62 | else if (role == "Operator") |
| 63 | { |
Ed Tanous | 20fa6a2 | 2024-05-20 18:02:58 -0700 | [diff] [blame] | 64 | privArray.emplace_back("Login"); |
| 65 | privArray.emplace_back("ConfigureSelf"); |
| 66 | privArray.emplace_back("ConfigureComponents"); |
AppaRao Puli | 8fcb65b | 2018-12-27 14:11:55 +0530 | [diff] [blame] | 67 | } |
AppaRao Puli | c80fee5 | 2019-10-16 14:49:36 +0530 | [diff] [blame] | 68 | else if (role == "ReadOnly") |
AppaRao Puli | 8fcb65b | 2018-12-27 14:11:55 +0530 | [diff] [blame] | 69 | { |
Ed Tanous | 20fa6a2 | 2024-05-20 18:02:58 -0700 | [diff] [blame] | 70 | privArray.emplace_back("Login"); |
| 71 | privArray.emplace_back("ConfigureSelf"); |
AppaRao Puli | 8fcb65b | 2018-12-27 14:11:55 +0530 | [diff] [blame] | 72 | } |
AppaRao Puli | 8fcb65b | 2018-12-27 14:11:55 +0530 | [diff] [blame] | 73 | else |
| 74 | { |
Ed Tanous | 20fa6a2 | 2024-05-20 18:02:58 -0700 | [diff] [blame] | 75 | return std::nullopt; |
AppaRao Puli | 8fcb65b | 2018-12-27 14:11:55 +0530 | [diff] [blame] | 76 | } |
Ed Tanous | 20fa6a2 | 2024-05-20 18:02:58 -0700 | [diff] [blame] | 77 | return privArray; |
AppaRao Puli | 8fcb65b | 2018-12-27 14:11:55 +0530 | [diff] [blame] | 78 | } |
| 79 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 80 | inline void requestRoutesRoles(App& app) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 81 | { |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 82 | BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Roles/<str>/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 83 | .privileges(redfish::privileges::getRole) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 84 | .methods(boost::beast::http::verb::get)( |
Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 85 | [&app](const crow::Request& req, |
| 86 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 87 | const std::string& roleId) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 88 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| 89 | { |
| 90 | return; |
| 91 | } |
Ed Tanous | 20fa6a2 | 2024-05-20 18:02:58 -0700 | [diff] [blame] | 92 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 93 | std::optional<nlohmann::json::array_t> privArray = |
| 94 | getAssignedPrivFromRole(roleId); |
| 95 | if (!privArray) |
| 96 | { |
| 97 | messages::resourceNotFound(asyncResp->res, "Role", roleId); |
Lewanczyk, Dawid | 4e49bd4 | 2018-01-25 11:30:19 +0100 | [diff] [blame] | 98 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 99 | return; |
| 100 | } |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 101 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 102 | asyncResp->res.jsonValue["@odata.type"] = "#Role.v1_2_2.Role"; |
| 103 | asyncResp->res.jsonValue["Name"] = "User Role"; |
| 104 | asyncResp->res.jsonValue["Description"] = roleId + " User Role"; |
| 105 | asyncResp->res.jsonValue["OemPrivileges"] = |
| 106 | nlohmann::json::array(); |
| 107 | asyncResp->res.jsonValue["IsPredefined"] = true; |
| 108 | asyncResp->res.jsonValue["Id"] = roleId; |
| 109 | asyncResp->res.jsonValue["RoleId"] = roleId; |
| 110 | asyncResp->res.jsonValue["@odata.id"] = boost::urls::format( |
| 111 | "/redfish/v1/AccountService/Roles/{}", roleId); |
| 112 | asyncResp->res.jsonValue["AssignedPrivileges"] = |
| 113 | std::move(*privArray); |
| 114 | }); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 115 | } |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 116 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 117 | inline void requestRoutesRoleCollection(App& app) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 118 | { |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 119 | BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Roles/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 120 | .privileges(redfish::privileges::getRoleCollection) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 121 | .methods(boost::beast::http::verb::get)( |
Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 122 | [&app](const crow::Request& req, |
| 123 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 124 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 125 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 126 | return; |
Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 127 | } |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 128 | |
| 129 | asyncResp->res.jsonValue["@odata.id"] = |
| 130 | "/redfish/v1/AccountService/Roles"; |
| 131 | asyncResp->res.jsonValue["@odata.type"] = |
| 132 | "#RoleCollection.RoleCollection"; |
| 133 | asyncResp->res.jsonValue["Name"] = "Roles Collection"; |
| 134 | asyncResp->res.jsonValue["Description"] = "BMC User Roles"; |
| 135 | |
| 136 | sdbusplus::asio::getProperty<std::vector<std::string>>( |
| 137 | *crow::connections::systemBus, |
| 138 | "xyz.openbmc_project.User.Manager", |
| 139 | "/xyz/openbmc_project/user", |
| 140 | "xyz.openbmc_project.User.Manager", "AllPrivileges", |
| 141 | [asyncResp](const boost::system::error_code& ec, |
| 142 | const std::vector<std::string>& privList) { |
| 143 | if (ec) |
| 144 | { |
| 145 | messages::internalError(asyncResp->res); |
| 146 | return; |
| 147 | } |
| 148 | nlohmann::json& memberArray = |
| 149 | asyncResp->res.jsonValue["Members"]; |
| 150 | memberArray = nlohmann::json::array(); |
| 151 | for (const std::string& priv : privList) |
| 152 | { |
| 153 | std::string role = getRoleFromPrivileges(priv); |
| 154 | if (!role.empty()) |
| 155 | { |
| 156 | nlohmann::json::object_t member; |
| 157 | member["@odata.id"] = boost::urls::format( |
| 158 | "/redfish/v1/AccountService/Roles/{}", |
| 159 | role); |
| 160 | memberArray.emplace_back(std::move(member)); |
| 161 | } |
| 162 | } |
| 163 | asyncResp->res.jsonValue["Members@odata.count"] = |
| 164 | memberArray.size(); |
| 165 | }); |
| 166 | }); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 167 | } |
Lewanczyk, Dawid | 4e49bd4 | 2018-01-25 11:30:19 +0100 | [diff] [blame] | 168 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 169 | } // namespace redfish |