/**
  * Updates an existing UserProfile model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id
  * @return mixed
  */
 public function actionBind()
 {
     if (ZCommonSessionFun::get_user_id() < 1) {
         $url = Yii::$app->urlManager->createUrl([ZCommonSessionFun::urlLoginUserStr]);
         return $this->redirect($url);
     }
     $this->view->title = '个人设置';
     $this->layout = false;
     $condition['uid'] = ZCommonSessionFun::get_user_id();
     $model = $model = UserProfile::findOne($condition);
     if (!$model) {
         $model = new UserProfile();
     }
     $model->uid = ZCommonSessionFun::get_user_id();
     $model->birthday = $model->birthday ? date('Y-m-d', strtotime($model->birthday)) : '';
     $load = $model->load(Yii::$app->request->post());
     if ($load && $model->save()) {
         $userInfo = ZCommonSessionFun::get_user_session();
         $userInfo['profile'] = $model->attributes;
         $userInfo['nickname'] = $model->nickname;
         $userInfo['head_image'] = $model->head_image;
         $userInfo['intro'] = $model->intro;
         ZCommonSessionFun::set_user_session($userInfo);
         return $this->redirect(['bind']);
     } else {
         return $this->render('_form', ['model' => $model]);
     }
 }
示例#2
0
 /**
  * 设置测试次数
  * @param integer $uid
  */
 public function setTestCount($uid)
 {
     $condition['uid'] = $uid;
     $count = Survey::find()->where($condition)->count();
     $testing_count = Survey::find()->where($condition)->sum('answer_count');
     $model_UserProfile = UserProfile::findOne($uid);
     $model_UserProfile ? null : ($model_UserProfile = new UserProfile());
     $model_UserProfile->uid = $uid;
     $model_UserProfile->test_count = $count;
     $model_UserProfile->testing_count = $testing_count;
     return $model_UserProfile->save();
 }
 public function actionEditProfile()
 {
     $teacherProfile = UserProfile::findOne(['user_id' => Yii::$app->user->id]);
     if (!isset($teacherProfile)) {
         $teacherProfile = new UserProfile();
         $teacherProfile->user_id = Yii::$app->user->id;
     }
     if ($teacherProfile->load(Yii::$app->request->post()) && $teacherProfile->save()) {
         Yii::$app->session->setFlash('info', 'Profile updated successfully');
         return $this->redirect(['/teacher']);
     }
     return $this->render('edit-profile', compact('teacherProfile'));
 }
 /**
  * Creates a new UserProfile model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new UserProfile();
     try {
         if ($model->load($_POST) && $model->save()) {
             return $this->redirect(Url::previous());
         } elseif (!\Yii::$app->request->isPost) {
             $model->load($_GET);
         }
     } catch (\Exception $e) {
         $msg = isset($e->errorInfo[2]) ? $e->errorInfo[2] : $e->getMessage();
         $model->addError('_exception', $msg);
     }
     return $this->render('create', ['model' => $model]);
 }
 /**
  * Signs user up.
  *
  * @return User|null the saved model or null if saving fails
  */
 public function signup()
 {
     if (!$this->validate()) {
         return null;
     }
     $user = new User();
     $user->username = $this->username;
     $user->email = $this->email;
     $user->setPassword($this->password);
     $user->generateAuthKey();
     if ($user->save()) {
         $profile = new UserProfile();
         $profile->user_id = $user->id;
         return $profile->save() ? $user : null;
     }
     return null;
 }
示例#6
0
 /**
  * Updates an existing User model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id
  * @return mixed
  */
 public function actionUpdate($id)
 {
     $model = $this->findModel($id);
     $model->setScenario('update');
     if ($model->profile) {
         $profile = $model->profile;
     } else {
         $profile = new UserProfile();
         $profile->user_id = $model->id;
     }
     $post = Yii::$app->request->post();
     if ($model->load($post) && $model->save()) {
         if ($profile->load($post) && $profile->save()) {
         }
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('update', ['model' => $model, 'profile' => $profile]);
     }
 }
