Drupal 6で、独自の認証を加える方法を書きます。
Drupal 6からはOpenIDがついているので、それを使うのも手です。しかし私は、既存の認証関数を利用したかったので、Drupal 6用に独自の認証モジュールを作りました。独自認証は、Usernameがユーザー名@chikunai.local の時に動くとします。改良すればドメイン名なしでもいけます。
Drupal 6では、Drupal 5にあったhook_authが無くなりました。
Distributed Authentication changes
http://drupal.org/node/114774#dist-auth
しかし、ここの説明にあるとおり、Site Networkモジュールを活用すれば、独自の認証が作れます。Site Networkモジュールは、Drupal 6で無くなったDrupalモジュールです。
Site Network
http://drupal.org/project/site_network
ではさっそくカスタマイズを進めます。
site_network.settings.inc は管理画面のファイルです。不要な項目がたくさんあるので、認証機能を有効・無効にするものだけにします。
- function _site_network_settings() {
- '#description' => t('Your Drupal site might accept logins with the user names of other Drupal sites.'),
- '#title' => t('Client'),
- '#type' => 'fieldset',
- );
- '#type' => 'checkbox',
- '#title' => t('Distributed Authentication'),
- '#default_value' => variable_get('site_network_client', FALSE),
- );
- }
site_network.module はSite Networkモジュールの本体です。site_network_form_user_login_alter()は、ログイン画面を表示させるところです。ここを書き換えます。// Login notice 以降はいらないので削除しちゃいました。
- function site_network_form_user_login_alter(&$form, $form_state) {
- if (variable_get('site_network_client', FALSE)) {
- // Reset the validate and submit functions
- module_load_include('authentication.inc', 'site_network');
- }
- return $form;
- }
site_network_help()、site_network_menu()の文言は適当に直してください。
site_network.authentication.inc は認証のファイルになります。_site_network_login_validate()を独自の認証関数を使うように書き換えます。独自の認証のアカウント名を「アカウント名@chikunai.local」としたときは以下のとおりです。
- function _site_network_login_validate(&$form, &$form_state) {
- if (variable_get('site_network_client', FALSE)) {
- // 独自認証用関数の入ったPHPファイル
- require_once('function_auth.php');
- // The username must be split in two: the first part is the
- // username itself and the second is the server
- $username = $name[0];
- $server = $name[1];
- // Check if the server is the own site
- if ($server == $site_url[1]) {
- }
- // Check if the server is the own site
- if ($server == $site_url[1]) {
- }
- // ドメインが無いときはDrupal認証
- _site_network_login( $username, $form_state['values']['pass']);
- }
- else {
- if ($server == 'chikunai.local') {
- // 独自の認証関数の呼び出し
- $aid = dokuji_auth( $username, $form_state['values']['pass']);
- }
- // Log using the external account if the authentication succeded
- // Put both username and server together again
- $name = "$username@$server";
- // Check if the account already exists or its a new user
- $account = user_external_load($name);
- user_external_login($account);
- }
- else {
- user_external_login_register($name, 'site_network');
- }
- }
- elseif (variable_get('site_network_client_server_only', FALSE)) {
- // Disable the other validation processes, since it already failed
- form_set_error('name', t("Sorry, unrecognized username or password."));
- }
- }
- }
- elseif (variable_get('site_network_client_server_only', FALSE)) {
- form_set_error('name', t('Server is required but not set'));
- }
- }
この記事用に作り変えたので、動作は未確認です(汗
あとは、うまく自分たちの環境に合わせてカスタマイズください。




新しいコメントの投稿