Beispiel #1
0
 public function actionRegister()
 {
     $model = new User('register');
     // Uncomment the following line if AJAX validation is needed
     $this->performAjaxValidation($model);
     if (isset($_POST['User'])) {
         $model->setAttributes($_POST['User']);
         $model->birthday = DateTimeHelper::convertToDTStringFromPOSTForFieldName('birthday', $_POST);
         //captcha validating
         require_once Yii::app()->basePath . '/vendor/recaptchalib.php';
         $privatekey = "6LfXBe8SAAAAAA7fOOWIfyfmvWbIjk-s15UFfGec";
         $resp = recaptcha_check_answer($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);
         if (!$resp->is_valid) {
             $error = $resp->error;
             if ($resp->error == 'incorrect-captcha-sol') {
                 $error = '確認碼不正確';
             }
             $this->render('register', array('model' => $model, 'captchaError' => $error));
             return;
         } else {
             // Your code here to handle a successful verification
             /** store the password for the athentication user
              *  Since the password will be hashed once it is saved
              */
             $rawPassword = $model->password;
             if ($model->validate()) {
                 if ($model->save()) {
                     $identity = new UserIdentity($model->user_name, $rawPassword);
                     $duration = 3600 * 24 * 30;
                     // 30 days
                     $identity->authenticate();
                     if (Yii::app()->user->login($identity, $duration)) {
                         Yii::app()->user->setFlash(FlashMsg::SUCCESS, "歡迎加入!");
                     } else {
                         Yii::app()->user->setFlash(FlashMsg::WARNING, "歡迎加入! 請在登入頁面登入");
                     }
                     $this->redirect(array('index'));
                 } else {
                     Yii::app()->user->setFlash(FlashMsg::ERROR, "創建使用者失敗");
                 }
             } else {
                 $model->purgePassword();
             }
         }
     }
     $this->render('register', array('model' => $model, 'captchaError' => ''));
 }
 public function actionUpdateUser()
 {
     $model = User::model()->findByPk(Yii::app()->user->id);
     $fileModel = new FileUploadFormModel();
     $fileModel->setScenario(FileUploadFormModel::FILE_UPLOAD_MODE_SINGLE);
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['User'])) {
         $model->setScenario('update');
         $model->setAttributes($_POST['User']);
         $model->birthday = DateTimeHelper::convertToDTStringFromPOSTForFieldName('birthday', $_POST);
         $test = $model->summary;
         if ($model->validate()) {
             $fileModel->LoadUploadedeImage();
             if ($fileModel->isUploadedFile()) {
                 if ($fileModel->validate()) {
                     $SavedFileRelativePaths = $fileModel->processImage(get_class($model), $model->user_name, md5(DateTimeHelper::now()));
                     $model->icon_src = ArrayHelper::array2string($SavedFileRelativePaths);
                     if ($model->save()) {
                         Yii::app()->user->setFlash(FlashMsg::SUCCESS, "個人資料已更新");
                         $this->redirect(array('index'));
                     } else {
                         Yii::app()->user->setFlash(FlashMsg::ERROR, "更新個人資料失敗");
                     }
                 } else {
                     $this->render('updateUser', array('model' => $model, 'fileModel' => $fileModel));
                 }
             }
             if ($model->save()) {
                 Yii::app()->user->setFlash(FlashMsg::SUCCESS, "個人資料已更新");
                 $this->redirect(array('index'));
             }
         }
     }
     $this->render('updateUser', array('model' => $model, 'fileModel' => $fileModel));
 }