Hi, My name is Krister Ekstrom and i live in Sweden. I'm trying my best to set up a drupal site for me and help a friend set up her site. I'm new to Drupal and to the list so please pardon me if this question has been asked before, but as you all know you get the role of "authenticated user" assigned to you as soon as you become registered on the site. This role comes with the system and as i understand it, the only thing you can edit with this role are the permissions, not the name, for example. My question is, is there a module for Drupal v5.x that lets me as admin assign another role than "authenticated user" to users when they sign up? I know there was such a module called "UserAutoRole" or something like that for v4.x but does there exist such a module for v5.x and if not, is there a way for a non-programmer to solve this problem? Thanks in advance. /Krister
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; } }
Ooops ... I included a function from the notify module that I used for the hook_user reference that shouldn't be in there ... create autoassignrole.module with this code and not the previous ... Sorry about that
<?php define("AUTOASSIGNROLE_ROLE", ""); /** * 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; } }
On 4/24/07, Cyberswat cyberswat@gmail.com wrote:
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.
I think I speak for everyone when I say "please contribute this to CVS"!
This is something that has been requested for a long time: http://drupal.org/node/23574 and it would be nice to have a contrib for this. I know that I need it on a site and it would be great having a project for it instead of just this email.
Thanks, Greg
Sure thing ... http://drupal.org/project/autoassignrole ... code will be available for download next time the Drupal packaging scripts run (within 12 hours).
On 4/24/07, Greg Knaddison - GVS Greg@growingventuresolutions.com wrote:
On 4/24/07, Cyberswat cyberswat@gmail.com wrote:
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.
I think I speak for everyone when I say "please contribute this to CVS"!
This is something that has been requested for a long time: http://drupal.org/node/23574 and it would be nice to have a contrib for this. I know that I need it on a site and it would be great having a project for it instead of just this email.
Thanks, Greg -- [ Drupal support list | http://lists.drupal.org/ ]