blob: dd8a790800564d522e7de488c3ab21f111abac0c [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>
Ed Tanous168e20c2021-12-13 14:39:53 -080019#include <dbus_utility.hpp>
Ed Tanous45ca1b82022-03-25 13:07:27 -070020#include <query.hpp>
Ed Tanoused398212021-06-09 17:05:54 -070021#include <registries/privilege_registry.hpp>
Jonathan Doman1e1e5982021-06-11 09:36:17 -070022#include <sdbusplus/asio/property.hpp>
Lewanczyk, Dawid4e49bd42018-01-25 11:30:19 +010023
Ed Tanousabf2add2019-01-22 16:40:12 -080024#include <variant>
Ed Tanous1abe55e2018-09-05 08:30:59 -070025namespace redfish
26{
Lewanczyk, Dawid4e49bd42018-01-25 11:30:19 +010027
AppaRao Puli8fcb65b2018-12-27 14:11:55 +053028inline std::string getRoleFromPrivileges(std::string_view priv)
29{
30 if (priv == "priv-admin")
31 {
32 return "Administrator";
33 }
Ed Tanous3174e4d2020-10-07 11:41:22 -070034 if (priv == "priv-user")
AppaRao Puli8fcb65b2018-12-27 14:11:55 +053035 {
AppaRao Pulic80fee52019-10-16 14:49:36 +053036 return "ReadOnly";
AppaRao Puli8fcb65b2018-12-27 14:11:55 +053037 }
Ed Tanous3174e4d2020-10-07 11:41:22 -070038 if (priv == "priv-operator")
AppaRao Puli8fcb65b2018-12-27 14:11:55 +053039 {
40 return "Operator";
41 }
Ed Tanous3174e4d2020-10-07 11:41:22 -070042 if (priv == "priv-noaccess")
jayaprakash Mutyalae9e6d242019-07-29 11:59:08 +000043 {
44 return "NoAccess";
45 }
AppaRao Puli8fcb65b2018-12-27 14:11:55 +053046 return "";
47}
48
49inline bool getAssignedPrivFromRole(std::string_view role,
50 nlohmann::json& privArray)
51{
52 if (role == "Administrator")
53 {
54 privArray = {"Login", "ConfigureManager", "ConfigureUsers",
55 "ConfigureSelf", "ConfigureComponents"};
56 }
57 else if (role == "Operator")
58 {
59 privArray = {"Login", "ConfigureSelf", "ConfigureComponents"};
60 }
AppaRao Pulic80fee52019-10-16 14:49:36 +053061 else if (role == "ReadOnly")
AppaRao Puli8fcb65b2018-12-27 14:11:55 +053062 {
63 privArray = {"Login", "ConfigureSelf"};
64 }
jayaprakash Mutyalae9e6d242019-07-29 11:59:08 +000065 else if (role == "NoAccess")
66 {
67 privArray = nlohmann::json::array();
68 }
AppaRao Puli8fcb65b2018-12-27 14:11:55 +053069 else
70 {
71 return false;
72 }
73 return true;
74}
75
John Edward Broadbent7e860f12021-04-08 15:57:16 -070076inline void requestRoutesRoles(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -070077{
John Edward Broadbent7e860f12021-04-08 15:57:16 -070078 BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Roles/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -070079 .privileges(redfish::privileges::getRole)
John Edward Broadbent7e860f12021-04-08 15:57:16 -070080 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -070081 [&app](const crow::Request& req,
82 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
83 const std::string& roleId) {
84 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
85 {
86 return;
87 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -070088 nlohmann::json privArray = nlohmann::json::array();
Ed Tanouse05aec52022-01-25 10:28:56 -080089 if (!getAssignedPrivFromRole(roleId, privArray))
John Edward Broadbent7e860f12021-04-08 15:57:16 -070090 {
91 messages::resourceNotFound(asyncResp->res, "Role", roleId);
Lewanczyk, Dawid4e49bd42018-01-25 11:30:19 +010092
John Edward Broadbent7e860f12021-04-08 15:57:16 -070093 return;
94 }
zhanghch058d1b46d2021-04-01 11:18:24 +080095
John Edward Broadbent7e860f12021-04-08 15:57:16 -070096 asyncResp->res.jsonValue = {
97 {"@odata.type", "#Role.v1_2_2.Role"},
98 {"Name", "User Role"},
99 {"Description", roleId + " User Role"},
100 {"OemPrivileges", nlohmann::json::array()},
101 {"IsPredefined", true},
102 {"Id", roleId},
103 {"RoleId", roleId},
104 {"@odata.id", "/redfish/v1/AccountService/Roles/" + roleId},
105 {"AssignedPrivileges", std::move(privArray)}};
106 });
107}
zhanghch058d1b46d2021-04-01 11:18:24 +0800108
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700109inline void requestRoutesRoleCollection(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700110{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700111 BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Roles/")
Ed Tanoused398212021-06-09 17:05:54 -0700112 .privileges(redfish::privileges::getRoleCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700113 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700114 [&app](const crow::Request& req,
115 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
116 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
117 {
118 return;
119 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700120 asyncResp->res.jsonValue = {
121 {"@odata.id", "/redfish/v1/AccountService/Roles"},
122 {"@odata.type", "#RoleCollection.RoleCollection"},
123 {"Name", "Roles Collection"},
124 {"Description", "BMC User Roles"}};
Lewanczyk, Dawid4e49bd42018-01-25 11:30:19 +0100125
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700126 sdbusplus::asio::getProperty<std::vector<std::string>>(
127 *crow::connections::systemBus,
128 "xyz.openbmc_project.User.Manager",
129 "/xyz/openbmc_project/user",
130 "xyz.openbmc_project.User.Manager", "AllPrivileges",
Ed Tanous168e20c2021-12-13 14:39:53 -0800131 [asyncResp](const boost::system::error_code ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700132 const std::vector<std::string>& privList) {
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700133 if (ec)
134 {
135 messages::internalError(asyncResp->res);
136 return;
137 }
138 nlohmann::json& memberArray =
139 asyncResp->res.jsonValue["Members"];
140 memberArray = nlohmann::json::array();
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700141 for (const std::string& priv : privList)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700142 {
143 std::string role = getRoleFromPrivileges(priv);
144 if (!role.empty())
145 {
146 memberArray.push_back(
147 {{"@odata.id",
148 "/redfish/v1/AccountService/Roles/" +
149 role}});
150 }
151 }
152 asyncResp->res.jsonValue["Members@odata.count"] =
153 memberArray.size();
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700154 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700155 });
156}
Lewanczyk, Dawid4e49bd42018-01-25 11:30:19 +0100157
Ed Tanous1abe55e2018-09-05 08:30:59 -0700158} // namespace redfish