簡體   English   中英

將Yii用戶與Opauth一起使用

[英]Using Yii-user with Opauth

我一直試圖讓Opauth以理智的方式將參數傳遞給Yii-User。 這是我正在使用的擴展git repos:

Opauth(通過Yii Framework模塊)具有來自官方Opauth存儲庫的更新庫
Yii-用戶模塊倉庫

在修改Opauth的回調控制器后,我從Opauth獲得了正確的數組,直接在頁面上顯示了我的Google帳戶信息。

這是回調控制器中的代碼:



    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!'; } } }

我嘗試直接將響應保存到模塊(顯示在控制器的末尾),但這不起作用-我認為每次運行都會創建模塊類。 我不確定如何將響應直接移到yii-user模塊中。

編輯:我正在回調控制器中嘗試這樣的事情:

$identity = new UserIdentity($response);
if ($identity->authenticate()) {
     Yii::app()->user->login($identity);
     $this->redirect('/');
} else {
     $this->redirect(Yii::app()->getModule('user')->returnUrl);
}

對於$ identity-> authenticate(),我有以下內容:



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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM