xref: /openbmc/bmcweb/features/redfish/lib/roles.hpp (revision 002d39b4a7a5ed7166e2acad84e0943c3def9492)
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 
187e860f15SJohn Edward Broadbent #include <app.hpp>
19168e20c1SEd Tanous #include <dbus_utility.hpp>
2045ca1b86SEd Tanous #include <query.hpp>
21ed398213SEd Tanous #include <registries/privilege_registry.hpp>
221e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
234e49bd4bSLewanczyk, Dawid 
24abf2add6SEd Tanous #include <variant>
251abe55efSEd Tanous namespace redfish
261abe55efSEd Tanous {
274e49bd4bSLewanczyk, Dawid 
288fcb65b6SAppaRao Puli inline std::string getRoleFromPrivileges(std::string_view priv)
298fcb65b6SAppaRao Puli {
308fcb65b6SAppaRao Puli     if (priv == "priv-admin")
318fcb65b6SAppaRao Puli     {
328fcb65b6SAppaRao Puli         return "Administrator";
338fcb65b6SAppaRao Puli     }
343174e4dfSEd Tanous     if (priv == "priv-user")
358fcb65b6SAppaRao Puli     {
36c80fee55SAppaRao Puli         return "ReadOnly";
378fcb65b6SAppaRao Puli     }
383174e4dfSEd Tanous     if (priv == "priv-operator")
398fcb65b6SAppaRao Puli     {
408fcb65b6SAppaRao Puli         return "Operator";
418fcb65b6SAppaRao Puli     }
423174e4dfSEd Tanous     if (priv == "priv-noaccess")
43e9e6d240Sjayaprakash Mutyala     {
44e9e6d240Sjayaprakash Mutyala         return "NoAccess";
45e9e6d240Sjayaprakash Mutyala     }
468fcb65b6SAppaRao Puli     return "";
478fcb65b6SAppaRao Puli }
488fcb65b6SAppaRao Puli 
498fcb65b6SAppaRao Puli inline bool getAssignedPrivFromRole(std::string_view role,
508fcb65b6SAppaRao Puli                                     nlohmann::json& privArray)
518fcb65b6SAppaRao Puli {
528fcb65b6SAppaRao Puli     if (role == "Administrator")
538fcb65b6SAppaRao Puli     {
548fcb65b6SAppaRao Puli         privArray = {"Login", "ConfigureManager", "ConfigureUsers",
558fcb65b6SAppaRao Puli                      "ConfigureSelf", "ConfigureComponents"};
568fcb65b6SAppaRao Puli     }
578fcb65b6SAppaRao Puli     else if (role == "Operator")
588fcb65b6SAppaRao Puli     {
598fcb65b6SAppaRao Puli         privArray = {"Login", "ConfigureSelf", "ConfigureComponents"};
608fcb65b6SAppaRao Puli     }
61c80fee55SAppaRao Puli     else if (role == "ReadOnly")
628fcb65b6SAppaRao Puli     {
638fcb65b6SAppaRao Puli         privArray = {"Login", "ConfigureSelf"};
648fcb65b6SAppaRao Puli     }
65e9e6d240Sjayaprakash Mutyala     else if (role == "NoAccess")
66e9e6d240Sjayaprakash Mutyala     {
67e9e6d240Sjayaprakash Mutyala         privArray = nlohmann::json::array();
68e9e6d240Sjayaprakash Mutyala     }
698fcb65b6SAppaRao Puli     else
708fcb65b6SAppaRao Puli     {
718fcb65b6SAppaRao Puli         return false;
728fcb65b6SAppaRao Puli     }
738fcb65b6SAppaRao Puli     return true;
748fcb65b6SAppaRao Puli }
758fcb65b6SAppaRao Puli 
767e860f15SJohn Edward Broadbent inline void requestRoutesRoles(App& app)
771abe55efSEd Tanous {
787e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Roles/<str>/")
79ed398213SEd Tanous         .privileges(redfish::privileges::getRole)
807e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
8145ca1b86SEd Tanous             [&app](const crow::Request& req,
827e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
837e860f15SJohn Edward Broadbent                    const std::string& roleId) {
8445ca1b86SEd Tanous         if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
8545ca1b86SEd Tanous         {
8645ca1b86SEd Tanous             return;
8745ca1b86SEd Tanous         }
888fcb65b6SAppaRao Puli         nlohmann::json privArray = nlohmann::json::array();
89e05aec50SEd Tanous         if (!getAssignedPrivFromRole(roleId, privArray))
908fcb65b6SAppaRao Puli         {
918d1b46d7Szhanghch05             messages::resourceNotFound(asyncResp->res, "Role", roleId);
928d1b46d7Szhanghch05 
938fcb65b6SAppaRao Puli             return;
948fcb65b6SAppaRao Puli         }
958fcb65b6SAppaRao Puli 
961476687dSEd Tanous         asyncResp->res.jsonValue["@odata.type"] = "#Role.v1_2_2.Role";
971476687dSEd Tanous         asyncResp->res.jsonValue["Name"] = "User Role";
981476687dSEd Tanous         asyncResp->res.jsonValue["Description"] = roleId + " User Role";
99*002d39b4SEd Tanous         asyncResp->res.jsonValue["OemPrivileges"] = nlohmann::json::array();
1001476687dSEd Tanous         asyncResp->res.jsonValue["IsPredefined"] = true;
1011476687dSEd Tanous         asyncResp->res.jsonValue["Id"] = roleId;
1021476687dSEd Tanous         asyncResp->res.jsonValue["RoleId"] = roleId;
1031476687dSEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
1041476687dSEd Tanous             "/redfish/v1/AccountService/Roles/" + roleId;
105*002d39b4SEd Tanous         asyncResp->res.jsonValue["AssignedPrivileges"] = std::move(privArray);
1067e860f15SJohn Edward Broadbent         });
1074e49bd4bSLewanczyk, Dawid }
1084e49bd4bSLewanczyk, Dawid 
1097e860f15SJohn Edward Broadbent inline void requestRoutesRoleCollection(App& app)
1101abe55efSEd Tanous {
1117e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Roles/")
112ed398213SEd Tanous         .privileges(redfish::privileges::getRoleCollection)
1137e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
11445ca1b86SEd Tanous             [&app](const crow::Request& req,
1157e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
11645ca1b86SEd Tanous         if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
11745ca1b86SEd Tanous         {
11845ca1b86SEd Tanous             return;
11945ca1b86SEd Tanous         }
1201476687dSEd Tanous 
1211476687dSEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
1221476687dSEd Tanous             "/redfish/v1/AccountService/Roles";
1231476687dSEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
1241476687dSEd Tanous             "#RoleCollection.RoleCollection";
1251476687dSEd Tanous         asyncResp->res.jsonValue["Name"] = "Roles Collection";
1261476687dSEd Tanous         asyncResp->res.jsonValue["Description"] = "BMC User Roles";
1278fcb65b6SAppaRao Puli 
1281e1e598dSJonathan Doman         sdbusplus::asio::getProperty<std::vector<std::string>>(
129*002d39b4SEd Tanous             *crow::connections::systemBus, "xyz.openbmc_project.User.Manager",
130*002d39b4SEd Tanous             "/xyz/openbmc_project/user", "xyz.openbmc_project.User.Manager",
131*002d39b4SEd Tanous             "AllPrivileges",
132168e20c1SEd Tanous             [asyncResp](const boost::system::error_code ec,
1331e1e598dSJonathan Doman                         const std::vector<std::string>& privList) {
1348fcb65b6SAppaRao Puli             if (ec)
1358fcb65b6SAppaRao Puli             {
1368fcb65b6SAppaRao Puli                 messages::internalError(asyncResp->res);
1378fcb65b6SAppaRao Puli                 return;
1388fcb65b6SAppaRao Puli             }
139*002d39b4SEd Tanous             nlohmann::json& memberArray = asyncResp->res.jsonValue["Members"];
1408fcb65b6SAppaRao Puli             memberArray = nlohmann::json::array();
1411e1e598dSJonathan Doman             for (const std::string& priv : privList)
1428fcb65b6SAppaRao Puli             {
1438fcb65b6SAppaRao Puli                 std::string role = getRoleFromPrivileges(priv);
1448fcb65b6SAppaRao Puli                 if (!role.empty())
1458fcb65b6SAppaRao Puli                 {
1461476687dSEd Tanous                     nlohmann::json::object_t member;
1471476687dSEd Tanous                     member["@odata.id"] =
1481476687dSEd Tanous                         "/redfish/v1/AccountService/Roles/" + role;
1491476687dSEd Tanous                     memberArray.push_back(std::move(member));
1508fcb65b6SAppaRao Puli                 }
1518fcb65b6SAppaRao Puli             }
1528fcb65b6SAppaRao Puli             asyncResp->res.jsonValue["Members@odata.count"] =
1538fcb65b6SAppaRao Puli                 memberArray.size();
1541e1e598dSJonathan Doman             });
1557e860f15SJohn Edward Broadbent         });
1564e49bd4bSLewanczyk, Dawid }
1574e49bd4bSLewanczyk, Dawid 
1584e49bd4bSLewanczyk, Dawid } // namespace redfish
159