You are here

noreqnewpass.module in No Request New Password 7.2

File

noreqnewpass.module
View source
<?php

/**
 * Implementation of hook_menu().
 */
function noreqnewpass_menu() {
  $items = array();
  $items['admin/config/people/noreqnewpass'] = array(
    'title' => t('No Request New Password'),
    'description' => t('Manage password preferences'),
    'access arguments' => array(
      'administer noreqnewpass',
    ),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'noreqnewpass_admin_form',
    ),
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
 * Implementation of hook_menu_alter().
 */
function noreqnewpass_menu_alter(&$items) {
  $items['user/password']['access callback'] = 'noreqnewpass_access_check';
}
function noreqnewpass_access_check() {
  if (variable_get('noreqnewpass_disabled', TRUE)) {
    return FALSE;
  }
  return TRUE;
}

/**
 * Implementation of hook_permission().
 */
function noreqnewpass_permission() {
  return array(
    'administer noreqnewpass' => array(
      'title' => t('Administer No Request New Password'),
      'description' => t('Administer No Request New Password module'),
    ),
    'can change your own password' => array(
      'title' => t('Can change your own password'),
      'description' => t('Disable this module for a certain role.'),
    ),
  );
}

/**
 * Implementation of hook_form_alter().
 */
function noreqnewpass_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'user_login_block' && variable_get('noreqnewpass_disabled', true)) {
    $items = array();
    if (variable_get('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL)) {
      $items[] = l(t('Create new account'), 'user/register', array(
        'attributes' => array(
          'title' => t('Create a new user account.'),
        ),
      ));
    }
    $form['links'] = array(
      '#markup' => theme('item_list', array(
        'items' => $items,
      )),
    );
  }
  if (($form_id == 'user_login_block' || $form_id == 'user_login') && variable_get('noreqnewpass_disabled', true)) {
    $key = array_search('user_login_final_validate', $form['#validate']);
    $form['#validate'][$key] = 'noreqnewpass_user_login_final_validate';
  }

  // Remove pass field from user edit form if he cant change
  if ($form_id == 'user_profile_form' && !user_access("can change your own password")) {
    $form['account']['current_pass']['#access'] = false;
    $form['account']['pass']['#access'] = false;
  }
}

/**
 * FAPI definitions for administration form
 * 
 * @return 
 */
function noreqnewpass_admin_form() {
  $form = array();
  $form['noreqnewpass_disabled'] = array(
    '#title' => t('Disable Request new password link'),
    '#type' => 'checkbox',
    '#default_value' => variable_get('noreqnewpass_disabled', true),
    '#description' => t('If checked, Request new password link will be disabled.'),
  );
  return system_settings_form($form);
}

/**
 * Just for remove request password url at error messages.
 */
function noreqnewpass_user_login_final_validate($form_id, &$form_state) {
  if (empty($form_state['uid'])) {

    // Always register an IP-based failed login event.
    flood_register_event('failed_login_attempt_ip', variable_get('user_failed_login_ip_window', 3600));

    // Register a per-user failed login event.
    if (isset($form_state['flood_control_user_identifier'])) {
      flood_register_event('failed_login_attempt_user', variable_get('user_failed_login_user_window', 21600), $form_state['flood_control_user_identifier']);
    }
    if (isset($form_state['flood_control_triggered'])) {
      if ($form_state['flood_control_triggered'] == 'user') {
        form_set_error('name', format_plural(variable_get('user_failed_login_user_limit', 5), 'Sorry, there has been more than one failed login attempt for this account. It is temporarily blocked. Try again later or <a href="@url">request a new password</a>.', 'Sorry, there have been more than @count failed login attempts for this account. It is temporarily blocked. Try again later.'));
      }
      else {

        // We did not find a uid, so the limit is IP-based.
        form_set_error('name', t('Sorry, too many failed login attempts from your IP address. This IP address is temporarily blocked. Try again later.'));
      }
    }
    else {
      form_set_error('name', t('Sorry, unrecognized username or password.'));
      watchdog('user', 'Login attempt failed for %user.', array(
        '%user' => $form_state['values']['name'],
      ));
    }
  }
  elseif (isset($form_state['flood_control_user_identifier'])) {

    // Clear past failures for this user so as not to block a user who might
    // log in and out more than once in an hour.
    flood_clear_event('failed_login_attempt_user', $form_state['flood_control_user_identifier']);
  }
}

Functions

Namesort descending Description
noreqnewpass_access_check
noreqnewpass_admin_form FAPI definitions for administration form
noreqnewpass_form_alter Implementation of hook_form_alter().
noreqnewpass_menu Implementation of hook_menu().
noreqnewpass_menu_alter Implementation of hook_menu_alter().
noreqnewpass_permission Implementation of hook_permission().
noreqnewpass_user_login_final_validate Just for remove request password url at error messages.