Index: invite.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/invite/invite.install,v
retrieving revision 1.13.2.2
diff -u -r1.13.2.2 invite.install
--- invite.install	6 Oct 2008 13:03:08 -0000	1.13.2.2
+++ invite.install	12 Apr 2010 20:17:53 -0000
@@ -123,9 +123,11 @@
 
   // Delete variables
   $sql = "DELETE from {variable} WHERE name LIKE '%s%%'";
-  db_query($sql, 'invite_target_role_');
+  db_query($sql, 'invite_roles');
   db_query($sql, 'invite_maxnum_');
   db_query($sql, 'invite_maxmultiple_');
+  db_query($sql, 'invite_enabled_');
+  db_query($sql, 'invite_maxnum_');
 
   variable_del('invite_target_role_default');
   variable_del('invite_expiry');
Index: invite.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/invite/invite.module,v
retrieving revision 1.25.2.5
diff -u -r1.25.2.5 invite.module
--- invite.module	19 Apr 2009 20:51:04 -0000	1.25.2.5
+++ invite.module	12 Apr 2010 20:16:58 -0000
@@ -279,10 +279,15 @@
  */
 function invite_accept($invite) {
   global $user;
-
   if (!$user->uid && invite_validate($invite)) {
     $_SESSION[INVITE_SESSION] = $invite->reg_code;
-    drupal_goto('user/register');
+    $path = 'user/register';
+    // Find modules that alter the registration path
+    foreach (module_implements('invite_redirect') as $module) {
+      $function = $module .'_invite_redirect';
+      $path = call_user_func($function, $invite);
+    }
+    drupal_goto($path);
   }
 
   drupal_goto();
@@ -473,32 +478,16 @@
   // Default target role.
   $roles = array('default');
 
-  // Add roles of inviter.
-  $inviter_uid = db_result(db_query("SELECT uid FROM {invite} WHERE invitee = %d", $account->uid));
-  if ($inviter_uid && $inviter = user_load(array('uid' => $inviter_uid))) {
-    $roles = array_merge($roles, array_intersect($inviter->roles, user_roles(0, 'send invitations')));
-  }
-
-  // Map to configured target roles.
-  $targets = array();
-  foreach ($roles as $role) {
-    $role_no_space = str_replace(' ', '_', $role);
-    $target = variable_get('invite_target_role_'. $role_no_space, DRUPAL_AUTHENTICATED_RID);
-    if ($target != DRUPAL_AUTHENTICATED_RID) {
-      $targets[$target] = $target;
-    }
-  }
+  // Get the role assigned during invite
+  $data = db_result(db_query("SELECT data FROM {invite} WHERE invitee = %d", $account->uid));
+  $options = unserialize($data);
+  $invitation_role = $options['role'];
+  $targets[$invitation_role] = $invitation_role;
 
   // Notify other modules of changed user.
   $edit = array('roles' => $targets);
   user_module_invoke('update', $edit, $account);
 
-  // Save new user role(s).
-  foreach ($targets as $target) {
-    db_query("DELETE FROM {users_roles} WHERE uid = %d AND rid = %d", $account->uid, $target);
-    db_query("INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)", $account->uid, $target);
-  }
-
   // Notify other modules of role escalation.
   $args = array('invitee' => $account, 'inviter' => $inviter, 'roles' => $targets);
   module_invoke_all('invite', 'escalate', $args);
@@ -746,6 +735,48 @@
       '#value' => $edit['email'],
     );
   }
