00001 #ifndef DV_USER_H 00002 #define DV_USER_H 00003 // $Id: user.h,v 1.8 2008/03/12 17:45:16 dvermeir Exp $ 00004 00005 #include <sys/types.h> // uid_t 00006 #include <stdexcept> // runtime_error 00007 00008 namespace Dv { 00009 namespace Util { 00010 00011 /** A class representing a Unix user. 00012 * This class provides a convenient wrapper around information that is usually 00013 * obtained using getpwnam(). 00014 */ 00015 class User { 00016 public: 00017 /** Initialize to current user, equivalent with User(getuid()). */ 00018 User(); 00019 /** Initialize to user with given uid. 00020 * @exception std::runtime_error if not found 00021 */ 00022 User(uid_t uid) throw (std::runtime_error); 00023 /** Initialize to user with given user name. 00024 * @exception std::runtime_error if not found 00025 */ 00026 User(const std::string& name) throw (std::runtime_error); 00027 00028 /** Retrieve uid of this user. */ 00029 uid_t uid() const { return uid_; } 00030 /** Retrieve current group id of this user. */ 00031 gid_t gid() const { return gid_; } 00032 /** Retrieve pathname of home directory of this user. */ 00033 const std::string& home() const { return home_; } 00034 /** Retrieve pathname of shell program of this user. */ 00035 const std::string& shell() const { return shell_; } 00036 /** Retrieve user name of this user. */ 00037 const std::string& name() const { return name_; } 00038 /** Retrieve full name (gecos field) of this user. */ 00039 const std::string& full_name() const { return full_name_; } 00040 /** Return true iff called by root and password matches user's password. */ 00041 bool password(const std::string& pass) const; 00042 00043 /** Users are equal if they have the same uid. */ 00044 bool operator==(const User& u) const { return uid() == u.uid(); } 00045 /** Based on the comparison of the uid's. */ 00046 bool operator<(const User& u) const { return uid() < u.uid(); } 00047 bool operator>(const User& u) const { return u < *this; } 00048 bool operator!=(const User& u) const { return ! (u == *this); } 00049 bool operator>=(const User& u) const { return ! (*this < u); } 00050 bool operator<=(const User& u) const { return ! (*this > u); } 00051 private: 00052 void* getpwinfo(void*); 00053 std::string name_; 00054 std::string full_name_; 00055 uid_t uid_; 00056 gid_t gid_; 00057 std::string home_; 00058 std::string shell_; 00059 }; 00060 } 00061 } 00062 00063 #endif
dvutil-1.0.10 | [ 5 December, 2009] |