0

I know its dump question, using login form and insert user in database.

Actually, I have an API that includes users of company/organization and these users already have registered on other applications. So, using such API I can get user info using username and password.

Of course, we need to logged in users directly (without registration) who already have account on other applications.

For logging in, I need to, first, insert the user info in data (before insertion, I can fetch username and password from API) so user can get in the site as logged in user.

  • After fetching from API, how to create user in the database with username and password field only?
  • It should be done without email info.
  • mail is a required property, you can't have a Drupal account without an email address – Clive Oct 26 '17 at 12:00
  • It can be done without email address. See my answer. – usmanjutt84 Oct 27 '17 at 7:10
0

It is not possible to create a user account in Drupal without writing an email account.

An example of how you can create a user programmatically is:

 $new_user = array(
  'name' => 'name',
  'mail' => 'name@email.com',
  'pass' => 'password', // note: do not md5 the password
  'status' => 1
 );

 //custom fields
 $new_user->field_name_lasname['und'][0]['value'] = 'Name and Last Name';
 //custom fields ( custom fields) 
 $new_user->field_referenced_field_['und'][0]['target_id'] = 0;

 user_save(null, $new_user);

Please excuse me my English is not good

  • It can be done without email address. See my answer – usmanjutt84 Oct 27 '17 at 7:10
0

According to my question requirements, it can be done by using the user_external_login_register and user_external_load Drupal APIs. See the custom module below for more understanding. I hope this will help someone who needs the same requirement(s).

<?php
/**
* Implementation of hook_form_FORM_ID_alter
*/
function hook_form_user_login_alter(&$form, &$form_state) {
    _subscribe_user_login_form_alter($form, $form_state);
}
function _subscribe_user_login_form_alter(&$form, &$form_state) {
    $saveForm = $form;
    $form = array();
    // overrides the default validator
    foreach( $saveForm as $key => $value ) {
        if( $key == '#validate' ) {
            $form[ $key ] = array();
            foreach( $value as $validator ) {
                if( $validator == 'user_login_authenticate_validate' ) {
                    $validator = 'MODULENAME_authenticate_validate';
                }
            $form[ $key ][] = $validator;
            }
        } else {
            $form[ $key ] = $value;
        }
    }
}

function MODULENAME_authenticate_validate( $form, &$form_state ) {
    $name = $form_state[ 'values' ][ 'name' ];
    $pass = $form_state[ 'values' ][ 'pass' ];
    // use Drupal authentication for this user
    if ($name == 'admin'){
        return user_login_authenticate_validate($form, $form_state);
    }
    $authenticated = api_check_user($name, $pass);
    if ($authenticated == TRUE){
        //save external user if don't exist
        user_external_login_register($name, "MODULENAME");
        // we must set the 'uid' to pass back.  this looks up the logged in user and sets the Drupal UID
        $account = user_external_load($name);
        $form_state['uid'] = $account->uid;
        drupal_set_message(t('an error has occured'));
    }else{
        drupal_set_message('Error displayed'); // do nothing, Drupal will handle the errors
    }
}
function api_check_user($name, $pass){
    //write code to authenticate user from API
    //if username and password match then API returns true/false
    //if we get true
    return TRUE;
}

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.