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