diff -r --unified subuser/subuser.install subuser-new/subuser.install
--- subuser/subuser.install	2009-04-24 04:06:43.000000000 +0200
+++ subuser-new/subuser.install	2010-04-29 23:19:54.813759040 +0200
@@ -53,7 +53,13 @@
  * Implementation of hook_uninstall().
  */
 function subuser_uninstall() {
+  global $conf;
   drupal_uninstall_schema('subuser');
 
   variable_del('subuser_roles');
+  foreach (array_keys($conf) AS $variable) {
+    if (substr($variable, 0, 14) == 'subuser_roles_') {
+      variable_del($variable);
+    }
+  }
 }
diff -r --unified subuser/subuser.module subuser-new/subuser.module
--- subuser/subuser.module	2009-10-15 09:45:29.000000000 +0200
+++ subuser-new/subuser.module	2010-04-29 23:39:17.933759139 +0200
@@ -217,15 +217,52 @@
  */
 function subuser_user($op, &$edit, &$account, $category = NULL) {
   switch ($op) {
+    case 'delete':
+      db_query('DELETE FROM {user_relationship} WHERE uid = %d', $account->uid);
+      break;
+    case 'update':
+      db_query('DELETE FROM {user_relationship} WHERE uid = %d', $account->uid);
+      // don't break here
     case 'insert':
-      if (isset($edit['origin']) && $edit['origin'] == 'subuser') {
-        db_query('INSERT INTO {user_relationship} (parent_id, uid)
-                  VALUES (%d, %d)', $edit['parent_user'], $account->uid);
+      if (!empty($edit['parent_id'])) {
+        if ($category == 'account' || (isset($edit['origin']) && $edit['origin'] == 'subuser')) {
+          if (subuser_is_valid_parent_id($account, $edit['parent_id'])) {
+            db_query('INSERT INTO {user_relationship} (parent_id, uid)
+                      VALUES (%d, %d)', $edit['parent_id'], $account->uid);
+          }
+        }
+      }
+      // don't break here
+    case 'load':
+      // Add a list of all parent ids and the direct parent id in parent_id
+      $account->parents = array();
+      $uid = $account->uid;
+      while ($parent = db_fetch_object(db_query('SELECT parent_id
+                                                FROM {user_relationship}
+                                                WHERE uid = %d', $uid))) {
+        $uid = $parent->parent_id;
+        if (in_array($uid, $account->parents)) {
+          break; // Loop detected
+        }
+
+        $account->parents[] = $uid;
+      }
+
+      if (empty($account->parents)) {
+        $account->parent_id = null;
+      } else {
+        $account->parent_id = $account->parents[0];
       }
       break;
-    case 'delete':
-      db_query('DELETE FROM {user_relationship} WHERE uid = %d', $account->uid);
+    
+    case 'submit':
+      if ($category == "account") {
+        if (subuser_is_valid_parent_id($account, $edit['parent_id'])) {
+          $account->parent_id = $edit['parent_id'];
+        }
+      }
       break;
+
     case 'view':
       $parent = db_fetch_object(db_query('SELECT parent_id
                                           FROM {user_relationship}
@@ -340,3 +377,62 @@
   );
   return $data;
 }
+
+/**
+ * Liefert FALSE zurück, wenn
+ * - $user die UID <=1 hat
+ * - $user->uid == $parent_id ist
+ * - Die $parent_id eines der Kinder dieses Users ist
+ */
+function subuser_is_valid_parent_id($user, $parent_id) {
+  if ($parent_id == 0) {
+    return true;
+  }
+
+  if ($user->uid <= 1) {
+    return false;
+  }
+  
+  if ($user->uid == $parent_id) {
+    return false;
+  }
+
+  // Ensure that we are not the parent of one of this objects
+  if ($parentUser = user_load($parent_id)) {
+    foreach ($parentUser->parents AS $puid) {
+      if ($puid == $user->uid) {
+        return false;
+      }
+    }
+  }
+
+  return true;
+}
+
+/**
+ * Alter user_profile_form and add a field for the parent user
+ */
+function subuser_form_alter(&$form, $form_state, $form_id) {
+  if ($form_id == "user_profile_form") {
+    $user = user_load($form['#uid']);
+    if ($user->uid > 1) { // Not for root
+
+      // Get user list
+      $users = array(0 => "");
+      $result = db_query("SELECT u.uid, u.name FROM {users} u WHERE u.status <> 0 AND u.access <> 0 ORDER BY u.name ASC");
+      while ($parentUser = db_fetch_object($result)) {
+        if (subuser_is_valid_parent_id($user, $parentUser->uid)) {
+          $users[$parentUser->uid] = $parentUser->name;
+        }
+      }
+
+      $form['account']['parent_id'] = array(
+        '#type' => 'select',
+        '#title' => SUBUSER_PARENT,
+        '#default_value' => $user->parent_id,
+        '#description' => 'Owner of this user',
+        '#options' => $users,
+      );
+    }
+  }
+}
diff -r --unified subuser/subuser.pages.inc subuser-new/subuser.pages.inc
--- subuser/subuser.pages.inc	2009-10-14 06:40:34.000000000 +0200
+++ subuser-new/subuser.pages.inc	2010-04-29 23:19:40.343758885 +0200
@@ -54,9 +54,17 @@
       '#title' => t('Roles'),
       '#description' => t('Choose the roles you would like for sub users to automatically have when created.')
     );
+    foreach ($roles AS $rid => $role) {
+      $form['roles']['subuser_roles_' . $rid] = array(
+        '#type' => 'checkboxes',
+        '#title' => t('If created by %role', array('%role' => $role)),
+        '#options' => $roles,
+        '#default_value' => variable_get('subuser_roles_' . $rid, array()),
+      );
+    }
     $form['roles']['subuser_roles'] = array(
       '#type' => 'checkboxes',
-      '#title' => t('Available roles'),
+      '#title' => t('Default roles'),
       '#options' => $roles,
       '#default_value' => variable_get('subuser_roles', array()),
     );
@@ -87,7 +95,7 @@
 function subuser_create_form(&$form_state, $user) {
   $form = array();
 
-  $form['parent_user'] = array(
+  $form['parent_id'] = array(
     '#type' => 'value',
     '#value' => $user->uid,
   );
@@ -135,9 +143,19 @@
     '#type' => 'checkbox',
     '#title' => t('Notify user of new account'),
   );
+
+  // Merge roles depending on the current user's role
+  $roles = variable_get('subuser_roles', array());
+  foreach ($user->roles AS $rid => $role) {
+    foreach (variable_get("subuser_roles_$rid", array()) AS $srid => $svalue) {
+      if ($svalue) {
+        $roles[$srid] = $svalue;
+      }
+    }
+  }
   $form['roles'] = array(
     '#type' => 'value',
-    '#value' => variable_get('subuser_roles', array())
+    '#value' => $roles,
   );
   $form['submit'] = array(
     '#type' => 'submit',
