[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-core-devel
Subject:    [PATCH] KUser 3rd try
From:       Jan =?iso-8859-15?q?Sch=E4fer?= <JanSchaefer () gmx ! de>
Date:       2004-06-10 13:05:53
Message-ID: 200406101505.55646.JanSchaefer () gmx ! de
[Download RAW message or body]

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

I further improved the patch as follows:

- - Made ctors explicit
- - Added two static methods that return QStringList instead of a QValueList
- - KUserGroup::users now returns const QValueList<KUser>&

If nobody objects I'll commit.

Greetings,

	Jan
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2-rc1-SuSE (GNU/Linux)

iD8DBQFAyFyzyXPDOD1/5gcRAln2AJ9ENERQ5lOfjf62RDBt8TeHOUTRxACgkkwf
kMeVRfzPPW/99O8R43DivJY=
=kTuu
-----END PGP SIGNATURE-----

["kuser.patch" (text/x-diff)]

? kuser.patch
Index: kuser.cpp
===================================================================
RCS file: /home/kde/kdelibs/kdecore/kuser.cpp,v
retrieving revision 1.7
diff -u -p -r1.7 kuser.cpp
--- kuser.cpp	31 Oct 2003 17:23:56 -0000	1.7
+++ kuser.cpp	10 Jun 2004 12:41:58 -0000
@@ -22,13 +22,15 @@
 #include <kuser.h>
 
 #include "kstringhandler.h"
-
+#include <qvaluelist.h>
 #include <qstringlist.h>
 
 #include <sys/types.h>
 #include <pwd.h>
 #include <unistd.h>
 #include <stdlib.h>
+#include <grp.h>
+
 
 class KUserPrivate : public KShared
 {
@@ -89,6 +91,21 @@ KUser::KUser(const char *name) {
 	fillName( name );
 }
 
+KUser::KUser(struct passwd *p) {
+    fillPasswd(p);
+}
+
+KUser::KUser(const KUser & user) 
+  : d(user.d) 
+{
+}
+
+KUser& KUser::operator =(const KUser& user) 
+{
+  d = user.d;
+  return *this;
+}
+
 bool KUser::operator ==(const KUser& user) const {
     if (isValid() != user.isValid())
 	return false;
@@ -196,5 +213,189 @@ QString KUser::shell() const {
 		return QString::null;
 }
 
+QValueList<KUserGroup> KUser::groups() const {
+  QValueList<KUserGroup> result;
+  QValueList<KUserGroup> allGroups = KUserGroup::allGroups();
+  QValueList<KUserGroup>::iterator it;
+  for ( it = allGroups.begin(); it != allGroups.end(); ++it ) {
+    KUserGroup group = (*it);
+    QValueList<KUser> users = group.users();
+    if ( users.find( *this ) != users.end()) {
+       result.append(*it);
+    }
+  }
+  return result;
+}
+
+QValueList<KUser> KUser::allUsers() {
+  QValueList<KUser> result;
+
+  struct passwd* p;
+
+  while ((p = getpwent()))  {
+    result.append(KUser(p));
+  }
+
+  endpwent();
+
+  return result;
+}
+
+QStringList KUser::allUserNames() {
+  QStringList result;
+
+  struct passwd* p;
+
+  while ((p = getpwent()))  {
+    result.append(QString::fromLocal8Bit(p->pw_name));
+  }
+
+  endpwent();
+  return result;
+}
+
+
 KUser::~KUser() {
 }
+
+class KUserGroupPrivate : public KShared
+{
+public:
+  bool valid;
+  long gid;
+  QString name;
+  QValueList<KUser> users;
+  
+  KUserGroupPrivate() : valid(false) {}
+  
+  KUserGroupPrivate(long _gid, 
+                const QString & _name, 
+                const QValueList<KUser> & _users):
+    valid(true), 
+    gid(_gid), 
+    name(_name),
+    users(_users) {}
+};
+
+KUserGroup::KUserGroup(KUser::UIDMode mode) {
+  KUser user(mode);
+  fillGroup(getgrgid(user.gid()));
+}
+
+KUserGroup::KUserGroup(long gid) {
+  fillGroup(getgrgid(gid));
+}
+
+KUserGroup::KUserGroup(const QString& name) {
+  fillName(name.local8Bit().data());
+}
+
+KUserGroup::KUserGroup(const char *name) {
+  fillName(name);
+}
+
+KUserGroup::KUserGroup(struct group *g) {
+  fillGroup(g);
+}
+
+
+KUserGroup::KUserGroup(const KUserGroup & group) 
+  : d(group.d)
+{
+}
+
+KUserGroup& KUserGroup::operator =(const KUserGroup& group) {
+  d = group.d;
+  return *this;
+}
+
+bool KUserGroup::operator ==(const KUserGroup& group) const {
+  if (isValid() != group.isValid())
+    return false;
+  if (isValid())
+    return gid() == group.gid();
+  else
+    return true;
+}
+
+bool KUserGroup::operator !=(const KUserGroup& user) const {
+  return !operator ==(user);
+}
+
+void KUserGroup::fillName(const char *name) {
+  fillGroup(name ? ::getgrnam( name ) : 0);
+}
+
+void KUserGroup::fillGroup(struct group *p) {
+  if (!p) {
+    d = new KUserGroupPrivate();
+    return;
+  }
+  
+  QString name = KStringHandler::from8Bit(p->gr_name); 
+  QValueList<KUser> users;
+  
+  char **user = p->gr_mem;  
+  for ( ; *user; user++) {
+    KUser kUser(QString::fromLocal8Bit(*user));
+    users.append(kUser);
+  }
+  
+  d = new KUserGroupPrivate(p->gr_gid,
+            QString::fromLocal8Bit(p->gr_name),
+            users);  
+
+}
+
+bool KUserGroup::isValid() const {
+  return d->valid;
+}
+
+long KUserGroup::gid() const {
+  if (d->valid)
+    return d->gid;
+  else
+    return -1;
+}
+
+QString KUserGroup::name() const {
+  if (d->valid)
+    return d->name;
+  else
+    return QString::null;
+}
+
+const QValueList<KUser>& KUserGroup::users() const {
+  return d->users;
+}
+
+QValueList<KUserGroup> KUserGroup::allGroups() {
+  QValueList<KUserGroup> result;
+  
+  struct group* g;
+  while ((g = getgrent()))  {
+     result.append(KUserGroup(g));
+  }
+
+  endgrent();
+
+  return result;
+}
+
+QStringList KUserGroup::allGroupNames() {
+  QStringList result;
+  
+  struct group* g;
+  while ((g = getgrent()))  {
+     result.append(QString::fromLocal8Bit(g->gr_name));
+  }
+
+  endgrent();
+
+  return result;
+}
+
+
+KUserGroup::~KUserGroup() {
+}
+
Index: kuser.h
===================================================================
RCS file: /home/kde/kdelibs/kdecore/kuser.h,v
retrieving revision 1.7
diff -u -p -r1.7 kuser.h
--- kuser.h	12 Mar 2004 17:04:21 -0000	1.7
+++ kuser.h	10 Jun 2004 12:41:58 -0000
@@ -2,6 +2,7 @@
  *  KUser - represent a user/account
  *  Copyright (C) 2002-2003 Tim Jansen <tim@tjansen.de>
  *  Copyright (C) 2003 Oswald Buddenhagen <ossi@kde.org>
+ *  Copyright (C) 2004 Jan Schaefer <j_schaef@informatik.uni-kl.de>
  *
  *  $Id: kuser.h,v 1.7 2004/03/12 17:04:21 goossens Exp $
  *
@@ -25,10 +26,12 @@
 
 #include "ksharedptr.h"
 
+class KUserGroup;
 class QString;
-
+class QStringList;
 class KUserPrivate;
 struct passwd;
+template <class T> class QValueList;
 
 /**
  * @short Represents a user on your system
@@ -65,7 +68,7 @@ public:
   /**
    * Creates an object for the user with the given user id.
    * If the user does not exist isValid() will return false.
-   * @param uid       	the user id
+   * @param uid the user id
    */
   KUser(long uid);
 
@@ -94,6 +97,19 @@ public:
   KUser(struct passwd *p);
 
   /**
+   * Creates an object from another KUser object
+   * @param user the user to create the new object from
+   */
+  KUser(const KUser & user);
+  
+  /**
+   * Copies a user
+   * @param user the user to copy
+   * @return this object
+   */
+  KUser& operator =(const KUser& user);
+  
+  /**
    * Two KUser objects are equal if isValid() is true 
    * and the uid() are identical.
    */
@@ -179,14 +195,164 @@ public:
   QString shell() const;
 
   /**
+   * Returns all groups of the user
+   * @return all groups of the user
+   */
+  QValueList<KUserGroup> groups() const;
+  
+  /**
    * Destructor.
    */
   ~KUser();
 
+  /** 
+   * Returns all users of the system.
+   * @return all users of the system.
+   */
+  static QValueList<KUser> allUsers();
+
+  /** 
+   * Returns all user names of the system.
+   * @return all user names of the system.
+   */
+  static QStringList allUserNames();
+
 private:
   KSharedPtr<KUserPrivate> d;
   void fillPasswd(struct passwd* p);
   void fillName(const char* name);
 };
 
+class KUserGroupPrivate;
+
+struct group;
+
+/**
+ * @short Represents a group on your system
+ *
+ * This class represents a group on your system. You can either get
+ * information about the group of the current user, of fetch information about
+ * a group on the system. Instances of this class will be explicitly shared,
+ * so copying objects is very cheap and you can safely pass objects by value.
+ *
+ * @author Jan Schaefer <j_schaef@informatik.uni-kl.de>
+ * @since 3.3
+ */
+class KUserGroup {
+
+public:
+
+  /**
+   * Create an object from the group of the current user.
+   * @param mode if @ref UseEffectiveUID is passed the effective user will be used.
+   *        If @ref UseRealUserID is passed the real user will be used. 
+   *        The RUID will be different than the EUID in setuid programs; in  
+   *        such a case use the EUID for checking permissions, and the RUID 
+   *        for displaying information about the group.
+   */
+  explicit KUserGroup(KUser::UIDMode mode = KUser::UseEffectiveUID);
+  
+  /**
+   * Create an object from a group id.
+   * If the group does not exist, isValid() will return false.
+   * @param gid the group id
+   */
+  explicit KUserGroup(long gid);
+  
+  /**
+   * Create an object from a group name.
+   * If the group does not exist, isValid() will return false.
+   * @param name the name of the group
+   */
+  explicit KUserGroup(const QString& name);
+  
+  /**
+   * Create an object from a group name.
+   * If the group does not exist, isValid() will return false.
+   * @param name the name of the group
+   */
+  explicit KUserGroup(const char *name);
+  
+  /**
+   * Creates an object from a group structure.
+   * If the pointer is null, isValid() will return false.
+   * @param g the group structure to create the group from.
+   */
+  explicit KUserGroup(struct group *g);
+  
+  /**
+   * Creates a new KUserGroup instance from another KUserGroup object
+   * @param group the KUserGroup to copy
+   */
+  KUserGroup(const KUserGroup & group);
+  
+  /**
+   * Copies a group
+   * @param group the group that should be copied
+   * @return this group
+   */
+  KUserGroup& operator =(const KUserGroup& group);
+  
+  /**
+   * Two KUserGroup objects are equal if isValid() is true
+   * and gid() are identical
+   * @return true if the groups are identical
+   */
+  bool operator ==(const KUserGroup& group) const;
+  
+  /**
+   * Two KUserGroup objects are not equal if either 
+   * isValid() is not true or gid() are not identical
+   * @return true if the groups are not identical
+   */
+  bool operator !=(const KUserGroup& group) const;
+  
+  /**
+   * Returns wether the group is valid.
+   * A KUserGroup object can be invalid if it is 
+   * created with a non-existing gid or name.
+   * @return true if the group is valid
+   */
+  bool isValid() const;
+  
+  /**
+   * Returns the group id of the group.
+   * @return the group id of the group or -1 if the group is invalid
+   */
+  long gid() const;
+  
+  /**
+   * The name of the group.
+   * @return the name of the group
+   */
+  QString name() const;
+  
+  /**
+   * Returns a list of all users of the group.
+   * @return a list of all users of the group
+   */
+  const QValueList<KUser>& users() const;
+  
+  /**
+   * Destructor.
+   */
+  ~KUserGroup(); 
+  
+  /**
+   * Returns a list of all groups on this system
+   */
+  static QValueList<KUserGroup> allGroups();
+  
+  /**
+   * Returns a list of all group names on this system
+   */
+  static QStringList allGroupNames();
+  
+private:
+  KSharedPtr<KUserGroupPrivate> d;
+  void fillGroup(struct group* g);
+  void fillName(const char* name);
+};
+
+
 #endif


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic