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 | } |
AppaRao Puli | 8fcb65b | 2018-12-27 14:11:55 +0530 | [diff] [blame] | 31 | else if (priv == "priv-user") |
| 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 | } |
| 35 | else if (priv == "priv-operator") |
| 36 | { |
| 37 | return "Operator"; |
| 38 | } |
| 39 | return ""; |
| 40 | } |
| 41 | |
| 42 | inline bool getAssignedPrivFromRole(std::string_view role, |
| 43 | nlohmann::json& privArray) |
| 44 | { |
| 45 | if (role == "Administrator") |
| 46 | { |
| 47 | privArray = {"Login", "ConfigureManager", "ConfigureUsers", |
| 48 | "ConfigureSelf", "ConfigureComponents"}; |
| 49 | } |
| 50 | else if (role == "Operator") |
| 51 | { |
| 52 | privArray = {"Login", "ConfigureSelf", "ConfigureComponents"}; |
| 53 | } |
AppaRao Puli | c80fee5 | 2019-10-16 14:49:36 +0530 | [diff] [blame] | 54 | else if (role == "ReadOnly") |
AppaRao Puli | 8fcb65b | 2018-12-27 14:11:55 +0530 | [diff] [blame] | 55 | { |
| 56 | privArray = {"Login", "ConfigureSelf"}; |
| 57 | } |
AppaRao Puli | 8fcb65b | 2018-12-27 14:11:55 +0530 | [diff] [blame] | 58 | else |
| 59 | { |
| 60 | return false; |
| 61 | } |
| 62 | return true; |
| 63 | } |
| 64 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 65 | class Roles : public Node |
| 66 | { |
| 67 | public: |
| 68 | Roles(CrowApp& app) : |
AppaRao Puli | 8fcb65b | 2018-12-27 14:11:55 +0530 | [diff] [blame] | 69 | Node(app, "/redfish/v1/AccountService/Roles/<str>/", std::string()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 70 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 71 | entityPrivileges = { |
| 72 | {boost::beast::http::verb::get, {{"Login"}}}, |
| 73 | {boost::beast::http::verb::head, {{"Login"}}}, |
| 74 | {boost::beast::http::verb::patch, {{"ConfigureManager"}}}, |
| 75 | {boost::beast::http::verb::put, {{"ConfigureManager"}}}, |
| 76 | {boost::beast::http::verb::delete_, {{"ConfigureManager"}}}, |
| 77 | {boost::beast::http::verb::post, {{"ConfigureManager"}}}}; |
| 78 | } |
Lewanczyk, Dawid | 4e49bd4 | 2018-01-25 11:30:19 +0100 | [diff] [blame] | 79 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 80 | private: |
| 81 | void doGet(crow::Response& res, const crow::Request& req, |
| 82 | const std::vector<std::string>& params) override |
| 83 | { |
AppaRao Puli | 8fcb65b | 2018-12-27 14:11:55 +0530 | [diff] [blame] | 84 | if (params.size() != 1) |
| 85 | { |
| 86 | messages::internalError(res); |
| 87 | res.end(); |
| 88 | return; |
| 89 | } |
| 90 | const std::string& roleId = params[0]; |
| 91 | nlohmann::json privArray = nlohmann::json::array(); |
| 92 | if (false == getAssignedPrivFromRole(roleId, privArray)) |
| 93 | { |
| 94 | messages::resourceNotFound(res, "Role", roleId); |
| 95 | res.end(); |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | res.jsonValue = { |
Zbigniew Kurzynski | ec8abe6 | 2019-05-31 16:59:52 +0200 | [diff] [blame] | 100 | {"@odata.type", "#Role.v1_2_2.Role"}, |
AppaRao Puli | 8fcb65b | 2018-12-27 14:11:55 +0530 | [diff] [blame] | 101 | {"@odata.context", "/redfish/v1/$metadata#Role.Role"}, |
| 102 | {"Name", "User Role"}, |
Ed Tanous | 0f26153 | 2019-02-08 11:13:29 -0800 | [diff] [blame] | 103 | {"Description", roleId + " User Role"}, |
AppaRao Puli | 8fcb65b | 2018-12-27 14:11:55 +0530 | [diff] [blame] | 104 | {"OemPrivileges", nlohmann::json::array()}, |
| 105 | {"IsPredefined", true}, |
| 106 | {"Id", roleId}, |
Zbigniew Kurzynski | ec8abe6 | 2019-05-31 16:59:52 +0200 | [diff] [blame] | 107 | {"RoleId", roleId}, |
AppaRao Puli | 8fcb65b | 2018-12-27 14:11:55 +0530 | [diff] [blame] | 108 | {"@odata.id", "/redfish/v1/AccountService/Roles/" + roleId}, |
| 109 | {"AssignedPrivileges", std::move(privArray)}}; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 110 | res.end(); |
| 111 | } |
Lewanczyk, Dawid | 4e49bd4 | 2018-01-25 11:30:19 +0100 | [diff] [blame] | 112 | }; |
| 113 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 114 | class RoleCollection : public Node |
| 115 | { |
| 116 | public: |
| 117 | RoleCollection(CrowApp& app) : |
| 118 | Node(app, "/redfish/v1/AccountService/Roles/") |
| 119 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 120 | entityPrivileges = { |
| 121 | {boost::beast::http::verb::get, {{"Login"}}}, |
| 122 | {boost::beast::http::verb::head, {{"Login"}}}, |
| 123 | {boost::beast::http::verb::patch, {{"ConfigureManager"}}}, |
| 124 | {boost::beast::http::verb::put, {{"ConfigureManager"}}}, |
| 125 | {boost::beast::http::verb::delete_, {{"ConfigureManager"}}}, |
| 126 | {boost::beast::http::verb::post, {{"ConfigureManager"}}}}; |
| 127 | } |
Lewanczyk, Dawid | 4e49bd4 | 2018-01-25 11:30:19 +0100 | [diff] [blame] | 128 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 129 | private: |
| 130 | void doGet(crow::Response& res, const crow::Request& req, |
| 131 | const std::vector<std::string>& params) override |
| 132 | { |
AppaRao Puli | 8fcb65b | 2018-12-27 14:11:55 +0530 | [diff] [blame] | 133 | auto asyncResp = std::make_shared<AsyncResp>(res); |
| 134 | res.jsonValue = {{"@odata.context", |
| 135 | "/redfish/v1/" |
| 136 | "$metadata#RoleCollection.RoleCollection"}, |
| 137 | {"@odata.id", "/redfish/v1/AccountService/Roles"}, |
| 138 | {"@odata.type", "#RoleCollection.RoleCollection"}, |
| 139 | {"Name", "Roles Collection"}, |
| 140 | {"Description", "BMC User Roles"}}; |
| 141 | |
| 142 | crow::connections::systemBus->async_method_call( |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 143 | [asyncResp](const boost::system::error_code ec, |
| 144 | const std::variant<std::vector<std::string>>& resp) { |
AppaRao Puli | 8fcb65b | 2018-12-27 14:11:55 +0530 | [diff] [blame] | 145 | if (ec) |
| 146 | { |
| 147 | messages::internalError(asyncResp->res); |
| 148 | return; |
| 149 | } |
| 150 | nlohmann::json& memberArray = |
| 151 | asyncResp->res.jsonValue["Members"]; |
| 152 | memberArray = nlohmann::json::array(); |
| 153 | const std::vector<std::string>* privList = |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 154 | std::get_if<std::vector<std::string>>(&resp); |
Ed Tanous | 66664f2 | 2019-10-11 13:05:49 -0700 | [diff] [blame] | 155 | if (privList == nullptr) |
| 156 | { |
| 157 | messages::internalError(asyncResp->res); |
| 158 | return; |
| 159 | } |
AppaRao Puli | 8fcb65b | 2018-12-27 14:11:55 +0530 | [diff] [blame] | 160 | for (const std::string& priv : *privList) |
| 161 | { |
| 162 | std::string role = getRoleFromPrivileges(priv); |
| 163 | if (!role.empty()) |
| 164 | { |
| 165 | memberArray.push_back( |
| 166 | {{"@odata.id", |
| 167 | "/redfish/v1/AccountService/Roles/" + role}}); |
| 168 | } |
| 169 | } |
| 170 | asyncResp->res.jsonValue["Members@odata.count"] = |
| 171 | memberArray.size(); |
| 172 | }, |
| 173 | "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user", |
| 174 | "org.freedesktop.DBus.Properties", "Get", |
| 175 | "xyz.openbmc_project.User.Manager", "AllPrivileges"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 176 | } |
Lewanczyk, Dawid | 4e49bd4 | 2018-01-25 11:30:19 +0100 | [diff] [blame] | 177 | }; |
| 178 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 179 | } // namespace redfish |