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