示例#7
0
 public function actionProfile()
 {
     //        if(\Yii::$app->user->isGuest)
     //        return $this->redirect( Yii::$app->urlManager->createUrl('site/login'));
     $userdetails = \Yii::$app->user->identity;
     $model = UserProfile::find()->where(['user_id' => $userdetails->id])->one();
     if (empty($model)) {
         $model = new UserProfile();
     }
     $address = Address::find()->where(['user_id' => $userdetails->id])->one();
     if (empty($address)) {
         $address = new Address();
     }
     $userpay = UserPay::find()->where(['user_profile_id' => $userdetails->id])->one();
     if (empty($userpay)) {
         $userpay = new UserPay();
     }
     $user = Userform::findOne(['id' => $userdetails->id]);
     if (!empty($model->image)) {
         $oldpic = $model->image;
     } else {
         $oldpic = '';
     }
     //        if (isset($_POST['User'])) {
     //            $user->fname = $_POST['User']['fname'];
     //            $user->lname = $_POST['User']['lname'];
     //            if ($user->validate(array('fname', 'lname'))) {
     //                $user->update();
     //            }
     //        }
     if (isset($_POST['UserProfile'])) {
         //            if (empty($_POST['UserProfile']['state_id'])) {
         //
         //                $model->addError('state_id', 'Please select a state', array('class' => 'errorMsg'));
         //                return $this->render('profile', ['model' => $model, 'user' => $user]);
         ////                 $model->validate();
         ////            echo "<pre>";print_r('hello');die;
         //                Yii::$app->end();
         //            }
         $model->attributes = $_POST['UserProfile'];
         if (isset($model) && $model->user_id) {
             $model->modified = new \yii\db\Expression('NOW()');
         } else {
             $model->created = new \yii\db\Expression('NOW()');
         }
         $model->user_id = Yii::$app->user->identity->id;
         $model->description = $_POST['UserProfile']['description'];
         $model->gender = $_POST['UserProfile']['gender'];
         //$model->phone=$_POST['UserProfile']['phone'];
         //$model->address=$_POST['UserProfile']['address'];
         //$model->city_id=$_POST['UserProfile']['city_id'];
         //$model->country_id=$_POST['UserProfile']['country_id'];
         //$model->zip=$_POST['UserProfile']['zip'];
         //$model->state_id=$_POST['UserProfile']['state_id'];
         $model->image = $oldpic;
         //            if (!empty($_POST['userimage'])) {
         //                $folder = 'userimages/';
         //                $myArray = explode('_', $_POST['userimage'], 2);
         //                $tmpfolder = yii::$app->basePath . DIRECTORY_SEPARATOR . "web" . DIRECTORY_SEPARATOR . "imgtemp" . DIRECTORY_SEPARATOR;
         //                $name = Yii::$app->basePath . "/web/imgtemp/" . $_POST['userimage'];
         //                if (file_exists($name)) {
         //                $filename = $folder . $myArray[1];
         //
         //                list($width, $height) = getimagesize($name);
         //                $image = Yii::app()->iwi->load($name);
         //                if($width >= 160 && $height >= 160){
         //                $image->resize(160, 160, Iwi::NONE);}
         //                $image->save();
         //                $ar = explode('.', $filename);
         //                $newname = $ar[0].'_160x160.'.$ar[1];
         //                $dest = $tmpfolder . time() . '_usrimg' . $myArray[1];
         //                copy($name, $dest);
         //                SiteUtil::UploadS3($newname, $dest);
         //                SiteUtil::UploadS3($filename, $dest);
         //
         //                $image = Yii::app()->iwi->load($name);
         //                if($width >= 70 && $height >= 70){
         //                $image->resize(70, 70, Iwi::NONE);}
         //                $image->save();
         //                $ar = explode('.', $filename);
         //                $newname = $ar[0].'_70x70.'.$ar[1];
         //                $dest1 = $tmpfolder . time() . '_usrimgthumb' . $myArray[1];
         //                copy($name, $dest1);
         //                SiteUtil::UploadS3($newname, $dest1);
         //
         //                $image = Yii::app()->iwi->load($name);
         //                if($width >= 50 && $height >= 50){
         //                $image->resize(50, 50, Iwi::NONE);}
         //                $image->save();
         //                $ar = explode('.', $filename);
         //                $newname = $ar[0].'_50x50.'.$ar[1];
         //                $dest2 = $tmpfolder . time() . '_usrimg1' . $myArray[1];
         //                copy($name, $dest2);
         //                SiteUtil::UploadS3($newname, $dest2);
         //
         //                $image = Yii::app()->iwi->load($name);
         //                if($width >= 35 && $height >= 35){
         //                $image->resize(35, 35, Iwi::NONE);}
         //                $image->save();
         //                $ar = explode('.', $filename);
         //                $newname = $ar[0].'_35x35.'.$ar[1];
         //                $dest3 = $tmpfolder . time() . '_usrimg2' . $myArray[1];
         //                copy($name, $dest3);
         //                SiteUtil::UploadS3($newname, $dest3);
         //
         //                unlink($name);
         //                unlink($dest);
         //                unlink($dest1);
         //                unlink($dest2);
         //                unlink($dest3);
         //
         //                $model->image = $filename;
         //                }
         //            }
         if ($model->validate() && $user->validate()) {
             $transaction = \Yii::$app->db->beginTransaction();
             try {
                 if ($model->save()) {
                     $address->user_id = $model->id;
                     $address->address = $_POST['Address']['address'];
                     $address->landmark = $_POST['Address']['landmark'];
                     $address->pincode = $_POST['Address']['pincode'];
                     $address->country_id = $_POST['Address']['country_id'];
                     $address->state_id = $_POST['Address']['state_id'];
                     $address->city_id = $_POST['Address']['city_id'];
                     $userpay->user_profile_id = $model->id;
                     $userpay->currency_id = 1;
                     $userpay->time_slot_id = 5;
                     $userpay->amount = $_POST['UserPay']['amount'];
                     if ($address->validate() && $userpay->validate()) {
                         $address->save();
                         $userpay->save();
                     }
                 }
                 $transaction->commit();
             } catch (Exception $e) {
                 $transaction->rollback();
             }
         }
     }
     return $this->render('profile', ['model' => $model, 'user' => $user, 'address' => $address, 'userpay' => $userpay]);
 }
