Skip to content

Commit

Permalink
user_mgr: move constructor codes to separate func
Browse files Browse the repository at this point in the history
Rafactorred the code so that the AccountPolicy part is moved into a
separate function out of the constructor. This is alligned with
existing code: |initUserObject|.

Tested: code moving only.

Before this serial of code, coverage is
  lines......: 61.7% (1217 of 1973 lines)
  functions..: 77.9% (173 of 222 functions)
  branches...: 32.2% (1852 of 5746 branches)

Signed-off-by: Nan Zhou <[email protected]>
Change-Id: I83fdc79a12c64039217605ed65def2f2352d7ef6
  • Loading branch information
FighterNan committed Nov 11, 2022
1 parent fef6303 commit 4bc6981
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 72 deletions.
148 changes: 76 additions & 72 deletions user_mgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 56,6 @@ static constexpr const char* passwdFileName = "/etc/passwd";
static constexpr size_t ipmiMaxUserNameLen = 16;
static constexpr size_t systemMaxUserNameLen = 30;
static constexpr const char* grpSsh = "ssh";
static constexpr uint8_t minPasswdLength = 8;
static constexpr int success = 0;
static constexpr int failure = -1;

Expand Down Expand Up @@ -1131,78 1130,8 @@ UserInfoMap UserMgr::getUserInfo(std::string userName)
return userInfo;
}

void UserMgr::initUserObjects(void)
void UserMgr::initializeAccountPolicy()
{
// All user management lock has to be based on /etc/shadow
// TODO phosphor-user-manager#10 phosphor::user::shadow::Lock lock{};
std::vector<std::string> userNameList;
std::vector<std::string> sshGrpUsersList;
UserSSHLists userSSHLists = getUserAndSshGrpList();
userNameList = std::move(userSSHLists.first);
sshGrpUsersList = std::move(userSSHLists.second);

if (!userNameList.empty())
{
std::map<std::string, std::vector<std::string>> groupLists;
for (auto& grp : groupsMgr)
{
if (grp == grpSsh)
{
groupLists.emplace(grp, sshGrpUsersList);
}
else
{
std::vector<std::string> grpUsersList = getUsersInGroup(grp);
groupLists.emplace(grp, grpUsersList);
}
}
for (auto& grp : privMgr)
{
std::vector<std::string> grpUsersList = getUsersInGroup(grp);
groupLists.emplace(grp, grpUsersList);
}

for (auto& user : userNameList)
{
std::vector<std::string> userGroups;
std::string userPriv;
for (const auto& grp : groupLists)
{
std::vector<std::string> tempGrp = grp.second;
if (std::find(tempGrp.begin(), tempGrp.end(), user) !=
tempGrp.end())
{
if (std::find(privMgr.begin(), privMgr.end(), grp.first) !=
privMgr.end())
{
userPriv = grp.first;
}
else
{
userGroups.emplace_back(grp.first);
}
}
}
// Add user objects to the Users path.
sdbusplus::message::object_path tempObjPath(usersObjPath);
tempObjPath /= user;
std::string objPath(tempObjPath);
std::sort(userGroups.begin(), userGroups.end());
usersList.emplace(user, std::make_unique<phosphor::user::Users>(
bus, objPath.c_str(), userGroups,
userPriv, isUserEnabled(user), *this));
}
}
}

UserMgr::UserMgr(sdbusplus::bus_t& bus, const char* path) :
Ifaces(bus, path, Ifaces::action::defer_emit), bus(bus), path(path),
pamPasswdConfigFile(defaultPamPasswdConfigFile),
pamAuthConfigFile(defaultPamAuthConfigFile)
{
UserMgrIface::allPrivileges(privMgr);
std::sort(groupsMgr.begin(), groupsMgr.end());
UserMgrIface::allGroups(groupsMgr);
std::string valueStr;
auto value = minPasswdLength;
unsigned long tmp = 0;
Expand Down Expand Up @@ -1306,6 1235,81 @@ UserMgr::UserMgr(sdbusplus::bus_t& bus, const char* path) :
}
AccountPolicyIface::accountUnlockTimeout(value32);
}
}

void UserMgr::initUserObjects(void)
{
// All user management lock has to be based on /etc/shadow
// TODO phosphor-user-manager#10 phosphor::user::shadow::Lock lock{};
std::vector<std::string> userNameList;
std::vector<std::string> sshGrpUsersList;
UserSSHLists userSSHLists = getUserAndSshGrpList();
userNameList = std::move(userSSHLists.first);
sshGrpUsersList = std::move(userSSHLists.second);

if (!userNameList.empty())
{
std::map<std::string, std::vector<std::string>> groupLists;
for (auto& grp : groupsMgr)
{
if (grp == grpSsh)
{
groupLists.emplace(grp, sshGrpUsersList);
}
else
{
std::vector<std::string> grpUsersList = getUsersInGroup(grp);
groupLists.emplace(grp, grpUsersList);
}
}
for (auto& grp : privMgr)
{
std::vector<std::string> grpUsersList = getUsersInGroup(grp);
groupLists.emplace(grp, grpUsersList);
}

for (auto& user : userNameList)
{
std::vector<std::string> userGroups;
std::string userPriv;
for (const auto& grp : groupLists)
{
std::vector<std::string> tempGrp = grp.second;
if (std::find(tempGrp.begin(), tempGrp.end(), user) !=
tempGrp.end())
{
if (std::find(privMgr.begin(), privMgr.end(), grp.first) !=
privMgr.end())
{
userPriv = grp.first;
}
else
{
userGroups.emplace_back(grp.first);
}
}
}
// Add user objects to the Users path.
sdbusplus::message::object_path tempObjPath(usersObjPath);
tempObjPath /= user;
std::string objPath(tempObjPath);
std::sort(userGroups.begin(), userGroups.end());
usersList.emplace(user, std::make_unique<phosphor::user::Users>(
bus, objPath.c_str(), userGroups,
userPriv, isUserEnabled(user), *this));
}
}
}

UserMgr::UserMgr(sdbusplus::bus_t& bus, const char* path) :
Ifaces(bus, path, Ifaces::action::defer_emit), bus(bus), path(path),
pamPasswdConfigFile(defaultPamPasswdConfigFile),
pamAuthConfigFile(defaultPamAuthConfigFile)
{
UserMgrIface::allPrivileges(privMgr);
std::sort(groupsMgr.begin(), groupsMgr.end());
UserMgrIface::allGroups(groupsMgr);
initializeAccountPolicy();
initUserObjects();

// emit the signal
Expand Down
3 changes: 3 additions & 0 deletions user_mgr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 40,7 @@ namespace user

inline constexpr size_t ipmiMaxUsers = 15;
inline constexpr size_t maxSystemUsers = 30;
inline constexpr uint8_t minPasswdLength = 8;

using UserMgrIface = sdbusplus::xyz::openbmc_project::User::server::Manager;
using UserSSHLists =
Expand Down Expand Up @@ -333,6 334,8 @@ class UserMgr : public Ifaces
*/
void throwForInvalidGroups(const std::vector<std::string>& groupName);

void initializeAccountPolicy();

private:
/** @brief sdbusplus handler */
sdbusplus::bus_t& bus;
Expand Down

0 comments on commit 4bc6981

Please sign in to comment.