You are here

public static function UserRestrictions::check in User restrictions 7

Checks the user input against user restrictions.

This was handled by drupal_is_denied() in version of Drupal prior to 7.x.

Parameters

$form_state: The array passed to form API functions.

$form_type: The form for which the function is invoked.

Return value

A message error if there are restrictions agains the entered value, or an empty string if there are no restrictions that do not allow to use the entered value.

5 calls to UserRestrictions::check()
user_restrictions_login_form_validate in ./user_restrictions.module
Form validation handler for user_login(), and user_login_block().
user_restrictions_ui_check_email_submit in ./user_restrictions_ui.admin.inc
Form submission handler for user_restrictions_ui_overview_form().
user_restrictions_ui_check_username_submit in ./user_restrictions_ui.admin.inc
Form submission handler for user_restrictions_ui_overview_form().
user_restrictions_user_profile_form_validate in ./user_restrictions.module
Form validation handler for user_profile_form().
user_restrictions_user_register_form_validate in ./user_restrictions.module
Form validation handler for user_register_form().

File

./user_restrictions.classes.inc, line 57
Contains the classes used by the User restrictions module.

Class

UserRestrictions
The main class used by the User Restrictions module.

Code

public static function check($form_state, $form_type = 'login') {
  $result =& drupal_static('user_restrictions_check', array());
  if (isset($result[$form_type])) {
    return $result[$form_type];
  }
  $fields = module_invoke_all('user_restrictions_info', 'fields', $form_state, $form_type);
  foreach ($fields as $type => $mask) {
    $args = array(
      ':type' => $type,
      ':subtype' => $form_type,
      ':mask' => $mask,
      ':now' => REQUEST_TIME,
    );
    $context = array(
      'type' => $type,
      'mask' => $mask,
      'time' => REQUEST_TIME,
      'form_state' => $form_state,
      'form_type' => $form_type,
    );
    $sql = "SELECT 1 FROM {user_restrictions} WHERE type = :type AND (subtype = :subtype OR subtype = '') AND LOWER(:mask) LIKE LOWER(mask) AND status = :status AND (expire > :now OR expire = 0)";

    // We deny access if the only matching records in the {user_restrictions}
    // table have status 0 (deny). If any have status 1 (allow), or if there are
    // no matching records, we allow access.
    $denied = db_query_range($sql, 0, 1, $args + array(
      ':status' => 0,
    ))
      ->fetchField() && !db_query_range($sql, 0, 1, $args + array(
      ':status' => 1,
    ))
      ->fetchField();
    $error = array(
      'field' => '',
      'message' => '',
    );

    // Allow third-party modules to alter the user restriction rule.
    drupal_alter('user_restrictions', $denied, $error, $context);
    if ($denied) {
      $result[$form_type] = $error;

      // If the rules module is enabled, an event is triggered signifying
      // that a user has been restricted access.
      if (module_exists('rules')) {
        rules_invoke_event('user_restrictions_denied', $type, $mask, $form_type);
      }
      break;
    }
  }
  return $error;
}