Exemple #1
0
 /**
  * 处理人人网登录事宜
  * Enter description here ...
  * @throws CHttpException
  */
 public function actionRennAuth()
 {
     $rennService = new RennService();
     $rennClient = $rennService->getClient();
     // 处理code -- 根据code来获得token
     if (isset($_REQUEST['code'])) {
         $keys = array();
         // 获得code
         $keys['code'] = $_REQUEST['code'];
         $keys['redirect_uri'] = Yii::app()->createAbsoluteUrl('/oauth/rennAuth');
         try {
             // 根据code来获得token
             $token = $rennClient->getTokenFromTokenEndpoint('code', $keys);
         } catch (RennException $e) {
             error_log($e->getTraceAsString());
             throw new CHttpException(403, Yii::t('app', '没有得到授权,不能访问'));
             Yii::app()->end();
         }
     }
     // 获得用户接口
     $userService = $rennClient->getUserService();
     // 获得当前登录的人人用户信息
     try {
         $rennUser = $userService->getUserLogin();
     } catch (Exception $e) {
         throw new CHttpException(403, Yii::t('app', '没有得到授权,不能访问'));
         Yii::app()->end();
     }
     if (isset($rennUser)) {
         //检查是否为新用户
         //			$rrOauth = Oauth::model()->find('rrid=:rrid',array('rrid'=>$rennUser['id']));
         $userInfo = UserInfo::model()->findByAttributes(array('rennId' => $rennUser['id']));
         //如果是新用户,在站内创建一个对应用户
         if (!$userInfo) {
             $transaction = Yii::app()->db->beginTransaction();
             try {
                 $user = new User();
                 $user->email = 'RRId' . $rennUser['id'] . '@daxidao.com';
                 $user->password = DxdUtil::randCode(32);
                 $user->salt = DxdUtil::randCode(32);
                 $userSaved = $user->validate() && $user->save();
                 $userInfo = new UserInfo();
                 $userInfo->id = $user->getPrimaryKey();
                 $userInfo->email = $user->email;
                 $userInfo->name = $rennUser['name'];
                 $userInfo->addTime = time();
                 $userInfo->rennId = $rennUser['id'];
                 $userInfo->status = 'ok';
                 $userInfoSaved = $userInfo->save();
                 if (!$userSaved || !$userInfoSaved) {
                     Yii::app()->user->setFlash('error', Yii::t('app', '抱歉,创建用户失败'));
                     throw new Exception(Yii::t('app', '创建用户失败!'));
                 }
                 $transaction->commit();
             } catch (Exception $e) {
                 $transaction->rollback();
                 $this->redirect(Yii::app()->baseUrl);
                 Yii::app()->end();
             }
         }
         //站内用户登录
         /*		$identity=new UserIdentity($userInfo->email,'somepassword');
         			$identity->authenticate(true);
         
         			if($identity->errorCode===UserIdentity::ERROR_NONE)
         			{
         				//$duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
         				Yii::app()->user->login($identity, 0);
         				//print_r(Yii::app()->user);
         			}*/
         $user = User::model()->findByAttributes(array('email' => $userInfo->email));
         $user->loginWithoutPassword();
     }
     //如果还没有填写email
     if ($userInfo->noEmail()) {
         $this->redirect(array('u/registerOauth'));
         Yii::app()->end();
     }
     Yii::app()->user->setFlash('success', Yii::t('app', '人人网登录成功!'));
     $this->redirect($this->homePage);
 }
Exemple #2
0
<?php

if (isset(Yii::app()->params['settings']['openAuth']['enabled']) && Yii::app()->params['settings']['openAuth']['enabled'] && (isset(Yii::app()->params['settings']['openAuth']['rennEnabled']) && Yii::app()->params['settings']['openAuth']['rennEnabled'])) {
    $rennService = new RennService();
    $rennClient = $rennService->getClient();
    $code_url = $rennClient->getAuthorizeURL(Yii::app()->createAbsoluteUrl('/oauth/rennAuth'), 'code');
    $img = CHtml::image(Yii::app()->baseUrl . "/images/renren_connect.png");
    echo CHtml::link($img, $code_url);
}