This is the code you need. In the Drupal site create the following module:
function my_special_login_menu()
{
$items['authinticate-by-customerid/%'] = array( // or any other custom path you choose, % should stand eventually for the customerid
'title' => 'Login by customerid',
'access callback' => true, // no point in authenticating at this point in code execution
'page callback' => 'my_special_login_function',
'page arguments' => array(1),
'type' => MENU_CALLBACK,
);
return $items;
}
function my_special_login_function($customerid)
{
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'user')
->propertyCondition('uid', 1, '<>') // User number 1 should not be able to log in using this method
->fieldCondition('field_customerid', 'value', $customerid, '=')
->range(0, 1);
$result = $query->execute();
if (isset($result['user'])) {
$uid = array_keys($result['user'])[0];
$account = user_load($uid);
global $user;
$user->uid = $uid;
$user->name = $account->name;
$user->timezone = $account->timezone;
user_login_finalize(); // Log user in
} else {
drupal_goto('/user/register');
}
}
For this code to work you need Entity API module and you should serve the site over SSL in order to encrypt the URL (which includes the customerid key).