有这么一个例子,输入任何以dave开头的用户名,都会自动插入这个用户名到用户列表,并登陆。比如输入davejone,davaamy等等都可以。
<?php
/**
* Implements hook_form_alter().
* We replace the local login validation handler with our own.
*/
function authdave_form_alter(&$form, &$form_state, $form_id) {
// In this simple example we authenticate on username to see whether starts with dave
if ($form_id == 'user_login' || $form_id == 'user_login_block') {
$form['#validate'][] = 'authdave_user_form_validate';
}
}
/**
* Custom form validation function
*/
function authdave_user_form_validate($form, &$form_state) {
if (!authdave_authenticate($form_state)) {
form_set_error('name', t('Unrecognized username.'));
}
}
/**
* Custom user authentication function
*/
function authdave_authenticate($form_state) {
// get the first four characters of the users name
$username = $form_state['input']['name'];
$testname = drupal_substr(drupal_strtolower($username),0,4);
// check to see if the person is a dave
if ($testname == "dave") {
// if it’s a dave then use the external_login_register function
// to either log the person in or create a new account if that
// person doesn’t exist as a Drupal user
user_external_login_register($username, ‘authdave’);
watchdog('authdave','true!!!');
return TRUE;
} else {
watchdog('authdave','false!!!');
return FALSE;
}
}
/**
* Implements hook_user_insert().
*/
function authdave_user_insert(&$edit, &$account, $category = NULL) {
global $authdave_authenticated;
if ($authdave_authenticated) {
$email = mycompany_email_lookup($account->name);
// Set e-mail address in the users table for this user.
db_update('users')
->fields(
array(
'mail' => $email,
)
)
->condition('uid', $account->uid)
->execute();
}
}
也就是重新写了用户登录验证函数。改为authdave_user_form_validate。又通过user_external_login_registe 来吧新以dave开头的用户写入user表。但是始终无法用davejone或者其他用户名登录。在user表里面又插入了这些用户名和密码。但是在authmap表中没有任何数据。
于是在authdave_authenticate 加入watchdog测试。查看log日志。

结果显示authdave_authenticate返回结果是fales,也就是说 if ($testname == "dave") 这个判断错了。
但是我反复检查这两行
$username = $form_state['input']['name']; $testname = drupal_substr(drupal_strtolower($username),0,4);
应该没有错。为什么结果不对呢?
还有就是明明判断都是错了,为什么还进入user_external_login_register($username, ‘authdave’);这句里面。并且在这函数中调用的 user_set_authmaps()错误。
请帮忙看看。
Drupal 版本