+  // Find out which roles this user can assign
+  $target_roles = user_roles(TRUE);
+  if ($user->uid != 1) {
+    $user_roles = $user->roles;
+    foreach ($user_roles as $rid => $name) {
+      $user_allowed_roles = variable_get('invite_enabled_'. $rid, array());
+      if (!empty($user_allowed_roles)) {
+        foreach ($user_allowed_roles as $role_id => $status) {
+          if ($role_id == $status) {
+            $allowed_roles[$role_id] = $target_roles[$role_id];
+          }
+        }
+      }
+    }
+  }
+  else {
+    $invite_roles = variable_get('invite_roles', array());
+    if (!empty($invite_roles)) {
+      foreach($invite_roles as $r => $status) {
+        if ($r == $status) {
+          $allowed_roles[$r] = $target_roles[$r];
+        }
+      }
+    }
+    else {
+      $allowed_roles = array();
+    }
+  }
+  if (!empty($allowed_roles)) {
+    if (count($allowed_roles) > 1) {
+      $form['target_role'] = array(
+      '#type' => 'select',
+      '#options' => $allowed_roles,
+      '#title' => 'Choose target role for this invitation',
+      );
+    }
+    else {
+      $keys = array_keys($allowed_roles);
+      $value = $keys[0];
+      $form['target_role'] = array('#value' => $value);
+    }
+  }
 
   // Message subject.
   if ($edit && !empty($edit['data']['subject'])) {
@@ -823,6 +854,49 @@
     '#description' => $description,
     '#required' => TRUE,
   );
+  // Find out which roles this user can assign
+  $target_roles = user_roles(TRUE);
+  if ($user->uid != 1) {
+    $user_roles = $user->roles;
+    foreach ($user_roles as $rid => $name) {
+      $user_allowed_roles = variable_get('invite_enabled_'. $rid, array());
+      if (!empty($user_allowed_roles)) {
+        foreach ($user_allowed_roles as $role_id => $status) {
+          if ($role_id == $status) {
+            $allowed_roles[$role_id] = $target_roles[$role_id];
+          }
+        }
+      }
+    }
+  }
+  else {
+    $invite_roles = variable_get('invite_roles', array());
+    if (!empty($invite_roles)) {
+      foreach($invite_roles as $r => $status) {
+        if ($r == $status) {
+          $allowed_roles[$r] = $target_roles[$r];
+        }
+      }
+    }
+    else {
+      $allowed_roles = array();
+    }
+  }
+  if (!empty($allowed_roles)) {
+    if (count($allowed_roles) > 1) {
+      $form['target_role'] = array(
+      '#type' => 'select',
+      '#options' => $allowed_roles,
+      '#title' => 'Choose target role for this invitation',
+      );
+    }
+    else {
+      $keys = array_keys($allowed_roles);
+      $value = $keys[0];
+      $form['target_role'] = array('#value' => $value);
+    }
+  }
+
   $form['submit'] = array(
     '#type' => 'submit',
     '#value' => t('Send invite'),
@@ -1036,6 +1110,7 @@
     // Never pass an empty string to drupal_mail()
     $from = NULL;
   }
+  $assigned_role = $form_state['values']['target_role'];
 
   foreach ($form_state['values']['valid_emails'] as $email) {
     // Create the invite object.
@@ -1044,7 +1119,7 @@
       'email' => $email,
       'code'  => $code,
       'resent'  => $form_state['values']['resent'],
-      'data'  => array('subject' => $subject, 'message' => $message),
+      'data'  => array('subject' => $subject, 'message' => $message, 'role' => $assigned_role),
     ));
 
     // Send e-mail.
@@ -1056,7 +1131,7 @@
 
       // Notify other modules.
       if (!$form_state['values']['resent']) {
-        $args = array('inviter' => $invite->inviter, 'email' => $invite->email, 'code' => $invite->code);
+        $args = array('inviter' => $invite->inviter, 'email' => $invite->email, 'code' => $invite->code, 'role' => $assigned_role);
         module_invoke_all('invite', 'invite', $args);
       }
 
Index: invite_admin.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/invite/invite_admin.inc,v
retrieving revision 1.3.2.1
diff -u -r1.3.2.1 invite_admin.inc
--- invite_admin.inc	6 Oct 2008 13:03:08 -0000	1.3.2.1
+++ invite_admin.inc	12 Apr 2010 19:41:33 -0000
@@ -23,14 +23,6 @@
     '#collapsible' => TRUE,
     '#collapsed' => TRUE,
   );
-  $form['general']['invite_target_role_default'] = array(
-    '#type' => 'select',
-    '#title' => t('Default target role'),
-    '#default_value' => variable_get('invite_target_role_default', DRUPAL_AUTHENTICATED_RID),
-    '#options' => $target_roles,
-    '#description' => t('Choose the default role that invited users will be added to when they register. For example, <em>authenticated user</em>.'),
-    '#required' => TRUE,
-  );
   $form['general']['invite_expiry'] = array(
     '#type' => 'select',
     '#title' => t('Invitation expiry'),
@@ -50,30 +42,58 @@
     '#collapsed' => TRUE,
   );
 
