Esempio n. 1
0
 public function run()
 {
     require_once Yii::getPathOfAlias('ext') . "/OAuth/renren/rennclient/RennClient.php";
     $config = OAuth::getConf('renren');
     $rennClient = new RennClient($config['app_key'], $config['app_secret']);
     // 处理code -- 根据code来获得token
     if (isset($_REQUEST['code'])) {
         // 验证state,防止伪造请求跨站攻击
         $state = $_REQUEST['state'];
         if (empty($state) || $state !== Yii::app()->session['renren_state']) {
             throw new CHttpException(500, 'Error: Illegal Request');
         }
         unset(Yii::app()->session['renren_state']);
         // 获得code
         $code = $_REQUEST['code'];
         $redirect_uri = $config['callback'];
         try {
             // 根据code来获得token
             $token = $rennClient->authWithAuthorizationCode($code, $redirect_uri);
         } catch (RennException $e) {
             throw new CHttpException(500, 'Error:' . $e->getMessage());
         }
     } else {
         throw new CHttpException(500, 'Auth Failed');
     }
     if ($token) {
         // 获得保存的token
         $rennClient->authWithStoredToken();
         // 获得用户接口
         $user_service = $rennClient->getUserService();
         // 获得用户信息
         $user = $user_service->getUser(null);
         if (!$user) {
             throw new CHttpException('500', Yii::t('common', 'Login Failed') . '(get userinfo failed)');
         }
         // 获取accessToken
         $access_token = $token->accessToken;
         $openid = $user['id'];
         //查看是否已绑定
         $bind = OAuthRenren::model()->findByPk($openid);
         //数据
         $data = array('type' => 'renren', 'access_token' => $access_token, 'openid' => $openid, 'uid' => $bind ? $bind->uid : 0, 'username' => $user['name'], 'avatar' => $user['avatar'][1]['url']);
         //绑定注册
         $this->controller->bind_register($bind, $data);
     } else {
         $this->controller->message('error', Yii::t('common', 'Login Failed') . '(renren_x_0000)', $this->createUrl('user/login'));
     }
 }
 /**
  *
  * 绑定注册
  * @param $bind 是否已经绑定
  * @param $data 接收的第三方用户数据
  *
  */
 public function bind_register($bind, $data = array())
 {
     if (!$data['username']) {
         throw new CHttpException(500, Yii::t('common', 'Login Failed') . '(bind_x_1000)');
     }
     //初始密码
     $initPwd = ' ';
     if (!$bind) {
         //生成本地用户名
         $username = $data['username'] . '_' . $data['type'];
         //调取不同接口
         switch ($data['type']) {
             case 'qq':
                 $model = new OAuthQQ();
                 break;
             case 'sinawb':
                 $model = new OAuthSinawb();
                 break;
             case 'weixin':
                 $model = new OAuthWinxin();
                 break;
             case 'renren':
                 $model = new OAuthRenren();
                 break;
             default:
                 throw new CHttpException(500, Yii::t('common', 'Login Failed') . '(bind_x_1001)');
                 break;
         }
         $model->uid = 0;
         $model->openid = $data['openid'];
         $model->access_token = $data['access_token'];
         //判断用户名是否已经存在
         $if_exist = User::model()->find('username=:username', array(':username' => $username));
         if (!$if_exist) {
             //注册本地账号
             $user_model = new User('bind_register');
             $user_model->username = $username;
             //分配用户名
             $user_model->password = $initPwd;
             $user_model->avatar = $data['avatar'];
             //调用空间头像(100*100)
             $user_model->status = 1;
             $user_model->groupid = 1;
             $user_model->logins = 0;
             $user_model->register_ip = $this->_request->userHostAddress;
             //注册ip
             $user_model->last_login_ip = $this->_request->userHostAddress;
             $user_model->username_editable = 'Y';
             //允许修改用户名
             if ($user_model->save()) {
                 $model->uid = $uid = $user_model->uid;
                 //保存第三方授权信息
                 $model->save();
                 //保存远程图片到本地
                 $avatar_name = 'small_' . $user_model->uid . '_' . substr(md5(uniqid('file')), 0, 11);
                 $filepath = 'uploads/avatar/' . date('Ym', time());
                 $remote = Helper::downloadImage($data['avatar'], WWWPATH . '/' . $filepath, $avatar_name);
                 if ($remote) {
                     $local_avatar = $filepath . '/' . $remote['filename'];
                     $bind_user = User::model()->findByPk($uid);
                     $bind_user->avatar = $local_avatar;
                     $bind_user->save();
                 }
             } else {
                 throw new CHttpException(500, Yii::t('common', 'Login Failed') . '(bind_x_1002)');
             }
         } else {
             $uid = $if_exist->uid;
             $model->uid = $uid;
             //保存绑定qq用户
             $model->save();
         }
     } else {
         //已经绑定
         $uid = $data['uid'];
     }
     $user = User::model()->findByPk($uid);
     $username = $user->username;
     //自动登录
     $duration = 3600 * 24 * 30;
     // 30 days
     $identity = new OAuthUserIdentity($username, '');
     $identity->authenticate();
     if ($identity->errorCode === OAuthUserIdentity::ERROR_NONE) {
         Yii::app()->user->login($identity, $duration);
         //更新登录次数和登录ip
         $user->last_login_ip = $this->_request->userHostAddress;
         $user->logins = $user->logins + 1;
         if ($user->save()) {
             $this->redirect(Yii::app()->homeUrl);
         } else {
             throw new CHttpException(500, Yii::t('common', 'Login Failed') . '(bind_x_1003)');
         }
     } else {
         throw new CHttpException(500, Yii::t('common', 'Login Failed') . '(bind_x_1004)');
     }
 }
