kuser.cpp
/*
* KUser - represent a user/account
* Copyright (C) 2002 Tim Jansen <tim@tjansen.de>
*
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <kuser.h>
#include "kstringhandler.h"
#include <qvaluelist.h>
#include <qstringlist.h>
#include <sys/types.h>
#include <pwd.h>
#include <unistd.h>
#include <stdlib.h>
#include <grp.h>
class KUserPrivate : public KShared
{
public:
bool valid;
long uid, gid;
QString loginName, fullName;
QString roomNumber, workPhone, homePhone;
QString homeDir, shell;
KUserPrivate() : valid(false) {}
KUserPrivate(long _uid,
long _gid,
const QString &_loginname,
const QString &_fullname,
const QString &_room,
const QString &_workPhone,
const QString &_homePhone,
const QString &_homedir,
const QString &_shell) :
valid(true),
uid(_uid),
gid(_gid),
loginName(_loginname),
fullName(_fullname),
roomNumber(_room),
workPhone(_workPhone),
homePhone(_homePhone),
homeDir(_homedir),
shell(_shell) {}
};
KUser::KUser(UIDMode mode) {
long _uid = ::getuid(), _euid;
if (mode == UseEffectiveUID && (_euid = ::geteuid()) != _uid )
fillPasswd( ::getpwuid( _euid ) );
else {
fillName( ::getenv( "LOGNAME" ) );
if (uid() != _uid) {
fillName( ::getenv( "USER" ) );
if (uid() != _uid)
fillPasswd( ::getpwuid( _uid ) );
}
}
}
KUser::KUser(long uid) {
fillPasswd( ::getpwuid( uid ) );
}
KUser::KUser(const QString& name) {
fillName( name.local8Bit().data() );
}
KUser::KUser(const char *name) {
fillName( name );
}
KUser::KUser(struct passwd *p) {
fillPasswd(p);
}
KUser::KUser(const KUser & user)
: d(user.d)
{
}
KUser& KUser::operator =(const KUser& user)
{
d = user.d;
return *this;
}
bool KUser::operator ==(const KUser& user) const {
if (isValid() != user.isValid())
return false;
if (isValid())
return uid() == user.uid();
else
return true;
}
bool KUser::operator !=(const KUser& user) const {
return !operator ==(user);
}
void KUser::fillName(const char *name) {
fillPasswd(name ? ::getpwnam( name ) : 0);
}
void KUser::fillPasswd(struct passwd *p) {
if (p) {
QString gecos = KStringHandler::from8Bit(p->pw_gecos);
QStringList gecosList = QStringList::split(',', gecos, true);
d = new KUserPrivate(p->pw_uid,
p->pw_gid,
QString::fromLocal8Bit(p->pw_name),
(gecosList.size() > 0) ? gecosList[0] : QString::null,
(gecosList.size() > 1) ? gecosList[1] : QString::null,
(gecosList.size() > 2) ? gecosList[2] : QString::null,
(gecosList.size() > 3) ? gecosList[3] : QString::null,
QString::fromLocal8Bit(p->pw_dir),
QString::fromLocal8Bit(p->pw_shell));
}
else
d = new KUserPrivate();
}
bool KUser::isValid() const {
return d->valid;
}
long KUser::uid() const {
if (d->valid)
return d->uid;
else
return -1;
}
long KUser::gid() const {
if (d->valid)
return d->gid;
else
return -1;
}
bool KUser::isSuperUser() const {
return uid() == 0;
}
QString KUser::loginName() const {
if (d->valid)
return d->loginName;
else
return QString::null;
}
QString KUser::fullName() const {
if (d->valid)
return d->fullName;
else
return QString::null;
}
QString KUser::roomNumber() const {
if (d->valid)
return d->roomNumber;
else
return QString::null;
}
QString KUser::workPhone() const {
if (d->valid)
return d->workPhone;
else
return QString::null;
}
QString KUser::homePhone() const {
if (d->valid)
return d->homePhone;
else
return QString::null;
}
QString KUser::homeDir() const {
if (d->valid)
return d->homeDir;
else
return QString::null;
}
QString KUser::shell() const {
if (d->valid)
return d->shell;
else
return QString::null;
}
QValueList<KUserGroup> KUser::groups() const {
QValueList<KUserGroup> result;
QValueList<KUserGroup> allGroups = KUserGroup::allGroups();
QValueList<KUserGroup>::const_iterator it;
for ( it = allGroups.begin(); it != allGroups.end(); ++it ) {
QValueList<KUser> users = (*it).users();
if ( users.find( *this ) != users.end()) {
result.append(*it);
}
}
return result;
}
QStringList KUser::groupNames() const {
QStringList result;
QValueList<KUserGroup> allGroups = KUserGroup::allGroups();
QValueList<KUserGroup>::const_iterator it;
for ( it = allGroups.begin(); it != allGroups.end(); ++it ) {
QValueList<KUser> users = (*it).users();
if ( users.find( *this ) != users.end()) {
result.append((*it).name());
}
}
return result;
}
QValueList<KUser> KUser::allUsers() {
QValueList<KUser> result;
struct passwd* p;
while ((p = getpwent())) {
result.append(KUser(p));
}
endpwent();
return result;
}
QStringList KUser::allUserNames() {
QStringList result;
struct passwd* p;
while ((p = getpwent())) {
result.append(QString::fromLocal8Bit(p->pw_name));
}
endpwent();
return result;
}
KUser::~KUser() {
}
class KUserGroupPrivate : public KShared
{
public:
bool valid;
long gid;
QString name;
QValueList<KUser> users;
KUserGroupPrivate() : valid(false) {}
KUserGroupPrivate(long _gid,
const QString & _name,
const QValueList<KUser> & _users):
valid(true),
gid(_gid),
name(_name),
users(_users) {}
};
KUserGroup::KUserGroup(KUser::UIDMode mode) {
KUser user(mode);
fillGroup(getgrgid(user.gid()));
}
KUserGroup::KUserGroup(long gid) {
fillGroup(getgrgid(gid));
}
KUserGroup::KUserGroup(const QString& name) {
fillName(name.local8Bit().data());
}
KUserGroup::KUserGroup(const char *name) {
fillName(name);
}
KUserGroup::KUserGroup(struct group *g) {
fillGroup(g);
}
KUserGroup::KUserGroup(const KUserGroup & group)
: d(group.d)
{
}
KUserGroup& KUserGroup::operator =(const KUserGroup& group) {
d = group.d;
return *this;
}
bool KUserGroup::operator ==(const KUserGroup& group) const {
if (isValid() != group.isValid())
return false;
if (isValid())
return gid() == group.gid();
else
return true;
}
bool KUserGroup::operator !=(const KUserGroup& user) const {
return !operator ==(user);
}
void KUserGroup::fillName(const char *name) {
fillGroup(name ? ::getgrnam( name ) : 0);
}
void KUserGroup::fillGroup(struct group *p) {
if (!p) {
d = new KUserGroupPrivate();
return;
}
QString name = KStringHandler::from8Bit(p->gr_name);
QValueList<KUser> users;
char **user = p->gr_mem;
for ( ; *user; user++) {
KUser kUser(QString::fromLocal8Bit(*user));
users.append(kUser);
}
d = new KUserGroupPrivate(p->gr_gid,
QString::fromLocal8Bit(p->gr_name),
users);
}
bool KUserGroup::isValid() const {
return d->valid;
}
long KUserGroup::gid() const {
if (d->valid)
return d->gid;
else
return -1;
}
QString KUserGroup::name() const {
if (d->valid)
return d->name;
else
return QString::null;
}
const QValueList<KUser>& KUserGroup::users() const {
return d->users;
}
QStringList KUserGroup::userNames() const {
QStringList result;
QValueList<KUser>::const_iterator it;
for ( it = d->users.begin(); it != d->users.end(); ++it ) {
result.append((*it).loginName());
}
return result;
}
QValueList<KUserGroup> KUserGroup::allGroups() {
QValueList<KUserGroup> result;
struct group* g;
while ((g = getgrent())) {
result.append(KUserGroup(g));
}
endgrent();
return result;
}
QStringList KUserGroup::allGroupNames() {
QStringList result;
struct group* g;
while ((g = getgrent())) {
result.append(QString::fromLocal8Bit(g->gr_name));
}
endgrent();
return result;
}
KUserGroup::~KUserGroup() {
}
KUserGroup::allGroupNames
static QStringList allGroupNames()
Returns a list of all group names on this system.
Definition: kuser.cpp:409
KUserGroup::isValid
bool isValid() const
Returns wether the group is valid.
Definition: kuser.cpp:363
KUserGroup::name
QString name() const
The name of the group.
Definition: kuser.cpp:374
KUser::UseEffectiveUID
Use the effective user id.
Definition: kuser.h:50
KUser::gid
long gid() const
Returns the group id of the user.
Definition: kuser.cpp:156
KUserGroup
Represents a group on your system.
Definition: kuser.h:256
KUserGroup::gid
long gid() const
Returns the group id of the group.
Definition: kuser.cpp:367
KUser::fullName
QString fullName() const
The full name of the user.
Definition: kuser.cpp:174
KUserGroup::KUserGroup
KUserGroup(KUser::UIDMode mode=KUser::UseEffectiveUID)
Create an object from the group of the current user.
Definition: kuser.cpp:293
KUser::groups
QValueList< KUserGroup > groups() const
Returns all groups of the user.
Definition: kuser.cpp:216
KUser::uid
long uid() const
Returns the user id of the user.
Definition: kuser.cpp:149
KUser::operator!=
bool operator!=(const KUser &user) const
Two KUser objects are not equal if either isValid() is not true or uid() are not identical.
Definition: kuser.cpp:118
KUser::allUsers
static QValueList< KUser > allUsers()
Returns all users of the system.
Definition: kuser.cpp:243
KUser
Represents a user on your system.
Definition: kuser.h:45
KUser::operator==
bool operator==(const KUser &user) const
Two KUser objects are equal if isValid() is true and the uid() are identical.
Definition: kuser.cpp:109
KUser::allUserNames
static QStringList allUserNames()
Returns all user names of the system.
Definition: kuser.cpp:257
KUserGroup::users
const QValueList< KUser > & users() const
Returns a list of all users of the group.
Definition: kuser.cpp:381
KUser::loginName
QString loginName() const
The login name of the user.
Definition: kuser.cpp:167
KUserGroup::operator==
bool operator==(const KUserGroup &group) const
Two KUserGroup objects are equal if isValid() is true and gid() are identical.
Definition: kuser.cpp:325
KUserGroup::operator=
KUserGroup & operator=(const KUserGroup &group)
Copies a group.
Definition: kuser.cpp:320
KUser::groupNames
QStringList groupNames() const
Returns all group names of the user.
Definition: kuser.cpp:229
KUserGroup::userNames
QStringList userNames() const
Returns a list of all user login names of the group.
Definition: kuser.cpp:385
KShell::homeDir
QString homeDir(const QString &user)
Obtain a user's home directory.
Definition: kshell.cpp:369
KUser::UIDMode
UIDMode
Definition: kuser.h:49
KUser::homeDir
QString homeDir() const
The path to the user's home directory.
Definition: kuser.cpp:202
KUserGroup::allGroups
static QValueList< KUserGroup > allGroups()
Returns a list of all groups on this system.
Definition: kuser.cpp:396
KUser::shell
QString shell() const
The path to the user's login shell.
Definition: kuser.cpp:209
KUser::workPhone
QString workPhone() const
The user's work phone.
Definition: kuser.cpp:188
KUser::isSuperUser
bool isSuperUser() const
Checks whether the user it the super user (root).
Definition: kuser.cpp:163
KUserGroup::~KUserGroup
~KUserGroup()
Destructor.
Definition: kuser.cpp:423
KUser::~KUser
~KUser()
Destructor.
Definition: kuser.cpp:271
KUser::operator=
KUser & operator=(const KUser &user)
Copies a user.
Definition: kuser.cpp:103
KUser::isValid
bool isValid() const
Returns true if the user is valid.
Definition: kuser.cpp:145
KUser::homePhone
QString homePhone() const
The user's home phone.
Definition: kuser.cpp:195
KUser::KUser
KUser(UIDMode mode=UseEffectiveUID)
Creates an object that contains information about the current user.
Definition: kuser.cpp:68
KStringHandler::from8Bit
static QString from8Bit(const char *str)
Construct QString from a c string, guessing whether it is UTF8- or Local8Bit-encoded.
Definition: kstringhandler.cpp:652
KUserGroup::operator!=
bool operator!=(const KUserGroup &group) const
Two KUserGroup objects are not equal if either isValid() is not true or gid() are not identical...
Definition: kuser.cpp:334
KUser::roomNumber
QString roomNumber() const
The user's room number.
Definition: kuser.cpp:181
KShared
Reference counting for shared objects.
Definition: ksharedptr.h:40

KDE Home | KDE Accessibility Home | Description of Access Keys