Example #1
0
 public function actionCreate()
 {
     if (!Yii::app()->user->isGuest) {
         if (Yii::app()->user->checkAccess('backend_access')) {
             $this->redirect(Yii::app()->createUrl('/apartments/backend/main/create'));
         } else {
             $this->redirect(Yii::app()->createUrl('/userads/main/create'));
         }
     }
     if (param('user_registrationMode') == 'without_confirm') {
         $user = new User('register_without_confirm');
     } else {
         $user = new User('register');
     }
     $login = new LoginForm();
     $model = new Apartment();
     $model->active = Apartment::STATUS_DRAFT;
     $model->period_activity = param('apartment_periodActivityDefault', 'always');
     $model->references = $model->getFullInformation($model->id, $model->type);
     $isAdmin = false;
     $activeTab = 'tab_register';
     $isUpdate = Yii::app()->request->getPost('is_update');
     if (!$isUpdate && isset($_POST['LoginForm']) && ($_POST['LoginForm']['username'] || $_POST['LoginForm']['password'])) {
         if (Yii::app()->user->getState('attempts-login') >= LoginForm::ATTEMPTSLOGIN) {
             $login->scenario = 'withCaptcha';
         }
         $activeTab = 'tab_login';
         $login->attributes = $_POST['LoginForm'];
         if ($login->validate() && $login->login()) {
             Yii::app()->user->setState('attempts-login', 0);
             User::updateUserSession();
             $isAdmin = Yii::app()->user->checkAccess('backend_access');
             $user = User::model()->findByPk(Yii::app()->user->id);
         } else {
             Yii::app()->user->setState('attempts-login', Yii::app()->user->getState('attempts-login', 0) + 1);
             if (Yii::app()->user->getState('attempts-login') >= LoginForm::ATTEMPTSLOGIN) {
                 $login->scenario = 'withCaptcha';
             }
         }
     }
     if (isset($_POST['Apartment'])) {
         $model->attributes = $_POST['Apartment'];
         if (!$isUpdate) {
             $adValid = $model->validate();
             $userValid = false;
             if ($activeTab == 'tab_register' && param('useUserRegistration')) {
                 $user->attributes = $_POST['User'];
                 $userValid = $user->validate();
                 if ($adValid && $userValid) {
                     $user->activatekey = User::generateActivateKey();
                     $userData = User::createUser($user->attributes);
                     if ($userData) {
                         $user = $userData['userModel'];
                         $user->password = $userData['password'];
                         $user->activatekey = $userData['activatekey'];
                         $user->activateLink = $userData['activateLink'];
                         $notifier = new Notifier();
                         $notifier->raiseEvent('onNewUser', $user, array('forceEmail' => $user->email));
                     }
                 }
             }
             if ($user->id && ($activeTab == 'tab_login' && $adValid || $activeTab == 'tab_register' && param('useUserRegistration') && $adValid && $userValid)) {
                 if (param('useUseradsModeration', 1)) {
                     $model->active = Apartment::STATUS_MODERATION;
                 } else {
                     $model->active = Apartment::STATUS_ACTIVE;
                 }
                 $model->owner_active = Apartment::STATUS_ACTIVE;
                 $model->owner_id = $user->id;
                 if ($model->save(false)) {
                     if (!$isAdmin && param('useUseradsModeration', 1)) {
                         Yii::app()->user->setFlash('success', tc('The listing is succesfullty added and is awaiting moderation'));
                     } else {
                         Yii::app()->user->setFlash('success', tc('The listing is succesfullty added'));
                     }
                     if ($activeTab == 'tab_register') {
                         if (param('user_registrationMode') == 'without_confirm') {
                             $login = new LoginForm();
                             $login->setAttributes(array('username' => $user['username'], 'password' => $user['password']));
                             if ($login->validate() && $login->login()) {
                                 User::updateUserSession();
                                 User::updateLatestInfo(Yii::app()->user->id, Yii::app()->controller->currentUserIp);
                                 $this->redirect(array('/usercpanel/main/index'));
                             } else {
                                 /*echo 'getErrors=<pre>';
                                 		print_r($login->getErrors());
                                 		echo '</pre>';
                                 		exit;*/
                                 showMessage(Yii::t('common', 'Registration'), Yii::t('common', 'You were successfully registered.'));
                             }
                         } else {
                             showMessage(Yii::t('common', 'Registration'), Yii::t('common', 'You were successfully registered. The letter for account activation has been sent on {useremail}', array('{useremail}' => $user['email'])));
                         }
                     } else {
                         if ($isAdmin) {
                             NewsProduct::getProductNews();
                             $this->redirect(array('/apartments/backend/main/update', 'id' => $model->id));
                             Yii::app()->end();
                         } else {
                             $this->redirect(array('/userads/main/update', 'id' => $model->id));
                         }
                     }
                 }
             }
         }
     } else {
         $objTypes = array_keys(Apartment::getObjTypesArray());
         $model->setDefaultType();
         $model->obj_type_id = reset($objTypes);
         $user->unsetAttributes(array('verifyCode'));
     }
     $this->render('create', array('model' => $model, 'user' => $user, 'login' => $login, 'activeTab' => $activeTab));
 }
