Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use local files for getting information about users and group #133

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions lxqt-admin-user/usermanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,15 @@ void UserManager::loadUsersAndGroups()
// if multiple sources are specified in nsswitch.conf(5).

// load groups
setgrent();
FILE *fp = NULL;

if (!(fp = fopen(qPrintable(GROUP_FILE), "r")))
{
qWarning() << "Failed to open file " << GROUP_FILE << endl;
return;
}
struct group * grp;
while((grp = getgrent())) {
while((grp = fgetgrent(fp))) {
if (mGroups.cend() != std::find_if(mGroups.cbegin(), mGroups.cend(), [grp] (const GroupInfo * g) -> bool { return g->gid() == grp->gr_gid; }))
continue;
GroupInfo* group = new GroupInfo(grp);
Expand All @@ -73,15 +79,19 @@ void UserManager::loadUsersAndGroups()
group->addMember(QString::fromLatin1(*member_name));
}
}
endgrent();
fclose(fp);
std::sort(mGroups.begin(), mGroups.end(), [](GroupInfo* g1, GroupInfo* g2) {
return g1->name() < g2->name();
});

// load users
setpwent();
if (!(fp = fopen(qPrintable(PASSWD_FILE), "r")))
{
qWarning() << "Failed to open file " << PASSWD_FILE << endl;
return;
}
struct passwd * pw;
while((pw = getpwent())) {
while((pw = fgetpwent(fp))) {
if (mUsers.cend() != std::find_if(mUsers.cbegin(), mUsers.cend(), [pw] (const UserInfo * u) -> bool { return u->uid() == pw->pw_uid; }))
continue;
UserInfo* user = new UserInfo(pw);
Expand All @@ -93,7 +103,7 @@ void UserManager::loadUsersAndGroups()
}
}
}
endpwent();
fclose(fp);
std::sort(mUsers.begin(), mUsers.end(), [](UserInfo*& u1, UserInfo*& u2) {
return u1->name() < u2->name();
});
Expand Down