-  foreach ($roles as $role) {
-    $role_no_space = str_replace(' ', '_', $role);
-    $form['role'][$role_no_space] = array(
-      '#type' => 'fieldset',
-      '#title' => t('@role settings', array('@role' => drupal_ucfirst($role))),
-      '#collapsible' => TRUE,
-      '#collapsed' => TRUE,
-    );
-    $form['role'][$role_no_space]['invite_target_role_'. $role_no_space] = array(
-      '#type' => 'select',
-      '#title' => t('Target role'),
-      '#default_value' => variable_get('invite_target_role_'. $role_no_space, DRUPAL_AUTHENTICATED_RID),
-      '#options' => $target_roles,
-      '#description' => t('You may choose to add invited users to another role (in addition to the default role set in the general section) when they have been invited by a member of %role.', array('%role' => $role)),
-      '#required' => TRUE,
-    );
-    $form['role'][$role_no_space]['invite_maxnum_'. $role_no_space] = array(
-      '#type' => 'select',
-      '#title' => t('Invitation limit'),
-      '#default_value' => variable_get('invite_maxnum_'. $role_no_space, INVITE_UNLIMITED),
-      '#options' => array(5 => 5, 10 => 10, 20 => 20, 50 => 50, 100 => 100, 500 => 500, 1000 => 1000, INVITE_UNLIMITED => t('unlimited')),
-      '#description' => t('Allows to limit the total number of invitations members of %role can send.', array('%role' => $role)),
-      '#multiple' => FALSE,
-      '#required' => TRUE,
+  $invite_roles = variable_get('invite_roles', array());
+  $form['role']['invite_roles'] = array(
+    '#type' => 'checkboxes',
+    '#title' => t('Roles that can be assigned to invited users'),
+    '#description' => t('Please select which roles can be assigned to invited users'),
+    '#options' => $target_roles,
+    '#default_value' => $invite_roles,
+  );
+  if (!empty($invite_roles)) {
+    foreach($invite_roles as $r => $status) {
+      if ($r == $status) {
+        $allowed_roles[$r] = $target_roles[$r];
+      }
+    }
+  }
+  else {
+    $allowed_roles = array();
+  }
+
+  if (!empty($allowed_roles)) {
+    foreach ($roles as $rid => $role) {
+      $role_no_space = str_replace(' ', '_', $role);
+      $form['role'][$rid] = array(
+        '#type' => 'fieldset',
+        '#title' => t('@role settings', array('@role' => drupal_ucfirst($role))),
+        '#collapsible' => TRUE,
+        '#collapsed' => TRUE,
+      );
+      $form['role'][$rid]['invite_enabled_'. $rid] = array(
+        '#type' => 'checkboxes',
+        '#title' => t('Allow assigning this role'),
+        '#description' => t('Please select which roles members of %role can
+        assign.', array('%role' => $role)),
+        '#options' => $allowed_roles,
+        '#default_value' => variable_get('invite_enabled_'. $rid, array()),
+      );
+      foreach ($allowed_roles as $role_id => $role_name) {
+        $form['role'][$rid][$role_id]['invite_maxnum_'. $rid .'_'. $role_id] = array(
+          '#type' => 'select',
+          '#title' => t('Invitation limit for %rolename', array('%rolename' => $role_name)),
+          '#default_value' => variable_get('invite_maxnum_'. $rid .'_'. $role_id, INVITE_UNLIMITED),
+          '#options' => array(5 => 5, 10 => 10, 20 => 20, 50 => 50, 100 => 100, 500 => 500, 1000 => 1000, INVITE_UNLIMITED => t('unlimited')),
+          '#description' => t('Allows to limit the total number of invitations members of %role can send.', array('%role' => $role)),
+          '#multiple' => FALSE,
+        );
+      }
+    }
+  }
+  else {
+    $form['role']['notice'] = array(
+      '#value' => t('Please select at least one role in order to allow
+      assigning roles during invite process'),
     );
   }
 

