blob: b5deed16b5ec29dd42de04df30199805966ef39c [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 }
31 else if (priv == "priv-callback")
32 {
33 return "Callback";
34 }
35 else if (priv == "priv-user")
36 {
37 return "User";
38 }
39 else if (priv == "priv-operator")
40 {
41 return "Operator";
42 }
43 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 }
58 else if (role == "User")
59 {
60 privArray = {"Login", "ConfigureSelf"};
61 }
62 else if (role == "Callback")
63 {
64 privArray = {"Login"};
65 }
66 else
67 {
68 return false;
69 }
70 return true;
71}
72
Ed Tanous1abe55e2018-09-05 08:30:59 -070073class Roles : public Node
74{
75 public:
76 Roles(CrowApp& app) :
AppaRao Puli8fcb65b2018-12-27 14:11:55 +053077 Node(app, "/redfish/v1/AccountService/Roles/<str>/", std::string())
Ed Tanous1abe55e2018-09-05 08:30:59 -070078 {
Ed Tanous1abe55e2018-09-05 08:30:59 -070079 entityPrivileges = {
80 {boost::beast::http::verb::get, {{"Login"}}},
81 {boost::beast::http::verb::head, {{"Login"}}},
82 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
83 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
84 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
85 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
86 }
Lewanczyk, Dawid4e49bd42018-01-25 11:30:19 +010087
Ed Tanous1abe55e2018-09-05 08:30:59 -070088 private:
89 void doGet(crow::Response& res, const crow::Request& req,
90 const std::vector<std::string>& params) override
91 {
AppaRao Puli8fcb65b2018-12-27 14:11:55 +053092 if (params.size() != 1)
93 {
94 messages::internalError(res);
95 res.end();
96 return;
97 }
98 const std::string& roleId = params[0];
99 nlohmann::json privArray = nlohmann::json::array();
100 if (false == getAssignedPrivFromRole(roleId, privArray))
101 {
102 messages::resourceNotFound(res, "Role", roleId);
103 res.end();
104 return;
105 }
106
107 res.jsonValue = {
108 {"@odata.type", "#Role.v1_0_2.Role"},
109 {"@odata.context", "/redfish/v1/$metadata#Role.Role"},
110 {"Name", "User Role"},
Ed Tanous0f261532019-02-08 11:13:29 -0800111 {"Description", roleId + " User Role"},
AppaRao Puli8fcb65b2018-12-27 14:11:55 +0530112 {"OemPrivileges", nlohmann::json::array()},
113 {"IsPredefined", true},
114 {"Id", roleId},
115 {"@odata.id", "/redfish/v1/AccountService/Roles/" + roleId},
116 {"AssignedPrivileges", std::move(privArray)}};
Ed Tanous1abe55e2018-09-05 08:30:59 -0700117 res.end();
118 }
Lewanczyk, Dawid4e49bd42018-01-25 11:30:19 +0100119};
120
Ed Tanous1abe55e2018-09-05 08:30:59 -0700121class RoleCollection : public Node
122{
123 public:
124 RoleCollection(CrowApp& app) :
125 Node(app, "/redfish/v1/AccountService/Roles/")
126 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700127 entityPrivileges = {
128 {boost::beast::http::verb::get, {{"Login"}}},
129 {boost::beast::http::verb::head, {{"Login"}}},
130 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
131 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
132 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
133 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
134 }
Lewanczyk, Dawid4e49bd42018-01-25 11:30:19 +0100135
Ed Tanous1abe55e2018-09-05 08:30:59 -0700136 private:
137 void doGet(crow::Response& res, const crow::Request& req,
138 const std::vector<std::string>& params) override
139 {
AppaRao Puli8fcb65b2018-12-27 14:11:55 +0530140 auto asyncResp = std::make_shared<AsyncResp>(res);
141 res.jsonValue = {{"@odata.context",
142 "/redfish/v1/"
143 "$metadata#RoleCollection.RoleCollection"},
144 {"@odata.id", "/redfish/v1/AccountService/Roles"},
145 {"@odata.type", "#RoleCollection.RoleCollection"},
146 {"Name", "Roles Collection"},
147 {"Description", "BMC User Roles"}};
148
149 crow::connections::systemBus->async_method_call(
Ed Tanousabf2add2019-01-22 16:40:12 -0800150 [asyncResp](const boost::system::error_code ec,
151 const std::variant<std::vector<std::string>>& resp) {
AppaRao Puli8fcb65b2018-12-27 14:11:55 +0530152 if (ec)
153 {
154 messages::internalError(asyncResp->res);
155 return;
156 }
157 nlohmann::json& memberArray =
158 asyncResp->res.jsonValue["Members"];
159 memberArray = nlohmann::json::array();
160 const std::vector<std::string>* privList =
Ed Tanousabf2add2019-01-22 16:40:12 -0800161 std::get_if<std::vector<std::string>>(&resp);
AppaRao Puli8fcb65b2018-12-27 14:11:55 +0530162 for (const std::string& priv : *privList)
163 {
164 std::string role = getRoleFromPrivileges(priv);
165 if (!role.empty())
166 {
167 memberArray.push_back(
168 {{"@odata.id",
169 "/redfish/v1/AccountService/Roles/" + role}});
170 }
171 }
172 asyncResp->res.jsonValue["Members@odata.count"] =
173 memberArray.size();
174 },
175 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
176 "org.freedesktop.DBus.Properties", "Get",
177 "xyz.openbmc_project.User.Manager", "AllPrivileges");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700178 }
Lewanczyk, Dawid4e49bd42018-01-25 11:30:19 +0100179};
180
Ed Tanous1abe55e2018-09-05 08:30:59 -0700181} // namespace redfish