You are here

function genpass_form_alter in Generate Password 7

Same name and namespace in other branches
  1. 8 genpass.module \genpass_form_alter()
  2. 5 genpass.module \genpass_form_alter()
  3. 6 genpass.module \genpass_form_alter()

Implementation of hook_form_alter()

File

./genpass.module, line 76

Code

function genpass_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id) {

    // User admin settings form at admin/config/people/accounts
    case 'user_admin_settings':
      $form['registration_cancellation']['genpass_mode'] = array(
        '#type' => 'radios',
        '#title' => t('Password handling'),
        '#default_value' => variable_get('genpass_mode', GENPASS_REQUIRED),
        '#options' => array(
          GENPASS_REQUIRED => t('Users <strong>must</strong> enter a password on registration. This is disabled if e-mail verification is enabled above.'),
          GENPASS_OPTIONAL => t('Users <strong>may</strong> enter a password on registration. If left empty, a random password will be generated. This always applies when an administer is creating the account.'),
          GENPASS_RESTRICTED => t('Users <strong>cannot</strong> enter a password on registration; a random password will be generated. This always applies for the regular user registration form if e-mail verification is enabled above.'),
        ),
        '#description' => t('Choose a password handling mode for new users.'),
      );
      $form['registration_cancellation']['genpass_length'] = array(
        '#type' => 'textfield',
        '#title' => t('Generated password length'),
        '#default_value' => variable_get('genpass_length', 12),
        '#size' => 2,
        '#maxlength' => 2,
        '#description' => t('Set the length of generated passwords here. Allowed range: 5 to 32.'),
      );
      $form['registration_cancellation']['genpass_entropy'] = array(
        '#type' => 'textfield',
        '#title' => t('Generated password entropy'),
        '#size' => 100,
        '#default_value' => variable_get('genpass_entropy', _GENPASS_REQUIRED_entropy()),
        '#description' => t('Give a list of possible characters for a generated password. Note that the list must contain at least X different characters where X is defined by the length you have given above.'),
      );

      // Provide a selection mechanism to choose the preferred algorithm for
      // generating passwords. Any module which implements hook_password() is
      // displayed here.
      $form['registration_cancellation']['genpass_algorithm'] = array(
        '#type' => 'radios',
        '#title' => t('Password generation algorithm'),
        '#default_value' => genpass_algorithm_module(),
        '#options' => genpass_add_samples(genpass_algorithm_modules()),
        '#description' => t('If third party modules define a password generation algorithm, you can select which one to use. Note that algorithms other than genpass will ignore the preferred entropy and password length. The currently selected algorithm produced the password @pw.', array(
          '@pw' => genpass_generate(),
        )),
      );
      $form['registration_cancellation']['genpass_display'] = array(
        '#type' => 'radios',
        '#title' => t('Generated password display'),
        '#default_value' => variable_get('genpass_display', GENPASS_DISPLAY_BOTH),
        '#options' => array(
          GENPASS_DISPLAY_NONE => t('Do not display.'),
          GENPASS_DISPLAY_ADMIN => t('Display when site administrators create new user accounts.'),
          GENPASS_DISPLAY_USER => t('Display when users create their own accounts.'),
          GENPASS_DISPLAY_BOTH => t('Display to both site administrators and users.'),
        ),
        '#description' => t('Whether or not the generated password should display after a user account is created.'),
      );
      $form['#validate'][] = 'genpass_user_admin_settings_validate';

      // Move the "When cancelling a user account" field down.
      $form['registration_cancellation']['user_cancel_method']['#weight'] = 1;
      break;

    // User registration form at admin/people/create
    case 'user_register_form':
      $mode = variable_get('genpass_mode', GENPASS_REQUIRED);

      // Add validation function, where password may get set
      $form['#validate'][] = 'genpass_register_validate';

      // Administrator is creating the user
      if ($_GET['q'] == 'admin/people/create') {

        // Switch to optional mode
        $mode = GENPASS_OPTIONAL;

        // Help avoid obvious consequence of password being optional
        $notify_item =& _genpass_get_form_item($form, 'notify');
        $notify_item['#description'] = t('This is recommended when auto-generating the password; otherwise, neither you nor the new user will know the password.');
      }

      // Pass mode to validation function
      $form['genpass_mode'] = array(
        '#type' => 'value',
        '#value' => $mode,
      );
      $pass_item =& _genpass_get_form_item($form, 'pass');
      switch ($mode) {

        // If password is optional, don't require it, and give the user an
        // indication of what will happen if left blank
        case GENPASS_OPTIONAL:
          $pass_item['#required'] = FALSE;
          $pass_item['#description'] = (empty($pass_item['#description']) ? '' : $pass_item['#description'] . ' ') . t('If left blank, a password will be generated for you.');
          break;

        // If password is restricted, remove access
        case GENPASS_RESTRICTED:
          $pass_item['#access'] = FALSE;
          $pass_item['#required'] = FALSE;
          break;
      }
      break;
  }
}