1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright OpenBMC Authors
// SPDX-FileCopyrightText: Copyright 2018 Intel Corporation
#pragma once
#include "app.hpp"
#include "async_resp.hpp"
#include "dbus_utility.hpp"
#include "error_messages.hpp"
#include "http_request.hpp"
#include "query.hpp"
#include "registries/privilege_registry.hpp"
#include <boost/beast/http/verb.hpp>
#include <boost/url/format.hpp>
#include <nlohmann/json.hpp>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
namespace redfish
{
inline std::string getRoleFromPrivileges(std::string_view priv)
{
if (priv == "priv-admin")
{
return "Administrator";
}
if (priv == "priv-user")
{
return "ReadOnly";
}
if (priv == "priv-operator")
{
return "Operator";
}
return "";
}
inline std::optional<nlohmann::json::array_t> getAssignedPrivFromRole(
std::string_view role)
{
nlohmann::json::array_t privArray;
if (role == "Administrator")
{
privArray.emplace_back("Login");
privArray.emplace_back("ConfigureManager");
privArray.emplace_back("ConfigureUsers");
privArray.emplace_back("ConfigureSelf");
privArray.emplace_back("ConfigureComponents");
}
else if (role == "Operator")
{
privArray.emplace_back("Login");
privArray.emplace_back("ConfigureSelf");
privArray.emplace_back("ConfigureComponents");
}
else if (role == "ReadOnly")
{
privArray.emplace_back("Login");
privArray.emplace_back("ConfigureSelf");
}
else
{
return std::nullopt;
}
return privArray;
}
inline void requestRoutesRoles(App& app)
{
BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Roles/<str>/")
.privileges(redfish::privileges::getRole)
.methods(boost::beast::http::verb::get)(
[&app](const crow::Request& req,
const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
const std::string& roleId) {
if (!redfish::setUpRedfishRoute(app, req, asyncResp))
{
return;
}
std::optional<nlohmann::json::array_t> privArray =
getAssignedPrivFromRole(roleId);
if (!privArray)
{
messages::resourceNotFound(asyncResp->res, "Role", roleId);
return;
}
asyncResp->res.jsonValue["@odata.type"] = "#Role.v1_2_2.Role";
asyncResp->res.jsonValue["Name"] = "User Role";
asyncResp->res.jsonValue["Description"] = roleId + " User Role";
asyncResp->res.jsonValue["OemPrivileges"] =
nlohmann::json::array();
asyncResp->res.jsonValue["IsPredefined"] = true;
asyncResp->res.jsonValue["Id"] = roleId;
asyncResp->res.jsonValue["RoleId"] = roleId;
asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
"/redfish/v1/AccountService/Roles/{}", roleId);
asyncResp->res.jsonValue["AssignedPrivileges"] =
std::move(*privArray);
});
}
inline void requestRoutesRoleCollection(App& app)
{
BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Roles/")
.privileges(redfish::privileges::getRoleCollection)
.methods(boost::beast::http::verb::get)(
[&app](const crow::Request& req,
const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
if (!redfish::setUpRedfishRoute(app, req, asyncResp))
{
return;
}
asyncResp->res.jsonValue["@odata.id"] =
"/redfish/v1/AccountService/Roles";
asyncResp->res.jsonValue["@odata.type"] =
"#RoleCollection.RoleCollection";
asyncResp->res.jsonValue["Name"] = "Roles Collection";
asyncResp->res.jsonValue["Description"] = "BMC User Roles";
dbus::utility::getProperty<std::vector<std::string>>(
"xyz.openbmc_project.User.Manager",
"/xyz/openbmc_project/user",
"xyz.openbmc_project.User.Manager", "AllPrivileges",
[asyncResp](const boost::system::error_code& ec,
const std::vector<std::string>& privList) {
if (ec)
{
messages::internalError(asyncResp->res);
return;
}
nlohmann::json& memberArray =
asyncResp->res.jsonValue["Members"];
memberArray = nlohmann::json::array();
for (const std::string& priv : privList)
{
std::string role = getRoleFromPrivileges(priv);
if (!role.empty())
{
nlohmann::json::object_t member;
member["@odata.id"] = boost::urls::format(
"/redfish/v1/AccountService/Roles/{}",
role);
memberArray.emplace_back(std::move(member));
}
}
asyncResp->res.jsonValue["Members@odata.count"] =
memberArray.size();
});
});
}
} // namespace redfish
|