/** @inheritdoc */ public function bootstrap($app) { /** @var Module $module */ /** @var \yii\db\ActiveRecord $modelName */ if ($app->hasModule('user') && ($module = $app->getModule('user')) instanceof Module) { Yii::$container->setSingleton(UserFinder::className(), ['userQuery' => \jarrus90\User\models\User::find(), 'profileQuery' => \jarrus90\User\models\Profile::find(), 'tokenQuery' => \jarrus90\User\models\Token::find(), 'accountQuery' => \jarrus90\User\models\Account::find()]); if (!isset($app->get('i18n')->translations['rbac'])) { $app->get('i18n')->translations['rbac'] = ['class' => 'yii\\i18n\\PhpMessageSource', 'basePath' => __DIR__ . '/messages', 'sourceLanguage' => 'en-US']; } if (!isset($app->get('i18n')->translations['user'])) { $app->get('i18n')->translations['user'] = ['class' => PhpMessageSource::className(), 'basePath' => __DIR__ . '/messages', 'sourceLanguage' => 'en-US']; } if (!$app instanceof ConsoleApplication) { $module->controllerNamespace = 'jarrus90\\User\\Controllers'; if (!Yii::$container->has('yii\\web\\User')) { Yii::$container->set('yii\\web\\User', ['enableAutoLogin' => true, 'loginUrl' => ['/user/security/login'], 'identityClass' => \jarrus90\User\models\User::className()]); } $configUrlRule = ['prefix' => $module->urlPrefix, 'rules' => $module->urlRules]; if ($module->urlPrefix != 'user') { $configUrlRule['routePrefix'] = 'user'; } $configUrlRule['class'] = 'yii\\web\\GroupUrlRule'; $rule = Yii::createObject($configUrlRule); $app->urlManager->addRules([$rule], false); if (!$app->has('authClientCollection')) { $app->set('authClientCollection', ['class' => Collection::className()]); } $app->params['admin']['menu']['user'] = function () use($module) { return $module->getAdminMenu(); }; $app->params['admin']['menu']['logout'] = ['label' => Yii::t('user', 'Logout'), 'icon' => '<i class="fa fa-sign-out"></i>', 'url' => '/user/security/logout']; } else { if (empty($app->controllerMap['migrate'])) { $app->controllerMap['migrate']['class'] = 'yii\\console\\controllers\\MigrateController'; } $app->controllerMap['migrate']['migrationNamespaces'][] = 'jarrus90\\User\\migrations'; } if (!$app->authManager instanceof DbManager) { $app->set('authManager', ['class' => DbManager::className(), 'cache' => $app->cache]); } Yii::$container->set('jarrus90\\User\\Mailer', $module->mailer); } }
/** * Resets user's password. * * @param Token $token * * @return bool */ public function resetPassword(Token $token) { if (!$this->validate() || $token->user === null) { return false; } if ($token->user->resetPassword($this->password)) { Yii::$app->session->setFlash('success', Yii::t('user', 'Your password has been changed successfully.')); $token->delete(); } else { Yii::$app->session->setFlash('danger', Yii::t('user', 'An error occurred and your password has not been changed. Please try again later.')); } return true; }
/** * This method is used to register new user account. If Module::enableConfirmation is set true, this method * will generate new confirmation token and use mailer to send it to the user. * * @return bool */ public function register() { if ($this->getIsNewRecord() == false) { throw new \RuntimeException('Calling "' . __CLASS__ . '::' . __METHOD__ . '" on existing user'); } $this->confirmed_at = $this->module->enableConfirmation ? null : time(); $this->password = $this->module->enableGeneratingPassword ? Password::generate(8) : $this->password; $this->trigger(self::BEFORE_REGISTER); if (!$this->save()) { return false; } if ($this->module->enableConfirmation) { /** @var Token $token */ $token = Yii::createObject(['class' => Token::className(), 'type' => Token::TYPE_CONFIRMATION]); $token->link('user', $this); } $this->mailer->sendWelcomeMessage($this, isset($token) ? $token : null); $this->trigger(self::AFTER_REGISTER); return true; }
/** * Creates new confirmation token and sends it to the user. * * @return bool */ public function resend() { if (!$this->validate()) { return false; } /** @var Token $token */ $token = Yii::createObject(['class' => Token::className(), 'user_id' => $this->user->id, 'type' => Token::TYPE_CONFIRMATION]); $token->save(false); $this->mailer->sendConfirmationMessage($this->user, $token); Yii::$app->session->setFlash('info', Yii::t('user', 'A message has been sent to your email address. It contains a confirmation link that you must click to complete registration.')); return true; }
/** * Sends a confirmation message to both old and new email addresses with link to confirm changing of email. * * @throws \yii\base\InvalidConfigException */ protected function secureEmailChange() { $this->defaultEmailChange(); /** @var Token $token */ $token = Yii::createObject(['class' => Token::className(), 'user_id' => $this->user->id, 'type' => Token::TYPE_CONFIRM_OLD_EMAIL]); $token->save(false); $this->mailer->sendReconfirmationMessage($this->user, $token); // unset flags if they exist $this->user->flags &= ~User::NEW_EMAIL_CONFIRMED; $this->user->flags &= ~User::OLD_EMAIL_CONFIRMED; $this->user->save(false); Yii::$app->session->setFlash('info', Yii::t('user', 'We have sent confirmation links to both old and new email addresses. You must click both links to complete your request')); }