Ejemplo n.º 1
0
 /**
  * render a user profile
  */
 public function actionView()
 {
     // load the user profile according to the request
     if (isset($_GET['u'])) {
         // look for the right user criteria to use according to the viewer permissions
         if (Yii::app()->user->pbac(array('user.admin', 'admin.admin'))) {
             $criteria = array('id' => $_GET['u']);
         } else {
             $criteria = array('id' => $_GET['u'], 'status' => UserGroupsUser::ACTIVE);
         }
         // load the profile
         $model = UserGroupsUser::model()->findByAttributes($criteria);
         if ($model === null || $model->relUserGroupsGroup->level > Yii::app()->user->level && !UserGroupsConfiguration::findRule('public_profiles')) {
             throw new CHttpException(404, Yii::t('userGroupsModule.general', 'The requested page does not exist.'));
         }
     } else {
         $model = $this->loadModel(Yii::app()->user->id);
     }
     // load the profile extensions
     $profiles = array();
     $profile_list = Yii::app()->controller->module->profile;
     foreach ($profile_list as $p) {
         // check if the profile data exist on the current user, otherwise
         // create an instance of the profile extension
         $relation = "rel{$p}";
         if (!$model->{$relation} instanceof CActiveRecord) {
             $p_instance = new $p();
         } else {
             $p_instance = $model->{$relation};
         }
         // check if the profile extension is supporting profile views
         $views = $p_instance->profileViews();
         if (isset($views[UserGroupsUser::VIEW])) {
             $profiles[] = array('view' => $views[UserGroupsUser::VIEW], 'model' => $p_instance);
         }
     }
     $service = Yii::app()->request->getQuery('service');
     if (isset($service) && !Yii::app()->user->isGuest) {
         $authIdentity = Yii::app()->eauth->getIdentity($service);
         $authIdentity->redirectUrl = Yii::app()->user->returnUrl;
         $authIdentity->cancelUrl = $this->createAbsoluteUrl('/profile/update');
         if ($authIdentity->authenticate()) {
             $identity = new ServiceUserIdentity($authIdentity);
             // успешная авторизация
             if ($identity->setAccountParams()) {
                 //Yii::app()->user->login($identity);
                 $serviceModel = UsergroupsSocialServices::model()->findByAttributes(array('service_name' => $identity->external_auth_id));
                 $isInAnoter = UsergroupsUserSocialAccounts::model()->findByAttributes(array('xml_id' => $identity->xml_id, 'external_auth_id' => $identity->external_auth_id), 'ug_id !=' . Yii::app()->user->id);
                 if ($serviceModel && (!$isInAnoter || $isInAnoter && count($isInAnoter->user->social_accounts) <= 1)) {
                     $userRes = array();
                     $oldUsers = UserGroupsUser::model()->findAllByAttributes(array('xml_id' => $identity->xml_id, 'external_auth_id' => $identity->external_auth_id), 'id !=' . Yii::app()->user->id);
                     if ($isInAnoter) {
                         $oldUsers[] = $isInAnoter->user;
                     }
                     if ($oldUsers) {
                         $userRes = Yii::app()->user->userModel->eatUsers($oldUsers);
                     }
                     $account = UsergroupsUserSocialAccounts::model()->findByPk(array('ug_id' => Yii::app()->user->id, 'service_id' => $serviceModel->id));
                     if (!$account) {
                         $account = new UsergroupsUserSocialAccounts();
                         $account->ug_id = Yii::app()->user->id;
                         $account->service_id = $serviceModel->id;
                     }
                     $account->xml_id = $identity->xml_id;
                     $account->external_auth_id = $identity->external_auth_id;
                     if ($account->save()) {
                         Yii::app()->user->setFlash('user', 'Аккаунт сервиса ' . $serviceModel->name . ' успешно добавлен!' . ($userRes ? '<br /> Поглощено пользователей: ' . $userRes['usersCnt'] . '<br /> Переназначено ям:' . $userRes['holesCnt'] . '<br /> Переназначено комментариев:' . $userRes['commentsCnt'] : ''));
                     }
                 } elseif ($isInAnoter) {
                     Yii::app()->user->setFlash('user', '<span style="color:red;">Ошибка! Этот аккаунт уже ассоциирован с пользователем ' . CHtml::link($isInAnoter->user->Fullname, array('/profile/view', 'id' => $isInAnoter->user->id)) . '.</span>');
                 }
                 // специальное перенаправления для корректного закрытия всплывающего окна
                 $this->redirect(array('/profile/update/'));
             } else {
                 // закрытие всплывающего окна и перенаправление на cancelUrl
                 $authIdentity->cancel();
             }
         }
         // авторизация не удалась, перенаправляем на страницу входа
         Yii::app()->user->setFlash('user', 'Ошибка! Невозможно авторизовать аккаунт.');
         $this->redirect(array('/profile/update/'));
     }
     if (Yii::app()->request->isAjaxRequest || isset($_GET['_isAjax'])) {
         $this->renderPartial('view', array('model' => $model, 'profiles' => $profiles), false, true);
     } else {
         $this->render('view', array('model' => $model, 'profiles' => $profiles));
     }
 }
