/** * Sets username and password for person * * @param string $username Contains username * @param string $new_password Contains the new password to set */ public function set_account($username, $new_password) { $this->_account = midcom_core_account::get($this->_person); if (!empty($new_password)) { $new_password_encrypted = midcom_connection::prepare_password($new_password); //check if the new encrypted password was already used if ($this->check_password_reuse($new_password_encrypted) && $this->check_password_strength($new_password)) { $this->_save_old_password(); $this->_account->set_password($new_password); } else { $this->errstr = "password strength too low"; return false; } } $this->_account->set_username($username); //probably username not unique if (!$this->_account->save()) { $this->errstr = "Failed to save account"; return false; } if (!empty($new_password)) { //add timestamp of password-change $this->_person->set_parameter("org_openpsa_user_password", "last_change", time()); } //sets privilege midcom::get('auth')->request_sudo($this->_component); $this->_person->set_privilege('midgard:owner', "user:" . $this->_person->guid); midcom::get('auth')->drop_sudo(); return true; }
/** * This is an internal helper function, resetting the password to a randomly generated one. */ private function _reset_password() { if (!midcom::get('auth')->request_sudo($this->_component)) { throw new midcom_error('Failed to request sudo privileges.'); } $qb = midcom_db_person::new_query_builder(); if (array_key_exists('username', $this->_controller->datamanager->types)) { $user = midcom::get('auth')->get_user_by_name($this->_controller->datamanager->types['username']->value); if (!$user) { midcom::get('auth')->drop_sudo(); throw new midcom_error("Cannot find user. For some reason the QuickForm validation failed."); } $qb->add_constraint('guid', '=', $user->guid); } if (array_key_exists('email', $this->_controller->datamanager->types)) { $qb->add_constraint('email', '=', $this->_controller->datamanager->types['email']->value); } $results = $qb->execute(); if (sizeof($results) != 1) { midcom::get('auth')->drop_sudo(); throw new midcom_error("Cannot find user. For some reason the QuickForm validation failed."); } $person = $results[0]; $account = new midcom_core_account($person); // Generate a random password $length = max(8, $this->_config->get('password_minlength')); $password = org_openpsa_user_accounthelper::generate_password($length); $account->set_password($password); if (!$account->save()) { midcom::get('auth')->drop_sudo(); throw new midcom_error("Could not update the password: " . midcom_connection::get_error_string()); } midcom::get('auth')->drop_sudo(); $this->_send_reset_mail($person, $password); }