KanoopCommonQt 2.1.1
Kanoop foundational Qt utility library
Loading...
Searching...
No Matches
userutil.h
1/**
2 * UserUtil
3 *
4 * Some static helper methods for Linux users / groups
5 *
6 * Stephen Punak, July 08 2019
7 */
8#ifndef USERUTIL_H
9#define USERUTIL_H
10
11#ifndef __WIN32
12#include <pwd.h>
13#include <grp.h>
14#endif
15
16#include <QString>
17#include "kanoopcommon.h"
18
19/**
20 * @brief Static helper methods for querying Unix user and group information.
21 *
22 * POSIX-only methods are guarded by `#ifndef __WIN32` and are unavailable on Windows.
23 */
24class KANOOP_EXPORT UserUtil
25{
26public:
27#ifndef __WIN32
28 /**
29 * @brief Look up the numeric GID for a group name.
30 * @param name Group name string
31 * @return Group ID, or (gid_t)-1 if not found
32 */
33 static gid_t gidFromName(const QString& name);
34
35 /**
36 * @brief Look up the numeric UID for a user name.
37 * @param name User name string
38 * @return User ID, or (uid_t)-1 if not found
39 */
40 static uid_t uidFromName(const QString& name);
41
42 /**
43 * @brief Return the group name for a numeric GID.
44 * @param gid Group ID to look up
45 * @return Group name string, or empty string if not found
46 */
47 static QString nameFromGid(gid_t gid);
48
49 /**
50 * @brief Return the user name for a numeric UID.
51 * @param uid User ID to look up
52 * @return User name string, or empty string if not found
53 */
54 static QString nameFromUid(uid_t uid);
55
56 /**
57 * @brief Return the UID of the process owner.
58 * @return UID of the currently running process
59 */
60 static uid_t currentUser();
61
62 /**
63 * @brief Return the full (display) name of the process owner.
64 * @return GECOS full name string, or empty string on error
65 */
66 static QString currentUserFullName();
67
68 /**
69 * @brief Test whether a user is a member of a group.
70 * @param uid User ID to check
71 * @param gid Group ID to check membership in
72 * @return true if the user is a member of the group
73 */
74 static bool isUserMemberOfGroup(uid_t uid, gid_t gid);
75#endif
76
77 /**
78 * @brief Return the login name of the current user (cross-platform).
79 * @return User name string
80 */
81 static QString currentUserName();
82};
83
84#endif // USERUTIL_H
UserUtil.
Definition userutil.h:25
static QString nameFromUid(uid_t uid)
Return the user name for a numeric UID.
static QString nameFromGid(gid_t gid)
Return the group name for a numeric GID.
static QString currentUserName()
Return the login name of the current user (cross-platform).
static uid_t uidFromName(const QString &name)
Look up the numeric UID for a user name.
static bool isUserMemberOfGroup(uid_t uid, gid_t gid)
Test whether a user is a member of a group.
static QString currentUserFullName()
Return the full (display) name of the process owner.
static uid_t currentUser()
Return the UID of the process owner.
static gid_t gidFromName(const QString &name)
Look up the numeric GID for a group name.