Example #2
0
 public function actionRegister()
 {
     if (!param('useUserRegistration', 0)) {
         throw404();
     }
     $this->showSearchForm = false;
     $this->layout = '//layouts/inner';
     if (Yii::app()->user->isGuest) {
         if (param('user_registrationMode') == 'without_confirm') {
             $model = new User('register_without_confirm');
         } else {
             $model = new User('register');
         }
         if (isset($_POST['User']) && BlockIp::checkAllowIp(Yii::app()->controller->currentUserIpLong)) {
             $model->attributes = $_POST['User'];
             if ($model->validate()) {
                 $model->activatekey = User::generateActivateKey();
                 $user = User::createUser($model->attributes);
                 if ($user) {
                     $model->id = $user['id'];
                     $model->password = $user['password'];
                     $model->email = $user['email'];
                     $model->username = $user['username'];
                     $model->activatekey = $user['activatekey'];
                     $model->activateLink = $user['activateLink'];
                     $notifier = new Notifier();
                     $notifier->raiseEvent('onNewUser', $model, array('user' => $user['userModel']));
                     if (param('user_registrationMode') == 'without_confirm') {
                         $login = new LoginForm();
                         $login->setAttributes(array('username' => $user['username'], 'password' => $user['password']));
                         if ($login->validate() && $login->login()) {
                             User::updateUserSession();
                             User::updateLatestInfo(Yii::app()->user->id, Yii::app()->controller->currentUserIp);
                             $this->redirect(array('/usercpanel/main/index'));
                         } else {
                             /*echo 'getErrors=<pre>';
                             		print_r($login->getErrors());
                             		echo '</pre>';
                             		exit;*/
                             showMessage(Yii::t('common', 'Registration'), Yii::t('common', 'You were successfully registered.'));
                         }
                     } else {
                         showMessage(Yii::t('common', 'Registration'), Yii::t('common', 'You were successfully registered. The letter for account activation has been sent on {useremail}', array('{useremail}' => $user['email'])));
                     }
                 } else {
                     showMessage(Yii::t('common', 'Registration'), Yii::t('common', 'Error. Repeat attempt later'));
                 }
             } else {
                 $model->unsetAttributes(array('verifyCode'));
             }
         }
         $this->render('register', array('model' => $model));
     } else {
         $this->redirect('index');
     }
 }
