[development] Assigning user to roles programatically

Adrian Rossouw adrian at bryght.com
Fri Jan 27 08:05:41 UTC 2006


On 27 Jan 2006, at 4:14 AM, Rob Thorne wrote:
> Does anyone know how to coerce Drupal 4.6 into doing this?  Or  
> sweet talk it into this?  It would be nice if there was an API for  
> this, but AFAIK there isn't.  What hook should I use, and which 2x4  
> do I need to apply?

Just did this recently :


----- snip ---
$user = user_load(array('uid' => $uid));
$edit['roles'][] = _user_get_rid('administrator');

user_save($user, $edit);
---- snip ---

function _user_get_rid($name) {
   return db_result(db_query("SELECT rid FROM {role} WHERE name ='%s'  
limit 1", $name));
}


I needed to use the user api since I have more specific rules that I  
check for.
IE: you can be one of the following 3 roles, in a certain order of  
preference, but never
more than one.


--
Adrian Rossouw
Drupal developer and Bryght Guy
http://drupal.org | http://bryght.com




More information about the development mailing list