--- ucreate.module	2010-09-09 16:56:32.995417300 -0400
+++ ucreate.new.module	2010-09-09 17:02:57.535411700 -0400
@@ -113,112 +113,75 @@
  * Break out form for creating users.
  */
 function ucreate_user_form() {
-  $form['name'] = array(
-    '#type' => 'textfield',
-    '#title' => t('User name'),
-    '#required' => TRUE,
-    '#element_validate' => array('ucreate_validate_name'),
-    );
-  $form['mail'] = array(
-    '#type' => 'textfield',
-    '#title' => t('E-mail'),
-    '#required' => TRUE,
-    '#element_validate' => array('ucreate_validate_mail'),
-    );
-  $form['mail_confirm'] = array(
-    '#type' => 'textfield',
-    '#title' => t('E-mail (confirm)'),
-    '#required' => TRUE,
-    );
-  if (user_access('assign user roles') || user_access('administer users')) {
-    $form['roles'] = array(
-      '#type' => 'checkboxes',
-      '#title' => t('User roles'),
-    );
-    $default_roles = variable_get('ucreate_default_roles', array());
-    foreach (user_roles() as $rid => $role) {
-      // Exclude 'anonymous user'
-      if ($rid !== 1) {
-        $form['roles'][$rid] = array(
-          '#title' => $role,
-          '#type' => 'checkbox',
-          '#default_value' => in_array($rid, $default_roles, TRUE) || $rid === 2,
-          '#disabled' => $rid === 2 ? TRUE : FALSE,
-        );
-      }
-    }
-  }
-  else {
-    $form['roles'] = array(
-      '#type' => 'value',
-      '#value' => drupal_map_assoc(variable_get('ucreate_default_roles', array())),
-      );
-  }
-  // The personal welcome message will be added to the top of the mail.
-  // @todo: Ideal would be offering the full notification message for edit
-  //        * updated by ajax call back (we shouldn't show tokens to users)
-  //        * or in a second step of the form
-  //        Both approaches have ramifications for the use of the form in ajaxy popups.
-  $form['welcome_message_body'] = array(
-    '#type' => 'textarea',
-    '#title' => t('Personal welcome message'),
-    '#default_value' => '',
-    '#description' => t('This welcome message will appear at the top of the e-mail notification sent to the new user.')
-    );
-  $form['submit'] = array(
-    '#type' => 'submit',
-    '#value' => t('Add'),
-    '#weight' => 20,
-    );
-  $form['#redirect'] = $_GET['q'];
-  return $form;
+  ucreate_impersonate('start');
+  $output = drupal_get_form('user_register');
+  ucreate_impersonate('end');
+  return $output;
 }
 
 /**
- * Element validator for usernames.
- */
-function ucreate_validate_name($element, &$form_state) {
-  if ($account = user_load(array('name' => $element['#value']))) {
-    $name = user_access('access user profiles') ? l($account->name, 'user/'. $account->uid) : $account->name;
-    form_set_error('name', t('User !name already exists.', array('!name' => $name)));
-  }
-  else if ($error = user_validate_name($element['#value'])) {
-    form_set_error('name', $error);
-  }
-}
-
-/**
- * Element validator for mail.
- */
-function ucreate_validate_mail($element, &$form_state) {
-  if ($form_state['values']['mail_confirm'] !== $element['#value']) {
-    form_set_error('mail_confirm', t('E-mail addresses don\'t match'));
-  }
-  else if (user_load(array('mail' => $element['#value']))) {
-    form_set_error('mail', t('User with this e-mail address already exists.'));
-  }
-  else if ($error = user_validate_mail($element['#value'])) {
-    form_set_error('mail', $error);
+ * Impersonate user 1, making sure that the session is not saved. (No elevating
+ * the current user to admin unintentionally!) We do this by changing the UID
+ * instead of doing a user_load for user 1. This is for two reasons: we don't
+ * need the overhead of a user_load call, and we only want the permisisons of
+ * user 1, we still want to have the account info of the registered user available.
+ *
+ * @global <type> $user
+ * @staticvar <type> $saved_user
+ * @param <type> $status
+ */
+function ucreate_impersonate($action='check'){
+  static $saved_user;
+  global $user;
+  if($action == 'start'){
+    if(!isset($saved_user)){
+      session_save_session(FALSE);
+      $saved_user = clone $user;
+      $user->uid = 1;
+    }
+  }else if ($action == 'end'){
+    if(isset($saved_user)){
+      $user = clone $saved_user;
+      unset($saved_user);
+      session_save_session(TRUE);
+    }
+  }else{
+    return isset($saved_user) ? $saved_user : false;
   }
 }
 
-/**
- * Submit handler for ucreate_user_form().
- */
-function ucreate_user_form_submit($form, &$form_state) {
-  // If user roles were handled through a UI element, process accordingly.
-  // This sucks. See user_save() for why this is necessary.
-  if ($form['roles']['#type'] != 'value') {
-    $rids = array();
-    foreach ($form_state['values']['roles'] as $rid => $enabled) {
-      if (!empty($enabled)) {
-        $rids[$rid] = $rid;
+function ucreate_form_user_register_alter(&$form, &$form_state)
+{
+  // Will return false if no user account has been saved, meaning we know that
+  // the code within this block will only run if the form is being displayed
+  // on the ucreate page with an impersonated user 1.
+  if($account = ucreate_impersonate()){
+    // If the user (the real user, not the one we are impersonating) has
+    // permission to assign roles, let them do so. Otherwise set the default
+    // roles as a hidden field.
+    if (user_access('assign user roles',$account) || user_access('administer users',$account)){
+      // Set defaults.
+      $form['account']['roles']['#default_value'] = variable_get('ucreate_default_roles', array());
+
+      // Remove disabled options - they are not needed here
+      foreach($form['account']['roles'] as $k=>$v){
+        if(is_numeric($k) && $v['#disabled']){
+          unset($form['account']['roles'][$k]);
+        }
       }
+    }else{
+      $form['account']['roles'] = array(
+        '#type' => 'value',
+        '#value' => drupal_map_assoc(variable_get('ucreate_default_roles', array()))
+      );
     }
-    $form_state['values']['roles'] = $rids;
-  }
 
-  ucreate_user_create($form_state['values']);
+    // Remove the "notify user" and "status" options. 
+    $form['account']['notify']['#type'] = 'value';
+    $form['account']['notify']['#value'] = 1
+    $form['account']['status']['#type'] = 'value';
+    $form['account']['status']['#value'] = 1
+  }
 }
 
 /**
@@ -273,68 +236,4 @@
   }
   // Unlikely.
   drupal_set_message(t('There was an error in changing the account status.'), 'error');
-}
-
-/**
- * Create user
- *
- * @param array $edit
- *   Values in format accepted by user_save().
- *   Required values:
- *   $edit['name']
- *   $edit['mail']
- */
-function ucreate_user_create($edit) {
-  // Send in the language in which the user is viewing the site.
-  global $language;
-  // Sanitize the $params array which will get sent to drupal_mail.
-  $params = array();
-  // Define who the mail will be sent from.
-  $from = variable_get('site_mail', ini_get('sendmail_from'));
-  // Create account.
-  $account = new stdClass();
-  $password = user_password();
-  $edit['pass'] = $password;
-  $edit['status'] = 1;
-  $account = user_save($account, $edit);
-
-  // Notify user if successful.
-  if ($account->uid) {
-    drupal_set_message(t('You have created an account for !name. The username and password have been sent along with log in instructions to the e-mail address !email.', array('!name' => $edit['name'], '!email' => l($edit['mail'], 'mailto:'. $edit['mail']))));
-
-    $params['subject'] = t('[!site_name] We have created an account for you', array('!site_name' => variable_get('site_name', 'Drupal')));
-    $variables = array(
-      '!name' => $edit['name'],
-      '!site' => variable_get('site_name', 'Drupal'),
-      '!login_url' => user_pass_reset_url($account) .'/login',
-      '!url' => trim(url('<front>', array('absolute' => TRUE)), '/'),
-      '!password' => $password,
-      );
-    if (trim($edit['welcome_message_body'])) {
-      $body .= $edit['welcome_message_body'];
-      $body .= "\n\n================================================\n";
-    }
-    else {
-      $body .= t("\nHello !name,\n", $variables);
-    }
-    // @todo: Would love to use one time login link here - alas it is only valid for 24 hrs and needs to be renewed then.
-    $body .= t("\nWe have created an account for you on !site\n!url.\n\nYou can log in to the site with the following username and password\n\n!name\n!password\n\nPlease change your password after the first time you log in.\n\nWelcome to !site", $variables);
-    // Put the completed $body in the $params array for hook_mail
-    $params['body'] = $body;
-    if (!drupal_mail('ucreate', 'ucreate-create', $edit['mail'], $language, $params, $from)) {
-      drupal_set_message(t('Error sending notification mail to user.'), 'error');
-    }
-  }
-  else {
-    drupal_set_message(t('Error creating user.'), 'error');
-  }
-  return $account;
-}
-
-/**
- * Implementation of hook_mail().
- */
-function ucreate_mail($key, &$message, $params) {
-  $message['subject'] = $params['subject'];
-  $message['body'][] = $params['body'];
-}
+}
\ No newline at end of file
