/** * Function to unblock an account after too many failed to login attempts * * @param array $args Contains the guid, parameter & parameter names to get username&password */ function reopen_account($args, &$handler) { midcom::get('auth')->request_sudo($this->_component); try { $person = new midcom_db_person($args['guid']); } catch (midcom_error $e) { $msg = 'Person with guid #' . $args['guid'] . ' does not exist'; debug_add($msg, MIDCOM_LOG_ERROR); $handler->print_error($msg); midcom::get('auth')->drop_sudo(); return false; } $accounthelper = new org_openpsa_user_accounthelper($person); try { $accounthelper->reopen_account(); } catch (midcom_error $e) { midcom::get('auth')->drop_sudo(); $e->log(); $handler->print_error($e->getMessage()); midcom::get('auth')->drop_sudo(); return false; } midcom::get('auth')->drop_sudo(); return true; }
/** * Helper function to create account based on data from DM2 * * @param midcom_db_person $person The person we're working on * @param midcom_helper_datamanager2_formmanager $formmanager The formmanager instance to use */ public function create_account(midcom_db_person $person, midcom_helper_datamanager2_formmanager $formmanager) { if (empty($formmanager->_types['username'])) { return; } $account_helper = new org_openpsa_user_accounthelper(); $formdata = $formmanager->get_submit_values(); $password = ""; //take user password? if ((int) $formdata['org_openpsa_user_person_account_password_switch'] > 0) { $password = $formmanager->_types['password']->value; } $account_helper->create_account($person->guid, $formmanager->_types["username"]->value, $person->email, $password, $formmanager->_types["send_welcome_mail"]->value); }
/** * 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); }
private function _update_account($fields) { $stat = false; $password = null; //new password? if (!empty($fields["new_password"]->value)) { $password = $fields["new_password"]->value; } $accounthelper = new org_openpsa_user_accounthelper($this->_person); // Update account $stat = $accounthelper->set_account($fields["username"]->value, $password); if (!$stat && midcom_connection::get_error() != MGD_ERR_OK) { // Failure, give a message midcom::get('uimessages')->add($this->_l10n->get('org.openpsa.user'), $this->_l10n->get("failed to update the user account, reason") . ': ' . midcom_connection::get_error_string(), 'error'); } return $stat; }
/** * @depends testCheck_password_strength * @depends testCheck_password_reuse * @depends testGenerate_safe_password */ public function testSet_account() { $accounthelper = new org_openpsa_user_accounthelper(self::$_user); $account = midcom_core_account::get(self::$_user); $password = $account->get_password(); $username = $account->get_username(); midcom::get('auth')->request_sudo('org.openpsa.user'); self::$_user->delete_parameter('org_openpsa_user_password', 'old_passwords'); self::$_user->delete_parameter('org_openpsa_user_password', 'last_change'); do { $new_password = $accounthelper->generate_safe_password(); } while ($password === $new_password); $new_username = $username . time(); $this->assertTrue($accounthelper->set_account($new_username, $new_password)); midcom::get('auth')->drop_sudo(); $this->assertEquals(midcom_connection::prepare_password($new_password), $account->get_password()); $this->assertEquals($new_username, $account->get_username()); $this->assertFalse(is_null(self::$_user->get_parameter('org_openpsa_user_password', 'last_change'))); $this->assertEquals(serialize(array($password)), self::$_user->get_parameter('org_openpsa_user_password', 'old_passwords')); }