Hi Everybody. I asked for a feature that assigned new users a specific role based on what email address they supplied. A while back I paid a developer to code such a thing for Drupal 5. I am in the process of updating to 6 and wondered if anyone would like to take over this module and update it for drupal 6. It doesn't seem like it would be that much work but I don't know much about module development.

I guess since I paid for this I can release it to the community. Hopyfully somone can port this to Drupal 6 and make a module page for it. It was called autoassignrole but that name is the same as another (different) module. So changing the name will be the first part.

This was in a file called autoassignrole.module *note the name conflict above.


/**
 * module: Auto assign role
 * Assign a role to registering users based on their email-address automatically.
 */

// The number of rules if fixed, but variable. Change this value, if you need more rules.
define('AUTOASSIGNROLE_NORULES', 5);

/**
 * Implementation of hook_menu().
 */
function autoassignrole_menu($may_cache) {
  $items = array();

  if ($may_cache) {

    $items[] = array(
      'path' => 'admin/settings/autoassignrole',
      'title' => t('Auto Assign Role'),
      'description' => t('Settings for the automatic assignment of roles by email-address.'),
      'position' => 'left',
      'callback' => 'drupal_get_form',
      'callback arguments' => array('autoassignrole_admin_settings'),
      'access' => user_access('administer site configuration')
    );

  };

  return $items;
}

/**
 * Admin panel.
 */

function autoassignrole_admin_settings() {
  $roles = user_roles(true);
  $options = array();
  foreach ($roles as $rid => $name)
  	if ($rid != DRUPAL_AUTHENTICATED_RID)
      $options[$rid] = $name;

  // assignment rules
  $form['rules'] = array(
    '#type' => 'fieldset',
    '#title' => t('<h3>Rules</h3>'),
    '#prefix' => '<table class="autoassignrole-form-table">',
    '#suffix' => '</table>',
    '#description' => t('Define rules for the automatic assignment. If a user enters a specific email-adress an additional role is automatically assigned. The user as well as an admin are notified about the assignment.'),
  );

  for ($a = 0; $a < AUTOASSIGNROLE_NORULES; $a++) {
    $form['rules']['autoassignrole_email'.($a + 1)] = array(
      '#type' => 'textfield',
      '#title' => t('Rule @n', array('@n' => ($a + 1))),
      '#field_prefix' => t('name@'),
      '#field_suffix' => t('is assigned role'),
      '#default_value' => variable_get('autoassignrole_email'.($a + 1), ''),
      '#prefix' => '<tr><td>',
      '#suffix' => '</td>',
    );
    $form['rules']['autoassignrole_role'.($a + 1)] = array(
      '#type' => 'select',
      '#options' => $options,
      '#default_value' => variable_get('autoassignrole_role'.($a + 1), ''),
      '#prefix' => '<td>',
      '#suffix' => '</td></tr>',
    );
  }

  // admin notification

  $form['email'] = array(
    '#type' => 'fieldset',
    '#title' => t('Admin e-mail settings'));
  $form['email']['autoassignrole_adminmail'] = array(
    '#type' => 'textfield',
    '#title' => t('Admin E-mail address'),
    '#default_value' => variable_get('autoassignrole_adminmail', variable_get('site_mail', ini_get('sendmail_from'))),
    '#description' => t('A valid e-mail address to be informed about automatic role assignments.'),
    '#required' => TRUE,
  );
  $form['email']['autoassignrole_adminmail_subject'] = array(
    '#type' => 'textfield',
    '#title' => t('Subject of notification e-mail'),
    '#default_value' => variable_get('autoassignrole_adminmail_subject', t('Notification of automatic role assigment to !username')),
    '#maxlength' => 180,
    '#description' => t('Customize the subject of the admin notification e-mail, which is sent, if a user role is automatically assigned upon registration.') .' '. t('Available variables are:') .' !site, !username, !mail, !date, !role.'
  );
  $form['email']['autoassignrole_adminmail_body'] = array(
    '#type' => 'textarea',
    '#title' => t('Body of notification e-mail'),
    '#default_value' => variable_get('autoassignrole_adminmail_body', t('A role has been automatically assigned to a user !username upon registration.')),
    '#rows' => 15,
    '#description' => t('Customize the body of the admin notification e-mail, which is sent, if a user role is automatically assigned upon registration.') .' '. t('Available variables are:') .' !site, !username, !mail, !date, !role.'
  );

  // user notification (message)
  $form['message'] = array(
    '#type' => 'fieldset',
    '#title' => t('User notification settings'));
  $form['message']['autoassignrole_message_approved'] = array(
    '#type' => 'textfield',
    '#title' => t('Message on approval'),
    '#default_value' => variable_get('autoassignrole_message_approved', t('You e-mail-address has been approved. Additional user privileges granted.')),
    '#maxlength' => 180,
    '#size' => 100,
    '#description' => t('Customize the message that a user gets, if a role is automatically assigned upon registering.')
  );
  $form['message']['autoassignrole_message_not_approved'] = array(
    '#type' => 'textfield',
    '#title' => t('Message, if <b>not</b> approved'),
    '#default_value' => variable_get('autoassignrole_message_not_approved', t('You e-mail-address has <b>not</b> been approved. <b>No</b> additional user privileges granted.')),
    '#maxlength' => 180,
    '#size' => 100,
    '#description' => t('Customize the message that a user gets, if a specific role is not assigned upon registering.')
  );

  return system_settings_form($form);
}

