You are here

no_autocomplete.module in No Autocomplete 7

Same filename and directory in other branches
  1. 8 no_autocomplete.module
  2. 6 no_autocomplete.module

No_autocomplete.module.

This module adds the autocomplete=off attribute to selected key user forms. On a browser that respects this setting, it means that the browser will not try to autocomplete the password on the user login forms, or the whole user edit form.

Many browsers, including current Chrome as of this writing and LastPass do not respect this setting. But it can 1. Offer some security on the login form 2. Prevent naive users from getting the browser-fill on the first password form and not the second, making them angry and confused.

File

no_autocomplete.module
View source
<?php

/**
 * @file
 * No_autocomplete.module.
 *
 * This module adds the autocomplete=off attribute to selected key user forms.
 * On a browser that respects this setting, it means that the browser will not
 * try to autocomplete the password on the user login forms, or the whole
 * user edit form.
 *
 * Many browsers, including current Chrome as of this writing and LastPass
 * do not respect this setting. But it can
 * 1. Offer some security on the login form
 * 2. Prevent naive users from getting the browser-fill on the first password
 *    form and not the second, making them angry and confused.
 */

/**
 * Implements hook_help().
 */
function no_autocomplete_help($path, $arg) {
  switch ($path) {

    // Main module help.
    case 'admin/help#no_autocomplete':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('The No Autocomplete module adds the autocomplete=off attribute to selected key user forms. On a browser that respects this setting, it means that the browser will not try to autocomplete the password on the user login forms.') . '</p>';
      $output .= '<h3>' . t('Uses') . '</h3>';
      $output .= '<dl>';
      $output .= '<dt>' . t('Configuring the module') . '</dt>';
      $output .= '<dd>' . t("To configure the module visit the <a href='@config-page'>No Autocomplete</a> page, <em>check</em> which feature you'd like to enable and save the configuration. For this you need the <em>Administer No Autocomplete</em> permission.", array(
        '@config-page' => url('admin/config/people/no_autocomplete'),
      )) . '</dd>';
      $output .= '</dl>';
      return $output;
  }
}

/**
 * Implements hook_menu().
 *
 * Add admin settings form.
 */
function no_autocomplete_menu() {
  $items['admin/config/people/no_autocomplete'] = array(
    'title' => 'No Autocomplete',
    'description' => 'Turn on autocomplete=off for key user forms',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'no_autocomplete_admin_settings_form',
    ),
    'access arguments' => array(
      'administer no_autocomplete',
    ),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'no_autocomplete.admin.inc',
  );
  return $items;
}

/**
 * Implements hook_form_alter().
 *
 * Turn off autocomplete on key forms.
 */
function no_autocomplete_form_alter(&$form, $form_state, $form_id) {

  // Add to the user_login and user_profile_form.
  if (substr($form_id, 0, 10) == 'user_login' && variable_get('no_autocomplete_login_form', FALSE)) {
    $form['pass']['#attributes']['autocomplete'] = 'off';
  }
}

/**
 * Implements hook_permission().
 */
function no_autocomplete_permission() {
  return array(
    'administer no_autocomplete' => array(
      'title' => t('Administer No Autocomplete'),
      'description' => t('Allow access to configure the module settings.'),
    ),
  );
}