/** * Updates an user profile * * @param array $data the profile data attributes * * @return \yii\web\Response|null * @throws EmailException */ protected function update($data) { $event = new UpdateEvent(); $event->extract($data); $event->model->scenario = Module::SCN_PROFILE; $post = Yii::$app->request->post(); $hasProfile = $this->_module->getProfileSetting('enabled'); $emailOld = $event->model->email; $this->_module->trigger(Module::EVENT_PROFILE_UPDATE_BEGIN, $event); $transaction = static::tranInit($event); try { if ($hasProfile || isset($post['UserProfile'])) { $validate = $event->model->load($post) && $event->profile->load($post) && Model::validateMultiple([$event->model, $event->profile]); } else { $validate = $event->model->load($post) && $event->model->validate(); } if ($validate) { $timeLeft = Module::timeLeft('email change confirmation', $event->model->getEmailChangeKeyExpiry()); $emailNew = null; if ($event->model->validateEmailChange($emailOld)) { $emailNew = $event->model->email_new; } $event->model->save(); if ($hasProfile || isset($post['UserProfile'])) { $event->profile->uploadAvatar(); $event->profile->save(); } $event->flashType = 'success'; $event->message = Yii::t('user', 'The user profile was updated successfully.'); $this->_module->trigger(Module::EVENT_PROFILE_UPDATE_COMPLETE, $event); $action = $this->fetchAction(Module::ACTION_PROFILE_INDEX); self::setFlash($event); if (!$event->model->sendEmail('newemail', $timeLeft)) { throw new EmailException(Yii::t('user', 'Your email change to <b>{email}</b> could not be processed. Please contact the system administrator or try again later.', ['email' => $emailNew])); } static::tranCommit($transaction); Yii::$app->session->setFlash('info', Yii::t('user', 'Instructions to confirm the new email has been sent to your new email address <b>{email}</b>. {timeLeft}', ['email' => $emailNew, 'timeLeft' => $timeLeft])); return $this->eventRedirect($event, [$action], false); } } catch (Exception $e) { $this->handleException($e); static::tranRollback($transaction); } return null; }
/** * Requests password reset. * * @return string|\yii\web\Response */ public function actionRecovery() { /** * @var RecoveryForm $model * @var User $class * @var User $user */ $class = $this->fetchModel(Module::MODEL_RECOVERY); $model = new $class(); $event = new RecoveryEvent(); $event->model = $model; $this->_module->trigger(Module::EVENT_RECOVERY_BEGIN, $event); if ($model->load(Yii::$app->request->post()) && $model->validate()) { $transaction = static::tranInit($event); try { $class = $this->fetchModel(Module::MODEL_USER); $user = $class::findByEmail($model->email); $proceed = true; $expiry = $user->getResetKeyExpiry(); if (!$class::isKeyValid($user->reset_key, $expiry)) { $user->scenario = Module::SCN_RECOVERY; $user->generateResetKey(); $proceed = $user->save(); } $timeLeft = Module::timeLeft('reset', $expiry); if ($proceed && $user->sendEmail('recovery', $timeLeft)) { $event->flashType = 'success'; $event->message = Yii::t('user', 'Check your email for further instructions to reset your password. {timeLeft}', ['timeLeft' => $timeLeft]); $event->handled = false; $this->_module->trigger(Module::EVENT_RECOVERY_COMPLETE, $event); static::setFlash($event); static::tranCommit($transaction); return $this->eventRedirect($event, $this->goHome()); } else { $event->flashType = 'error'; $event->message = Yii::t('user', 'Sorry, the password cannot be reset for the email provided. Retry again later.'); $this->_module->trigger(Module::EVENT_RECOVERY_COMPLETE, $event); throw new Exception('Error resetting password'); } } catch (Exception $e) { static::tranRollback($transaction); $this->raise($e, $event); } } static::setFlash($event); return $this->display($event->viewFile ? $event->viewFile : Module::VIEW_RECOVERY, ['model' => $model]); }