public function testGenerate_password() { $password = org_openpsa_user_accounthelper::generate_password(); $this->assertTrue(is_string($password)); $this->assertEquals(8, strlen($password)); $password = org_openpsa_user_accounthelper::generate_password(16); $this->assertEquals(16, strlen($password)); }
/** * 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); }