Registration form username auto generation

I am using Elgg 1.12.4 and profile manager 9.1. 

In the profile manager plugin settings there is an option titled "Generate username from email", it is working correctly only when the username grabbed from the email does not exists in the users_entity or the user being looked up has been validated already. 

if user one comes with email address abcd@email.com and registers, his username will be abcd. If a second user comes with email address abcd@@xyz.com, although the email username are the same the following code makes the username unique but only when abcd@email.com user has been validated. 

profile_manager/libs/hooks.php

// generate username
    $username = get_input('username');
    $email = get_input('email');
    if (empty($username) && !empty($email) && (elgg_get_plugin_setting("generate_username_from_email", "profile_manager") == "yes")) {
        
        $email_parts = explode('@', $email);
        $base_username = $email_parts[0];
        $tmp_username = $base_username;
        
        $i = 1;
        while (get_user_by_username($tmp_username)) {
            $tmp_username = $base_username . $i;
            $i++;
        }
                
        set_input('username', $tmp_username);
    }
}

Function get_user_by_username seems to be able to read only when the username has been already validated. 

Line 241 in http://reference.elgg.org/users_8php_source.html

248 
249  // Fixes #6052. Username is frequently sniffed from the path info, which,
250  // unlike $_GET, is not URL decoded. If the username was not URL encoded,
251  // this is harmless.
252  $username = rawurldecode($username);
253 
256 
257  // Caching
258  if ((isset($USERNAME_TO_GUID_MAP_CACHE[$username]))
259  && (_elgg_retrieve_cached_entity($USERNAME_TO_GUID_MAP_CACHE[$username]))) {
260  return _elgg_retrieve_cached_entity($USERNAME_TO_GUID_MAP_CACHE[$username]);
261  }
262 
263  $query = "SELECT e.* FROM {$CONFIG->dbprefix}users_entity u
264  JOIN {$CONFIG->dbprefix}entities e ON e.guid = u.guid
265  WHERE u.username = '$username' AND $access";
266 
267  $entity = get_data_row($query, 'entity_row_to_elggstar');
268  if ($entity) {
269  $USERNAME_TO_GUID_MAP_CACHE[$username] = $entity->guid;
270  } else {
271  $entity = false;
272  }
273 
274  return $entity;
275 }

Should I create my own SQL and remove the access part or there is another built function that I can use?

$query = "SELECT e.* FROM {$CONFIG->dbprefix}users_entity u
264  JOIN {$CONFIG->dbprefix}entities e ON e.guid = u.guid
265  WHERE u.username = '$username'";
  • If this happens, then its a bug with the plugin and the best thing is to report this to the plugin author in the plugin listing page.

    For time being, you have to options

    1. Use your own sql as you mentioned
    2. Use access_show_hidden_entities() before searching for existing users.
  • Thank you Webgalli. It looks like the issue appears to be with the browser's cache. I noticed that when I cleared my browser's cache after registering a test user this does not happen when attempting to register a another user with the same email id but different domain. I will ask the plugin author if he is aware of this.  

Form and related plugins

Form and related plugins

User-generated content, flexible user and group profiles, registration forms, custom file forms