通过yii中的开放图扩展,使用Facebook API对用户进行身份验证


Authenticate users using Facebook API through open-graph extension in yii

我使用Facebook Opengraph扩展登录系统。Facebook登录工作非常完美,而且我可以很容易地将数据保存在我的数据库表中。但它并没有对YII系统中的用户进行身份验证。

例如,我对events使用accessRules,如下所示:

public function accessRules()
{
    return array(
        array('allow', // allow authenticated user to perform 'buy' actions
            'actions'=>array('buy'),
            'users'=>array('@'),
        ),
    );
}

当用户使用Facebook登录时,无法访问event/buy页面。它重定向到profile page(用户登录时的默认页面)。

以下代码我用于Facebook login:

//Facebook login
$user = Yii::app()->facebook->getUser();
try{
    $user_profile = Yii::app()->facebook->api('/me?scope=email');
} catch(Exception $e){}
if($user) {
    if(Yii::app()->user->isGuest){
        $mbr = Facebook::model()->findByPk($user_profile['id']);
        if($mbr===null){
            $randomNum = rand(0,99999);
            $fb = new Facebook;
            $fb->mbrm_id = $user_profile['id'];
            $fb->mbrm_username = $user_profile['username'].$randomNum;
            $fb->mbrm_emailid = $user_profile['email'];
            $fb->mbrm_name = $user_profile['name'];
            $fb->mbrm_visits = "0";
            $fb->mbrm_sts = "a";
            if($fb->save()){
                Yii::app()->user->setState('title',$user_profile['username'].$randomNum);
                $this->redirect(array('profile/view','id'=>$user_profile['username'].$randomNum));
            } 
        } else {
            Yii::app()->user->setState('title',$mbr->mbrm_username);
            $mbr->saveCounters(array('mbrm_visits'=>1));
            $this->redirect(array('profile/view','id'=>$mbr->mbrm_username));
        }
    } else {
            $this->redirect(array('profile/view','id'=>$mbr->mbrm_username));
        }
}

即使我在使用Yii::app()->user->setState('title',$mbr->mbrm_username);,但它不起作用。请帮忙!

我在controller中添加了以下规则。这有助于验证Facebook用户。

代码

array('allow', // allow authenticated user to perform 'create' and 'update' actions
    'actions'=>array('buy'),
    'expression'=>'Yii::app()->facebook->getUser()',
),