`
haohappy2
  • 浏览: 317404 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

drupal 外部验证登陆

阅读更多

Drupal 6.x 和 5.x 的 HOOK 函数和参数表有很大的差别,所以建议大家多看看 Drupal API手册。后来绕了一大圈发现 Drupal 6.x 的默认模组 (modules) 里面,有一个叫 OpenId 的模组可以实现这个功能。当然,我们的外部身份验证需求通常不唯一的,比如可能是 Discuz! 论坛或者你当前站点的身份验证,制定一个外部身份验证模组正是我们今天的题目解决的问题。

首先我们做开发一个模组的例行工作

exuser.info- (模组信息)

name = External authenticate
description = Allows users to log using external authenticate.
package = Bun Drupal
version = 0.1
core = 6.x

exuser.install- (模组安装脚本)

<?php
/**
* Implementation of hook_install().
*/
function exuser_install() {
  // Create table.
  drupal_install_schema('exuser');
}
 
/**
* Implementation of hook_uninstall().
*/
function exuser_uninstall() {
  // Remove table.
  drupal_uninstall_schema('exuser');
}
 
/**
* Implementation of hook_schema().
*/
function exuser_schema() {
  $schema['exuser_authenticate'] = array(
    'description' => 'Stores eway users',
    'fields' => array(
      'uid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'name' => array(
        'type' => 'char',
        'length' => 11,
        'not null' => TRUE,
      ),
    ),
    'primary key' => array('uid'),
  );
  return $schema;
}

这个脚本告诉 Drupal 安装的时候会生成一个数据表 exuser_authenticate,里面的两个字段是 Drupal 的 uid 和你的身份验证的用户名,因为每个验证用户都需要在 Drupal 的 users 表产生一个用户记录,那么 exuser_authenticate 就是一个对照表

exuser.module- (模组脚本)

<?php
/**
* Implementation of hook_form_alter.
*/
function exuser_form_alter(&$form, $form_state, $form_id)
{
  if ($form_id == 'user_login_block' || $form_id == 'user_login') {
    drupal_add_js(drupal_get_path('module', 'exuser') .'/exuser.js');
    $form['external_identifier'] = array(
      '#type' => 'checkbox',
      '#title' => '外部登录',
      '#weight' => -3,
    );
    $form['external_name'] = array(
      '#type' => 'textfield',
      '#title' => '外部用户名',
      '#size' => 15,
      '#maxlength' => 11,
      '#required' => true,
      '#weight' => -2,
    );
    $form['external_pass'] = array(
      '#type' => 'password',
      '#title' => '外部密码',
      '#size' => 15,
      '#required' => true,
      '#weight' => -1,
    );
    if ($form_id == 'user_login') {
      $form['external_name']['#description'] = '请输入外部用户名';
      $form['external_pass']['#description'] = '请输入外部密码';
      unset($form['external_name']['#size'], $form['external_pass']['#size']);
    }

    if (isset($form_state['post']['external_identifier'])) {
      $form['name']['#required'] = false;
      $form['pass']['#required'] = false;
      unset($form['#submit']);
      $form['#validate'] = array('exuser_login_validate');
    } else {
      $form['external_name']['#required'] = false;
      $form['external_pass']['#required'] = false;
    }
  }
}

function exuser_login_validate($form, &$form_state)
{
  $external_name = $form_state['values']['external_name'];
  $external_pass = $form_state['values']['external_pass'];
  // TODO使用 $external_name $external_pass 进行外部验证函数,返回 true false
  $result = true;
  if ($result) {
   exuser_authenticate_complete($external_name, $form_state['values']);
  } else {
    drupal_set_message('用户登录失败', 'error');
  }
}
 
function exuser_authenticate_complete($identity, $values)
{
  $account = exuser_external_load($identity);
  if (isset($account->uid)) {
    user_external_login($account, $values);
  } else {
    // 构造注册表单
    $form_state = array();
    $form_state['redirect'] = NULL;
    $form_state['values']['name'] = $identity;
    $form_state['values']['mail'] = $identity . '@mobile';
    $form_state['values']['pass'] = user_password();
    $form_state['values']['status'] = true;
    $form_state['values']['response'] = $response;
    $form_state['values']['auth_openid'] = $identity;
    $form = drupal_retrieve_form('user_register', $form_state);
    drupal_prepare_form('user_register', $form, $form_state);
    drupal_validate_form('user_register', $form, $form_state);
    if (form_get_errors()) {
      drupal_set_message('用户注册失败', 'error');
      $destination = drupal_get_destination();
      unset($_REQUEST['destination']);
      drupal_goto('user/register', $destination);
    } else {
      // 注册成功
      $account = user_save('', $form_state['values']);
      if (!$account) {
        drupal_set_message('保存用户失败', 'error');
        drupal_goto();
      }
      // 插入用户名与 uid 对照表
      exuser_external_save($account, $identity);
      user_external_login($account);
    }
    drupal_redirect_form($form, $form_state['redirect']);
  }
}
 
function exuser_external_load($identity)
{
  $result = db_query("SELECT uid FROM {exuser_authenticate} WHERE name = '%s'", $identity);
  if ($user = db_fetch_array($result)) {
    return user_load($user);
  } else {
    return false;
  }
}
 
function exuser_external_save($account, $identity)
{
  db_query("INSERT INTO {exuser_authenticate} (`uid`, `name`) VALUES (%d, '%s')", $account->uid, $identity);
}

调用外部验证的时候,把 // TODO 那行改成你的验证方法就可以了。

在 user_login 和 user_login_block 的页面上会调用一个 JS 用来切换登录模式

exuser.js- (模组 Javascript)

Drupal.behaviors.exuser=function(context){
  var$loginElements=$("#edit-name-wrapper, #edit-pass-wrapper");
  var$ewayLoginElements=$("#edit-external-name-wrapper, #edit-external-pass-wrapper");
  var$identifierElement=$('#edit-external-identifier');
  varsetStatus=function(e){
    if($identifierElement.attr('checked')){
      $loginElements.hide();
      $ewayLoginElements.show();
    }else{
      $ewayLoginElements.hide();
      $loginElements.show();
    }
  };
  setStatus();
  $identifierElement.click(setStatus);
};

分享到:
评论

相关推荐

    自己写的 drupal module 验证码

    由于drupal form 用的很不顺手,所以偶尔自定义form,这样用第3方的drupal验证码不太方便了,所以就写了这个module,验证码的实现方法来自网络。(注意,这个不是用drupal form做的,但是可以嵌入到drupal里)

    Drupal7宝典+Drupal开发指南+Using Drupal

    包含:Drupal7宝典; Drupal开发指南; Using Drupal(强烈推荐) 值得你下载!

    Drupal data Drupal data

    Drupal dataDrupal data

    Enterprise Drupal 8 Development

    "Enterprise Drupal 8 Development: For Advanced Projects and Large Development Teams" English | ISBN: 1484202546 | 2017 | 309 pages | PDF | 9 MB Successfully architect a Drupal 8 website that scales ...

    drupal6版本(这是drupal6)

    drupal6的安装,drupal6的安装drupal6的安装drupal6的安装

    drupal7与drupal6版本修改内容

    drupal7 vs drupal6 详细的列出了从drupal6升级到drupal7所做的一些改动。 从代码,配置,UI,API等全方面的诠释drupal7与drupal6 的不同之处。

    Drupal 7.54 英文版.zip

    Drupal是一个开源的内容管理系统(CMS)平台,它是用PHP写成的。Drupal有一个优秀的模块化结构,提供了许多模块,包括短消息、个性化书签、网站管理、Blog、日记、...文件验证错误消息现在在后续上传有效文件后被删除。

    Decoupled Drupal in Practice

    Decoupled Drupal in Practice: Architect and Implement Decoupled Drupal Architectures Across the Stack By 作者: Preston So ISBN-10 书号: 1484240715 ISBN-13 书号: 9781484240717 Edition 版本: 1st ed. ...

    PHP 编程 Drupal Patch

    PHP 编程 Drupal Patch

    jQuery-Drupal-Webform-Validation-Plugin:用于Drupal Webform验证的jQuery插件

    jQuery-Drupal-Webform-Validation-Plugin jQuery Drupal Webform验证v1.0.0 高度可配置的Drupal Webform验证插件。 在Drupal站点中用于验证表单提交所需的几个字段通过如何使用:1.加载jQuery并包含Webform验证插件...

    零起点学习Drupal教程

    零起点学习Drupal教程零起点学习Drupal教程零起点学习Drupal教程

    drupal7安装说明

    drupal7图文安装教程

    Drupal6开发手册

    Drupal6开发手册Drupal6开发手册Drupal6开发手册Drupal6开发手册Drupal6开发手册Drupal6开发手册Drupal6开发手册

    Beginning Drupal 8(Apress,2015)

    Beginning Drupal 8 teaches you how to build, maintain, and manage Drupal 8-based web sites. The book covers what Drupal is, using Drupal when building a new web site, installing and configuring Drupal...

    Drupal专业开发指南(Drupal5)

    Drupal专业开发指南,这是drupal5版的。可能现在还有人用。

    Drupal宝典.doc

    Drupal宝典 用drupal建站必备

    Drupal 7 高级开发第三版 Drupal7专业开发指南

    Drupal 7 高级开发第三版 Drupal7专业开发指南

    Drupal 8 Module Development 2nd Edition

    Write a Drupal 8 module with custom functionality and hook into various extension points Master numerous Drupal 8 sub-systems and APIs Model, store, and manipulate data in various ways and for various...

    drupal网上书店

    Drupal 的核心模块是Drupal 最重要的组成部分,它们是Drupal 主要功能的承载。Drupal 自带有33个模块,基本上涵盖了当前网站所应具有的全部功能:用户管理、博客、论坛、评论、相册以及日志管理的,还有新闻聚合等...

    Drupal_1 Drupal

    Drupal_1 Drupal_1 Drupal_1 Drupal_1 Drupal_1 Drupal_1

Global site tag (gtag.js) - Google Analytics