blob: b375ff026c756f3098b3ec39e23f1b4196cd3393 [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
John Edward Broadbent7e860f12021-04-08 15:57:16 -070018#include <app.hpp>
Lewanczyk, Dawid4e49bd42018-01-25 11:30:19 +010019
Ed Tanousabf2add2019-01-22 16:40:12 -080020#include <variant>
21
Ed Tanous1abe55e2018-09-05 08:30:59 -070022namespace redfish
23{
Lewanczyk, Dawid4e49bd42018-01-25 11:30:19 +010024
AppaRao Puli8fcb65b2018-12-27 14:11:55 +053025inline std::string getRoleFromPrivileges(std::string_view priv)
26{
27 if (priv == "priv-admin")
28 {
29 return "Administrator";
30 }
Ed Tanous3174e4d2020-10-07 11:41:22 -070031 if (priv == "priv-user")
AppaRao Puli8fcb65b2018-12-27 14:11:55 +053032 {
AppaRao Pulic80fee52019-10-16 14:49:36 +053033 return "ReadOnly";
AppaRao Puli8fcb65b2018-12-27 14:11:55 +053034 }
Ed Tanous3174e4d2020-10-07 11:41:22 -070035 if (priv == "priv-operator")
AppaRao Puli8fcb65b2018-12-27 14:11:55 +053036 {
37 return "Operator";
38 }
Ed Tanous3174e4d2020-10-07 11:41:22 -070039 if (priv == "priv-noaccess")
jayaprakash Mutyalae9e6d242019-07-29 11:59:08 +000040 {
41 return "NoAccess";
42 }
AppaRao Puli8fcb65b2018-12-27 14:11:55 +053043 return "";
44}
45
46inline bool getAssignedPrivFromRole(std::string_view role,
47 nlohmann::json& privArray)
48{
49 if (role == "Administrator")
50 {
51 privArray = {"Login", "ConfigureManager", "ConfigureUsers",
52 "ConfigureSelf", "ConfigureComponents"};
53 }
54 else if (role == "Operator")
55 {
56 privArray = {"Login", "ConfigureSelf", "ConfigureComponents"};
57 }
AppaRao Pulic80fee52019-10-16 14:49:36 +053058 else if (role == "ReadOnly")
AppaRao Puli8fcb65b2018-12-27 14:11:55 +053059 {
60 privArray = {"Login", "ConfigureSelf"};
61 }
jayaprakash Mutyalae9e6d242019-07-29 11:59:08 +000062 else if (role == "NoAccess")
63 {
64 privArray = nlohmann::json::array();
65 }
AppaRao Puli8fcb65b2018-12-27 14:11:55 +053066 else
67 {
68 return false;
69 }
70 return true;
71}
72
John Edward Broadbent7e860f12021-04-08 15:57:16 -070073inline void requestRoutesRoles(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -070074{
John Edward Broadbent7e860f12021-04-08 15:57:16 -070075 BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Roles/<str>/")
76 .privileges({"Login"})
77 .methods(boost::beast::http::verb::get)(
78 [](const crow::Request&,
79 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
80 const std::string& roleId) {
81 nlohmann::json privArray = nlohmann::json::array();
82 if (false == getAssignedPrivFromRole(roleId, privArray))
83 {
84 messages::resourceNotFound(asyncResp->res, "Role", roleId);
Lewanczyk, Dawid4e49bd42018-01-25 11:30:19 +010085
John Edward Broadbent7e860f12021-04-08 15:57:16 -070086 return;
87 }
zhanghch058d1b46d2021-04-01 11:18:24 +080088
John Edward Broadbent7e860f12021-04-08 15:57:16 -070089 asyncResp->res.jsonValue = {
90 {"@odata.type", "#Role.v1_2_2.Role"},
91 {"Name", "User Role"},
92 {"Description", roleId + " User Role"},
93 {"OemPrivileges", nlohmann::json::array()},
94 {"IsPredefined", true},
95 {"Id", roleId},
96 {"RoleId", roleId},
97 {"@odata.id", "/redfish/v1/AccountService/Roles/" + roleId},
98 {"AssignedPrivileges", std::move(privArray)}};
99 });
100}
zhanghch058d1b46d2021-04-01 11:18:24 +0800101
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700102inline void requestRoutesRoleCollection(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700103{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700104 BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Roles/")
105 .privileges({"Login"})
106 .methods(boost::beast::http::verb::get)(
107 [](const crow::Request&,
108 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
109 asyncResp->res.jsonValue = {
110 {"@odata.id", "/redfish/v1/AccountService/Roles"},
111 {"@odata.type", "#RoleCollection.RoleCollection"},
112 {"Name", "Roles Collection"},
113 {"Description", "BMC User Roles"}};
Lewanczyk, Dawid4e49bd42018-01-25 11:30:19 +0100114
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700115 crow::connections::systemBus->async_method_call(
116 [asyncResp](
117 const boost::system::error_code ec,
Ed Tanousabf2add2019-01-22 16:40:12 -0800118 const std::variant<std::vector<std::string>>& resp) {
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700119 if (ec)
120 {
121 messages::internalError(asyncResp->res);
122 return;
123 }
124 nlohmann::json& memberArray =
125 asyncResp->res.jsonValue["Members"];
126 memberArray = nlohmann::json::array();
127 const std::vector<std::string>* privList =
128 std::get_if<std::vector<std::string>>(&resp);
129 if (privList == nullptr)
130 {
131 messages::internalError(asyncResp->res);
132 return;
133 }
134 for (const std::string& priv : *privList)
135 {
136 std::string role = getRoleFromPrivileges(priv);
137 if (!role.empty())
138 {
139 memberArray.push_back(
140 {{"@odata.id",
141 "/redfish/v1/AccountService/Roles/" +
142 role}});
143 }
144 }
145 asyncResp->res.jsonValue["Members@odata.count"] =
146 memberArray.size();
147 },
148 "xyz.openbmc_project.User.Manager",
149 "/xyz/openbmc_project/user",
150 "org.freedesktop.DBus.Properties", "Get",
151 "xyz.openbmc_project.User.Manager", "AllPrivileges");
152 });
153}
Lewanczyk, Dawid4e49bd42018-01-25 11:30:19 +0100154
Ed Tanous1abe55e2018-09-05 08:30:59 -0700155} // namespace redfish