Tom Joseph | 536ea32 | 2018-09-14 10:02:20 +0530 | [diff] [blame^] | 1 | #include <xyz/openbmc_project/Common/error.hpp> |
| 2 | #include <xyz/openbmc_project/User/Common/error.hpp> |
| 3 | #include <phosphor-logging/log.hpp> |
| 4 | #include <phosphor-logging/elog.hpp> |
| 5 | #include <phosphor-logging/elog-errors.hpp> |
| 6 | #include "config.h" |
| 7 | #include "ldap_mapper_mgr.hpp" |
| 8 | |
| 9 | namespace phosphor |
| 10 | { |
| 11 | namespace user |
| 12 | { |
| 13 | |
| 14 | using namespace phosphor::logging; |
| 15 | using InvalidArgument = |
| 16 | sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument; |
| 17 | using Argument = xyz::openbmc_project::Common::InvalidArgument; |
| 18 | using PrivilegeMappingExists = sdbusplus::xyz::openbmc_project::User::Common:: |
| 19 | Error::PrivilegeMappingExists; |
| 20 | |
| 21 | LDAPMapperMgr::LDAPMapperMgr(sdbusplus::bus::bus &bus, const char *path) : |
| 22 | MapperMgrIface(bus, path), bus(bus), path(path) |
| 23 | { |
| 24 | } |
| 25 | |
| 26 | ObjectPath LDAPMapperMgr::create(std::string groupName, std::string privilege) |
| 27 | { |
| 28 | checkPrivilegeMapper(groupName); |
| 29 | checkPrivilegeLevel(privilege); |
| 30 | |
| 31 | entryId++; |
| 32 | |
| 33 | // Object path for the LDAP group privilege mapper entry |
| 34 | auto mapperObject = |
| 35 | std::string(mapperMgrRoot) + "/" + std::to_string(entryId); |
| 36 | |
| 37 | // Create mapping for LDAP privilege mapper entry |
| 38 | auto entry = std::make_unique<phosphor::user::LDAPMapperEntry>( |
| 39 | bus, mapperObject.c_str(), groupName, privilege, *this); |
| 40 | |
| 41 | PrivilegeMapperList.emplace(entryId, std::move(entry)); |
| 42 | |
| 43 | return mapperObject; |
| 44 | } |
| 45 | |
| 46 | void LDAPMapperMgr::deletePrivilegeMapper(Id id) |
| 47 | { |
| 48 | PrivilegeMapperList.erase(id); |
| 49 | } |
| 50 | |
| 51 | void LDAPMapperMgr::checkPrivilegeMapper(const std::string &groupName) |
| 52 | { |
| 53 | if (groupName.empty()) |
| 54 | { |
| 55 | log<level::ERR>("Group name is empty"); |
| 56 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("Group name"), |
| 57 | Argument::ARGUMENT_VALUE("Null")); |
| 58 | } |
| 59 | |
| 60 | for (const auto &val : PrivilegeMapperList) |
| 61 | { |
| 62 | if (val.second.get()->groupName() == groupName) |
| 63 | { |
| 64 | log<level::ERR>("Group name already exists"); |
| 65 | elog<PrivilegeMappingExists>(); |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | void LDAPMapperMgr::checkPrivilegeLevel(const std::string &privilege) |
| 71 | { |
| 72 | if (privilege.empty()) |
| 73 | { |
| 74 | log<level::ERR>("Privilege level is empty"); |
| 75 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("Privilege level"), |
| 76 | Argument::ARGUMENT_VALUE("Null")); |
| 77 | } |
| 78 | |
| 79 | if (std::find(privMgr.begin(), privMgr.end(), privilege) == privMgr.end()) |
| 80 | { |
| 81 | log<level::ERR>("Invalid privilege"); |
| 82 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("Privilege level"), |
| 83 | Argument::ARGUMENT_VALUE(privilege.c_str())); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | } // namespace user |
| 88 | } // namespace phosphor |