Create a "password_reset" resource for Services

Just another quick snippet to add to the Grasmash Services archive.

This will allow you to POST an email address to /your_endpoint/user/password_reset and trigger a 'Forgot Password' email to be sent to the appropriate user.

<?php
/**
* Implements hook_services_resources().
*/
function grasmash_services_resources() {
 
$definition['user']['actions']['password_reset'] = array(
   
'help' => 'Send forgot password email',
   
'callback' => 'grasmash_user_password_reset',
   
'access callback' => 'services_access_menu',
   
'args' => array(
      array(
       
'name' => 'email',
       
'optional' => FALSE,
       
'source' => 'data',
       
'type' => 'string',
       
'description' => 'The email address of the user whose password should be reset.',
      ),
    ),
  );

  return $definition;
}

/**
* Send a password reset email for the specified user.
*/
function grasmash_user_password_reset($data) {
  global
$language;

  $account = user_load_by_mail($data['email']);
  if (empty(
$account)) {
    return
services_error(t('There is no user associated with email address @email.', array('@email' => $data['email'])), 404);
  }

  $vars = array('%name' => $account->name, '%email' => $account->mail);
 
// Mail one time login URL and instructions using current language.
 
$mail = _user_mail_notify('password_reset', $account, $language);
  if (!empty(
$mail)) {
   
watchdog('grasmash', 'Password reset instructions mailed to %name at %email.', $vars);
  }
  else {
   
watchdog('grasmash', 'There was an error re-sending password reset instructions mailed to %name at %email', $vars);
  }
 
// Everything went right.
 
return TRUE;
}
?>

Drupal Version Compatibility: 

Comments

It is very hard for me to understand , so please tell the steps, so that i can understand it.

http://www.coomberlaw.com/

Thanks for saving my time. Though i've altered this function a little (because my callback file is in another place).
But it works like a charm :)

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.