10

I'm new to Craft and (attempting to) create a plugin so that I can import existing users from CSV.

I'm referencing this method:

https://docs.craftcms.com/api/v2/craft-usersservice.html#method-changepassword

In my function, I'm using the following code:

$user = craft()->users->getUserById(6);
$user->newPassword('mynewpassword123');
craft()->users->changePassword($user);
craft()->users->saveUser($user);

Am I completely misunderstanding what this is supposed to do?

William Isted
  • 328
  • 2
  • 14
casey
  • 437
  • 2
  • 11

1 Answers1

10

You shouldn't need to call changePassword() at all. Try this:

$user = craft()->users->getUserById(6);
$user->newPassword = 'mynewpassword123';
craft()->users->saveUser($user);

If it's not updating correctly, then there is likely a validation error somewhere. You can check that with:

$user->getErrors();

Which will return an array of properties that failed validation and their error messages.

Brandon Kelly
  • 34,307
  • 2
  • 71
  • 137
Brad Bell
  • 67,440
  • 6
  • 73
  • 143