14e49bd4bSLewanczyk, Dawid /* 24e49bd4bSLewanczyk, Dawid // Copyright (c) 2018 Intel Corporation 34e49bd4bSLewanczyk, Dawid // 44e49bd4bSLewanczyk, Dawid // Licensed under the Apache License, Version 2.0 (the "License"); 54e49bd4bSLewanczyk, Dawid // you may not use this file except in compliance with the License. 64e49bd4bSLewanczyk, Dawid // You may obtain a copy of the License at 74e49bd4bSLewanczyk, Dawid // 84e49bd4bSLewanczyk, Dawid // http://www.apache.org/licenses/LICENSE-2.0 94e49bd4bSLewanczyk, Dawid // 104e49bd4bSLewanczyk, Dawid // Unless required by applicable law or agreed to in writing, software 114e49bd4bSLewanczyk, Dawid // distributed under the License is distributed on an "AS IS" BASIS, 124e49bd4bSLewanczyk, Dawid // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 134e49bd4bSLewanczyk, Dawid // See the License for the specific language governing permissions and 144e49bd4bSLewanczyk, Dawid // limitations under the License. 154e49bd4bSLewanczyk, Dawid */ 164e49bd4bSLewanczyk, Dawid #pragma once 174e49bd4bSLewanczyk, Dawid 183ccb3adbSEd Tanous #include "app.hpp" 193ccb3adbSEd Tanous #include "dbus_utility.hpp" 203ccb3adbSEd Tanous #include "query.hpp" 213ccb3adbSEd Tanous #include "registries/privilege_registry.hpp" 223ccb3adbSEd Tanous 23*ef4c65b7SEd Tanous #include <boost/url/format.hpp> 241e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp> 254e49bd4bSLewanczyk, Dawid 26abf2add6SEd Tanous #include <variant> 271abe55efSEd Tanous namespace redfish 281abe55efSEd Tanous { 294e49bd4bSLewanczyk, Dawid 308fcb65b6SAppaRao Puli inline std::string getRoleFromPrivileges(std::string_view priv) 318fcb65b6SAppaRao Puli { 328fcb65b6SAppaRao Puli if (priv == "priv-admin") 338fcb65b6SAppaRao Puli { 348fcb65b6SAppaRao Puli return "Administrator"; 358fcb65b6SAppaRao Puli } 363174e4dfSEd Tanous if (priv == "priv-user") 378fcb65b6SAppaRao Puli { 38c80fee55SAppaRao Puli return "ReadOnly"; 398fcb65b6SAppaRao Puli } 403174e4dfSEd Tanous if (priv == "priv-operator") 418fcb65b6SAppaRao Puli { 428fcb65b6SAppaRao Puli return "Operator"; 438fcb65b6SAppaRao Puli } 448fcb65b6SAppaRao Puli return ""; 458fcb65b6SAppaRao Puli } 468fcb65b6SAppaRao Puli 478fcb65b6SAppaRao Puli inline bool getAssignedPrivFromRole(std::string_view role, 488fcb65b6SAppaRao Puli nlohmann::json& privArray) 498fcb65b6SAppaRao Puli { 508fcb65b6SAppaRao Puli if (role == "Administrator") 518fcb65b6SAppaRao Puli { 528fcb65b6SAppaRao Puli privArray = {"Login", "ConfigureManager", "ConfigureUsers", 538fcb65b6SAppaRao Puli "ConfigureSelf", "ConfigureComponents"}; 548fcb65b6SAppaRao Puli } 558fcb65b6SAppaRao Puli else if (role == "Operator") 568fcb65b6SAppaRao Puli { 578fcb65b6SAppaRao Puli privArray = {"Login", "ConfigureSelf", "ConfigureComponents"}; 588fcb65b6SAppaRao Puli } 59c80fee55SAppaRao Puli else if (role == "ReadOnly") 608fcb65b6SAppaRao Puli { 618fcb65b6SAppaRao Puli privArray = {"Login", "ConfigureSelf"}; 628fcb65b6SAppaRao Puli } 638fcb65b6SAppaRao Puli else 648fcb65b6SAppaRao Puli { 658fcb65b6SAppaRao Puli return false; 668fcb65b6SAppaRao Puli } 678fcb65b6SAppaRao Puli return true; 688fcb65b6SAppaRao Puli } 698fcb65b6SAppaRao Puli 707e860f15SJohn Edward Broadbent inline void requestRoutesRoles(App& app) 711abe55efSEd Tanous { 727e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Roles/<str>/") 73ed398213SEd Tanous .privileges(redfish::privileges::getRole) 747e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::get)( 7545ca1b86SEd Tanous [&app](const crow::Request& req, 767e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 777e860f15SJohn Edward Broadbent const std::string& roleId) { 783ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 7945ca1b86SEd Tanous { 8045ca1b86SEd Tanous return; 8145ca1b86SEd Tanous } 828fcb65b6SAppaRao Puli nlohmann::json privArray = nlohmann::json::array(); 83e05aec50SEd Tanous if (!getAssignedPrivFromRole(roleId, privArray)) 848fcb65b6SAppaRao Puli { 858d1b46d7Szhanghch05 messages::resourceNotFound(asyncResp->res, "Role", roleId); 868d1b46d7Szhanghch05 878fcb65b6SAppaRao Puli return; 888fcb65b6SAppaRao Puli } 898fcb65b6SAppaRao Puli 901476687dSEd Tanous asyncResp->res.jsonValue["@odata.type"] = "#Role.v1_2_2.Role"; 911476687dSEd Tanous asyncResp->res.jsonValue["Name"] = "User Role"; 921476687dSEd Tanous asyncResp->res.jsonValue["Description"] = roleId + " User Role"; 93002d39b4SEd Tanous asyncResp->res.jsonValue["OemPrivileges"] = nlohmann::json::array(); 941476687dSEd Tanous asyncResp->res.jsonValue["IsPredefined"] = true; 951476687dSEd Tanous asyncResp->res.jsonValue["Id"] = roleId; 961476687dSEd Tanous asyncResp->res.jsonValue["RoleId"] = roleId; 97*ef4c65b7SEd Tanous asyncResp->res.jsonValue["@odata.id"] = 98*ef4c65b7SEd Tanous boost::urls::format("/redfish/v1/AccountService/Roles/{}", roleId); 99002d39b4SEd Tanous asyncResp->res.jsonValue["AssignedPrivileges"] = std::move(privArray); 1007e860f15SJohn Edward Broadbent }); 1014e49bd4bSLewanczyk, Dawid } 1024e49bd4bSLewanczyk, Dawid 1037e860f15SJohn Edward Broadbent inline void requestRoutesRoleCollection(App& app) 1041abe55efSEd Tanous { 1057e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Roles/") 106ed398213SEd Tanous .privileges(redfish::privileges::getRoleCollection) 1077e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::get)( 10845ca1b86SEd Tanous [&app](const crow::Request& req, 1097e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 1103ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 11145ca1b86SEd Tanous { 11245ca1b86SEd Tanous return; 11345ca1b86SEd Tanous } 1141476687dSEd Tanous 1151476687dSEd Tanous asyncResp->res.jsonValue["@odata.id"] = 1161476687dSEd Tanous "/redfish/v1/AccountService/Roles"; 1171476687dSEd Tanous asyncResp->res.jsonValue["@odata.type"] = 1181476687dSEd Tanous "#RoleCollection.RoleCollection"; 1191476687dSEd Tanous asyncResp->res.jsonValue["Name"] = "Roles Collection"; 1201476687dSEd Tanous asyncResp->res.jsonValue["Description"] = "BMC User Roles"; 1218fcb65b6SAppaRao Puli 1221e1e598dSJonathan Doman sdbusplus::asio::getProperty<std::vector<std::string>>( 123002d39b4SEd Tanous *crow::connections::systemBus, "xyz.openbmc_project.User.Manager", 124002d39b4SEd Tanous "/xyz/openbmc_project/user", "xyz.openbmc_project.User.Manager", 125002d39b4SEd Tanous "AllPrivileges", 1265e7e2dc5SEd Tanous [asyncResp](const boost::system::error_code& ec, 1271e1e598dSJonathan Doman const std::vector<std::string>& privList) { 1288fcb65b6SAppaRao Puli if (ec) 1298fcb65b6SAppaRao Puli { 1308fcb65b6SAppaRao Puli messages::internalError(asyncResp->res); 1318fcb65b6SAppaRao Puli return; 1328fcb65b6SAppaRao Puli } 133002d39b4SEd Tanous nlohmann::json& memberArray = asyncResp->res.jsonValue["Members"]; 1348fcb65b6SAppaRao Puli memberArray = nlohmann::json::array(); 1351e1e598dSJonathan Doman for (const std::string& priv : privList) 1368fcb65b6SAppaRao Puli { 1378fcb65b6SAppaRao Puli std::string role = getRoleFromPrivileges(priv); 1388fcb65b6SAppaRao Puli if (!role.empty()) 1398fcb65b6SAppaRao Puli { 1401476687dSEd Tanous nlohmann::json::object_t member; 141*ef4c65b7SEd Tanous member["@odata.id"] = boost::urls::format( 142*ef4c65b7SEd Tanous "/redfish/v1/AccountService/Roles/{}", role); 143b2ba3072SPatrick Williams memberArray.emplace_back(std::move(member)); 1448fcb65b6SAppaRao Puli } 1458fcb65b6SAppaRao Puli } 1468fcb65b6SAppaRao Puli asyncResp->res.jsonValue["Members@odata.count"] = 1478fcb65b6SAppaRao Puli memberArray.size(); 1481e1e598dSJonathan Doman }); 1497e860f15SJohn Edward Broadbent }); 1504e49bd4bSLewanczyk, Dawid } 1514e49bd4bSLewanczyk, Dawid 1524e49bd4bSLewanczyk, Dawid } // namespace redfish 153