blob: 784f716a7fd81b15f53fe7732b1f16ca990ee0d1 [file] [log] [blame]
Lewanczyk, Dawid4e49bd42018-01-25 11:30:19 +01001/*
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
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080018#include "app.hpp"
19#include "dbus_utility.hpp"
20#include "query.hpp"
21#include "registries/privilege_registry.hpp"
22
Ed Tanousef4c65b2023-04-24 15:28:50 -070023#include <boost/url/format.hpp>
Jonathan Doman1e1e5982021-06-11 09:36:17 -070024#include <sdbusplus/asio/property.hpp>
Lewanczyk, Dawid4e49bd42018-01-25 11:30:19 +010025
Ed Tanousabf2add2019-01-22 16:40:12 -080026#include <variant>
Ed Tanous1abe55e2018-09-05 08:30:59 -070027namespace redfish
28{
Lewanczyk, Dawid4e49bd42018-01-25 11:30:19 +010029
AppaRao Puli8fcb65b2018-12-27 14:11:55 +053030inline std::string getRoleFromPrivileges(std::string_view priv)
31{
32 if (priv == "priv-admin")
33 {
34 return "Administrator";
35 }
Ed Tanous3174e4d2020-10-07 11:41:22 -070036 if (priv == "priv-user")
AppaRao Puli8fcb65b2018-12-27 14:11:55 +053037 {
AppaRao Pulic80fee52019-10-16 14:49:36 +053038 return "ReadOnly";
AppaRao Puli8fcb65b2018-12-27 14:11:55 +053039 }
Ed Tanous3174e4d2020-10-07 11:41:22 -070040 if (priv == "priv-operator")
AppaRao Puli8fcb65b2018-12-27 14:11:55 +053041 {
42 return "Operator";
43 }
44 return "";
45}
46
47inline bool getAssignedPrivFromRole(std::string_view role,
48 nlohmann::json& privArray)
49{
50 if (role == "Administrator")
51 {
52 privArray = {"Login", "ConfigureManager", "ConfigureUsers",
53 "ConfigureSelf", "ConfigureComponents"};
54 }
55 else if (role == "Operator")
56 {
57 privArray = {"Login", "ConfigureSelf", "ConfigureComponents"};
58 }
AppaRao Pulic80fee52019-10-16 14:49:36 +053059 else if (role == "ReadOnly")
AppaRao Puli8fcb65b2018-12-27 14:11:55 +053060 {
61 privArray = {"Login", "ConfigureSelf"};
62 }
AppaRao Puli8fcb65b2018-12-27 14:11:55 +053063 else
64 {
65 return false;
66 }
67 return true;
68}
69
John Edward Broadbent7e860f12021-04-08 15:57:16 -070070inline void requestRoutesRoles(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -070071{
John Edward Broadbent7e860f12021-04-08 15:57:16 -070072 BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Roles/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -070073 .privileges(redfish::privileges::getRole)
John Edward Broadbent7e860f12021-04-08 15:57:16 -070074 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -070075 [&app](const crow::Request& req,
76 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
77 const std::string& roleId) {
Carson Labrado3ba00072022-06-06 19:40:56 +000078 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -070079 {
80 return;
81 }
82 nlohmann::json privArray = nlohmann::json::array();
83 if (!getAssignedPrivFromRole(roleId, privArray))
84 {
85 messages::resourceNotFound(asyncResp->res, "Role", roleId);
Lewanczyk, Dawid4e49bd42018-01-25 11:30:19 +010086
Ed Tanous002d39b2022-05-31 08:59:27 -070087 return;
88 }
zhanghch058d1b46d2021-04-01 11:18:24 +080089
Ed Tanous002d39b2022-05-31 08:59:27 -070090 asyncResp->res.jsonValue["@odata.type"] = "#Role.v1_2_2.Role";
91 asyncResp->res.jsonValue["Name"] = "User Role";
92 asyncResp->res.jsonValue["Description"] = roleId + " User Role";
93 asyncResp->res.jsonValue["OemPrivileges"] = nlohmann::json::array();
94 asyncResp->res.jsonValue["IsPredefined"] = true;
95 asyncResp->res.jsonValue["Id"] = roleId;
96 asyncResp->res.jsonValue["RoleId"] = roleId;
Ed Tanousef4c65b2023-04-24 15:28:50 -070097 asyncResp->res.jsonValue["@odata.id"] =
98 boost::urls::format("/redfish/v1/AccountService/Roles/{}", roleId);
Ed Tanous002d39b2022-05-31 08:59:27 -070099 asyncResp->res.jsonValue["AssignedPrivileges"] = std::move(privArray);
100 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700101}
zhanghch058d1b46d2021-04-01 11:18:24 +0800102
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700103inline void requestRoutesRoleCollection(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700104{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700105 BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Roles/")
Ed Tanoused398212021-06-09 17:05:54 -0700106 .privileges(redfish::privileges::getRoleCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700107 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700108 [&app](const crow::Request& req,
109 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000110 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700111 {
112 return;
113 }
114
115 asyncResp->res.jsonValue["@odata.id"] =
116 "/redfish/v1/AccountService/Roles";
117 asyncResp->res.jsonValue["@odata.type"] =
118 "#RoleCollection.RoleCollection";
119 asyncResp->res.jsonValue["Name"] = "Roles Collection";
120 asyncResp->res.jsonValue["Description"] = "BMC User Roles";
121
122 sdbusplus::asio::getProperty<std::vector<std::string>>(
123 *crow::connections::systemBus, "xyz.openbmc_project.User.Manager",
124 "/xyz/openbmc_project/user", "xyz.openbmc_project.User.Manager",
125 "AllPrivileges",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800126 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -0700127 const std::vector<std::string>& privList) {
128 if (ec)
129 {
130 messages::internalError(asyncResp->res);
131 return;
132 }
133 nlohmann::json& memberArray = asyncResp->res.jsonValue["Members"];
134 memberArray = nlohmann::json::array();
135 for (const std::string& priv : privList)
136 {
137 std::string role = getRoleFromPrivileges(priv);
138 if (!role.empty())
Ed Tanous45ca1b82022-03-25 13:07:27 -0700139 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700140 nlohmann::json::object_t member;
Ed Tanousef4c65b2023-04-24 15:28:50 -0700141 member["@odata.id"] = boost::urls::format(
142 "/redfish/v1/AccountService/Roles/{}", role);
Patrick Williamsb2ba3072023-05-12 10:27:39 -0500143 memberArray.emplace_back(std::move(member));
Ed Tanous45ca1b82022-03-25 13:07:27 -0700144 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700145 }
146 asyncResp->res.jsonValue["Members@odata.count"] =
147 memberArray.size();
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700148 });
Ed Tanous002d39b2022-05-31 08:59:27 -0700149 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700150}
Lewanczyk, Dawid4e49bd42018-01-25 11:30:19 +0100151
Ed Tanous1abe55e2018-09-05 08:30:59 -0700152} // namespace redfish