Redfish: Added RoleCollection and Role support
Added RoleCollection by retriving information
from dbus user object and Role properties support.
Test:
Tested RoleCollection and Role redfish URI's by cross
validaing with dbus user object properties.
Change-Id: I519972f645c7a4d0486e64ae5a403a3009aa26f5
Signed-off-by: AppaRao Puli <apparao.puli@linux.intel.com>
diff --git a/redfish-core/lib/roles.hpp b/redfish-core/lib/roles.hpp
index 5e711b1..0f33b04 100644
--- a/redfish-core/lib/roles.hpp
+++ b/redfish-core/lib/roles.hpp
@@ -20,11 +20,59 @@
namespace redfish
{
+inline std::string getRoleFromPrivileges(std::string_view priv)
+{
+ if (priv == "priv-admin")
+ {
+ return "Administrator";
+ }
+ else if (priv == "priv-callback")
+ {
+ return "Callback";
+ }
+ else if (priv == "priv-user")
+ {
+ return "User";
+ }
+ else if (priv == "priv-operator")
+ {
+ return "Operator";
+ }
+ return "";
+}
+
+inline bool getAssignedPrivFromRole(std::string_view role,
+ nlohmann::json& privArray)
+{
+ if (role == "Administrator")
+ {
+ privArray = {"Login", "ConfigureManager", "ConfigureUsers",
+ "ConfigureSelf", "ConfigureComponents"};
+ }
+ else if (role == "Operator")
+ {
+ privArray = {"Login", "ConfigureSelf", "ConfigureComponents"};
+ }
+ else if (role == "User")
+ {
+ privArray = {"Login", "ConfigureSelf"};
+ }
+ else if (role == "Callback")
+ {
+ privArray = {"Login"};
+ }
+ else
+ {
+ return false;
+ }
+ return true;
+}
+
class Roles : public Node
{
public:
Roles(CrowApp& app) :
- Node(app, "/redfish/v1/AccountService/Roles/Administrator/")
+ Node(app, "/redfish/v1/AccountService/Roles/<str>/", std::string())
{
entityPrivileges = {
{boost::beast::http::verb::get, {{"Login"}}},
@@ -39,18 +87,31 @@
void doGet(crow::Response& res, const crow::Request& req,
const std::vector<std::string>& params) override
{
- res.jsonValue["@odata.id"] =
- "/redfish/v1/AccountService/Roles/Administrator";
- res.jsonValue["@odata.type"] = "#Role.v1_0_2.Role";
- res.jsonValue["@odata.context"] = "/redfish/v1/$metadata#Role.Role";
- res.jsonValue["Id"] = "Administrator";
- res.jsonValue["Name"] = "User Role";
- res.jsonValue["Description"] = "Administrator User Role";
- res.jsonValue["IsPredefined"] = true;
- res.jsonValue["AssignedPrivileges"] = {
- "Login", "ConfigureManager", "ConfigureUsers", "ConfigureSelf",
- "ConfigureComponents"};
- res.jsonValue["OemPrivileges"] = nlohmann::json::array();
+ if (params.size() != 1)
+ {
+ messages::internalError(res);
+ res.end();
+ return;
+ }
+ const std::string& roleId = params[0];
+ nlohmann::json privArray = nlohmann::json::array();
+ if (false == getAssignedPrivFromRole(roleId, privArray))
+ {
+ messages::resourceNotFound(res, "Role", roleId);
+ res.end();
+ return;
+ }
+
+ res.jsonValue = {
+ {"@odata.type", "#Role.v1_0_2.Role"},
+ {"@odata.context", "/redfish/v1/$metadata#Role.Role"},
+ {"Name", "User Role"},
+ {"Description", "Administrator User Role"},
+ {"OemPrivileges", nlohmann::json::array()},
+ {"IsPredefined", true},
+ {"Id", roleId},
+ {"@odata.id", "/redfish/v1/AccountService/Roles/" + roleId},
+ {"AssignedPrivileges", std::move(privArray)}};
res.end();
}
};
@@ -74,16 +135,47 @@
void doGet(crow::Response& res, const crow::Request& req,
const std::vector<std::string>& params) override
{
- res.jsonValue["@odata.id"] = "/redfish/v1/AccountService/Roles";
- res.jsonValue["@odata.type"] = "#RoleCollection.RoleCollection";
- res.jsonValue["@odata.context"] =
- "/redfish/v1/$metadata#RoleCollection.RoleCollection";
- res.jsonValue["Name"] = "Roles Collection";
- res.jsonValue["Description"] = "BMC User Roles";
- res.jsonValue["Members@odata.count"] = 1;
- res.jsonValue["Members"] = {
- {{"@odata.id", "/redfish/v1/AccountService/Roles/Administrator"}}};
- res.end();
+ auto asyncResp = std::make_shared<AsyncResp>(res);
+ res.jsonValue = {{"@odata.context",
+ "/redfish/v1/"
+ "$metadata#RoleCollection.RoleCollection"},
+ {"@odata.id", "/redfish/v1/AccountService/Roles"},
+ {"@odata.type", "#RoleCollection.RoleCollection"},
+ {"Name", "Roles Collection"},
+ {"Description", "BMC User Roles"}};
+
+ crow::connections::systemBus->async_method_call(
+ [asyncResp](
+ const boost::system::error_code ec,
+ const sdbusplus::message::variant<std::vector<std::string>>&
+ resp) {
+ if (ec)
+ {
+ messages::internalError(asyncResp->res);
+ return;
+ }
+ nlohmann::json& memberArray =
+ asyncResp->res.jsonValue["Members"];
+ memberArray = nlohmann::json::array();
+ const std::vector<std::string>* privList =
+ sdbusplus::message::variant_ns::get_if<
+ std::vector<std::string>>(&resp);
+ for (const std::string& priv : *privList)
+ {
+ std::string role = getRoleFromPrivileges(priv);
+ if (!role.empty())
+ {
+ memberArray.push_back(
+ {{"@odata.id",
+ "/redfish/v1/AccountService/Roles/" + role}});
+ }
+ }
+ asyncResp->res.jsonValue["Members@odata.count"] =
+ memberArray.size();
+ },
+ "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
+ "org.freedesktop.DBus.Properties", "Get",
+ "xyz.openbmc_project.User.Manager", "AllPrivileges");
}
};