Ejemplo n.º 2
0
 /**
  * This is the default 'index' action that is invoked
  * when an action is not explicitly requested by users.
  */
 public function actionUpdate()
 {
     $id = Yii::app()->user->id;
     $miscModel = $this->loadModel($id, 'changeMisc');
     $passModel = clone $miscModel;
     $passModel->setScenario('changePassword');
     $passModel->password = NULL;
     // pass the models inside the array for ajax validation
     $ajax_validation = array($miscModel, $passModel);
     // load additional profile models
     $profile_models = array();
     $profiles = array('Profile');
     foreach ($profiles as $p) {
         $external_profile = new $p();
         // check if the loaded profile has an update view
         $external_profile_views = $external_profile->profileViews();
         if (array_key_exists(UserGroupsUser::EDIT, $external_profile_views)) {
             // load the model data
             $loaded_data = $external_profile->findByAttributes(array('ug_id' => $id));
             $external_profile = $loaded_data ? $loaded_data : $external_profile;
             // set the scenario
             $external_profile->setScenario('updateProfile');
             // load the models inside both the ajax validation array and the profile models
             // array to pass it to the view
             $profile_models[$p] = $external_profile;
             $ajax_validation[] = $external_profile;
         }
     }
     if (!$miscModel->relProfile) {
         $miscModel->relProfile = new Profile();
     }
     // perform ajax validation
     $this->performAjaxValidation($ajax_validation);
     //print_r($profile_models);
     // check if an additional profile model form was sent
     if ($form = array_intersect_key($_POST, array_flip($profiles))) {
         $model_name = key($form);
         $form_values = reset($form);
         // load the form values into the model
         $miscModel->relProfile->attributes = $form_values;
         $miscModel->relProfile->ug_id = $id;
         // save the model
         if ($miscModel->relProfile->save()) {
             Yii::app()->user->setFlash('user', 'Данные успешно обновлены');
             //$this->redirect(Yii::app()->baseUrl . '/userGroups?_isAjax=1&u='.$passModel->username);
         } else {
             //Yii::app()->user->setFlash('user', Yii::t('userGroupsModule.general','An Error Occurred. Please try later.'));
         }
     }
     if (isset($_POST['UserGroupsUser']) && isset($_POST['formID'])) {
         // pass the right model according to the sended form and load the permitted values
         if ($_POST['formID'] === 'user-groups-password-form') {
             $model = $passModel;
         } else {
             if ($_POST['formID'] === 'user-groups-misc-form') {
                 $model = $miscModel;
             }
         }
         unset($_POST['UserGroupsUser']['group_id'], $_POST['UserGroupsUser']['creation_date']);
         $model->attributes = $_POST['UserGroupsUser'];
         //$model->unsetAttributes(Array('group_id','creation_date'));
         if ($model->validate()) {
             if ($model->username != $miscModel->username) {
                 $model->xml_id = '';
                 $model->external_auth_id = '';
             }
             if ($model->save()) {
                 Yii::app()->user->setFlash('user', 'Данные успешно обновлены');
                 $this->refresh();
                 //$this->renderPartial('update',array('miscModel'=>$miscModel,'passModel'=>$passModel, 'profiles' => $profile_models), false, true);
                 //$this->redirect(Array('/holes/personal'));
                 //$this->redirect(Yii::app()->baseUrl . '/userGroups?_isAjax=1&u='.$model->username);
             } else {
                 Yii::app()->user->setFlash('user', 'Произошла ошибка. Попробуйте позже.');
             }
         }
     }
     $socials = UsergroupsSocialServices::model()->with('account')->findAll();
     $this->render('update', array('miscModel' => $miscModel, 'passModel' => $passModel, 'profiles' => $profile_models, 'socials' => $socials), false, true);
 }