user_mgr: move constructor codes to separate func
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 <nanzhoumails@gmail.com>
Change-Id: I83fdc79a12c64039217605ed65def2f2352d7ef6
diff --git a/user_mgr.cpp b/user_mgr.cpp
index 7bc86b3..7b2b697 100644
--- a/user_mgr.cpp
+++ b/user_mgr.cpp
@@ -56,7 +56,6 @@
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;
@@ -1131,78 +1130,8 @@
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;
@@ -1306,6 +1235,81 @@
}
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
diff --git a/user_mgr.hpp b/user_mgr.hpp
index a535cc2..53097a8 100644
--- a/user_mgr.hpp
+++ b/user_mgr.hpp
@@ -40,6 +40,7 @@
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 =
@@ -333,6 +334,8 @@
*/
void throwForInvalidGroups(const std::vector<std::string>& groupName);
+ void initializeAccountPolicy();
+
private:
/** @brief sdbusplus handler */
sdbusplus::bus_t& bus;