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 | |
Lewanczyk, Dawid | 4e49bd4 | 2018-01-25 11:30:19 +0100 | [diff] [blame] | 18 | #include "node.hpp" |
| 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 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 73 | class Roles : public Node |
| 74 | { |
| 75 | public: |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 76 | Roles(App& app) : |
AppaRao Puli | 8fcb65b | 2018-12-27 14:11:55 +0530 | [diff] [blame] | 77 | Node(app, "/redfish/v1/AccountService/Roles/<str>/", std::string()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 78 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 79 | entityPrivileges = { |
| 80 | {boost::beast::http::verb::get, {{"Login"}}}, |
| 81 | {boost::beast::http::verb::head, {{"Login"}}}, |
| 82 | {boost::beast::http::verb::patch, {{"ConfigureManager"}}}, |
| 83 | {boost::beast::http::verb::put, {{"ConfigureManager"}}}, |
| 84 | {boost::beast::http::verb::delete_, {{"ConfigureManager"}}}, |
| 85 | {boost::beast::http::verb::post, {{"ConfigureManager"}}}}; |
| 86 | } |
Lewanczyk, Dawid | 4e49bd4 | 2018-01-25 11:30:19 +0100 | [diff] [blame] | 87 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 88 | private: |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame^] | 89 | void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 90 | const crow::Request&, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 91 | const std::vector<std::string>& params) override |
| 92 | { |
AppaRao Puli | 8fcb65b | 2018-12-27 14:11:55 +0530 | [diff] [blame] | 93 | if (params.size() != 1) |
| 94 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame^] | 95 | messages::internalError(asyncResp->res); |
| 96 | |
AppaRao Puli | 8fcb65b | 2018-12-27 14:11:55 +0530 | [diff] [blame] | 97 | return; |
| 98 | } |
| 99 | const std::string& roleId = params[0]; |
| 100 | nlohmann::json privArray = nlohmann::json::array(); |
| 101 | if (false == getAssignedPrivFromRole(roleId, privArray)) |
| 102 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame^] | 103 | messages::resourceNotFound(asyncResp->res, "Role", roleId); |
| 104 | |
AppaRao Puli | 8fcb65b | 2018-12-27 14:11:55 +0530 | [diff] [blame] | 105 | return; |
| 106 | } |
| 107 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame^] | 108 | asyncResp->res.jsonValue = { |
Zbigniew Kurzynski | ec8abe6 | 2019-05-31 16:59:52 +0200 | [diff] [blame] | 109 | {"@odata.type", "#Role.v1_2_2.Role"}, |
AppaRao Puli | 8fcb65b | 2018-12-27 14:11:55 +0530 | [diff] [blame] | 110 | {"Name", "User Role"}, |
Ed Tanous | 0f26153 | 2019-02-08 11:13:29 -0800 | [diff] [blame] | 111 | {"Description", roleId + " User Role"}, |
AppaRao Puli | 8fcb65b | 2018-12-27 14:11:55 +0530 | [diff] [blame] | 112 | {"OemPrivileges", nlohmann::json::array()}, |
| 113 | {"IsPredefined", true}, |
| 114 | {"Id", roleId}, |
Zbigniew Kurzynski | ec8abe6 | 2019-05-31 16:59:52 +0200 | [diff] [blame] | 115 | {"RoleId", roleId}, |
AppaRao Puli | 8fcb65b | 2018-12-27 14:11:55 +0530 | [diff] [blame] | 116 | {"@odata.id", "/redfish/v1/AccountService/Roles/" + roleId}, |
| 117 | {"AssignedPrivileges", std::move(privArray)}}; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 118 | } |
Lewanczyk, Dawid | 4e49bd4 | 2018-01-25 11:30:19 +0100 | [diff] [blame] | 119 | }; |
| 120 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 121 | class RoleCollection : public Node |
| 122 | { |
| 123 | public: |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 124 | RoleCollection(App& app) : Node(app, "/redfish/v1/AccountService/Roles/") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 125 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 126 | entityPrivileges = { |
| 127 | {boost::beast::http::verb::get, {{"Login"}}}, |
| 128 | {boost::beast::http::verb::head, {{"Login"}}}, |
| 129 | {boost::beast::http::verb::patch, {{"ConfigureManager"}}}, |
| 130 | {boost::beast::http::verb::put, {{"ConfigureManager"}}}, |
| 131 | {boost::beast::http::verb::delete_, {{"ConfigureManager"}}}, |
| 132 | {boost::beast::http::verb::post, {{"ConfigureManager"}}}}; |
| 133 | } |
Lewanczyk, Dawid | 4e49bd4 | 2018-01-25 11:30:19 +0100 | [diff] [blame] | 134 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 135 | private: |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame^] | 136 | void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 137 | const crow::Request&, const std::vector<std::string>&) override |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 138 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame^] | 139 | |
| 140 | asyncResp->res.jsonValue = { |
| 141 | {"@odata.id", "/redfish/v1/AccountService/Roles"}, |
| 142 | {"@odata.type", "#RoleCollection.RoleCollection"}, |
| 143 | {"Name", "Roles Collection"}, |
| 144 | {"Description", "BMC User Roles"}}; |
AppaRao Puli | 8fcb65b | 2018-12-27 14:11:55 +0530 | [diff] [blame] | 145 | |
| 146 | crow::connections::systemBus->async_method_call( |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 147 | [asyncResp](const boost::system::error_code ec, |
| 148 | const std::variant<std::vector<std::string>>& resp) { |
AppaRao Puli | 8fcb65b | 2018-12-27 14:11:55 +0530 | [diff] [blame] | 149 | if (ec) |
| 150 | { |
| 151 | messages::internalError(asyncResp->res); |
| 152 | return; |
| 153 | } |
| 154 | nlohmann::json& memberArray = |
| 155 | asyncResp->res.jsonValue["Members"]; |
| 156 | memberArray = nlohmann::json::array(); |
| 157 | const std::vector<std::string>* privList = |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 158 | std::get_if<std::vector<std::string>>(&resp); |
Ed Tanous | 66664f2 | 2019-10-11 13:05:49 -0700 | [diff] [blame] | 159 | if (privList == nullptr) |
| 160 | { |
| 161 | messages::internalError(asyncResp->res); |
| 162 | return; |
| 163 | } |
AppaRao Puli | 8fcb65b | 2018-12-27 14:11:55 +0530 | [diff] [blame] | 164 | for (const std::string& priv : *privList) |
| 165 | { |
| 166 | std::string role = getRoleFromPrivileges(priv); |
| 167 | if (!role.empty()) |
| 168 | { |
| 169 | memberArray.push_back( |
| 170 | {{"@odata.id", |
| 171 | "/redfish/v1/AccountService/Roles/" + role}}); |
| 172 | } |
| 173 | } |
| 174 | asyncResp->res.jsonValue["Members@odata.count"] = |
| 175 | memberArray.size(); |
| 176 | }, |
| 177 | "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user", |
| 178 | "org.freedesktop.DBus.Properties", "Get", |
| 179 | "xyz.openbmc_project.User.Manager", "AllPrivileges"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 180 | } |
Lewanczyk, Dawid | 4e49bd4 | 2018-01-25 11:30:19 +0100 | [diff] [blame] | 181 | }; |
| 182 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 183 | } // namespace redfish |