Esempio n. 3
0
 /**
  * 批量操作
  *
  */
 public function actionBatch()
 {
     if ($this->method() == 'GET') {
         $command = trim($this->_request->getParam('command'));
         $ids = intval($this->_request->getParam('id'));
     } elseif ($this->method() == 'POST') {
         $command = $this->_request->getPost('command');
         $ids = $this->_request->getPost('id');
     } else {
         throw new CHttpException(404, Yii::t('admin', 'Only POST Or GET'));
     }
     empty($ids) && $this->message('error', Yii::t('admin', 'No Select'));
     switch ($command) {
         case 'userLock':
             foreach ((array) $ids as $id) {
                 $userModel = User::model()->findByPk($id);
                 if ($userModel) {
                     $userModel->status = 0;
                     $userModel->save();
                 }
             }
             break;
         case 'userunLock':
             foreach ((array) $ids as $id) {
                 $userModel = User::model()->findByPk($id);
                 if ($userModel) {
                     $userModel->status = 1;
                     $userModel->save();
                 }
             }
             break;
         case 'userUnpass':
             foreach ((array) $ids as $id) {
                 $userModel = User::model()->findByPk($id);
                 if ($userModel) {
                     $userModel->status = -1;
                     $userModel->save();
                 }
             }
             break;
         case 'userDelete':
             foreach ((array) $ids as $id) {
                 $userModel = User::model()->findByPk($id);
                 if ($userModel) {
                     $userModel->delete();
                     //删除绑定用户
                     OAuthQQ::model()->deleteAllByAttributes(array('uid' => $id));
                     OAuthSinawb::model()->deleteAllByAttributes(array('uid' => $id));
                     OAuthRenren::model()->deleteAllByAttributes(array('uid' => $id));
                     OAuthWeixin::model()->deleteAllByAttributes(array('uid' => $id));
                     //删除头像
                     Uploader::deleteFile($userModel->avatar);
                 }
             }
             break;
         case 'groupDelete':
             foreach ((array) $ids as $id) {
                 $groupModel = UserGroup::model()->findByPk($id);
                 if ($groupModel) {
                     $groupModel->delete();
                 }
             }
         default:
             throw new CHttpException(404, Yii::t('admin', 'Error Operation'));
             break;
     }
     $this->message('success', Yii::t('admin', 'Batch Operate Success'), $this->createUrl('index'));
 }