Example #3
0
 public function actionLogin()
 {
     $this->layout = '//layouts/inner';
     $model = new LoginForm();
     if (Yii::app()->request->getQuery('soc_error_save')) {
         Yii::app()->user->setFlash('error', tt('Error saving data. Please try again later.', 'socialauth'));
     }
     if (Yii::app()->request->getQuery('deactivate')) {
         showMessage(tc('Login'), tt('Your account not active. Administrator deactivate your account.', 'socialauth'), null, true);
     }
     $service = Yii::app()->request->getQuery('service');
     if (isset($service)) {
         $authIdentity = Yii::app()->eauth->getIdentity($service);
         $authIdentity->redirectUrl = Yii::app()->user->returnUrl;
         $authIdentity->cancelUrl = $this->createAbsoluteUrl('site/login');
         if ($authIdentity->authenticate()) {
             $identity = new EAuthUserIdentity($authIdentity);
             // успешная авторизация
             if ($identity->authenticate()) {
                 //Yii::app()->user->login($identity);
                 $uid = $identity->id;
                 $firstName = $identity->firstName;
                 $email = $identity->email;
                 $service = $identity->serviceName;
                 $mobilePhone = $identity->mobilePhone;
                 $homePhone = $identity->homePhone;
                 $isNewUser = false;
                 $existId = User::getIdByUid($uid, $service);
                 if (!$existId) {
                     $isNewUser = true;
                     $email = !$email ? User::getRandomEmail() : $email;
                     $phone = '';
                     if ($mobilePhone) {
                         $phone = $mobilePhone;
                     } elseif ($homePhone) {
                         $phone = $homePhone;
                     }
                     $user = User::createUser(array('email' => $email, 'phone' => $phone, 'username' => $firstName), true);
                     if (!$user && isset($user['id'])) {
                         $authIdentity->redirect(Yii::app()->createAbsoluteUrl('/site/login') . '?soc_error_save=1');
                     }
                     $success = User::setSocialUid($user['id'], $uid, $service);
                     if (!$success) {
                         User::model()->findByPk($user['id'])->delete();
                         $authIdentity->redirect(Yii::app()->createAbsoluteUrl('/site/login') . '?soc_error_save=1');
                     }
                     $existId = User::getIdByUid($uid, $service);
                 }
                 if ($existId) {
                     $result = $model->loginSocial($existId);
                     User::updateUserSession();
                     if ($result) {
                         //						Yii::app()->user->clearState('id');
                         //						Yii::app()->user->clearState('first_name');
                         //						Yii::app()->user->clearState('nickname');
                         if ($result === 'deactivate') {
                             $authIdentity->redirect(Yii::app()->createAbsoluteUrl('/site/login') . '?deactivate=1');
                         }
                         if ($isNewUser) {
                             $authIdentity->redirect(Yii::app()->createAbsoluteUrl('/usercpanel/main/index') . '?soc_success=1');
                         } else {
                             $authIdentity->redirect(Yii::app()->createAbsoluteUrl('/usercpanel/main/index'));
                         }
                     }
                 }
                 // специальное перенаправления для корректного закрытия всплывающего окна
                 $authIdentity->redirect();
             } else {
                 // закрытие всплывающего окна и перенаправление на cancelUrl
                 $authIdentity->cancel();
             }
         }
         // авторизация не удалась, перенаправляем на страницу входа
         $this->redirect(array('site/login'));
     }
     if (isset($_POST['ajax']) && $_POST['ajax'] === 'login-form') {
         echo CActiveForm::validate($model);
         Yii::app()->end();
     }
     if (isset($_POST['LoginForm'])) {
         $model->attributes = $_POST['LoginForm'];
         if ($model->validate() && $model->login()) {
             User::updateUserSession();
             if (Yii::app()->user->getState('isAdmin')) {
                 NewsProduct::getProductNews();
                 $this->redirect(array('/apartments/backend/main/admin'));
                 Yii::app()->end();
             }
             /*if (Yii::app()->user->isGuest) {
             			$this->redirect(Yii::app()->user->returnUrl);
             		} else {
             			if (!Yii::app()->user->getState('returnedUrl')) {
             				$this->redirect(array('/usercpanel/main/index'));
             			} else {
             				$this->redirect(Yii::app()->user->getState('returnedUrl'));
             			}
             		}*/
             if (!Yii::app()->user->returnUrl) {
                 $this->redirect(array('/usercpanel/main/index'));
             } else {
                 $this->redirect(Yii::app()->user->returnUrl);
             }
         }
     }
     $this->render('login', array('model' => $model));
 }