3

I want my users to be able to login into Drupal with an external user database. I searched for a long time and don't get any module/trick/whatever that is out there. Please help me a little, I'm new to Drupal, I don't know anything about hooks or whatever. Isn't there a module exactly like this: http://drupal.org/project/ppauth only that I can use my own database with?

Help? Please, I'm desperate! :(

1 Answer 1

5

Is your external database provided by another authentication mechanism? There are mature modules for Active Directory, Social Network login, and many others besides.

If your database for authentication is totally custom, then you will need to write some custom code.

Drupal users generally login using the user_login form. When users login, a particular function, user_login_authenticate_validate, is called by the user_login form's submit hook. This function validates user credentials against the local database.

user_login_authenticate_validate can be replaced with another function by using hook_form_alter on the user_login_form. You will need to create a new module, and implement code something like the following...

/**
* Implements hook_form_FORM_ID_alter().
*/
function mymodule_form_user_login_alter(&$form, &$form_state, $form_id) {
  // Add your own validators to the form.
  $form['#validate'] = mymodule_validators();
}

/**
* Returns an array of user validation function callables.
* 
* @see user_login_default_validators().
*/
function mymodule_validators() {
  return array('user_login_name_validate', 'mymodule_authenticate_validate', 'user_login_final_validate');
}

/**
* Validation function, validates users against foo database.
* 
* @see user_login_authenticate_validate()
*/
function mymodule_authenticate_validate($form, &$form_state) {
  // Use the password and username from the form, and make sure you set
  // $form_state['uid'] to the correct uid for the auth user.
  $password = trim($form_state['values']['pass']);
  $username = $form_state['values']['name'];

 // Authenticate them against your database, and find their drupal uid....
 $form_state['uid'] = mydatabase_auth_function_that_returns_a_drupal_uid($username, $password);
}

Implementing your own authentication is a decision that you should not take lightly. Personally, I would never go down this path. There are serious security concerns when rolling your own authentication mechanism. Wherever possible, you should leverage mature and well-tested solutions.

1

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.