Skip to main content
edited the code to meet specific OP needs
Source Link
W.M.
  • 732
  • 6
  • 15

This is the code you need. In the Drupal site create the following module:

Update:

function my_special_login_menu()
{
    $items['authinticate-by-customerid/%']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)
{
    // post the data to /authinticate-by-customerid as json with a parameter / property named customerid holding the customerid
    $postData = file_get_contents('php://input');

    $Data = json_decode($postData);

    $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$Data->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
        drupal_goto('/user'); // or any other path to redirect logged in users to
    } 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 includesdata transferred between the customerid key)two sites.

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).

This is the code you need. In the Drupal site create the following module:

Update:

function my_special_login_menu()
{
    $items['authinticate-by-customerid'] = array( // or any other custom path you choose
        'title' => 'Login by customerid',
        'access callback' => true, // no point in authenticating at this point in code execution
        'page callback' => 'my_special_login_function',
        'type' => MENU_CALLBACK,
        );
    return $items;
}

function my_special_login_function($customerid)
{
    // post the data to /authinticate-by-customerid as json with a parameter / property named customerid holding the customerid
    $postData = file_get_contents('php://input');

    $Data = json_decode($postData);

    $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', $Data->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
        drupal_goto('/user'); // or any other path to redirect logged in users to
    } 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 data transferred between the two sites.

Source Link
W.M.
  • 732
  • 6
  • 15

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).