The title says it all but i'll clarify.

Lets say i have a user. Normally i can go in via admin panels and assign user to a given role and then check off the permissions.

I basically want to be able to do the same thing but via code.

Can anyone point me to the right hooks or offer up a snippet?

thanks

Comments

malcomio’s picture

With hook_user you can add roles during user registration or editing.

I wanted to assign a role based on certain conditions at registration, so used $op == 'insert'.

The thing that confused me was that I needed to have the number for the array item matching the role ID.

function nuyou_reg_user($op, &$edit, &$account, $category = NULL)
{
    if($op == 'insert')
    {
        // my conditions in here
        if(in_array($half_postcode, $allowed_postcodes))
            $edit['roles'][3] = 'Customer';
        else $edit['roles'][4] = 'Pending customer';
    }
}

http://api.drupal.org/api/function/hook_user