blob: c1a0c58b17848df6b9e8fbc04a33ad231ebc184e [file] [log] [blame]
Ed Tanous40e9b922024-09-10 13:50:16 -07001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright OpenBMC Authors
3// SPDX-FileCopyrightText: Copyright 2018 Intel Corporation
Lewanczyk, Dawid4e49bd42018-01-25 11:30:19 +01004#pragma once
5
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08006#include "app.hpp"
Ed Tanousd7857202025-01-28 15:32:26 -08007#include "async_resp.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08008#include "dbus_utility.hpp"
Ed Tanousd7857202025-01-28 15:32:26 -08009#include "error_messages.hpp"
10#include "http_request.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080011#include "query.hpp"
12#include "registries/privilege_registry.hpp"
13
Ed Tanousd7857202025-01-28 15:32:26 -080014#include <boost/beast/http/verb.hpp>
Ed Tanousef4c65b2023-04-24 15:28:50 -070015#include <boost/url/format.hpp>
Ed Tanous20fa6a22024-05-20 18:02:58 -070016#include <nlohmann/json.hpp>
Lewanczyk, Dawid4e49bd42018-01-25 11:30:19 +010017
Ed Tanousd7857202025-01-28 15:32:26 -080018#include <memory>
Ed Tanous20fa6a22024-05-20 18:02:58 -070019#include <optional>
Ed Tanousd7857202025-01-28 15:32:26 -080020#include <string>
Ed Tanous20fa6a22024-05-20 18:02:58 -070021#include <string_view>
Ed Tanousd7857202025-01-28 15:32:26 -080022#include <utility>
23#include <vector>
Ed Tanous1abe55e2018-09-05 08:30:59 -070024namespace redfish
25{
Lewanczyk, Dawid4e49bd42018-01-25 11:30:19 +010026
AppaRao Puli8fcb65b2018-12-27 14:11:55 +053027inline std::string getRoleFromPrivileges(std::string_view priv)
28{
29 if (priv == "priv-admin")
30 {
31 return "Administrator";
32 }
Ed Tanous3174e4d2020-10-07 11:41:22 -070033 if (priv == "priv-user")
AppaRao Puli8fcb65b2018-12-27 14:11:55 +053034 {
AppaRao Pulic80fee52019-10-16 14:49:36 +053035 return "ReadOnly";
AppaRao Puli8fcb65b2018-12-27 14:11:55 +053036 }
Ed Tanous3174e4d2020-10-07 11:41:22 -070037 if (priv == "priv-operator")
AppaRao Puli8fcb65b2018-12-27 14:11:55 +053038 {
39 return "Operator";
40 }
41 return "";
42}
43
Patrick Williams504af5a2025-02-03 14:29:03 -050044inline std::optional<nlohmann::json::array_t> getAssignedPrivFromRole(
45 std::string_view role)
AppaRao Puli8fcb65b2018-12-27 14:11:55 +053046{
Ed Tanous20fa6a22024-05-20 18:02:58 -070047 nlohmann::json::array_t privArray;
AppaRao Puli8fcb65b2018-12-27 14:11:55 +053048 if (role == "Administrator")
49 {
Ed Tanous20fa6a22024-05-20 18:02:58 -070050 privArray.emplace_back("Login");
51 privArray.emplace_back("ConfigureManager");
52 privArray.emplace_back("ConfigureUsers");
53 privArray.emplace_back("ConfigureSelf");
54 privArray.emplace_back("ConfigureComponents");
AppaRao Puli8fcb65b2018-12-27 14:11:55 +053055 }
56 else if (role == "Operator")
57 {
Ed Tanous20fa6a22024-05-20 18:02:58 -070058 privArray.emplace_back("Login");
59 privArray.emplace_back("ConfigureSelf");
60 privArray.emplace_back("ConfigureComponents");
AppaRao Puli8fcb65b2018-12-27 14:11:55 +053061 }
AppaRao Pulic80fee52019-10-16 14:49:36 +053062 else if (role == "ReadOnly")
AppaRao Puli8fcb65b2018-12-27 14:11:55 +053063 {
Ed Tanous20fa6a22024-05-20 18:02:58 -070064 privArray.emplace_back("Login");
65 privArray.emplace_back("ConfigureSelf");
AppaRao Puli8fcb65b2018-12-27 14:11:55 +053066 }
AppaRao Puli8fcb65b2018-12-27 14:11:55 +053067 else
68 {
Ed Tanous20fa6a22024-05-20 18:02:58 -070069 return std::nullopt;
AppaRao Puli8fcb65b2018-12-27 14:11:55 +053070 }
Ed Tanous20fa6a22024-05-20 18:02:58 -070071 return privArray;
AppaRao Puli8fcb65b2018-12-27 14:11:55 +053072}
73
John Edward Broadbent7e860f12021-04-08 15:57:16 -070074inline void requestRoutesRoles(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -070075{
John Edward Broadbent7e860f12021-04-08 15:57:16 -070076 BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Roles/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -070077 .privileges(redfish::privileges::getRole)
John Edward Broadbent7e860f12021-04-08 15:57:16 -070078 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -070079 [&app](const crow::Request& req,
80 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
81 const std::string& roleId) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -040082 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
83 {
84 return;
85 }
Ed Tanous20fa6a22024-05-20 18:02:58 -070086
Patrick Williamsbd79bce2024-08-16 15:22:20 -040087 std::optional<nlohmann::json::array_t> privArray =
88 getAssignedPrivFromRole(roleId);
89 if (!privArray)
90 {
91 messages::resourceNotFound(asyncResp->res, "Role", roleId);
Lewanczyk, Dawid4e49bd42018-01-25 11:30:19 +010092
Patrick Williamsbd79bce2024-08-16 15:22:20 -040093 return;
94 }
zhanghch058d1b46d2021-04-01 11:18:24 +080095
Patrick Williamsbd79bce2024-08-16 15:22:20 -040096 asyncResp->res.jsonValue["@odata.type"] = "#Role.v1_2_2.Role";
97 asyncResp->res.jsonValue["Name"] = "User Role";
98 asyncResp->res.jsonValue["Description"] = roleId + " User Role";
99 asyncResp->res.jsonValue["OemPrivileges"] =
100 nlohmann::json::array();
101 asyncResp->res.jsonValue["IsPredefined"] = true;
102 asyncResp->res.jsonValue["Id"] = roleId;
103 asyncResp->res.jsonValue["RoleId"] = roleId;
104 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
105 "/redfish/v1/AccountService/Roles/{}", roleId);
106 asyncResp->res.jsonValue["AssignedPrivileges"] =
107 std::move(*privArray);
108 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700109}
zhanghch058d1b46d2021-04-01 11:18:24 +0800110
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700111inline void requestRoutesRoleCollection(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700112{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700113 BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Roles/")
Ed Tanoused398212021-06-09 17:05:54 -0700114 .privileges(redfish::privileges::getRoleCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700115 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700116 [&app](const crow::Request& req,
117 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400118 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous45ca1b82022-03-25 13:07:27 -0700119 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400120 return;
Ed Tanous45ca1b82022-03-25 13:07:27 -0700121 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400122
123 asyncResp->res.jsonValue["@odata.id"] =
124 "/redfish/v1/AccountService/Roles";
125 asyncResp->res.jsonValue["@odata.type"] =
126 "#RoleCollection.RoleCollection";
127 asyncResp->res.jsonValue["Name"] = "Roles Collection";
128 asyncResp->res.jsonValue["Description"] = "BMC User Roles";
129
Ed Tanousdeae6a72024-11-11 21:58:57 -0800130 dbus::utility::getProperty<std::vector<std::string>>(
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400131 "xyz.openbmc_project.User.Manager",
132 "/xyz/openbmc_project/user",
133 "xyz.openbmc_project.User.Manager", "AllPrivileges",
134 [asyncResp](const boost::system::error_code& ec,
135 const std::vector<std::string>& privList) {
136 if (ec)
137 {
138 messages::internalError(asyncResp->res);
139 return;
140 }
141 nlohmann::json& memberArray =
142 asyncResp->res.jsonValue["Members"];
143 memberArray = nlohmann::json::array();
144 for (const std::string& priv : privList)
145 {
146 std::string role = getRoleFromPrivileges(priv);
147 if (!role.empty())
148 {
149 nlohmann::json::object_t member;
150 member["@odata.id"] = boost::urls::format(
151 "/redfish/v1/AccountService/Roles/{}",
152 role);
153 memberArray.emplace_back(std::move(member));
154 }
155 }
156 asyncResp->res.jsonValue["Members@odata.count"] =
157 memberArray.size();
158 });
159 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700160}
Lewanczyk, Dawid4e49bd42018-01-25 11:30:19 +0100161
Ed Tanous1abe55e2018-09-05 08:30:59 -0700162} // namespace redfish