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