• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KDECore

kuser_win.cpp

Go to the documentation of this file.
00001 /*
00002  *  KUser - represent a user/account (Windows)
00003  *  Copyright (C) 2007 Bernhard Loos <nhuh.put@web.de>
00004  *
00005  *  This library is free software; you can redistribute it and/or
00006  *  modify it under the terms of the GNU Library General Public
00007  *  License as published by the Free Software Foundation; either
00008  *  version 2 of the License, or (at your option) any later version.
00009  *
00010  *  This library is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  *  Library General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU Library General Public License
00016  *  along with this library; see the file COPYING.LIB.  If not, write to
00017  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  *  Boston, MA 02110-1301, USA.
00019  */
00020 
00021 #include "kuser.h"
00022 
00023 #include <QtCore/QMutableStringListIterator>
00024 #include <QtCore/QDir>
00025 
00026 #include <windows.h>
00027 #include <lm.h>
00028 #include <userenv.h>
00029 
00030 class KUser::Private : public KShared
00031 {
00032     public:
00033         PUSER_INFO_11 userInfo;
00034         PSID sid;
00035 
00036         Private() : userInfo(0), sid(0) {}
00037 
00038         Private(PUSER_INFO_11 userInfo_, PSID sid_ = 0) : userInfo(userInfo_) {}
00039 
00040         Private(const QString &name, PSID sid_ = 0) : userInfo(0), sid(0)
00041         {
00042             if (NetUserGetInfo(NULL, (LPCWSTR) name.utf16(), 11, (LPBYTE *) &userInfo) != NERR_Success)
00043                 goto error;
00044 
00045             if (!sid_) {
00046                 DWORD size = 0;
00047                 SID_NAME_USE nameuse;
00048                 DWORD cchReferencedDomainName = 0;
00049 
00050                 if (!LookupAccountNameW(NULL, (LPCWSTR) name.utf16(), NULL, &size, NULL, &cchReferencedDomainName, &nameuse))
00051                     goto error;
00052                 sid = (PSID) new BYTE[size];
00053                 if (!LookupAccountNameW(NULL, (LPCWSTR) name.utf16(), sid, &size, NULL, &cchReferencedDomainName, &nameuse))
00054                     goto error;
00055             }
00056             else {
00057                 if (!IsValidSid(sid_))
00058                     goto error;
00059 
00060                 DWORD sidlength = GetLengthSid(sid_);
00061                 sid = (PSID) new BYTE[sidlength];
00062                 if (!CopySid(sidlength, sid, sid_))
00063                     goto error;
00064             }
00065 
00066             return;
00067 
00068           error:
00069             delete[] sid;
00070             sid = 0;
00071             if (userInfo) {
00072                 NetApiBufferFree(userInfo);
00073                 userInfo = 0;
00074             }
00075         }
00076 
00077         ~Private()
00078         {
00079             if (userInfo)
00080                 NetApiBufferFree(userInfo);
00081 
00082             delete[] sid;
00083         }
00084 };
00085 
00086 KUser::KUser(UIDMode mode)
00087         : d(0)
00088 {
00089     Q_UNUSED(mode)
00090 
00091     DWORD bufferLen = UNLEN + 1;
00092     ushort buffer[UNLEN + 1];
00093 
00094     if (GetUserNameW((LPWSTR) buffer, &bufferLen))
00095         d = new Private(QString::fromUtf16(buffer));
00096 }
00097 
00098 KUser::KUser(K_UID uid)
00099     : d(0)
00100 {
00101     DWORD bufferLen = UNLEN + 1;
00102     ushort buffer[UNLEN + 1];
00103     SID_NAME_USE eUse;
00104 
00105     if (LookupAccountSidW(NULL, uid, (LPWSTR) buffer, &bufferLen, NULL, NULL, &eUse))
00106         d = new Private(QString::fromUtf16(buffer), uid);
00107 }
00108 
00109 KUser::KUser(const QString &name)
00110     : d(new Private(name))
00111 {
00112 }
00113 
00114 KUser::KUser(const char *name)
00115     :d(new Private(QString::fromLocal8Bit(name)))
00116 {
00117 }
00118 
00119 KUser::KUser(const KUser &user)
00120     : d(user.d)
00121 {
00122 }
00123 
00124 KUser &KUser::operator=(const KUser &user)
00125 {
00126     d = user.d;
00127     return *this;
00128 }
00129 
00130 bool KUser::operator==(const KUser &user) const
00131 {
00132     if (!isValid() || !user.isValid())
00133         return false;
00134     return EqualSid(d->sid, user.d->sid);
00135 }
00136 
00137 bool KUser::operator !=(const KUser &user) const
00138 {
00139     return !operator==(user);
00140 }
00141 
00142 bool KUser::isValid() const
00143 {
00144     return d->userInfo != 0 && d->sid != 0;
00145 }
00146 
00147 bool KUser::isSuperUser() const
00148 {
00149     return d->userInfo && d->userInfo->usri11_priv == USER_PRIV_ADMIN;
00150 }
00151 
00152 QString KUser::loginName() const
00153 {
00154     return (d->userInfo ? QString::fromUtf16((ushort *) d->userInfo->usri11_name) : QString());
00155 }
00156 
00157 QString KUser::fullName() const
00158 {
00159     return (d->userInfo ? QString::fromUtf16((ushort *) d->userInfo->usri11_full_name) : QString());
00160 }
00161 
00162 QString KUser::homeDir() const
00163 {
00164     return QDir::fromNativeSeparators(qgetenv("USERPROFILE"));
00165 }
00166 
00167 QString KUser::faceIconPath() const
00168 {
00169     // FIXME: this needs to be adapted to windows systems (BC changes)
00170     return QString();
00171 }
00172 
00173 QString KUser::shell() const
00174 {
00175     return QString::fromAscii("cmd.exe");
00176 }
00177 
00178 QList<KUserGroup> KUser::groups() const
00179 {
00180     QList<KUserGroup> result;
00181 
00182     QStringList names(groupNames());
00183 
00184     for (QStringList::const_iterator it = names.begin(); it != names.end(); ++it) {
00185         result.append(KUserGroup(*it));
00186     }
00187 
00188     return result;
00189 }
00190 
00191 QStringList KUser::groupNames() const
00192 {
00193     QStringList result;
00194 
00195     if (!d->userInfo) {
00196         return result;
00197     }
00198 
00199     PGROUP_USERS_INFO_0 pGroups = NULL;
00200     DWORD dwEntriesRead = 0;
00201     DWORD dwTotalEntries = 0;
00202     NET_API_STATUS nStatus;
00203 
00204     nStatus = NetUserGetGroups(NULL, d->userInfo->usri11_name, 0, (LPBYTE *) &pGroups, MAX_PREFERRED_LENGTH, &dwEntriesRead, &dwTotalEntries);
00205 
00206     if (nStatus == NERR_Success) {
00207         for (DWORD i = 0; i < dwEntriesRead; ++i) {
00208             result.append(QString::fromUtf16((ushort *) pGroups[i].grui0_name));
00209         }
00210     }
00211 
00212     if (pGroups) {
00213         NetApiBufferFree(pGroups);
00214     }
00215 
00216     return result;
00217 }
00218 
00219 K_UID KUser::uid() const
00220 {
00221     return d->sid;
00222 }
00223 
00224 QVariant KUser::property(UserProperty which) const
00225 {
00226     if (which == FullName) 
00227         return QVariant(d->userInfo ? QString::fromUtf16((ushort *) d->userInfo->usri11_full_name) : QString());
00228 
00229     return QVariant();
00230 }
00231 
00232 QList<KUser> KUser::allUsers()
00233 {
00234     QList<KUser> result;
00235 
00236     NET_API_STATUS nStatus;
00237     PUSER_INFO_11 pUser = NULL;
00238     DWORD dwEntriesRead = 0;
00239     DWORD dwTotalEntries = 0;
00240     DWORD dwResumeHandle = 0;
00241 
00242     KUser tmp;
00243 
00244     do {
00245         nStatus = NetUserEnum(NULL, 11, 0, (LPBYTE*) &pUser, 1, &dwEntriesRead, &dwTotalEntries, &dwResumeHandle);
00246 
00247         if ((nStatus == NERR_Success || nStatus == ERROR_MORE_DATA) && dwEntriesRead > 0) {
00248             tmp.d = new Private(pUser);
00249             result.append(tmp);
00250         }
00251     } while (nStatus == ERROR_MORE_DATA);
00252 
00253     return result;
00254 }
00255 
00256 QStringList KUser::allUserNames()
00257 {
00258     QStringList result;
00259 
00260     NET_API_STATUS nStatus;
00261     PUSER_INFO_0 pUsers = NULL;
00262     DWORD dwEntriesRead = 0;
00263     DWORD dwTotalEntries = 0;
00264 
00265     nStatus = NetUserEnum(NULL, 0, 0, (LPBYTE*) &pUsers, MAX_PREFERRED_LENGTH, &dwEntriesRead, &dwTotalEntries, NULL);
00266 
00267     if (nStatus == NERR_Success) {
00268         for (DWORD i = 0; i < dwEntriesRead; ++i) {
00269             result.append(QString::fromUtf16((ushort *) pUsers[i].usri0_name));
00270         }
00271     }
00272 
00273     if (pUsers) {
00274         NetApiBufferFree(pUsers);
00275     }
00276 
00277     return result;
00278 }
00279 
00280 KUser::~KUser()
00281 {
00282 }
00283 
00284 class KUserGroup::Private : public KShared
00285 {
00286     public:
00287         PGROUP_INFO_0 groupInfo;
00288 
00289         Private() : groupInfo(NULL) {}
00290         Private(PGROUP_INFO_0 groupInfo_) : groupInfo(groupInfo_) {}
00291         Private(const QString &Name) : groupInfo(NULL)
00292         {
00293             NetGroupGetInfo(NULL, (PCWSTR) Name.utf16(), 0, (PBYTE *) &groupInfo);
00294         }
00295 
00296         ~Private()
00297         {
00298             if (groupInfo) {
00299                 NetApiBufferFree(groupInfo);
00300             }
00301         }
00302 };
00303 
00304 KUserGroup::KUserGroup(const QString &_name)
00305     : d(new Private(_name))
00306 {
00307 }
00308 
00309 KUserGroup::KUserGroup(const char *_name)
00310     : d(new Private(QString(_name)))
00311 {
00312 }
00313 
00314 KUserGroup::KUserGroup(const KUserGroup &group)
00315     : d(group.d)
00316 {
00317 }
00318 
00319 KUserGroup& KUserGroup::operator =(const KUserGroup &group)
00320 {
00321     d = group.d;
00322     return *this;
00323 }
00324 
00325 bool KUserGroup::operator==(const KUserGroup &group) const
00326 {
00327     if (d->groupInfo == NULL || group.d->groupInfo == NULL) {
00328         return false;
00329     }
00330     return wcscmp(d->groupInfo->grpi0_name, group.d->groupInfo->grpi0_name) == 0;
00331 }
00332 
00333 bool KUserGroup::operator!=(const KUserGroup &group) const
00334 {
00335     return !operator==(group);
00336 }
00337 
00338 bool KUserGroup::isValid() const
00339 {
00340     return d->groupInfo != NULL;
00341 }
00342 
00343 QString KUserGroup::name() const
00344 {
00345     return QString::fromUtf16((ushort *) d->groupInfo->grpi0_name);
00346 }
00347 
00348 QList<KUser> KUserGroup::users() const
00349 {
00350     QList<KUser> Result;
00351 
00352     QStringList Names(userNames());
00353 
00354     for (QStringList::const_iterator it = Names.begin(); it != Names.end(); ++it) {
00355         Result.append(KUser(*it));
00356     }
00357 
00358     return Result;
00359 }
00360 
00361 QStringList KUserGroup::userNames() const
00362 {
00363     QStringList result;
00364 
00365     if (!d->groupInfo) {
00366         return result;
00367     }
00368 
00369     PGROUP_USERS_INFO_0 pUsers = NULL;
00370     DWORD dwEntriesRead = 0;
00371     DWORD dwTotalEntries = 0;
00372     NET_API_STATUS nStatus;
00373 
00374     nStatus = NetGroupGetUsers(NULL, d->groupInfo->grpi0_name, 0, (LPBYTE *) &pUsers, MAX_PREFERRED_LENGTH, &dwEntriesRead, &dwTotalEntries, NULL);
00375 
00376     if (nStatus == NERR_Success) {
00377         for (DWORD i = 0; i < dwEntriesRead; ++i) {
00378             result.append(QString::fromUtf16((ushort *) pUsers[i].grui0_name));
00379         }
00380     }
00381 
00382     if (pUsers) {
00383         NetApiBufferFree(pUsers);
00384     }
00385 
00386     return result;
00387 }
00388 
00389 QList<KUserGroup> KUserGroup::allGroups()
00390 {
00391     QList<KUserGroup> result;
00392 
00393     NET_API_STATUS nStatus;
00394     PGROUP_INFO_0 pGroup=NULL;
00395     DWORD dwEntriesRead=0;
00396     DWORD dwTotalEntries=0;
00397     DWORD dwResumeHandle=0;
00398 
00399     KUserGroup tmp("");
00400 
00401     do {
00402         nStatus = NetGroupEnum(NULL, 0, (LPBYTE*) &pGroup, 1, &dwEntriesRead, &dwTotalEntries, &dwResumeHandle);
00403 
00404         if ((nStatus == NERR_Success || nStatus == ERROR_MORE_DATA) && dwEntriesRead > 0) {
00405             tmp.d = new Private(pGroup);
00406             result.append(tmp);
00407         }
00408     } while (nStatus == ERROR_MORE_DATA);
00409 
00410     return result;
00411 }
00412 
00413 QStringList KUserGroup::allGroupNames()
00414 {
00415     QStringList result;
00416 
00417     NET_API_STATUS nStatus;
00418     PGROUP_INFO_0 pGroups=NULL;
00419     DWORD dwEntriesRead=0;
00420     DWORD dwTotalEntries=0;
00421 
00422     nStatus = NetGroupEnum(NULL, 0, (LPBYTE*) &pGroups, MAX_PREFERRED_LENGTH, &dwEntriesRead, &dwTotalEntries, NULL);
00423 
00424     if (nStatus == NERR_Success) {
00425         for (DWORD i = 0; i < dwEntriesRead; ++i) {
00426             result.append(QString::fromUtf16((ushort *) pGroups[i].grpi0_name));
00427         }
00428     }
00429 
00430     if (pGroups) {
00431         NetApiBufferFree(pGroups);
00432     }
00433 
00434     return result;
00435 }
00436 
00437 KUserGroup::~KUserGroup()
00438 {
00439 }

KDECore

Skip menu "KDECore"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • KIO
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal