blob: 4256c7ec47966fbfd337a1281322f88fc12f6440 [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
Lewanczyk, Dawid4e49bd42018-01-25 11:30:19 +010018#include "node.hpp"
19
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 }
AppaRao Puli8fcb65b2018-12-27 14:11:55 +053031 else if (priv == "priv-user")
32 {
AppaRao Pulic80fee52019-10-16 14:49:36 +053033 return "ReadOnly";
AppaRao Puli8fcb65b2018-12-27 14:11:55 +053034 }
35 else if (priv == "priv-operator")
36 {
37 return "Operator";
38 }
39 return "";
40}
41
42inline bool getAssignedPrivFromRole(std::string_view role,
43 nlohmann::json& privArray)
44{
45 if (role == "Administrator")
46 {
47 privArray = {"Login", "ConfigureManager", "ConfigureUsers",
48 "ConfigureSelf", "ConfigureComponents"};
49 }
50 else if (role == "Operator")
51 {
52 privArray = {"Login", "ConfigureSelf", "ConfigureComponents"};
53 }
AppaRao Pulic80fee52019-10-16 14:49:36 +053054 else if (role == "ReadOnly")
AppaRao Puli8fcb65b2018-12-27 14:11:55 +053055 {
56 privArray = {"Login", "ConfigureSelf"};
57 }
AppaRao Puli8fcb65b2018-12-27 14:11:55 +053058 else
59 {
60 return false;
61 }
62 return true;
63}
64
Ed Tanous1abe55e2018-09-05 08:30:59 -070065class Roles : public Node
66{
67 public:
68 Roles(CrowApp& app) :
AppaRao Puli8fcb65b2018-12-27 14:11:55 +053069 Node(app, "/redfish/v1/AccountService/Roles/<str>/", std::string())
Ed Tanous1abe55e2018-09-05 08:30:59 -070070 {
Ed Tanous1abe55e2018-09-05 08:30:59 -070071 entityPrivileges = {
72 {boost::beast::http::verb::get, {{"Login"}}},
73 {boost::beast::http::verb::head, {{"Login"}}},
74 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
75 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
76 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
77 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
78 }
Lewanczyk, Dawid4e49bd42018-01-25 11:30:19 +010079
Ed Tanous1abe55e2018-09-05 08:30:59 -070080 private:
81 void doGet(crow::Response& res, const crow::Request& req,
82 const std::vector<std::string>& params) override
83 {
AppaRao Puli8fcb65b2018-12-27 14:11:55 +053084 if (params.size() != 1)
85 {
86 messages::internalError(res);
87 res.end();
88 return;
89 }
90 const std::string& roleId = params[0];
91 nlohmann::json privArray = nlohmann::json::array();
92 if (false == getAssignedPrivFromRole(roleId, privArray))
93 {
94 messages::resourceNotFound(res, "Role", roleId);
95 res.end();
96 return;
97 }
98
99 res.jsonValue = {
Zbigniew Kurzynskiec8abe62019-05-31 16:59:52 +0200100 {"@odata.type", "#Role.v1_2_2.Role"},
AppaRao Puli8fcb65b2018-12-27 14:11:55 +0530101 {"@odata.context", "/redfish/v1/$metadata#Role.Role"},
102 {"Name", "User Role"},
Ed Tanous0f261532019-02-08 11:13:29 -0800103 {"Description", roleId + " User Role"},
AppaRao Puli8fcb65b2018-12-27 14:11:55 +0530104 {"OemPrivileges", nlohmann::json::array()},
105 {"IsPredefined", true},
106 {"Id", roleId},
Zbigniew Kurzynskiec8abe62019-05-31 16:59:52 +0200107 {"RoleId", roleId},
AppaRao Puli8fcb65b2018-12-27 14:11:55 +0530108 {"@odata.id", "/redfish/v1/AccountService/Roles/" + roleId},
109 {"AssignedPrivileges", std::move(privArray)}};
Ed Tanous1abe55e2018-09-05 08:30:59 -0700110 res.end();
111 }
Lewanczyk, Dawid4e49bd42018-01-25 11:30:19 +0100112};
113
Ed Tanous1abe55e2018-09-05 08:30:59 -0700114class RoleCollection : public Node
115{
116 public:
117 RoleCollection(CrowApp& app) :
118 Node(app, "/redfish/v1/AccountService/Roles/")
119 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700120 entityPrivileges = {
121 {boost::beast::http::verb::get, {{"Login"}}},
122 {boost::beast::http::verb::head, {{"Login"}}},
123 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
124 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
125 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
126 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
127 }
Lewanczyk, Dawid4e49bd42018-01-25 11:30:19 +0100128
Ed Tanous1abe55e2018-09-05 08:30:59 -0700129 private:
130 void doGet(crow::Response& res, const crow::Request& req,
131 const std::vector<std::string>& params) override
132 {
AppaRao Puli8fcb65b2018-12-27 14:11:55 +0530133 auto asyncResp = std::make_shared<AsyncResp>(res);
134 res.jsonValue = {{"@odata.context",
135 "/redfish/v1/"
136 "$metadata#RoleCollection.RoleCollection"},
137 {"@odata.id", "/redfish/v1/AccountService/Roles"},
138 {"@odata.type", "#RoleCollection.RoleCollection"},
139 {"Name", "Roles Collection"},
140 {"Description", "BMC User Roles"}};
141
142 crow::connections::systemBus->async_method_call(
Ed Tanousabf2add2019-01-22 16:40:12 -0800143 [asyncResp](const boost::system::error_code ec,
144 const std::variant<std::vector<std::string>>& resp) {
AppaRao Puli8fcb65b2018-12-27 14:11:55 +0530145 if (ec)
146 {
147 messages::internalError(asyncResp->res);
148 return;
149 }
150 nlohmann::json& memberArray =
151 asyncResp->res.jsonValue["Members"];
152 memberArray = nlohmann::json::array();
153 const std::vector<std::string>* privList =
Ed Tanousabf2add2019-01-22 16:40:12 -0800154 std::get_if<std::vector<std::string>>(&resp);
Ed Tanous66664f22019-10-11 13:05:49 -0700155 if (privList == nullptr)
156 {
157 messages::internalError(asyncResp->res);
158 return;
159 }
AppaRao Puli8fcb65b2018-12-27 14:11:55 +0530160 for (const std::string& priv : *privList)
161 {
162 std::string role = getRoleFromPrivileges(priv);
163 if (!role.empty())
164 {
165 memberArray.push_back(
166 {{"@odata.id",
167 "/redfish/v1/AccountService/Roles/" + role}});
168 }
169 }
170 asyncResp->res.jsonValue["Members@odata.count"] =
171 memberArray.size();
172 },
173 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
174 "org.freedesktop.DBus.Properties", "Get",
175 "xyz.openbmc_project.User.Manager", "AllPrivileges");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700176 }
Lewanczyk, Dawid4e49bd42018-01-25 11:30:19 +0100177};
178
Ed Tanous1abe55e2018-09-05 08:30:59 -0700179} // namespace redfish