Dreams of Thought

Are dreams thoughts… or are thoughts dreams..

RSS Feed

Archives for drupal

How to programmatically create and log in drupal users

0 Comments

Creating a new user is very easy in Drupal 6. Here’s how.

<p>><p>$new_user = array(</p><p>'name'                   => $username,<br /> 'mail'                     => $mail,<br /> 'pass'                    => user_password(),<br /> 'status'                  => 1,<br /> 'auth_MODULENAME' => $username<br />)</p><p>$user = user_save(NULL,$new_user)</p><p>// log the user in</p><p>$user = user_authenticate($new_user)</p><p>

Now for the explanation. We create a $new_user array with values we want the newly created user to have. We pass this along to the user_save function and set the 1st parameter as NULL. From the code comments in the user module -

* @param $account
* The $user object for the user to modify or add. If $user->uid is
* omitted, a new user will be added.
*
* @param $array
* (optional) An array of fields and values to save. For example,
* array(‘name’ => ‘My name’); Setting a field to NULL deletes it from
* the data column.

So setting the 1st parameter as NULL creates a new user.

The parameters name, mail, pass and status are all self explanatory. The user_password function generates a random password (by default with a length of 10).

The (optional) ‘auth_MODULENAME’ element will record the user as being created externally. This will result in an entry in the authmap table like this -

“aid” “uid” “authname” “module”
“2″ “20″ “USERNAME” “MODULENAME”

The user_authenticate function logs the user in. This function expects an array as a parameter. It first loads the user in using the user_load function and in case of no errors logs the user in.

This approach of logging in a user is useful when we have the array with us which contains the raw values used to create the user. If all you have is the uid of the user, then logging the user in is very simple. Just use the global $user object.

</p><p>global $user</p><p>$account = user_load( array('name' => 'USERNAME') ); // or user_load(UID)</p><p>$user = $account</p><p>

Don’t use the user_authenticate function here as it expects the raw values(form values). This wil not work -

</p><p>$account = user_load( array('name' => 'USERNAME') ); // or user_load(UID)</p><p>user_authenticate((array)$account)</p><p>

It will take whatever password is stored in the user object(the raw password), md5 it and then run a query to load that user. Since the password in the query is not the raw password but the md5, the query will return nothing. This will cause an error in the user_authenticate function.

For a direct code example for programmatic log in, check the devel module’s devel_switch_user function.

Filed under drupal
Feb 22, 2010

Gravatar support in Drupal

0 Comments

If you’ve been using WordPress you probably know about Gravatar. Gravatar is an abbreviation for “Globally Recognized Avatar”. It’s a service which provides an avatar across sites. While Gravatar support is built-in in WordPress, it requires an external module to be installed in Drupal. Recently I had to use the module in a Drupal 5 instance.

The module provides a gravatar only for comments. You can have the module insert the gravatar into the comment body itself or have it provide a field in the comment object ($comment->gravatar) which can be used by the theme. It does this by using the hook_comment() .

In our case though, we needed it to show the gravatar instead of the user picture if the user had not uploaded any. This was very easy to do. The key function here is _gravatar_get_gravatar(). You can provide it any or all of these parameters – mail, default, size or rating. Mail is the email id of the user. Gravatar can supply a unique generated avatar in case the email id you send them is not that of a registered Gravatar user. This can be of many types – identicon, wavatar or monsterid. Which of these are what you want, is specified in the default parameter. The size is of course, the size. The gravatars uploaded come with a rating – G, PG, R or X. You can specify a maximum rating with the rating parameter. You can read more about the gravatar parameters.

You can pass whichever of these parameters you want to _gravatar_get_gravatar() and it will return to you the url of the gravatar. Now pass this to the theme_gravatar() function in the gravatar module.

$picture = theme('gravatar',$gravatar_url,$username,$url);

You have to do all of this in the theme_user_picture function of your theme. If your theme is something like -

function THEMENAME_user_picture($account) {
  if (variable_get('user_pictures', 0)) {
    if ($account->picture && file_exists($account->picture)) {
      $picture = file_create_url($account->picture);
    }
    else if (variable_get('user_picture_default', '')) {
      $picture = variable_get('user_picture_default', '');
    }

You could change it to something like -

function THEMENAME_user_picture($account) {
  if (variable_get('user_pictures', 0)) {
    if ($account->picture && file_exists($account->picture)) {
      $picture = file_create_url($account->picture);
    }
    else if (user_access('use gravatar', $account) && (!user_access('disable own gravatar', $account) || !isset($account->gravatar) || $account->gravatar)) {
      $picture = _gravatar_get_gravatar(array('mail' => $account->mail));
      return theme('gravatar',$picture,$account->name,'');
    }
    else if (variable_get('user_picture_default', '')) {
      $picture = variable_get('user_picture_default', '');
    }

You could even rewrite the default theme_gravatar provided by the gravatar module with your own THEMENAME_gravatar theme function.

Filed under drupal
Nov 4, 2009