You are here

noreqnewpass.module in No Request New Password 5

File

noreqnewpass.module
View source
<?php

/**
 * Implementation of hook_menu().
 */
function noreqnewpass_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/settings/noreqnewpass',
      'title' => t('No Request New Password'),
      'description' => t('Manage password preferences'),
      'access' => user_access('administer noreqnewpass'),
      'callback' => 'drupal_get_form',
      'callback arguments' => 'noreqnewpass_admin_form',
    );
  }
  if (variable_get('noreqnewpass_disabled', true)) {
    $items[] = array(
      'path' => 'user/password',
      //'callback' => 'drupal_not_found',
      'access' => FALSE,
    );
  }
  return $items;
}

/**
 * Implementation of hook_perm().
 */
function noreqnewpass_perm() {
  return array(
    'administer noreqnewpass',
    'can change your own password',
  );
}

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

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

/**
 * 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);
}
function noreqnewpass_user_login_validate($form_id, $form_values) {
  if ($form_values['name']) {
    if (user_is_blocked($form_values['name'])) {

      // blocked in user administration
      form_set_error('name', t('The username %name has not been activated or is blocked.', array(
        '%name' => $form_values['name'],
      )));
    }
    else {
      if (drupal_is_denied('user', $form_values['name'])) {

        // denied by access controls
        form_set_error('name', t('The name %name is a reserved username.', array(
          '%name' => $form_values['name'],
        )));
      }
      else {
        if ($form_values['pass']) {
          $user = user_authenticate($form_values['name'], trim($form_values['pass']));
          if (!$user->uid) {
            form_set_error('name', t('Sorry, unrecognized username or password.'));
            watchdog('user', t('Login attempt failed for %user.', array(
              '%user' => $form_values['name'],
            )));
          }
        }
      }
    }
  }
}

Functions

Namesort descending Description
noreqnewpass_admin_form FAPI definitions for administration form
noreqnewpass_form_alter Implementation of hook_form_alter().
noreqnewpass_menu Implementation of hook_menu().
noreqnewpass_perm Implementation of hook_perm().
noreqnewpass_user_login_validate