does anyone know how to automatically assign users to a role

Joined: 01/09/2009
User offline. Last seen 2 years 18 weeks ago.

I am trying to find a way to automatically assign users to specific roles, or at very minimum allow them to select a role. I have tried using the auto assign role module, and setting it to allow users to select a role at registration, but I do not want them to be able to select any of the available roles, just 2 or 3 that I have set aside for this purpose. Also, I tried to find a way to configure the rules module so that after performing a certain action the user would automatically be assigned to a specific role, and had no luck with this either. Any help would be greatly appreciated.

Joined: 10/18/2008
User offline. Last seen 1 year 27 weeks ago.
I created my own module for

I created my own module for this. I basically modified the rolesignup module for my own needs.

First, I set up some path aliases like

"firstmembertype" = "user/register/role/8" (where 8 is the role number)
"secondmembertype" = "user/register/role/9"
etc

Then the module just basically took that alias and assigned the role:

/**
* Implementation of hook_form_alter()
   member = 7
   memberadmin = 8
*/
function myrolesignup_form_alter($form_id, &$form) {
  if ($form_id == "user_register") {
    // Make sure there is a role associated with this sign up
    // If not, give them the member role
    if ((arg(3) != 7) &&
        (arg(3) != 8) &&
        (arg(3) != 9)) {
       $myrole = 7; 
    } else {
       $myrole = arg(3);
    }
    $form['role'] = array(
      '#type' => 'value',
      '#value' => $myrole,
      );
  }
}

/**
* Implementation of hook_user()
*/
function myrolesignup_user($op, &$edit, &$account, $category = NULL) {
global $myrole;
  switch ($op) {
    case 'insert':
       // Make sure the role is defined
       if (($edit['role'] != 7) &&
          ($edit['role'] != 8) &&
          ($edit['role'] != 9)) {
          $myrole = 7; 
       } else {
          $myrole = $edit['role'];
       }
       // Insert the role into the DB
       db_query("INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)", $account->uid, $myrole);

       // Give them the authenticated role as well
       db_query("INSERT INTO {users_roles} (uid, rid) VALUES (%d, 2)", $account->uid);
  }
}

Not exactly the most elegant, but it works.

/ * Begin Signature */
It's a strange thing about determined seekers-after-wisdom that, no matter where they happen to be, they'll always seek that wisdom which is a long way off. Wisdom is one of the few things that looks bigger the further away it is.

Joined: 01/09/2009
User offline. Last seen 2 years 18 weeks ago.
Thanks for the help, is this

Thanks for the help, is this for drupal 5 or 6, or does it work for both?

Joined: 10/18/2008
User offline. Last seen 1 year 27 weeks ago.
The module was written for

The module was written for D5, but the only hook that changed was form_alter, so if you modified that, you should be good to go.

/ * Begin Signature */
It's a strange thing about determined seekers-after-wisdom that, no matter where they happen to be, they'll always seek that wisdom which is a long way off. Wisdom is one of the few things that looks bigger the further away it is.

Stevik
Stevik's picture
try assign from path field

try assign from path field set which you have to use content profile module or you should just check only those roles you want to user to choose from under the let user choose fieldset