I'm having a problem with a custom drupal authentication module that a contractor wrote to validate existing users in an external database on our internal site (i.e. not web facing).
When users try to login they are confronted by a HTTP 500 error (See here: https://dl.dropbox.com/u/1612084/http500.png) when they attempt to login on the site for the first. Subsequent login attempts are successful and do not show this HTTP 500 error. From my investigation it seems that the users are being successfully validated and the drupal user database is being updated with the new user information, but it's only on the first login attempt they are getting this HTTP 500 error.
I looked at the module code and couldn't find any obvious errors. I have listed the code below with some anonymisation in the hope that someone else may have a better idea on what the possible problem could be.
Many Thanks,
Saad.
When users try to login they are confronted by a HTTP 500 error (See here: https://dl.dropbox.com/u/1612084/http500.png) when they attempt to login on the site for the first. Subsequent login attempts are successful and do not show this HTTP 500 error. From my investigation it seems that the users are being successfully validated and the drupal user database is being updated with the new user information, but it's only on the first login attempt they are getting this HTTP 500 error.
I looked at the module code and couldn't find any obvious errors. I have listed the code below with some anonymisation in the hope that someone else may have a better idea on what the possible problem could be.
Many Thanks,
Saad.
Code:
<?php
// $Id$
/**
* Implementation of hook_form_alter().
* replace the local login validation handler with our own.
*/
function userauth_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'user_login' || $form_id == 'user_login_block') {
$form['pass']['#required'] = TRUE;
// If the user login form is being submitted, add our validation handler.
if (isset($form_state['post']['name'])) {
$testusername = $form_state['post']['name'];
//$testvalue = "bta";
if (($testusername == "babylink") || ($testusername == "babylink-<ANON>")
||($testusername == "babylink-<ANON>")||($testusername == "babylink-<ANON>"))
$form['#validate'][] = 'user_login_authenticate_validate';
else
{
// Find the local validation function's entry so we can replace it.
$array_key = array_search('user_login_authenticate_validate',
$form['#validate']);
if ($array_key === FALSE) {
// Could not find it. Some other module must have run form_alter().
// add our validation just before the final validator.
$final_validator = array_pop($form['#validate']);
$form['#validate'][] = 'userauth_login_validate';
$form['#validate'][] = $final_validator;
}
else {
// Found the local validation function. Replace with ours.
$form['#validate'][$array_key] = 'userauth_login_validate';
}
}
}
}
}
/**
* Form validation handler.
*/
function userauth_login_validate($form, &$form_state) {
global $user;
if (!empty($user->uid)) {
// Another module has already handled authentication.
return;
}
// Call our custom authentication function.
if (!userauth_authenticate($form_state['values'])) {
// Authentication failed.
form_set_error('name', t('Unrecognized username or password.'));
}
}
/**
* checking an external database
*/
function userauth_authenticate($form_values) {
global $userauth_authenticated;
$username = $form_values['name'];
$passwd = $form_values['pass'];
$server = '<DBSERVER>';
// Connect to MSSQL
$link = mssql_connect($server, '<USER>', '<PASS>');
if (!$link)
die('Something went wrong while connecting to MSSQL');
$testuserauth = mssql_query (
"SELECT [babydiary_name], [babydiary_reportpassword]" .
" FROM [badger3].[dbo].[babydiary_users] WHERE [babydiary_name]= '$username' AND [babydiary_reportpassword]='$passwd'");
$row1 = mssql_fetch_array($testuserauth);
$authusername =$row1[0];
$authpaswd =$row1[1];
if (($username == $authusername) && ($passwd == $authpaswd)) {
$userauth_authenticated = TRUE;
// Log user in, or register new user if not already present.
user_external_login_register($username, 'userauth');
// Write session, update timestamp, run user 'login' hook.
user_authenticate_finalize($form_state['values']);
return TRUE;
}
else {
return FALSE;
}
mssql_close($link);
}
/**
* Implementation of hook_user()
*/
function userauth_user($op, &$edit, &$account, $category = NULL) {
switch($op) {
case 'insert':
global $userauth_authenticated;
if ($userauth_authenticated) {
$email = mycompany_email_lookup($account->name);
db_query("UPDATE {users} SET mail = '%s' WHERE uid = %d", $email,
$account->uid);
}
break;
}
}