Php 将Yii用户与Opauth一起使用

Php 将Yii用户与Opauth一起使用,php,oauth,yii,yii-extensions,Php,Oauth,Yii,Yii Extensions,我一直试图让Opauth以一种理智的方式将参数传递给Yii用户。以下是我正在使用的git repos的扩展: 使用官方Opauth回购更新的LIB 在修改了Opauth的回调控制器后,我从Opauth获得了正确的数组,直接在页面上显示我的谷歌帐户信息 以下是回调控制器中的代码: class CallbackController extends Controller { public $defaultAction = 'callback';

我一直试图让Opauth以一种理智的方式将参数传递给Yii用户。以下是我正在使用的git repos的扩展:

使用官方Opauth回购更新的LIB

在修改了Opauth的回调控制器后,我从Opauth获得了正确的数组,直接在页面上显示我的谷歌帐户信息

以下是回调控制器中的代码:



    class CallbackController extends Controller {

        public $defaultAction = 'callback';

            public function actionCallBack() {
                if ( $this->module->getConfig() ) {

                    /**
                     * Instantiate Opauth with the loaded config but no run automatically
                     * Only create Opauth if one is not already there
                     */
                    if (!$this->module->getOpauth()) {
                        $this->module->setOpauth (new Opauth($this->module->getConfig(), false));
                        $Opauth = $this->module->getOpauth();
                    } else {
                        $Opauth = $this->module->getOpauth();
                    }


                    /**
                     * Fetch auth response, based on transport configuration for callback
                     */
                    $response = null;

                    switch($Opauth->env['callback_transport']) {
                        case 'session':
                            session_start();
                            $response = $_SESSION['opauth'];
                            unset($_SESSION['opauth']);
                            break;
                        case 'post':
                            $response = unserialize(base64_decode( $_POST['opauth'] ));
                            break;
                        case 'get':
                            $response = unserialize(base64_decode( $_GET['opauth'] ));
                            break;
                        default:
                            echo 'Error: Unsupported callback_transport.'."
\n"; break; } /** * Check if it's an error callback */ if (array_key_exists('error', $response)) { echo 'Authentication error: Opauth returns error auth response.'."
\n"; } /** * Auth response validation * * To validate that the auth response received is unaltered, especially auth response that * is sent through GET or POST. */ else{ if (empty($response['auth']) || empty($response['timestamp']) || empty($response['signature']) || empty($response['auth']['provider']) || empty($response['auth']['uid'])) { echo 'Invalid auth response: Missing key auth response components.'."
\n"; } elseif (!$Opauth->validate(sha1(print_r($response['auth'], true)), $response['timestamp'], $response['signature'], $reason)) { echo 'Invalid auth response: '.$reason.".
\n"; } else { echo 'OK: Auth response is validated.'."
\n"; /** * It's all good. Go ahead with your application-specific authentication logic */ $Opauth->response = $response; Yii::app()->getModule('opauth')->setOpauth($Opauth); $this->redirect(Yii::app()->getModule('user')->returnUrl); } } } else { echo 'No configuration loaded!'; } } }

对于$identity->authenticate(),我有以下内容:

$identity = new UserIdentity($response);
$identity->authenticate();

switch($identity->errorCode) {
    case $identity::ERROR_NONE:
        Yii::app()->user->login($identity);
        $this->redirect(array("page_after_login"));
        break;
     case $identity::ERROR_USERNAME_INVALID:
        $this->redirect(Yii::app()->user->registrationUrl);
        break;
     default:
        throw new CException ($identity->errorCode);
}

以下是我为使此部件正常工作所做的:


以下是我为使此部件正常工作所做的:



    public function authenticate()
        {
            if (strpos($this->username,"@")) {
                $user=User::model()->notsafe()->findByAttributes(array('email'=>$this->username));
            } else {
                $user=User::model()->notsafe()->findByAttributes(array('username'=>$this->username));
            }
            if($user===null)
                if (strpos($this->username,"@")) {
                    $this->errorCode=self::ERROR_EMAIL_INVALID;
                } else {
                    $this->errorCode=self::ERROR_USERNAME_INVALID;
                }
            //else if(Yii::app()->getModule('user')->encrypting($this->password)!==$user->password)
            //  $this->errorCode=self::ERROR_PASSWORD_INVALID;
            else if($user->status==0&&Yii::app()->getModule('user')->loginNotActiv==false)
                $this->errorCode=self::ERROR_STATUS_NOTACTIV;
            else if($user->status==-1)
                $this->errorCode=self::ERROR_STATUS_BAN;
            else {
                $this->_id=$user->id;
                $this->username=$user->username;
                $this->errorCode=self::ERROR_NONE;
            }
            return !$this->errorCode;
        }

$identity = new UserIdentity($response);
$identity->authenticate();

switch($identity->errorCode) {
    case $identity::ERROR_NONE:
        Yii::app()->user->login($identity);
        $this->redirect(array("page_after_login"));
        break;
     case $identity::ERROR_USERNAME_INVALID:
        $this->redirect(Yii::app()->user->registrationUrl);
        break;
     default:
        throw new CException ($identity->errorCode);
}