[support] New to the list with a module question

Cyberswat cyberswat at gmail.com
Tue Apr 24 16:07:48 UTC 2007


I would create a custom module to do it that ties into hook_user and assigns
the new user to a role.  You mentioned non-programmer so here is my quick
attempt at how that module would look ... the admin page for it would be at
admin/settings/autoassignrole and you would need to type the name of the
role you were wanting to auto assign there.  This breaks down to two files
autoassignrole.info, and autoassignrole.module ... Hope it helps.

create the directory modules/autoassignrole

place this code in a file called modules/autoassignrole/autoassignrole.info

; $Id $
name = "Auto Assign Role"
description = "Designate a role to assign all new users to."
version = "1.0"





Place this code in modules/autoassignrole/autoassignrole.module

<?php
define("AUTOASSIGNROLE_ROLE", "");
/**
 * Implementation of hook_user().
 */
function notify_user($type, &$edit, &$user, $category = NULL) {
  switch ($type) {
    case 'insert':
      db_query('INSERT INTO {users_role} (uid, rid) values (%d, %d)',
$user->uid, $rid );
      break;
  }
}
/**
 * Implementation of hook_menu().
 *
 * @return array
 */
function autoassignrole_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    $items[] = array(
    'path' => 'admin/settings/autoassignrole',
    'title' => t('Auto Assign Role Settings'),
    'description' => t('Auto Assign Role Settings page.'),
    'callback' => 'drupal_get_form',
    'callback arguments' => 'autoassignrole_settings',
    'access' => user_access('administer autoassignrole'),
    );
  }
  return $items;
}
function autoassignrole_settings() {
  $form['autoassignrole_settings'] = array(
  '#type' => 'fieldset',
  '#title' => t('Auto Assign Role Settings'),
  '#collapsible' => FALSE,
  '#collapsed' => FALSE,
  );
  $form['autoassignrole_settings']['AUTOASSIGNROLE_ROLE'] = array(
  '#type' => 'textfield',
  '#title' => t('.'),
  '#default_value' => variable_get('AUTOASSIGNROLE_ROLE', ''),
  '#size' => 70,
  '#maxlength' => 255,
  '#description' => t('The role you want new users assigned to.')
  );
  return system_settings_form($form);
}
/**
 * Implementation of hook_perm().
 * @return array
 */
function autoassignrole_perm() {
  return array('administer autoassignrole');
}

/**
 * Implementation of hook_user().
 */
function autoassignrole_user($type, &$edit, &$user, $category = NULL) {
  switch ($type) {
    case 'insert':
      $sql = "SELECT r.rid FROM {role} r WHERE r.name = '%s'";
      $role =
db_fetch_object(db_query($sql,variable_get('AUTOASSIGNROLE_ROLE','')));
      db_query('INSERT INTO {users_roles} (uid, rid) values (%d, %d)',
$user->uid, $role->rid );
      break;
  }
}

 <http://www.kevinbridges.org/>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.drupal.org/pipermail/support/attachments/20070424/7e293e40/attachment.htm 


More information about the support mailing list