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.