Ejemplo n.º 1
0
 /**
  * This is where the system is being initialized from.
  */
 public function init()
 {
     // Yii initialization is a must
     parent::init();
     // universal storage
     $this->var = new MVariable();
     // call our initialization class
     W3Init::controller();
     // set user preferences (interface, language, and so on)
     if (!Yii::app()->user->isGuest) {
         if (isset(Yii::app()->user->interface) && !empty(Yii::app()->user->interface)) {
             // set user preferred interface
             W3::setInterface(Yii::app()->user->interface);
         }
         if (isset(Yii::app()->user->language) && !empty(Yii::app()->user->language)) {
             // set user preferred language
             W3::setLanguage(Yii::app()->user->language);
         }
     }
     // parameters were loaded before language was set, now they need to be translated
     MParams::i18n();
 }
Ejemplo n.º 2
0
 /**
  * Authenticates the password.
  * This is the 'authenticate' validator as declared in rules().
  */
 public function authenticate($attribute, $params)
 {
     if (!$this->hasErrors()) {
         $identity = new _CUserIdentity($this->{self::getLoggingWithField()}, $this->password);
         $identity->authenticate();
         switch ($identity->errorCode) {
             case _CUserIdentity::ERROR_NONE:
                 // if user is already logged in
                 if (!Yii::app()->user->isGuest) {
                     // log user out from the current account. i want to sleep well, do you? ;)
                     Yii::app()->user->logout();
                     if (!Yii::app()->getSession()->getIsStarted()) {
                         // restore http session. this is necessary for login
                         Yii::app()->getSession()->open();
                     }
                 }
                 // remember for 30 days. makes sence only if auto-login is allowed
                 $duration = Yii::app()->user->allowAutoLogin && $this->rememberMe ? 3600 * 24 * 30 : 0;
                 // log user in and save in session all appended data
                 Yii::app()->user->login($identity, $duration);
                 // set user preferences (for welcome message, and so on)
                 if (isset(Yii::app()->user->interface) && !empty(Yii::app()->user->interface)) {
                     // set user preferred interface
                     W3::setInterface(Yii::app()->user->interface);
                 }
                 if (isset(Yii::app()->user->language) && !empty(Yii::app()->user->language)) {
                     // set user preferred language
                     W3::setLanguage(Yii::app()->user->language);
                 }
                 break;
             case _CUserIdentity::ERROR_USERNAME_INVALID:
                 if (self::getLoggingWithField() === 'username') {
                     $this->addError('username', Yii::t('t', 'Username is incorrect.'));
                 } else {
                     if (self::getLoggingWithField() === 'email') {
                         $this->addError('email', Yii::t('t', 'Email is incorrect.'));
                     } else {
                         if (self::getLoggingWithField() === 'usernameOrEmail') {
                             $this->addError('usernameOrEmail', Yii::t('t', 'Username or email is incorrect.'));
                         }
                     }
                 }
                 break;
             case _CUserIdentity::ERROR_ACCOUNT_IS_INACTIVE:
                 // set the error message
                 MUserFlash::setTopError(Yii::t('hint', 'We are sorry, but your member account is marked as "inactive". Inactive member accounts are temporarely inaccessible. {contactLink}.', array('{contactLink}' => CHtml::link(Yii::t('link', 'Contact us'), array('site/contact')))));
                 // add to username (first field in the login form) error css class
                 // and make the validate() to fail
                 $attribute = self::getLoggingWithField();
                 $attribute !== 'username' && $attribute !== 'email' && $attribute !== 'usernameOrEmail' && ($attribute = 'username');
                 $this->addError($attribute, '');
                 break;
             case _CUserIdentity::ERROR_IS_NOT_ADMINISTRATOR:
                 // set the error message
                 MUserFlash::setTopError(Yii::t('hint', 'We are sorry, but your access type is {accessType}. Required access type: {requiredAccessType}.', array('{accessType}' => Yii::app()->controller->var->userAccessType, '{requiredAccessType}' => Yii::t('t', User::ADMINISTRATOR_T))));
                 unset(Yii::app()->controller->var->userAccessType);
                 // we do not need this any more
                 // add to username (first field in the login form) error css class
                 // and make the validate() to fail
                 $attribute = self::getLoggingWithField();
                 $attribute !== 'username' && $attribute !== 'email' && $attribute !== 'usernameOrEmail' && ($attribute = 'username');
                 $this->addError($attribute, '');
                 break;
             case _CUserIdentity::ERROR_PASSWORD_INVALID:
             default:
                 $this->addError('password', Yii::t('t', 'Password is incorrect.'));
                 break;
         }
     }
 }
Ejemplo n.º 3
0
 /**
  * Update user interface.
  * Accessible only to authenticated users and admin.
  * If update is successful, the browser will be redirected to the 'show' page.
  */
 public function actionUpdateInterface()
 {
     if (!Yii::app()->user->checkAccess($this->route, array('model' => $this->loadModel()))) {
         // access denied
         MUserFlash::setTopError(Yii::t('accessDenied', $this->route));
         $this->redirect($this->getGotoUrl());
     }
     $pkIsPassed = isset($_GET['id']);
     if (($model = $this->loadModel()) === null) {
         // model not found
         MUserFlash::setTopError(Yii::t('modelNotFound', $this->id));
         $this->redirect($this->getGotoUrl());
     }
     // explicitly set model scenario to be current action
     $model->setScenario($this->action->id);
     if (is_object($model->details)) {
         $model->details->setScenario($this->action->id);
     }
     // whether data is passed
     if (isset($_POST['User'])) {
         // collect user input data
         $model->attributes = $_POST['User'];
         // validate with the current action as scenario and save without validation
         if (($validated = $model->validate()) !== false && ($saved = $model->save(false)) !== false) {
             // take care of updateTime (this is not critical)
             $model->details->saveAttributes(array('updateTime' => time()));
             // update variables first defined in {@link _CUserIdentity} class
             if ($model->isMe) {
                 // update user states in the session for {@link _CController::init}
                 Yii::app()->user->setState('interface', $model->interface);
                 // set user preferred interface
                 if (!empty($model->interface)) {
                     W3::setInterface($model->interface);
                 }
                 // we do not need to update user cookie any more because
                 // we overrode auto-login with {@link _CWebUser::restoreFromCookie}
             }
             // set success message
             MUserFlash::setTopSuccess(Yii::t('hint', $model->isMe ? '{screenName}, new user interface has been applied.' : 'The user interface for member account "{screenName}" has been updated.', array('{screenName}' => MHtml::wrapInTag($model->screenName, 'strong'))));
             // go to 'show' page
             $this->redirect($model->isMe ? array('show') : array('show', 'id' => $model->id));
         } else {
             if ($validated && !$saved) {
                 // set error message
                 MUserFlash::setTopError(Yii::t('hint', $model->isMe ? 'Error! {screenName}, new user interface could not be applied.' : 'Error! The user interface for member account "{screenName}" could not be updated.', array('{screenName}' => MHtml::wrapInTag($model->screenName, 'strong'))));
                 Yii::log(W3::t('system', 'Could not save attributes of the {model} model. Model ID: {modelId}. Method called: {method}.', array('{model}' => get_class($model), '{modelId}' => $model->id, '{method}' => __METHOD__ . '()')), 'error', 'w3');
             }
         }
     }
     // display the update form
     $this->render($this->action->id, array('model' => $model, 'pkIsPassed' => $pkIsPassed));
 }