通过facebook登录时,根据pk设置用户ID

时间:2014-04-29 13:33:40

标签: facebook yii

我可以使用Facebook登录/注销我的网络应用程序。但问题是当用户需要在我的应用程序上更新他的个人资料时,他不能。可以说他只能查看自己的页面。发生这种情况的唯一方法是可能未设置用户身份。但我不知道如何纠正这个问题。当我尝试Yii::app()->user->user_id时,Id实际上是正确的id,即用户模型中的pk。那他怎么不能更新他自己的页面呢?

在我的facebookUserIdentity中:

    public function authenticate() 
        {
            if($this->getIsAccessTokenValid() && $this->setFBUser())
            {
                $this->_user = $this->getUserFromDatabase();
                if($this->_user === false)
                    return false;

                $this->setState('isUser', false);
                $this->setState('isAdmin', false);
                $this->setState('isShop', false);
                $this->setState('isSuper', false);
                $this->setState('user',$this->_user);

                $this->_id = $this->_FBUser['id'];
//I've tried doing something like $this->_id = Yii::app()->user->user_id; or like $this->_user->user_id; ?
                $this->_name = $this->_FBUser['name'];

                return true;
            }
            else {
                return false;
            }
        }

从db获取用户:

protected function getUserFromDatabase()
    {
        $FBUser = $this->_FBUser;

        if($FBUser)
        {
            $user = User::model()->findByAttributes (array('oauth_uid'=>$FBUser['id']));
            if(!$user)
            {
                $user = new User;
                $user->oauth_uid = $FBUser['id'];
                $user->username = $FBUser['id'];
                $user->first_name = $FBUser['first_name'];
              //other info etc

                $user->image = "https://graph.facebook.com/" . $FBUser['id'] . "/picture?type=large";
            }
            else 
            {
                if ($user->oauth_permission == 'n')
                {
                    $this->errorMessage = self::ERROR_LOGIN_DISABLE;
                    return false;
                }
                $user->last_login_date =  date('Y-m-d H:i:s');
            }
            if($user->save())
            {
                return $user;
            }
            else 
                $this->errorMessage = CJSON::encode ( $user->getErrors());
            return false;
        }
        else {
            $this->errorMessage = "Failed getting facebook user data";
            return false;
        }
    }

最后,控制器页面规则:

public function accessRules()
{
$params=array();
$id=Yii::app()->request->getParam('id'); 
$params['model']=$this->loadModel($id);
return array(
    //stuff
array('allow', // allow authenticated user to perform 'create' and 'update' actions
'actions'=>array('create','update','RemoveImage'),
'roles'=>array('admin','super','owner'=>$params),
),
    //stuff

0 个答案:

没有答案