Drupal 6、独自認証モジュールの作り方

in

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 は管理画面のファイルです。不要な項目がたくさんあるので、認証機能を有効・無効にするものだけにします。

<?php
function _site_network_settings() {
  
$form['client'] = array(
    
'#description'    => t('Your Drupal site might accept logins with the user names of other Drupal sites.'),
    
'#title'          => t('Client'),
    
'#type'           => 'fieldset',
  );
  
$form['client']['site_network_client'] = array(
    
'#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 以降はいらないので削除しちゃいました。

<?php
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');
    
array_unshift($form['#validate'], '_site_network_login_validate');
  }
  return 
$form;
}
?>

site_network_help()、site_network_menu()の文言は適当に直してください。

site_network.authentication.inc は認証のファイルになります。_site_network_login_validate()を独自の認証関数を使うように書き換えます。独自の認証のアカウント名を「アカウント名@chikunai.local」としたときは以下のとおりです。

<?php
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
    
$name     explode('@'trim($form_state['values']['name']));
    
$username $name[0];
    
$server $name[1];

    
// Check if the server is the own site
    
global $base_url;
    
$site_url explode('://'$base_url);
    if (
$server == $site_url[1]) {
      unset(
$server);
    }

    
// Check if the server is the own site
    
global $base_url;
    
$site_url explode('://'$base_url);
    if (
$server == $site_url[1]) {
      unset(
$server);
    }

    if (!(
$server == 'chikunai.local' or isset($server) or $server == '')) {
      
// ドメインが無いときは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
      
if (!empty($aid)) {
        
// 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);
        if (isset(
$account->uid)) {
          
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_state['#validate'] = array();
        
$form_state['#submit']  = array();

        
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'));
  }
}
?>

この記事用に作り変えたので、動作は未確認です(汗

あとは、うまく自分たちの環境に合わせてカスタマイズください。



コメント

新しいコメントの投稿

  • ウェブページアドレスとメールアドレスは、自動的にハイパーリンクに変換されます。
  • 使用できるHTMLタグ: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • 行と段落は自動的に折り返されます。
  • You may insert videos with [video:URL]
  • You may use [inline] tags to display contents inline.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]".
  • You can use BBCode tags in the text. URLs will automatically be converted to links.

書式オプションに関するさらに詳しい情報...

CAPTCHA
この問題はユーザが人間であるかどうかをテストし、スパムによる自動投稿を防ぐためのものです。
イメージ CAPTCHA
画像に表示されている文字をそのまま入力してください。(大文字・小文字は区別されます。)

Back to top