You are here

function genpass_form_alter in Generate Password 6

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

Implementation of hook_form_alter()

File

./genpass.module, line 72

Code

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

    // User admin settings form
    case 'user_admin_settings':
      $form['registration']['genpass_mode'] = array(
        '#type' => 'radios',
        '#title' => t('Password handling'),
        '#default_value' => variable_get('genpass_mode', GENPASS_DEFAULT),
        '#options' => array(
          GENPASS_DEFAULT => t('Users must enter a password on registration (default Drupal behaviour).'),
          GENPASS_OPTIONAL => t('Users may enter a password on registration. If not, a random password will be generated.'),
          GENPASS_RESTRICTED => t('Users cannot choose a password on registration. It will be generated.'),
        ),
        '#description' => t('Choose a password handling mode for new users. Note that if "Require e-mail verification when a visitor creates an account" is selected above, then the third option always applies for the regular user registration form. Also note that for manual user creation by an administrator, the second option always applies.'),
      );
      $form['registration']['genpass_length'] = array(
        '#type' => 'textfield',
        '#title' => t('Generated password length'),
        '#default_value' => variable_get('genpass_length', 8),
        '#size' => 2,
        '#maxlength' => 2,
        '#description' => t('Set the length of generated passwords here. Allowed range: 5 to 32.'),
      );
      $form['registration']['genpass_entropy'] = array(
        '#type' => 'textfield',
        '#title' => t('Generated password entropy'),
        '#default_value' => variable_get('genpass_entropy', _genpass_default_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']['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']['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';
      break;

    // User registration form
    case 'user_register':
      $mode = variable_get('genpass_mode', GENPASS_DEFAULT);

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

      // Administrator is creating the user
      if ($_GET['q'] == 'admin/user/user/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;
  }
}