dqxmf02844 2013-11-12 03:14
浏览 108
已采纳

Wordpress用户身份验证过程

I'm building a wordpress system where I want to authenticate users from external source instead of wordpress DB. I'm using wsdl service to communicate with the external DB and I'm getting the proper user information based on their credentials. However I'm not getting how to proceed with the obtained result further. Somebody please help me.

Following are steps I've done so far

Created custom function in pluggable.php and calling it in user.php

function wp_authenticate_username_password($user, $username, $password) {
    if ( is_a($user, 'WP_User') ) { return $user; }
    if ( empty($username) || empty($password) ) {
        if ( is_wp_error( $user ) )
            return $user;

        $error = new WP_Error();

        if ( empty($username) )
            $error->add('empty_username', __('<strong>ERROR</strong>: The username field is empty.'));

        if ( empty($password) )
            $error->add('empty_password', __('<strong>ERROR</strong>: The password field is empty.'));

        return $error;
    }

    //$user = get_user_by('login', $username);  /*Replaced it with the below*/

    $user = validate_ep($username,$password);    

    echo "<pre>";
    print_r($user);  /*Produces the result in step 3*/
    echo "</pre>";
    exit;

Custom Function in pluggable.php that communicates with my external DB

function validate_ep($username, $userpwd) { 
    $wsdl = "my web service path";
    $client = new SoapClient($wsdl); //(Parameter is the wsdl file in which the services are written.
    $newObj = new stdClass;
    $user_name = ucfirst($username);
    $user_pwd = md5($userpwd);
    $display_type = 'wp';

    try {
        $result = $client->log_process(array(0 => $user_name, 1 => $user_pwd, 2 => $display_type));              
        if ($result==FALSE)
            return FALSE;
        foreach($result->item as $key=>$valObj) {
            if(!is_numeric($valObj->key)) {
                $newObj->{$valObj->key} = $valObj->value;
            }
        }               

        /*$actual = unserialize(base64_decode($result));*/
        if (count($result) > 0) {
            $user = new WP_User;
            $user->init($newObj);
            return $user;
        }
    } catch (SoapFault $exp) {
        //print_r( $exp->getMessage());               
    }
    return false;
}

The result returned from web service

WP_User Object
(
    [data] => stdClass Object
    (
        [id] => ID
        [organization] => ID
        [login] => UserName
        [password] => ***
        [name] => Name

    )

    [ID] => 0
    [caps] => Array
    (
    )

    [cap_key] => wp_capabilities
    [roles] => Array
    (
    )

    [allcaps] => Array
    (
    )

    [filter] => 
)

Somebody please help what can I do after these steps.

  • 写回答

1条回答 默认 最新

  • 普通网友 2014-01-29 05:07
    关注

    I would suggest not to change WordPress core files as you did for user.php as it will be overwritten once WordPress core is upgraded. Instead I would suggest to go through the following article:

    WordPress Replace built in user authentication

    It has explanation in video presentation as well.

    I am adding just summary below:

    What to keep in mind when replacing the built-in authentication

    WordPress relies heavily on it's built-in user system. Because of this there are lots of references to users in the WordPress database that are made. While slightly annoying, it is still fairly simple to work around these limitations.

    WordPress requires that a real user (WordPress user) be present in the WordPress database in order to perform operations on that user. Luckily WordPress contains function to create, manipulate, and delete users. So when we build our service we will actually be taking the following steps, which should be fairly authentication type agnostic:

    • Authenticate user via alternate method
      • If invalid user display invalid login message
      • If valid user
        • Check to see if the user exists in the WordPress user table
        • If user exists load and return user data in a WP_User object
        • If user does not exist
          • Automagically create a new user from alternate authentication service user information
          • After creating the user load and return user data in a WP_User object
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误