Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

user.h

Go to the documentation of this file.
00001 #ifndef DV_USER_H 00002 #define DV_USER_H 00003 // $Id: user.h,v 1.7 2002/10/18 13:17:18 dvermeir Exp $ 00004 00005 #include <sys/types.h> // uid_t 00006 #include <stdexcept> // runtime_error 00007 00008 /*! \file 00009 A \ref Dv::Util::User class object represents information about a Unix user, as obtained 00010 by the getpwnam() function. 00011 */ 00012 00013 namespace Dv { 00014 namespace Util { 00015 00016 //! Exception class, thrown by some User function members. 00017 class UserError: public std::runtime_error { 00018 public: 00019 //! Name of this class. 00020 static const std::string NAME; 00021 //! Prepend NAME to message to obtain runtime_error::what(). 00022 UserError(const std::string& message): std::runtime_error(NAME+": "+message) {} 00023 }; 00024 00025 //! A class representing a Unix user. 00026 /*! This class provides a convenient wrapper around information that 00027 is usually obtained using getpwnam(). 00028 */ 00029 class User { 00030 public: 00031 //! Initialize to current user, equivalent with User(getuid()). 00032 User(); 00033 //! Initialize to user with given uid, throw UserError if not found. 00034 User(uid_t uid) throw (UserError); 00035 //! Initialize to user with given user name, throw UserError if not found. 00036 User(const std::string& name) throw (UserError); 00037 00038 //! Retrieve uid of this user. 00039 uid_t uid() const { return uid_; } 00040 //! Retrieve current group id of this user. 00041 gid_t gid() const { return gid_; } 00042 //! Retrieve pathname of home directory of this user. 00043 const std::string& home() const { return home_; } 00044 //! Retrieve pathname of shell program of this user. 00045 const std::string& shell() const { return shell_; } 00046 //! Retrieve user name of this user. 00047 const std::string& name() const { return name_; } 00048 //! Retrieve full name (gecos field) of this user. 00049 const std::string& full_name() const { return full_name_; } 00050 //! Return true iff called by root and password matches user's password. 00051 bool password(const std::string& pass) const; 00052 00053 //! Users are equal if they have the same uid. 00054 bool operator==(const User& u) const { return uid() == u.uid(); } 00055 //! Based on the comparison of the uid's. 00056 bool operator<(const User& u) const { return uid() < u.uid(); } 00057 00058 bool operator>(const User& u) const { return u < *this; } 00059 bool operator!=(const User& u) const { return ! (u == *this); } 00060 bool operator>=(const User& u) const { return ! (*this < u); } 00061 bool operator<=(const User& u) const { return ! (*this > u); } 00062 00063 private: 00064 void* getpwinfo(void*); 00065 00066 std::string name_; 00067 std::string full_name_; 00068 uid_t uid_; 00069 gid_t gid_; 00070 std::string home_; 00071 std::string shell_; 00072 }; 00073 00074 }} 00075 #endif

dvutil-0.13.15 [30 December, 2004]