23#include <QtCore/QMutableStringListIterator>
24#include <QtCore/QDebug>
31class KUser::Private :
public KShared
34 PUSER_INFO_11 userInfo;
37 Private() : userInfo(0), sid(0) {}
39 Private(PUSER_INFO_11 userInfo_, PSID sid_ = 0) : userInfo(userInfo_) {}
41 Private(
const QString &name, PSID sid_ = 0) : userInfo(0), sid(NULL)
44 NET_API_STATUS status = NetGetAnyDCName(0, 0, &servername);
45 if (status != NERR_Success)
50 if (NetUserGetInfo((LPCWSTR) servername, (LPCWSTR) name.utf16(), 11, (LPBYTE *) &userInfo) != NERR_Success) {
55 NetApiBufferFree(servername);
62 DWORD cchReferencedDomainName = 0;
63 WCHAR* referencedDomainName = NULL;
68 LookupAccountNameW(NULL, (LPCWSTR) name.utf16(), sid, &size, referencedDomainName, &cchReferencedDomainName, &nameuse);
69 sid = (PSID)
new SID[size + 1];
70 referencedDomainName =
new WCHAR[cchReferencedDomainName + 1];
71 if (!LookupAccountNameW(NULL, (LPCWSTR) name.utf16(), sid, &size, referencedDomainName, &cchReferencedDomainName, &nameuse)) {
72 delete[] referencedDomainName;
84 delete[] referencedDomainName;
87 if (!IsValidSid(sid_))
90 DWORD sidlength = GetLengthSid(sid_);
91 sid = (PSID)
new BYTE[sidlength];
92 if (!CopySid(sidlength, sid, sid_))
102 NetApiBufferFree(userInfo);
107 NetApiBufferFree(servername);
115 NetApiBufferFree(userInfo);
126 DWORD bufferLen = UNLEN + 1;
127 ushort buffer[UNLEN + 1];
129 if (GetUserNameW((LPWSTR) buffer, &bufferLen))
130 d =
new Private(QString::fromUtf16(buffer));
136 DWORD bufferLen = UNLEN + 1;
137 ushort buffer[UNLEN + 1];
140 if (LookupAccountSidW(NULL, uid, (LPWSTR) buffer, &bufferLen, NULL, NULL, &eUse))
141 d =
new Private(QString::fromUtf16(buffer), uid);
145 : d(new Private(name))
150 :d(new Private(
QString::fromLocal8Bit(name)))
169 return EqualSid(d->sid, user.d->sid);
179 return d->userInfo != 0 && d->sid != 0;
184 return d->userInfo && d->userInfo->usri11_priv == USER_PRIV_ADMIN;
189 return (d->userInfo ? QString::fromUtf16((ushort *) d->userInfo->usri11_name) : QString());
192#ifndef KDE_NO_DEPRECATED
195 return (d->userInfo ? QString::fromUtf16((ushort *) d->userInfo->usri11_full_name) : QString());
201 return QDir::fromNativeSeparators(QString::fromLocal8Bit(qgetenv(
"USERPROFILE")));
212 return QString::fromLatin1(
"cmd.exe");
217 QList<KUserGroup> result;
219 Q_FOREACH (
const QString &name,
groupNames()) {
220 result.append(KUserGroup(name));
234 PGROUP_USERS_INFO_0 pGroups = NULL;
235 DWORD dwEntriesRead = 0;
236 DWORD dwTotalEntries = 0;
237 NET_API_STATUS nStatus;
239 nStatus = NetUserGetGroups(NULL, d->userInfo->usri11_name, 0, (LPBYTE *) &pGroups, MAX_PREFERRED_LENGTH, &dwEntriesRead, &dwTotalEntries);
241 if (nStatus == NERR_Success) {
242 for (DWORD i = 0; i < dwEntriesRead; ++i) {
243 result.append(QString::fromUtf16((ushort *) pGroups[i].grui0_name));
248 NetApiBufferFree(pGroups);
262 return QVariant(d->userInfo ? QString::fromUtf16((ushort *) d->userInfo->usri11_full_name) : QString());
271 NET_API_STATUS nStatus;
272 PUSER_INFO_11 pUser = NULL;
273 DWORD dwEntriesRead = 0;
274 DWORD dwTotalEntries = 0;
275 DWORD dwResumeHandle = 0;
280 nStatus = NetUserEnum(NULL, 11, 0, (LPBYTE*) &pUser, 1, &dwEntriesRead, &dwTotalEntries, &dwResumeHandle);
282 if ((nStatus == NERR_Success || nStatus == ERROR_MORE_DATA) && dwEntriesRead > 0) {
283 tmp.d =
new Private(pUser);
286 }
while (nStatus == ERROR_MORE_DATA);
295 NET_API_STATUS nStatus;
296 PUSER_INFO_0 pUsers = NULL;
297 DWORD dwEntriesRead = 0;
298 DWORD dwTotalEntries = 0;
300 nStatus = NetUserEnum(NULL, 0, 0, (LPBYTE*) &pUsers, MAX_PREFERRED_LENGTH, &dwEntriesRead, &dwTotalEntries, NULL);
302 if (nStatus == NERR_Success) {
303 for (DWORD i = 0; i < dwEntriesRead; ++i) {
304 result.append(QString::fromUtf16((ushort *) pUsers[i].usri0_name));
309 NetApiBufferFree(pUsers);
319class KUserGroup::Private :
public KShared
322 PGROUP_INFO_0 groupInfo;
324 Private() : groupInfo(NULL) {}
325 Private(PGROUP_INFO_0 groupInfo_) : groupInfo(groupInfo_) {}
326 Private(
const QString &Name) : groupInfo(NULL)
328 NetGroupGetInfo(NULL, (PCWSTR) Name.utf16(), 0, (PBYTE *) &groupInfo);
334 NetApiBufferFree(groupInfo);
340 : d(new Private(_name))
345 : d(new Private(QLatin1String(_name)))
362 if (d->groupInfo == NULL ||
group.d->groupInfo == NULL) {
365 return wcscmp(d->groupInfo->grpi0_name,
group.d->groupInfo->grpi0_name) == 0;
375 return d->groupInfo != NULL;
380 if(d && d->groupInfo)
381 return QString::fromUtf16((ushort *) d->groupInfo->grpi0_name);
389 Q_FOREACH(
const QString &user,
userNames()) {
390 Result.append(KUser(user));
404 PGROUP_USERS_INFO_0 pUsers = NULL;
405 DWORD dwEntriesRead = 0;
406 DWORD dwTotalEntries = 0;
407 NET_API_STATUS nStatus;
409 nStatus = NetGroupGetUsers(NULL, d->groupInfo->grpi0_name, 0, (LPBYTE *) &pUsers, MAX_PREFERRED_LENGTH, &dwEntriesRead, &dwTotalEntries, NULL);
411 if (nStatus == NERR_Success) {
412 for (DWORD i = 0; i < dwEntriesRead; ++i) {
413 result.append(QString::fromUtf16((ushort *) pUsers[i].grui0_name));
418 NetApiBufferFree(pUsers);
426 QList<KUserGroup> result;
428 NET_API_STATUS nStatus;
429 PGROUP_INFO_0 pGroup=NULL;
430 DWORD dwEntriesRead=0;
431 DWORD dwTotalEntries=0;
432 DWORD dwResumeHandle=0;
437 nStatus = NetGroupEnum(NULL, 0, (LPBYTE*) &pGroup, 1, &dwEntriesRead, &dwTotalEntries, (PDWORD_PTR)&dwResumeHandle);
439 if ((nStatus == NERR_Success || nStatus == ERROR_MORE_DATA) && dwEntriesRead > 0) {
440 tmp.d =
new Private(pGroup);
443 }
while (nStatus == ERROR_MORE_DATA);
452 NET_API_STATUS nStatus;
453 PGROUP_INFO_0 pGroups=NULL;
454 DWORD dwEntriesRead=0;
455 DWORD dwTotalEntries=0;
457 nStatus = NetGroupEnum(NULL, 0, (LPBYTE*) &pGroups, MAX_PREFERRED_LENGTH, &dwEntriesRead, &dwTotalEntries, NULL);
459 if (nStatus == NERR_Success) {
460 for (DWORD i = 0; i < dwEntriesRead; ++i) {
461 result.append(QString::fromUtf16((ushort *) pGroups[i].grpi0_name));
466 NetApiBufferFree(pGroups);
Represents a group on your system.
QList< KUser > users() const
Returns a list of all users of the group.
QString name() const
The name of the group.
KUserGroup(const QString &name)
Create an object from a group name.
bool isValid() const
Returns whether the group is valid.
bool operator!=(const KUserGroup &group) const
Two KUserGroup objects are not equal if their gid()s are not identical.
bool operator==(const KUserGroup &group) const
Two KUserGroup objects are equal if their gid()s are identical.
static QStringList allGroupNames()
Returns a list of all group names on this system.
static QList< KUserGroup > allGroups()
Returns a list of all groups on this system.
QStringList userNames() const
Returns a list of all user login names of the group.
KUserGroup & operator=(const KUserGroup &group)
Copies a group.
Represents a user on your system.
QString faceIconPath() const
The path to the user's face file.
KUser(UIDMode mode=UseEffectiveUID)
Creates an object that contains information about the current user.
QString homeDir() const
The path to the user's home directory.
QStringList groupNames() const
Returns all group names of the user.
QVariant property(UserProperty which) const
Returns an extended property.
QString shell() const
The path to the user's login shell.
static QList< KUser > allUsers()
Returns all users of the system.
bool operator!=(const KUser &user) const
Two KUser objects are not equal if uid() are not identical.
static QStringList allUserNames()
Returns all user names of the system.
QList< KUserGroup > groups() const
Returns all groups of the user.
bool operator==(const KUser &user) const
Two KUser objects are equal if the uid() are identical.
K_UID uid() const
Returns the user id of the user.
QString loginName() const
The login name of the user.
QString fullName() const
The full name of the user.
bool isValid() const
Returns true if the user is valid.
bool isSuperUser() const
Checks whether the user is the super user (root).
KUser & operator=(const KUser &user)
Copies a user.