示例#8
0
 /**
  * 绑定用户信息
  * @param unknown $user 用户名
  * @param unknown $pass 密码
  * @param unknown $uid 用户id
  * @param unknown $openid 开放id
  * @param unknown $type 类型
  * @param unknown $nickname 昵称
  * @param unknown $head_image 头像
  * @param string $is_register true注册用户|false老用户绑定
  * @return number
  */
 public function userBind($user, $pass, $uid, $openid, $type, $nickname, $head_image, $is_register = false)
 {
     $model_OauthBind = new OauthBind();
     $condition['openid'] = $openid;
     $condition['type'] = $type;
     $model_OauthBind = $model_OauthBind->findOne($condition);
     if ($model_OauthBind && $model_OauthBind->uid > 0) {
         $is_register = false;
         if ($uid > 0) {
             $model_User = User::findOne($uid);
         } else {
             $model_User = User::findOne($model_OauthBind->uid);
         }
     } else {
         if ($uid > 0) {
             $is_register = false;
             $model_User = User::findOne($uid);
         }
     }
     //已存在用户
     if (!$is_register) {
     } else {
         $model_User = new User();
         $max_uid = User::find()->max('uid');
         $max_uid++;
         $model_User->user = $max_uid . '';
         $model_User->pass = $model_User->user;
         $model_User->created = NOW_TIME_YmdHis;
         $model_User->save();
     }
     //         echo $uid;
     //         ZCommonFun::print_r_debug($model_User->attributes);
     //         ZCommonFun::print_r_debug($model_OauthBind->attributes);
     //         exit;
     //         $model_OauthBind = new OauthBind();
     //         $condition['openid'] = $openid;
     //         $condition['type'] = $type;
     //         $model_OauthBind=$model_OauthBind->findOne($condition);
     //已经绑定了
     if ($model_OauthBind) {
         $this->operationData['user'] = $model_User;
         $model_UserProfile = UserProfile::findOne(['uid' => $model_User->uid]);
         if (!$model_UserProfile) {
             $model_UserProfile = new UserProfile();
             $model_UserProfile->uid = $model_User->uid;
             $model_UserProfile->nickname = $nickname;
             $model_UserProfile->head_image = $head_image;
             $model_UserProfile->money = 0;
             $model_UserProfile->friend_money = 0;
             $model_UserProfile->save();
             //                 ZCommonFun::print_r_debug($model_UserProfile);
             //                 exit;
         }
         $model_OauthBind->uid = $model_User->uid;
         $model_OauthBind->save();
         //             ZCommonFun::print_r_debug($model_User->uid);
         //             ZCommonFun::print_r_debug($model_OauthBind);
         //             exit;
         $this->operationData['oauth_bind'] = $model_OauthBind;
         $this->operationData['user_profile'] = $model_UserProfile;
         return 1;
     }
     $model_OauthBind = new OauthBind();
     $model_OauthBind->openid = $openid . '';
     $model_OauthBind->type = $type;
     $model_OauthBind->uid = $model_User->uid;
     $model_OauthBind->created = NOW_TIME_YmdHis;
     if ($model_OauthBind->save()) {
         $model_UserProfile = UserProfile::findOne(['uid' => $model_User->uid]);
         //如果没有设置过用户信息,就设置用户信息
         if (!$model_UserProfile) {
             $model_UserProfile = new UserProfile();
             $model_UserProfile->uid = $model_User->uid;
             $model_UserProfile->nickname = $nickname;
             $model_UserProfile->head_image = $head_image;
             $model_UserProfile->money = 0;
             $model_UserProfile->friend_money = 0;
             $model_UserProfile->save();
         }
         $this->operationData['user'] = $model_User;
         $this->operationData['user_profile'] = $model_UserProfile;
         return 0;
     }
     return -1;
 }