/**
 * Implementation of hook_user (which does all the magic)
 */

function autoassignrole_user($op, &$edit, &$account, $category = NULL) {
  if ($op == 'insert') {
  	// get email-fragment from curent user to check for
    $mailfragment = strtolower(substr($account->mail, strpos($account->mail, '@')+1));
    // check rules
    for ($a = 0; $a < AUTOASSIGNROLE_NORULES; $a++) {
      if ($mailfragment == strtolower(variable_get('autoassignrole_email'.($a + 1), ''))) {
      	// matching rule found!
        // 1. assign role
        $edit['roles'] = $account->roles; // get already assigned roles
        $roles = user_roles(true); // get role names
        $rid = variable_get('autoassignrole_role'.($a + 1), 1); // get role to assign
        $edit['roles'][$rid] = $roles[$rid]; // assign role
      	// 2. notify admin by email
        $variables = array(
          '!site' => variable_get('site_name', 'Drupal'),
          '!username' => $account->name,
          '!mail' => $account->mail,
          '!date' => format_date(time()),
          '!role' => $roles[$rid],
          );
        $mail = variable_get('autoassignrole_adminmail', variable_get('site_mail', ini_get('sendmail_from')));
        $subject = t(variable_get('autoassignrole_adminmail_subject', ''), $variables);
        $body = t(variable_get('autoassignrole_adminmail_body', ''), $variables);
        drupal_mail('autoassignrole-register-admin-notify', $mail, $subject, $body);
      	// 3. notify user with message
        drupal_set_message(variable_get('autoassignrole_message_approved',''));
        return;
      };
    }; // for

    // no mathcing rule found - display not approved message
    drupal_set_message(variable_get('autoassignrole_message_not_approved',''));
  }
}

This was in a file called autoassignrole.info *note the name conflict above.

name = Auto assign role
description = Assign a role to registering users based on their email-address automatically.
version = VERSION

Thanks everyone.
- Jayson

Comments

jaypan’s picture

Double post.

Contact me to contract me for D7 -> D10/11 migrations.

timefor’s picture

I found a couple of posts requesting this but none had any working code posted.

jaypan’s picture

If no one takes this up, I'm always available for hire.

Contact me to contract me for D7 -> D10/11 migrations.

brentonnmurray’s picture

Any progress on this?

tanjerine’s picture

thank you for sharing this code. it worked for my D6 installation. i just had to edit the menu item


/**
 * Implementation of hook_menu().
 */
function autoassignrole_menu() {
  $items = array();
  $items['admin/user/autoassignrole'] = array(
    'title' => t('Auto Assign Role Based on Email'),
    'description' => t('Settings for the automatic assignment of roles by email-address.'),
    'type' => MENU_LOCAL_TASK,
    'page callback' => 'drupal_get_form',
    'page arguments' => array('autoassignrole_admin_settings'),
    'access arguments' => array('administer users'),
   );
   return $items;
}

sarah azurin
php developer | drupal developer | book worm
linux registered user #318168

lastent’s picture

Don't you have plans to make this an module?

rantee’s picture

On another post (can't find it now), a user suggested using the Rules module for this but didn't leave instructions. In my case, I wanted to assign a role only to users that matched an existing list of email addresses on a Drupal 6 site. I created a database table to hold the email addresses, then used Rules to compare newly registered users with them and assign a role if there's a match.

After creating the database table (and adding some data), configure a Rule to:

1) Execute custom php code (below) AND check a truth value, set to true

$yourvariable = db_query("SELECT mail FROM users WHERE (users.mail = yourtable.yourfield)");

if (isset($yourvariable)){
return TRUE;
}

2) Do: add user role

I needed a quick fix and this worked. As usual, use at your own risk..