Kin
Kin

Reputation: 4596

How to change user's password in joomla?

I am writing a custom registration / authorization module for joomla and I'm wondering how with default joomla methods I could change user password? At the moment I have:

private function UpdateUser(){
    $user = JFactory::getUser($this->user_data['username']);
    $user->set('password', $this->user_data['password']);
    $user->set('password2', $this->user_data['password']);
    // $user->bind();
    $user->save();
}

but the password is not updated.

P.S. $this->user_data includes other staff about user, but i need update only password.

Upvotes: 2

Views: 2253

Answers (1)

Kin
Kin

Reputation: 4596

Solved.

private function UpdateUser($username, $password){

    $user = JFactory::getUser($this->GetIdByUserName($username));
    $password = array('password' => $password, 'password2' => $password);
    if(!$user->bind($password)){
        die('Could not bind data. Error: '.$user->getError());
    }
    if(!$user->save()){
        die('Could not save user. Error: '.$user->getError());
    }
}


private function GetIdByUserName($username){
    $query = $this->db->getQuery(true);

    $query->select('id');
    $query->from('#__users');
    $query->where('username=' . $this->db->Quote($username));

    $this->db->setQuery($query);
    if(isset($this->db->loadObject()->id) && !empty($this->db->loadObject()->id))
        return $this->db->loadObject()->id;
    else
        die('No id');
}

Upvotes: 2

Related Questions