/** * 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(); }
/** * 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; } } }
/** * Updates a particular model. * Accessible only to authenticated users and admin. * If update is successful, the browser will be redirected to the 'show' page. */ public function actionUpdate() { $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']; $detailsCopy = $model->details; // email is assigned in {@link User::beforeValidate} // validate with the current action as scenario and save without validation if (($validated = $model->validate()) !== false && ($saved = $model->save(false)) !== false) { if ($model->isMe) { // update variables previously defined in {@link _CUserIdentity} class // update user states in the session for {@link _CController::init} Yii::app()->user->setState('language', $model->language); // update user screenName, so we continue calling visitor right, Yii::app()->user->setState('screenName', $model->screenName); // set user preferred language if (!empty($model->language)) { W3::setLanguage($model->language); } // we do not need to update user cookie any more because // we overrode auto-login with {@link _CWebUser::restoreFromCookie} } // user details $details = array(); if ($model->isActive === User::IS_ACTIVE && $model->details->deactivationTime !== null) { // FIXME: if null means active, then add || $model->isActive===null $details['deactivationTime'] = null; } else { if (($model->isActive === User::IS_NOT_ACTIVE || $model->isActive === null) && empty($model->details->deactivationTime)) { $details['deactivationTime'] = time(); } } if (isset($_POST['UserDetails']) || count($details) >= 1 || $model->details != $detailsCopy) { if (isset($_POST['UserDetails'])) { // collect user input data $model->details->attributes = $_POST['UserDetails']; } foreach ($details as $attribute => $value) { // set attributes outside of the form $model->details->{$attribute} = $value; } // validate with the current action as scenario if (($validated = $model->details->validate()) !== false) { if (($saved = $model->details->save()) !== false) { // set success message MUserFlash::setTopSuccess(Yii::t('hint', $model->isMe ? '{screenName}, your profile has been updated.' : 'The member account "{screenName}" has been updated.', array('{screenName}' => MHtml::wrapInTag($model->screenName, 'strong')))); // go to 'show' page $this->redirect($model->isMe && !$pkIsPassed ? array('show') : array('show', 'id' => $model->id)); } else { // set error message MUserFlash::setTopError(Yii::t('hint', $model->isMe ? 'Error! {screenName}, your profile could not be updated.' : 'Error! The 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->details), '{modelId}' => $model->details->userId, '{method}' => __METHOD__ . '()')), 'error', 'w3'); } } } } else { if ($validated && !$saved) { // set error message MUserFlash::setTopError(Yii::t('hint', $model->isMe ? 'Error! {screenName}, your profile could not be updated.' : 'Error! The 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)); }