Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Apress.Pro.Drupal.7.Development.3rd.Edition.Dec.2010.pdf
Скачиваний:
74
Добавлен:
14.03.2016
Размер:
13 Мб
Скачать
☆

Download from Wow! eBook <www.wowebook.com>

CHAPTER 6 ■ WORKING WITH USERS

External Login

Sometimes, you may not want to use Drupal’s local users table. For example, maybe you already have a table of users in another database or in LDAP. Drupal makes it easy to integrate external authentication into the login process.

Let’s implement a very simple external authentication module to illustrate how external authentication works. Suppose your company hires only people named Dave, and usernames are assigned based on first and last names. This module authenticates anyone whose username begins with the string dave, so the users davebrown, davesmith, and davejones will all successfully log in. Our approach will be to use form_alter() to alter the user login validation handler so that it runs our own validation handler. Here is sites/all/modules/custom/authdave/authdave.info:

name = Authenticate Daves

description = External authentication for all Daves. package = Pro Drupal Development

core = 7.x

files[] = authdave.module

And here is the actual authdave.module:

<?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) {

130

CHAPTER 6 ■ WORKING WITH USERS

// 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’); return TRUE;

}else { return FALSE;

}

}

In the authdave module (see Figure 6-5), we simply swap out the second validation handler for our own. Compare Figure 6-5 with Figure 6-3, which shows the local user login process.

131

CHAPTER 6 ■ WORKING WITH USERS

Figure 6-5. Path of execution for external login with a second validation handler provided by the authdave module (compare with Figure 6-3)

The function user_external_login_register() is a helper function that registers the user if this is the first login and then logs the user in. The path of execution is shown in Figure 6-6 for a hypothetical user davejones logging in for the first time.

132

CHAPTER 6 ■ WORKING WITH USERS

If the username begins with “dave” and this is the first time this user has logged in, a row in the users table does not exist for this user, so one will be created. However, no e-mail address has been provided like it was for Drupal’s default local user registration, so a module this simple is not a real solution if your site relies on sending e-mail to users. You’ll want to set the mail column of the users table so you will have an e-mail address associated with the user. To do this, you can have your module respond to the insert operation of the user hook, which is fired whenever a new user is inserted:

/**

* 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();

}

}

Savvy readers will notice that there is no way for the code to tell whether the user is locally or externally authenticated, so we’ve cleverly saved a global variable indicating that our module did authentication. We could also have queried the authmap table like so:

db_query("SELECT uid FROM {authmap} WHERE uid = :uid AND module = :module", array(':uid' => $account->uid, 'module' => 'authdave');

All users who were added via external authentication will have a row in the authmap table as well as the users table. However, in this case the authentication and hook_user_insert run during the same request, so a global variable is a good alternative to a database query.

133

CHAPTER 6 ■ WORKING WITH USERS

Figure 6-6. Detail of the external user login/registration process

134

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]