blob: c18942f5b0c74ee339e3df53de863d1e968d3d94 [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 Tanoused398212021-06-09 17:05:54 -070020#include <registries/privilege_registry.hpp>
Jonathan Doman1e1e5982021-06-11 09:36:17 -070021#include <sdbusplus/asio/property.hpp>
Lewanczyk, Dawid4e49bd42018-01-25 11:30:19 +010022
Ed Tanousabf2add2019-01-22 16:40:12 -080023#include <variant>
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 }
Ed Tanous3174e4d2020-10-07 11:41:22 -070041 if (priv == "priv-noaccess")
jayaprakash Mutyalae9e6d242019-07-29 11:59:08 +000042 {
43 return "NoAccess";
44 }
AppaRao Puli8fcb65b2018-12-27 14:11:55 +053045 return "";
46}
47
48inline bool getAssignedPrivFromRole(std::string_view role,
49 nlohmann::json& privArray)
50{
51 if (role == "Administrator")
52 {
53 privArray = {"Login", "ConfigureManager", "ConfigureUsers",
54 "ConfigureSelf", "ConfigureComponents"};
55 }
56 else if (role == "Operator")
57 {
58 privArray = {"Login", "ConfigureSelf", "ConfigureComponents"};
59 }
AppaRao Pulic80fee52019-10-16 14:49:36 +053060 else if (role == "ReadOnly")
AppaRao Puli8fcb65b2018-12-27 14:11:55 +053061 {
62 privArray = {"Login", "ConfigureSelf"};
63 }
jayaprakash Mutyalae9e6d242019-07-29 11:59:08 +000064 else if (role == "NoAccess")
65 {
66 privArray = nlohmann::json::array();
67 }
AppaRao Puli8fcb65b2018-12-27 14:11:55 +053068 else
69 {
70 return false;
71 }
72 return true;
73}
74
John Edward Broadbent7e860f12021-04-08 15:57:16 -070075inline void requestRoutesRoles(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -070076{
John Edward Broadbent7e860f12021-04-08 15:57:16 -070077 BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Roles/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -070078 .privileges(redfish::privileges::getRole)
John Edward Broadbent7e860f12021-04-08 15:57:16 -070079 .methods(boost::beast::http::verb::get)(
80 [](const crow::Request&,
81 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
82 const std::string& roleId) {
83 nlohmann::json privArray = nlohmann::json::array();
84 if (false == getAssignedPrivFromRole(roleId, privArray))
85 {
86 messages::resourceNotFound(asyncResp->res, "Role", roleId);
Lewanczyk, Dawid4e49bd42018-01-25 11:30:19 +010087
John Edward Broadbent7e860f12021-04-08 15:57:16 -070088 return;
89 }
zhanghch058d1b46d2021-04-01 11:18:24 +080090
John Edward Broadbent7e860f12021-04-08 15:57:16 -070091 asyncResp->res.jsonValue = {
92 {"@odata.type", "#Role.v1_2_2.Role"},
93 {"Name", "User Role"},
94 {"Description", roleId + " User Role"},
95 {"OemPrivileges", nlohmann::json::array()},
96 {"IsPredefined", true},
97 {"Id", roleId},
98 {"RoleId", roleId},
99 {"@odata.id", "/redfish/v1/AccountService/Roles/" + roleId},
100 {"AssignedPrivileges", std::move(privArray)}};
101 });
102}
zhanghch058d1b46d2021-04-01 11:18:24 +0800103
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700104inline void requestRoutesRoleCollection(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700105{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700106 BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Roles/")
Ed Tanoused398212021-06-09 17:05:54 -0700107 .privileges(redfish::privileges::getRoleCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700108 .methods(boost::beast::http::verb::get)(
109 [](const crow::Request&,
110 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
111 asyncResp->res.jsonValue = {
112 {"@odata.id", "/redfish/v1/AccountService/Roles"},
113 {"@odata.type", "#RoleCollection.RoleCollection"},
114 {"Name", "Roles Collection"},
115 {"Description", "BMC User Roles"}};
Lewanczyk, Dawid4e49bd42018-01-25 11:30:19 +0100116
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700117 sdbusplus::asio::getProperty<std::vector<std::string>>(
118 *crow::connections::systemBus,
119 "xyz.openbmc_project.User.Manager",
120 "/xyz/openbmc_project/user",
121 "xyz.openbmc_project.User.Manager", "AllPrivileges",
Ed Tanous168e20c2021-12-13 14:39:53 -0800122 [asyncResp](const boost::system::error_code ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700123 const std::vector<std::string>& privList) {
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700124 if (ec)
125 {
126 messages::internalError(asyncResp->res);
127 return;
128 }
129 nlohmann::json& memberArray =
130 asyncResp->res.jsonValue["Members"];
131 memberArray = nlohmann::json::array();
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700132 for (const std::string& priv : privList)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700133 {
134 std::string role = getRoleFromPrivileges(priv);
135 if (!role.empty())
136 {
137 memberArray.push_back(
138 {{"@odata.id",
139 "/redfish/v1/AccountService/Roles/" +
140 role}});
141 }
142 }
143 asyncResp->res.jsonValue["Members@odata.count"] =
144 memberArray.size();
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700145 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700146 });
147}
Lewanczyk, Dawid4e49bd42018-01-25 11:30:19 +0100148
Ed Tanous1abe55e2018-09-05 08:30:59 -0700149} // namespace redfish