Posted by Stefan Vaduva on December 1, 2010 at 9:29pm
We will use the member table from IPB in order to authenticate users in Drupal.
When an IPB user tries to login for the first time in Drupal, his account will be created. This is based on a nice tutorial written by Atchijov on https://www.touchnoc.com/node/86 (thanks Atchijov!).
First you will need to tell Drupal about our third party table by configuring the database details in settings.php:
<?php
/*
* 'default' is your main drupal database
* 'ipb' is your IPB database
*/
$databases = array (
'default' =>
array (
'default' =>
array (
'driver' => 'mysql',
'database' => 'main_database',
'username' => 'drupal_database_user',
'password' => 'drupal_database_pass',
'host' => 'localhost',
'port' => '',
'prefix' => '',
),
'ipb' =>
array (
'driver' => 'mysql',
'database' => 'ipb_database',
'username' => 'ipb_database_user',
'password' => 'ipb_database_pass',
'host' => 'localhost',
'port' => '',
'prefix' => '',
),
),
);
?>
Next, the module:
<?php
function grind_form_user_login_block_alter(&$form, &$form_state) {
_grind_user_login_form_alter($form, $form_state);
}
function grind_form_user_login_alter(&$form, &$form_state) {
_grind_user_login_form_alter($form, $form_state);
}
function _grind_user_login_form_alter(&$form, &$form_state) {
$saveForm = $form;
$form = array();
foreach( $saveForm as $key => $value ) {
if( $key == '#validate' ) {
$form[ $key ] = array();
foreach( $value as $validator ) {
if( $validator == 'user_login_authenticate_validate' ) {
$validator = 'grind_authenticate_validate';
}
$form[ $key ][] = $validator;
}
} else {
$form[ $key ] = $value;
}
}
}
function grind_authenticate_validate( $form, &$form_state ) {
user_login_authenticate_validate($form, $form_state);
if($form_state['uid'] !== false)
return;
$name = $form_state[ 'values' ][ 'name' ];
$pass = $form_state[ 'values' ][ 'pass' ];
//the "target" is your custom database connection from settings.php
$records = db_select( 'ibf_members', 'u', array( 'target' => 'ipb' ))
->fields( 'u', array( 'member_id', 'name', 'members_pass_hash', 'members_pass_salt', 'email' ))
->condition( 'u.name', $name, '=' )
->execute();
if($records->rowCount() == 1) {
$ipbUser = $records->fetchObject();
$hash = md5( md5( $ipbUser->members_pass_salt ) . md5( $pass ) ); //this is how the IPB password hash is generated
if($hash == $ipbUser->members_pass_hash) {
$account = user_external_load($name);
if (!$account) {
$userinfo = array(
'name' => $name,
'mail' => $ipbUser->email,
'pass' => user_password(),
'init' => $name,
'status' => 1,
'access' => REQUEST_TIME,
);
$account = user_save(drupal_anonymous_user(), $userinfo);
if (!$account) {
drupal_set_message(t("Error saving IPB account."), 'error');
$form_state['uid'] = false;
return;
}
user_set_authmaps($account, array("authname_grind" => $name));
}
$form_state['uid'] = $account->uid;
}
}
}
?>
Comments
The function I've took from
The function I've took from the tutorial for altering the form validation can be simplified like this:
<?php
function _grind_user_login_form_alter(&$form, &$form_state) {
$k = array_search('user_login_authenticate_validate', $form['#validate']);
$form['#validate'][$k] = 'grind_authenticate_validate';
}
?>
And now we have an official
And now we have an official module: http://drupal.org/project/ipbridge :)
Official module work for me.
Official module work for me. need some changing from the code. thanks stefan Vaduva.