1

I have a website which needs to authenticate users using drupal users table.

If authentication fails, then I want to run the authentication against users table from another database.

lf authentication is successful , then I want to register this user inside drupal, so the next this user next time he logs in , he get authenticated from drupal users table , not the external users table.

I wish to get a complete solution for that.

the following code is my attempt :

function external_auth_form_user_login_alter(&$form,$form_state){

$form['#validate'] = array( 'user_login_name_validate' );
user_login_authenticate_validate( $form, $form_state );
if(!empty($form_state['uid'])){

db_set_active('barakah');
$result=db_query('select username,password from users where username= :name   and password= :pass',array(':name' =>$form_state['input']['name'],':pass'=>$form_state['input']['pass']));
db_set_active('default');
if(!empty($result)){
$username = $form_state['input']['name'];
user_external_login_register($username, 'external_auth');

}




} 

}

but as if the code doesn't exist, nothing happens

5
  • Can't you just import the user from the old DB to Drupal?
    – Matt
    Jan 7, 2014 at 9:56
  • You should extract user informations from the DB and use the User Import module : drupal.org/project/user_import. What might be tricky is keeping the password, it depends how it's encoded in your old database.
    – Matt
    Jan 7, 2014 at 10:01
  • my solution should work, it is similar to many tutorials, but as if my code is not executing, external_auth_form_user_login_alter should alter user login form, but in my case it doesn't Jan 7, 2014 at 10:07
  • This is never going to work with hook_form_alter. This hook is called when displaying the form, not on form validation. You should add a hook_validate on the form.
    – Matt
    Jan 7, 2014 at 10:10
  • can you post a solution please ? modify my code and then let me copy it and try it for my self Jan 7, 2014 at 10:11

2 Answers 2

0

You can use:

drupal_external_login_register

Helper function for authentication modules. Either logs in or registers the current user

An example implementation is detailed here:

https://api.drupal.org/comment/13434#comment-13434

4
  • you are absolutely right, but what i want is that external authentication undergo only if internal authentication fails, i hope that you got any clue for that. Jan 5, 2014 at 11:18
  • i am doing this because i have upgraded a system to drupal, and i have users on the new website, and also i have users on the old website, when i tried solution for external drupal login, external login works, but internal login fails ! Jan 5, 2014 at 11:23
  • You just need to add the user login validate handlers in the correct order, so local drupal ones run first. Jan 5, 2014 at 21:11
  • i edited the question, and i hope that you got any clue about why my code is not working Jan 7, 2014 at 9:49
0

Try something like this :

function custom_user_validate_login_validate(&$form,$form_state){
    $form['#validate'][] = 'custom_user_validate_login_validate';
}

function custom_user_validate_form_user_login_alter($form, &$form_state) {
    drupal_set_message("Custom Validation"); //Just to check if the hook_validate is called
    user_login_authenticate_validate( $form, $form_state );
    if(empty($form_state['uid'])){ // remove the ! here
        db_set_active('barakah');
        $result=db_query('select username,password from users where username= :name   and password= :pass',array(':name' =>$form_state['input']['name'],':pass'=>$form_state['input']['pass']));
        db_set_active('default');
        if(!empty($result)){
        $username = $form_state['input']['name'];
        user_external_login_register($username, 'external_auth');
        }
    }
}

[Edit] Need to remove ['submit'] from hook_form_alter

[Edit2] Renaming the function names: replace custom_user_validate by your module name

[Edit3] Updating the name of hook_validate to match function names

5
  • i just tried it , it didn't work, not even print the custom message ! Jan 7, 2014 at 10:35
  • Just edited the answer, you need to remove ['submit'] from hook_form_alter
    – Matt
    Jan 7, 2014 at 10:44
  • didn't work, not even custom message display Jan 7, 2014 at 10:46
  • One last try: the hook validate gets called with this code.
    – Matt
    Jan 7, 2014 at 10:49
  • Also, you should remove the "!" in front of empty
    – Matt
    Jan 7, 2014 at 10:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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