user_is_blocked() returns TRUE when called with an empty string

Created on 17 January 2020, almost 5 years ago
Updated 5 August 2023, over 1 year ago

With code similar to the following one, the code inside the if statement is executed.

if (user_is_blocked('')) {
  // The user is blocked.
}

It happens in any case the string passed to user_is_blocked() is empty.

🐛 Bug report
Status

Closed: works as designed

Version

7.0 ⚰️

Component
User module 

Last updated 7 days ago

Created by

🇮🇳India brijesh09

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇮🇹Italy apaderno Brescia, 🇮🇹
  • 🇮🇳India heykarthikwithu Bengaluru 🌍

    @apaderno condition check if the user is returning name or not will solve this?
    (below is the sample with couple of examples, one with object check & and other with using the fetchField)

    /**
     * Checks for usernames blocked by user administration.
     *
     * @param $name
     *   A string containing a name of the user.
     *
     * @return
     *   Object with property 'name' (the user name), if the user is blocked;
     *   FALSE if the user is not blocked.
     */
    function user_is_blocked($name) {
      $user = db_select('users')
        ->fields('users', array('name'))
        ->condition('name', db_like($name), 'LIKE')
        ->condition('status', 0)
        ->execute()->fetchObject();
      // Check if name is empty, if empty return FALSE.
      if (empty($user->name)) {
        return FALSE;
      }
      return $user;
    }
    /**
     * Checks for usernames blocked by user administration.
     *
     * @param $name
     *   A string containing a name of the user.
     *
     * @return
     *   Object with property 'name' (the user name), if the user is blocked;
     *   FALSE if the user is not blocked.
     */
    function user_is_blocked($name) {
      $user = db_select('users')
        ->fields('users', array('name'))
        ->condition('name', db_like($name), 'LIKE')
        ->condition('status', 0)
        ->execute();
      // Check if user name is empty, if empty return FALSE.
      if (empty($user->fetchField)) {
        return FALSE;
      }
      return $user->fetch();
    }

    Thanks :)

  • Status changed to Closed: works as designed over 1 year ago
  • 🇸🇰Slovakia poker10

    Thanks for reporting this.

    I agree with the #5, that this is an expected behavior. Whan you install a Drupal 7 site, there is an user with UID 0 and empty username in the database. Therefore the query will test this user, which is blocked, as it is an internal user for handling default access permissions. I would say that adding additional conditions here would make the function less clear, therefore closing this.

Production build 0.71.5 2024