0

I have used following code to delete the user picture on click of a form button element (using #ajax).

function delete_user_picture($form, &$form_state){
  global $user;
  $account = user_load($user->uid);
  unset($account->picture);
  $edit = (array) $account;
  user_save($user, $edit);
}

This successfully deletes the picture but also changes the password. How can I prevent it from doing so?

UPDATE: Based on Molot's Suggestion, I made the following change to code and now it works perfectly:

function profile_delete_user_picture($form, &$form_state){
  global $user;
  $account = user_load($user->uid);
  $file = file_load($account->picture->fid);
  unset($account->picture);
  $edit = array();
  //add just the picture info and not the whole account info and password won't be changed
  $edit['picture'] = $account->picture; 
  user_save($user, $edit);
  if ($file != NULL) {
    file_delete($file);
  }
}

Also added code to delete the file from database as per SlakeFistcrunch's Suggestion.

2 Answers 2

0

Yes it does. This works as designed, but you made mistake. When you obtain user object, it does not contain a password. It contains a password field, but it's not populated with actual password as it is impossible by design to know it. So you are feeding all fields to user_save, including field for password, but without password in it.

1
  • How do I encounter this?
    – kmdhrm
    Dec 3, 2013 at 15:22
0

Building off what Molot suggested (I was going to add as a comment, but not enough rep), is it possible to save the hashed password from the DB, and place it back in afterward? (Not an ideal solution of course, but I would think it's possible.)

Also, that doesn't actually delete the user picture from the filesystem, does it? If not, be sure to delete the images themselves, otherwise you'll wind up with a bunch of disconnected/useless images in your public files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.