示例#9
0
 public function onAuthSuccess($client)
 {
     /* @var $client \yii\authclient\OAuth2*/
     /* @var $user \common\models\User */
     $attributes = $client->getUserAttributes();
     /* @var $auth Auth */
     $auth = Auth::find()->where(['source' => $client->getId(), 'source_id' => $attributes['id']])->one();
     if (Yii::$app->user->isGuest) {
         if ($auth) {
             // вход
             $user = $auth->user;
             if ($user->status == User::STATUS_NOT_ACTIVE && $user->email == '') {
                 Yii::$app->getSession()->setFlash('success', [Yii::t('app', "To complete registration, enter the phone number and confirm the e-mail address.")]);
                 return $this->redirectUser($url = Url::to(['/main/finish-reg', 'id' => $user->id]));
             } elseif ($user->status == User::STATUS_NOT_ACTIVE && $user->email != '') {
                 Yii::$app->getSession()->setFlash('success', [Yii::t('app', "To complete registration, enter a phone number.")]);
                 return $this->redirectUser($url = Url::to(['/main/finish-reg', 'id' => $user->id]));
             } elseif ($user->status == User::STATUS_DELETED) {
                 Yii::$app->getSession()->setFlash('error', [Yii::t('app', "This user is blocked.")]);
                 return $this->redirectUser($url = Url::to(['/ad/view/all']));
             }
             Yii::$app->user->login($user);
         } else {
             // регистрация
             if (isset($attributes['email']) && ($user = User::findOne(['email' => $attributes['email']]))) {
                 // Если пользователь регитрировался ранее через форму регистации.
                 if ($user) {
                     if ($user->status == User::STATUS_DELETED) {
                         Yii::$app->getSession()->setFlash('error', Yii::t('app', "User <strong> {email} </strong> blocked.", ['email' => $user->email]));
                     } elseif ($user->auths->source) {
                         Yii::$app->getSession()->setFlash('error', [Yii::t('app', "Authorization using the email address <strong> {email} </strong> is already happening through the account <strong> {auths} </strong>.\n                            Log on using the account <strong> {auths} </strong> or use the link <strong> Forgot your password? </strong> for email <strong> {email} </strong> to restore the password..", ['email' => $user->email, 'auths' => $user->auths->source])]);
                     } else {
                         Yii::$app->getSession()->setFlash('error', Yii::t('app', "Authorization using the email address <strong> {email} </strong> has successfully passed through the registration form. Click on the link <strong> Forgot your password? </strong> to restore the password.", ['email' => $user->email]));
                     }
                 }
                 return $this->redirectUser($url = Url::to(['/main/login']));
             } else {
                 // Полученные данные заносим в переменные
                 /* @var $email string */
                 /* @var $first_name string */
                 /* @var $last_name string */
                 if (Yii::$app->request->get('authclient') == 'google') {
                     $first_name = $attributes['name']['givenName'];
                     $last_name = $attributes['name']['familyName'];
                     $email = $attributes['emails'][0]['value'];
                 } elseif (Yii::$app->request->get('authclient') == 'yandex') {
                     $first_name = $attributes['first_name'];
                     $last_name = $attributes['last_name'];
                     $email = $attributes['default_email'];
                 } elseif (Yii::$app->request->get('authclient') == 'facebook') {
                     $names = explode(' ', $attributes['name']);
                     $first_name = $names[0];
                     $last_name = $names[1];
                     $email = $attributes['email'];
                 } elseif (Yii::$app->request->get('authclient') == 'vkontakte') {
                     $first_name = $attributes['first_name'];
                     $last_name = $attributes['last_name'];
                     $email = false;
                 } elseif (Yii::$app->request->get('authclient') == 'twitter') {
                     $names = $attributes['name'];
                     $names = explode(' ', $names);
                     $first_name = $names[0];
                     $last_name = $names[1];
                     $email = false;
                 } elseif (Yii::$app->request->get('authclient') == 'linkedin') {
                     $first_name = $attributes['first_name'];
                     $last_name = $attributes['last_name'];
                     $email = $attributes['email'];
                 }
                 $password = Yii::$app->security->generateRandomString(6);
                 if ($email == false) {
                     $email = '';
                 }
                 $user = new User(['email' => $email, 'password' => $password, 'status' => User::STATUS_NOT_ACTIVE, 'country_id' => 182]);
                 $user->generateAuthKey();
                 $user->generateSecretKey();
                 $transaction = $user->getDb()->beginTransaction();
                 if ($user->save()) {
                     $auth = new Auth(['user_id' => $user->id, 'source' => $client->getId(), 'source_id' => (string) $attributes['id']]);
                     if ($auth->save()) {
                         /* @var $modelProfile /common/models/UserProfile */
                         $modelProfile = new UserProfile();
                         $modelProfile->user_id = $user->id;
                         $modelProfile->first_name = $first_name;
                         $modelProfile->last_name = $last_name;
                         if ($modelProfile->save()) {
                             if (RbacHelper::assignRole($user->id)) {
                                 $modelUserPrivilege = new UserPrivilege();
                                 $modelUserPrivilege->link('user', $user);
                                 $transaction->commit();
                             }
                             // если нет емайл, делаем перенаправление на main/finish-reg
                             if ($email == false) {
                                 Yii::$app->getSession()->setFlash('success', [Yii::t('app', "To complete registration, enter the phone number and confirm the e-mail address.")]);
                                 return $this->redirectUser($url = Url::to(['/main/finish-reg', 'id' => $user->id]));
                             }
                             Yii::$app->getSession()->setFlash('success', [Yii::t('app', "To complete registration, enter a phone number.")]);
                             return $this->redirectUser($url = Url::to(['/main/finish-reg', 'id' => $user->id]));
                         }
                     } else {
                         d($auth->getErrors());
                     }
                 } else {
                     /* @var $user \common\models\User */
                     $user = User::findOne(['email' => $user->email]);
                     // Если пользователь регитрировался ранее через форму регистации.
                     if ($user) {
                         if ($user->status == User::STATUS_DELETED) {
                             Yii::$app->getSession()->setFlash('error', Yii::t('app', "User <strong> {email} </strong> blocked.", ['email' => $user->email]));
                         } elseif ($user->auths->source) {
                             Yii::$app->getSession()->setFlash('error', [Yii::t('app', "Authorization using the email address <strong> {email} </strong> is already happening through the account <strong> {auths} </strong>.\n                            Log on using the account <strong> {auths} </strong> or use the link <strong> Forgot your password? </strong> for email <strong> {email} </strong> to restore the password..", ['email' => $user->email, 'auths' => $user->auths->source])]);
                         } else {
                             Yii::$app->getSession()->setFlash('error', Yii::t('app', "Authorization using the email address <strong> {email} </strong> has successfully passed through the registration form. Click on the link <strong> Forgot your password? </strong> to restore the password.", ['email' => $user->email]));
                         }
                     }
                     return $this->redirectUser($url = Url::to(['/main/login']));
                 }
             }
         }
     } else {
         // user already logged in
         if (!$auth) {
             // add auth provider
             $auth = new Auth(['user_id' => Yii::$app->user->id, 'source' => $client->getId(), 'source_id' => $attributes['id']]);
             $auth->save();
         }
     }
     return true;
 }