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> |
Jonathan Doman | 1e1e598 | 2021-06-11 09:36:17 -0700 | [diff] [blame] | 24 | #include <sdbusplus/asio/property.hpp> |
Lewanczyk, Dawid | 4e49bd4 | 2018-01-25 11:30:19 +0100 | [diff] [blame] | 25 | |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 26 | #include <variant> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 27 | namespace redfish |
| 28 | { |
Lewanczyk, Dawid | 4e49bd4 | 2018-01-25 11:30:19 +0100 | [diff] [blame] | 29 | |
AppaRao Puli | 8fcb65b | 2018-12-27 14:11:55 +0530 | [diff] [blame] | 30 | inline std::string getRoleFromPrivileges(std::string_view priv) |
| 31 | { |
| 32 | if (priv == "priv-admin") |
| 33 | { |
| 34 | return "Administrator"; |
| 35 | } |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 36 | if (priv == "priv-user") |
AppaRao Puli | 8fcb65b | 2018-12-27 14:11:55 +0530 | [diff] [blame] | 37 | { |
AppaRao Puli | c80fee5 | 2019-10-16 14:49:36 +0530 | [diff] [blame] | 38 | return "ReadOnly"; |
AppaRao Puli | 8fcb65b | 2018-12-27 14:11:55 +0530 | [diff] [blame] | 39 | } |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 40 | if (priv == "priv-operator") |
AppaRao Puli | 8fcb65b | 2018-12-27 14:11:55 +0530 | [diff] [blame] | 41 | { |
| 42 | return "Operator"; |
| 43 | } |
| 44 | return ""; |
| 45 | } |
| 46 | |
| 47 | inline bool getAssignedPrivFromRole(std::string_view role, |
| 48 | nlohmann::json& privArray) |
| 49 | { |
| 50 | if (role == "Administrator") |
| 51 | { |
| 52 | privArray = {"Login", "ConfigureManager", "ConfigureUsers", |
| 53 | "ConfigureSelf", "ConfigureComponents"}; |
| 54 | } |
| 55 | else if (role == "Operator") |
| 56 | { |
| 57 | privArray = {"Login", "ConfigureSelf", "ConfigureComponents"}; |
| 58 | } |
AppaRao Puli | c80fee5 | 2019-10-16 14:49:36 +0530 | [diff] [blame] | 59 | else if (role == "ReadOnly") |
AppaRao Puli | 8fcb65b | 2018-12-27 14:11:55 +0530 | [diff] [blame] | 60 | { |
| 61 | privArray = {"Login", "ConfigureSelf"}; |
| 62 | } |
AppaRao Puli | 8fcb65b | 2018-12-27 14:11:55 +0530 | [diff] [blame] | 63 | else |
| 64 | { |
| 65 | return false; |
| 66 | } |
| 67 | return true; |
| 68 | } |
| 69 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 70 | inline void requestRoutesRoles(App& app) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 71 | { |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 72 | BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Roles/<str>/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 73 | .privileges(redfish::privileges::getRole) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 74 | .methods(boost::beast::http::verb::get)( |
Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 75 | [&app](const crow::Request& req, |
| 76 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 77 | const std::string& roleId) { |
Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 78 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 79 | { |
| 80 | return; |
| 81 | } |
| 82 | nlohmann::json privArray = nlohmann::json::array(); |
| 83 | if (!getAssignedPrivFromRole(roleId, privArray)) |
| 84 | { |
| 85 | messages::resourceNotFound(asyncResp->res, "Role", roleId); |
Lewanczyk, Dawid | 4e49bd4 | 2018-01-25 11:30:19 +0100 | [diff] [blame] | 86 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 87 | return; |
| 88 | } |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 89 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 90 | asyncResp->res.jsonValue["@odata.type"] = "#Role.v1_2_2.Role"; |
| 91 | asyncResp->res.jsonValue["Name"] = "User Role"; |
| 92 | asyncResp->res.jsonValue["Description"] = roleId + " User Role"; |
| 93 | asyncResp->res.jsonValue["OemPrivileges"] = nlohmann::json::array(); |
| 94 | asyncResp->res.jsonValue["IsPredefined"] = true; |
| 95 | asyncResp->res.jsonValue["Id"] = roleId; |
| 96 | asyncResp->res.jsonValue["RoleId"] = roleId; |
Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame] | 97 | asyncResp->res.jsonValue["@odata.id"] = |
| 98 | boost::urls::format("/redfish/v1/AccountService/Roles/{}", roleId); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 99 | asyncResp->res.jsonValue["AssignedPrivileges"] = std::move(privArray); |
| 100 | }); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 101 | } |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 102 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 103 | inline void requestRoutesRoleCollection(App& app) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 104 | { |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 105 | BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Roles/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 106 | .privileges(redfish::privileges::getRoleCollection) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 107 | .methods(boost::beast::http::verb::get)( |
Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 108 | [&app](const crow::Request& req, |
| 109 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { |
Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 110 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 111 | { |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | asyncResp->res.jsonValue["@odata.id"] = |
| 116 | "/redfish/v1/AccountService/Roles"; |
| 117 | asyncResp->res.jsonValue["@odata.type"] = |
| 118 | "#RoleCollection.RoleCollection"; |
| 119 | asyncResp->res.jsonValue["Name"] = "Roles Collection"; |
| 120 | asyncResp->res.jsonValue["Description"] = "BMC User Roles"; |
| 121 | |
| 122 | sdbusplus::asio::getProperty<std::vector<std::string>>( |
| 123 | *crow::connections::systemBus, "xyz.openbmc_project.User.Manager", |
| 124 | "/xyz/openbmc_project/user", "xyz.openbmc_project.User.Manager", |
| 125 | "AllPrivileges", |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 126 | [asyncResp](const boost::system::error_code& ec, |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 127 | const std::vector<std::string>& privList) { |
| 128 | if (ec) |
| 129 | { |
| 130 | messages::internalError(asyncResp->res); |
| 131 | return; |
| 132 | } |
| 133 | nlohmann::json& memberArray = asyncResp->res.jsonValue["Members"]; |
| 134 | memberArray = nlohmann::json::array(); |
| 135 | for (const std::string& priv : privList) |
| 136 | { |
| 137 | std::string role = getRoleFromPrivileges(priv); |
| 138 | if (!role.empty()) |
Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 139 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 140 | nlohmann::json::object_t member; |
Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame] | 141 | member["@odata.id"] = boost::urls::format( |
| 142 | "/redfish/v1/AccountService/Roles/{}", role); |
Patrick Williams | b2ba307 | 2023-05-12 10:27:39 -0500 | [diff] [blame] | 143 | memberArray.emplace_back(std::move(member)); |
Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 144 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 145 | } |
| 146 | asyncResp->res.jsonValue["Members@odata.count"] = |
| 147 | memberArray.size(); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 148 | }); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 149 | }); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 150 | } |
Lewanczyk, Dawid | 4e49bd4 | 2018-01-25 11:30:19 +0100 | [diff] [blame] | 151 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 152 | } // namespace redfish |