KCoreAddons

kuser.h
1 /*
2  KUser - represent a user/account
3 
4  SPDX-FileCopyrightText: 2002-2003 Tim Jansen <[email protected]>
5  SPDX-FileCopyrightText: 2003 Oswald Buddenhagen <[email protected]>
6  SPDX-FileCopyrightText: 2004 Jan Schaefer <[email protected]>
7  SPDX-FileCopyrightText: 2014 Alex Richardson <[email protected]>
8 
9  SPDX-License-Identifier: LGPL-2.0-or-later
10 */
11 #ifndef KUSER_H
12 #define KUSER_H
13 
14 #include <kcoreaddons_export.h>
15 
16 #include <QSharedDataPointer>
17 #include <QVariant>
18 #include <qcontainerfwd.h>
19 
20 class KUserGroup;
21 class QString;
22 class QStringList;
23 
24 #ifdef Q_OS_WIN
25 typedef void *K_UID;
26 typedef void *K_GID;
27 struct WindowsSIDWrapper;
28 #else
29 #include <sys/types.h>
30 typedef uid_t K_UID;
31 typedef gid_t K_GID;
32 struct passwd;
33 struct group;
34 #endif
35 
36 // The following is to avoid compile errors using msvc, and it is done
37 // using a common #define to avoid helpful people accidentally cleaning this
38 // not quite pretty thing and breaking it for people on windows.
39 // See https://git.reviewboard.kde.org/r/127598/ for details
40 #define KCOREADDONS_UINT_MAX (std::numeric_limits<uint>::max)()
41 
58 template<typename T>
59 struct KCOREADDONS_EXPORT KUserOrGroupId {
60  typedef T NativeType;
61 protected:
67  explicit KUserOrGroupId(NativeType nativeId);
69  KUserOrGroupId(const KUserOrGroupId<T> &other);
70  KUserOrGroupId &operator=(const KUserOrGroupId<T> &other);
71  ~KUserOrGroupId();
72 public:
77  bool isValid() const;
84  NativeType nativeId() const;
89  QString toString() const;
91  bool operator==(const KUserOrGroupId &other) const;
93  bool operator!=(const KUserOrGroupId &other) const;
94 private:
95 #ifdef Q_OS_WIN
97 #else
98  NativeType id;
99 #endif
100 };
101 
102 #ifdef Q_OS_WIN
105 template<> KUserOrGroupId<void *>::KUserOrGroupId(KUserOrGroupId::NativeType nativeId);
108 template<> bool KUserOrGroupId<void *>::isValid() const;
109 template<> KUserOrGroupId<void *>::NativeType KUserOrGroupId<void *>::nativeId() const;
110 template<> QString KUserOrGroupId<void *>::toString() const;
111 template<> bool KUserOrGroupId<void *>::operator==(const KUserOrGroupId &other) const;
112 template<> bool KUserOrGroupId<void *>::operator!=(const KUserOrGroupId &other) const;
113 #endif
114 
120 struct KCOREADDONS_EXPORT KUserId : public KUserOrGroupId<K_UID> {
122  KUserId() {}
124  explicit KUserId(K_UID uid) : KUserOrGroupId(uid) {}
125  KUserId(const KUserId &other) : KUserOrGroupId(other) {}
126  ~KUserId() {}
130  static KUserId fromName(const QString &name);
132  static KUserId currentUserId();
137  static KUserId currentEffectiveUserId();
138 };
139 
145 struct KCOREADDONS_EXPORT KGroupId : public KUserOrGroupId<K_GID> {
147  KGroupId() {}
149  explicit KGroupId(K_GID gid) : KUserOrGroupId(gid) {}
150  KGroupId(const KGroupId &other) : KUserOrGroupId(other) {}
151  ~KGroupId() {}
155  static KGroupId fromName(const QString &name);
157  static KGroupId currentGroupId();
162  static KGroupId currentEffectiveGroupId();
163 };
164 
165 #ifndef Q_OS_WIN
166 inline uint qHash(const KUserId &id, uint seed = 0)
167 {
168  return qHash(id.nativeId(), seed);
169 }
170 inline uint qHash(const KGroupId &id, uint seed = 0)
171 {
172  return qHash(id.nativeId(), seed);
173 }
174 #else
175 // can't be inline on windows, because we would need to include windows.h (which can break code)
176 KCOREADDONS_EXPORT uint qHash(const KUserId &id, uint seed = 0);
177 KCOREADDONS_EXPORT uint qHash(const KGroupId &id, uint seed = 0);
178 #endif
179 
192 class KCOREADDONS_EXPORT KUser
193 {
194 
195 public:
196 
197  enum UIDMode {
199  UseRealUserID
200  };
201 
215  explicit KUser(UIDMode mode = UseEffectiveUID);
216 
222  explicit KUser(K_UID uid);
223 
229  explicit KUser(KUserId uid);
230 
237  explicit KUser(const QString &name);
238 
245  explicit KUser(const char *name);
246 
247 #ifndef Q_OS_WIN
248 
254  explicit KUser(const passwd *p);
255 #endif
256 
261  KUser(const KUser &user);
262 
268  KUser &operator =(const KUser &user);
269 
274  bool operator ==(const KUser &user) const;
275 
280  bool operator !=(const KUser &user) const;
281 
287  bool isValid() const;
288 
290  KUserId userId() const;
291 
293  KGroupId groupId() const;
294 
295 #if KCOREADDONS_ENABLE_DEPRECATED_SINCE(5, 0)
296 
301  KCOREADDONS_DEPRECATED_VERSION(5, 0, "Use KUser::groupId()")
302  K_GID gid() const
303  {
304  return groupId().nativeId();
305  }
306 #endif
307 
312  bool isSuperUser() const;
313 
318  QString loginName() const;
319 
320 #if KCOREADDONS_ENABLE_DEPRECATED_SINCE(5, 0)
321 
326  KCOREADDONS_DEPRECATED_VERSION(5, 0, "Use KUser::property(KUser::FullName).toString()")
327  QString fullName() const
328  {
329  return property(FullName).toString();
330  }
336  KCOREADDONS_DEPRECATED_VERSION(5, 0, "Use KUser::userId().nativeId()")
337  K_UID uid() const
338  {
339  return userId().nativeId();
340  }
341 #endif
342 
348  QString homeDir() const;
349 
355  QString faceIconPath() const;
356 
362  QString shell() const;
363 
368  QList<KUserGroup> groups(uint maxCount = KCOREADDONS_UINT_MAX) const;
369 
374  QStringList groupNames(uint maxCount = KCOREADDONS_UINT_MAX) const;
375 
376  enum UserProperty { FullName, RoomNumber, WorkPhone, HomePhone };
377 
386  QVariant property(UserProperty which) const;
387 
391  ~KUser();
392 
397  static QList<KUser> allUsers(uint maxCount = KCOREADDONS_UINT_MAX);
398 
403  static QStringList allUserNames(uint maxCount = KCOREADDONS_UINT_MAX);
404 
405 private:
406  class Private;
408 };
409 
422 class KCOREADDONS_EXPORT KUserGroup
423 {
424 
425 public:
426 
432  explicit KUserGroup(const QString &name);
433 
439  explicit KUserGroup(const char *name);
440 
446  explicit KUserGroup(KGroupId gid);
447 
459 
465  explicit KUserGroup(K_GID gid);
466 
467 #ifndef Q_OS_WIN
468 
473  explicit KUserGroup(const group *g);
474 #endif
475 
480  KUserGroup(const KUserGroup &group);
481 
487  KUserGroup &operator =(const KUserGroup &group);
488 
494  bool operator ==(const KUserGroup &group) const;
495 
501  bool operator !=(const KUserGroup &group) const;
502 
509  bool isValid() const;
510 
511 #if KCOREADDONS_ENABLE_DEPRECATED_SINCE(5, 0)
512 
517  KCOREADDONS_DEPRECATED_VERSION(5, 0, "Use KUserGroup::groupId().nativeId()")
518  K_GID gid() const
519  {
520  return groupId().nativeId();
521  }
522 #endif
523 
525  KGroupId groupId() const;
526 
531  QString name() const;
532 
537  QList<KUser> users(uint maxCount = KCOREADDONS_UINT_MAX) const;
538 
543  QStringList userNames(uint maxCount = KCOREADDONS_UINT_MAX) const;
544 
548  ~KUserGroup();
549 
554  static QList<KUserGroup> allGroups(uint maxCount = KCOREADDONS_UINT_MAX);
555 
560  static QStringList allGroupNames(uint maxCount = KCOREADDONS_UINT_MAX);
561 
562 private:
563  class Private;
565 };
566 
567 #if !defined(Q_OS_WIN)
568 // inline UNIX implementation of KUserOrGroupId
569 template<typename T>
570 inline bool KUserOrGroupId<T>::isValid() const
571 {
572  return id != NativeType(-1);
573 }
574 template<typename T>
575 inline bool KUserOrGroupId<T>::operator==(const KUserOrGroupId<T> &other) const
576 {
577  return id == other.id;
578 }
579 template<typename T>
580 inline bool KUserOrGroupId<T>::operator!=(const KUserOrGroupId<T> &other) const
581 {
582  return id != other.id;
583 }
584 template<typename T>
585 inline typename KUserOrGroupId<T>::NativeType KUserOrGroupId<T>::nativeId() const
586 {
587  return id;
588 }
589 template<typename T>
591 {
592  return QString::number(id);
593 }
594 template<typename T>
596  : id(-1)
597 {
598 }
599 template<typename T>
600 inline KUserOrGroupId<T>::KUserOrGroupId(KUserOrGroupId<T>::NativeType nativeId)
601  : id(nativeId)
602 {
603 }
604 template<typename T>
606  : id(other.id)
607 {
608 }
609 template<typename T>
611 {
612  id = other.id;
613  return *this;
614 }
615 template<typename T>
617 {
618 }
619 #endif // !defined(Q_OS_WIN)
620 
621 inline bool KUser::operator!=(const KUser &other) const
622 {
623  return !operator==(other);
624 }
625 
626 inline bool KUserGroup::operator!=(const KUserGroup &other) const
627 {
628  return !operator==(other);
629 }
630 
631 #endif
KUserId()
Creates an invalid KUserId.
Definition: kuser.h:122
Use the effective user id.
Definition: kuser.h:198
NativeType nativeId() const
Definition: kuser.h:585
Represents a group on your system.
Definition: kuser.h:422
QString toString() const
Definition: kuser.h:590
bool isValid() const
Definition: kuser.h:570
bool operator==(const KUserOrGroupId &other) const
Definition: kuser.h:575
bool operator!=(const KUser &user) const
Two KUser objects are not equal if userId() are not identical.
Definition: kuser.h:621
Represents a user on your system.
Definition: kuser.h:192
KUserOrGroupId()
Creates an invalid KUserOrGroupId.
Definition: kuser.h:595
QString number(int n, int base)
KCALENDARCORE_EXPORT uint qHash(const KCalendarCore::Period &key)
UIDMode
Definition: kuser.h:197
KUserId(K_UID uid)
Creates an KUserId from the native user ID type.
Definition: kuser.h:124
A platform independent group ID.
Definition: kuser.h:145
KGroupId()
Creates an invalid KGroupId.
Definition: kuser.h:147
bool operator!=(const KUserOrGroupId &other) const
Definition: kuser.h:580
KGroupId(K_GID gid)
Creates an KGroupId from the native group ID type.
Definition: kuser.h:149
bool operator!=(const KUserGroup &group) const
Two KUserGroup objects are not equal if their gid()s are not identical.
Definition: kuser.h:626
A platform independent user ID.
Definition: kuser.h:120
A platform independent user or group ID.
Definition: kuser.h:59
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Fri Aug 7 2020 23:00:04 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.