1

I have two websites - One of them is Drupal 7, and the other is a different site (Site #2). In Site #2, within the user tables, is a field called "customid". Each user on Site #2 has a unique, randomly generated 30 character alphanumeric string within their respective "customid" field.

I can pull the value of the "customid" field for the user on Site #2, and use it as an input value in a form post to the Drupal 7 site.

In the Drupal 7 site, I created a custom user field "customid" as well.

Is it possible to make a form post from Site #2 to Drupal 7 - with the "customid" value as an input, and if it matches the "customid" field in the Drupal user table, log them in, else - redirect to the registration page?

We're essentially bypassing the username/password to log them in - I'm trying to log them in via custom user field.

I searched everywhere, and just couldn't find anything where something like this had been discussed. Thanks anyone for your time and help!

UPDATE:

I've tried implementing W.M.'s solution below, however upon attempting to enable the module, I get the white screen of death. Nothing shows up in the logs for the error.

For my custom module, I have two files:
ra_login.module
ra_login.info

Info file:

name = RA Login
description = Check ID and Login if there is a match, else go to Registration Page.
core = 7.x

; Information added by Drupal.org packaging script on 2013-12-16
version = "7.x-1.0"
core = "7.x"
project = "ra_login"
datestamp = "1387203506"

Module File:

<?php

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

function ra_login_function($CONTACTID)
{
    // post the data to /authenticate-by as json with a parameter / property named CONTACTID holding the CONTACTID
    $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_contactid', 'value', $Data->CONTACTID, '=')
        ->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');
    }

}

Again, for some reason when I try to enable the module, I get a white screen. Thanks for anyone who can help!

UPDATE:

I turned on errors in php.ini - now instead of the white screen, it shows me the following error:

Parse error: syntax error, unexpected '[' in C:\inetpub\wwwroot\sites\all\modules\ra_login\ra_login.module on line 31 

Line 31 is:

$uid = array_keys($result['user'])[0];

Any help is greatly appreciated!

  • Make sure logging & display errors is enabled inside php.ini .. Make sure display errors is on for all types of errors in Drupal admin pages – W.M. Apr 14 '16 at 17:21
  • W.M - I modified php.ini to show errors - now instead of the white screen, it shows me the following error: Parse error: syntax error, unexpected '[' in C:\inetpub\wwwroot\sites\all\modules\ra_login\ra_login.module on line 31 Here is line 31: $uid = array_keys($result['user'])[0]; – NickyTheWrench Apr 14 '16 at 17:38
  • What php version are you running? – W.M. Apr 14 '16 at 18:25
  • I'm currently running PHP version 5.3.28 – NickyTheWrench Apr 14 '16 at 18:27
  • Try to change this: $uid = array_keys($result['user'])[0]; into this: $array_keys = array_keys($result['user']); $uid = $array_keys[0]; .. and report back :) – W.M. Apr 14 '16 at 19:04
0
function YOUR_MODULE_page_build(&$page) {
  if(isset($_POST['customid'])){
    $customid=$_POST['customid']; //get your id
    $uid=get_uid_based_on_customid($customid);//you function to identify if someone has customid that customid
    if(empty($uid)){
        //if uid not found send user to login
        drupal_goto('/user');
    }else{
        //if uid found load user, authenticat it and redirect to page
        global $user; //global because is same var as the one drupal uses
        $user = user_load($uid);//set used data for you and drupal
        $login_array = array('name' => $user->name);
        user_login_finalize($login_array);//key function, will record user sessions
        drupal_set_message(t('User loged in!'));
        drupal_goto('/');
    }
  }
}

function get_uid_based_on_customid($customid){

  $query = new EntityFieldQuery();
  $query->entityCondition('entity_type', 'user')
  //->propertyCondition('uid', 1, '<>')
  // User number 1 should not be able to log in using this method because he is always admin, pay attention and uncomment when you go to prod
  ->fieldCondition('field_customid', 'value', $customid, '=')
  ->range(0,1);

  $result = $query->execute();

  if(!empty($result)){
    $data=$result['user'];
    reset($data);
    $first_element=current($data);
    $uid = $first_element->uid;

    return $uid;
  }else{
    return '';
  }

}
  • I created a custom module with your code, however when I try to enable it - I get a white screen of death. Thank you for your time! – NickyTheWrench Apr 14 '16 at 15:47
  • have you checked the logs? I would gladly help you if you could provide more details. V.M.'s solution is also correct but the fact you got a white screen on both soutions shows a issue on your side. – Rotari Radu Apr 14 '16 at 16:07
  • Hey Rotari, The log is not showing any errors, unfortunately. I updated my question above to show how I implemented W.M.'s code within a custom module. Maybe you can take a look and see where I am going wrong? Thank you again for any help you can provide. – NickyTheWrench Apr 14 '16 at 17:15
  • Rotari, I turned on errors - and now instead of the white screen, I get this error from your code: Parse error: syntax error, unexpected ';' in C:\inetpub\wwwroot\sites\all\modules\ra_login2\ra_login2.module on line 16 Here is line 16: drupal_set_message(t('User loged in!'); Any help is greatly appreciated! – NickyTheWrench Apr 14 '16 at 18:02
  • was just a parenthesis missing code fully updated and tested(see the original answer)(replace YOUR_MODULE and is ready to go) – Rotari Radu Apr 14 '16 at 20:26
1

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.

  • Thanks for your time on this. I created the custom module, however when I try to enable it - I get the white screen of death. Can't find where the bug in the code is. Also - would really love to send the customid via form post to drupal, rather than putting it in a URL to the drupal site. Thank you again for any help you can provide! – NickyTheWrench Apr 14 '16 at 15:59
  • @NickyTheWrench Your module file must start with <?php – W.M. Apr 14 '16 at 16:19
  • I updated my question above, showing how I implemented your code - if maybe you can take a look, that would be great! Thank you again for your time! – NickyTheWrench Apr 14 '16 at 17:14
0

I have no exact solution for your problem, but get idea how to deal with this,

By implementing following algorithm you can achieve.

  • Requirement

    -In Drupal Site you are saving "customid" this id is refer to "Site #2".
    -When you store this "customid" in drupal database you need to update password field also and store this "customid" in password field by drupal encryption format.


  • Now how to login by "customid"
    -From site#2 you are sending "customid" to drupal site, write custom module to catch this customid in to drupal site.
    -Now check this "customid" into drupal db and fetch user_name of this related "customid".
    -Now you have user_name and password(customid), pass this to the drupal login form post function.
    -Drupal will check user_name and password if match user will log in.
0

Please review this contrib module: https://www.drupal.org/project/login_custom_field

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.