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