/** * @return User */ public function getUser() { if ($this->_user === null) { $this->_user = $this->finder->findUserByEmail($this->email); } return $this->_user; }
/** * Shows user's profile. * * @param int $id * * @return \yii\web\Response * @throws \yii\web\NotFoundHttpException */ public function actionShow($id) { $profile = $this->finder->findProfileById($id); if ($profile === null) { throw new NotFoundHttpException(); } return $this->render('show', ['profile' => $profile]); }
/** * @param $params * * @return ActiveDataProvider */ public function search($params) { $query = $this->finder->getUserTypeQuery(); $dataProvider = new ActiveDataProvider(['query' => $query]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['type_id' => $this->type_id])->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'alias', $this->alias]); return $dataProvider; }
/** @inheritdoc */ public function rules() { return ['emailTrim' => ['email', 'filter', 'filter' => 'trim'], 'emailRequired' => ['email', 'required'], 'emailPattern' => ['email', 'email'], 'emailExist' => ['email', 'exist', 'targetClass' => $this->module->modelMap['User'], 'message' => Yii::t('user', 'There is no user with this email address')], 'emailUnconfirmed' => ['email', function ($attribute) { $this->user = $this->finder->findUserByEmail($this->email); if ($this->user !== null && $this->module->enableConfirmation && !$this->user->isConfirmed) { $this->addError($attribute, Yii::t('user', 'You need to confirm your email address')); } if ($this->user->isBanned) { $this->addError($attribute, Yii::t('user', 'Your account has been blocked')); } }], 'passwordRequired' => ['password', 'required'], 'passwordLength' => ['password', 'string', 'min' => 6]]; }
/** * @param $params * * @return ActiveDataProvider */ public function search($params) { $query = $this->finder->getUserQuery(); $dataProvider = new ActiveDataProvider(['query' => $query]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } if ($this->creation_date !== null) { $date = strtotime($this->created_at); $query->andFilterWhere(['between', 'creation_date', $date, $date + 3600 * 24]); } $query->andFilterWhere(['like', 'username', $this->username])->andFilterWhere(['like', 'email', $this->email])->andFilterWhere(['signup_ip' => $this->signup_ip])->andFilterWhere(['user_type' => $this->user_type]); return $dataProvider; }
/** * Displays page where user can reset password. * * @param int $id * @param string $code * * @return string * @throws \yii\web\NotFoundHttpException */ public function actionReset($id, $code) { if (!$this->module->enablePasswordRecovery) { throw new NotFoundHttpException(); } /** @var Token $token */ $token = $this->finder->findToken(['user_id' => $id, 'code' => $code, 'type' => Token::TYPE_RECOVERY])->one(); $event = $this->getResetPasswordEvent($token); $this->trigger(self::EVENT_BEFORE_TOKEN_VALIDATE, $event); if ($token === null || $token->isExpired || $token->user === null) { $this->trigger(self::EVENT_AFTER_TOKEN_VALIDATE, $event); Yii::$app->session->setFlash('danger', Yii::t('user', 'Recovery link is invalid or expired. Please try requesting a new one.')); return $this->render('/message', ['title' => Yii::t('user', 'Invalid or expired link'), 'module' => $this->module]); } /** @var RecoveryForm $model */ $model = Yii::createObject(['class' => RecoveryForm::className(), 'scenario' => 'reset']); $event->setForm($model); $this->performAjaxValidation($model); $this->trigger(self::EVENT_BEFORE_RESET, $event); if ($model->load(Yii::$app->getRequest()->post()) && $model->resetPassword($token)) { $this->trigger(self::EVENT_AFTER_RESET, $event); return $this->render('/message', ['title' => Yii::t('user', 'Password has been changed'), 'module' => $this->module]); } return $this->render('reset', ['model' => $model]); }
/** @inheritdoc */ public function beforeValidate() { if (parent::beforeValidate()) { // Load the user before the validation takes place $this->user = $this->finder->findUserByUsernameOrEmail(trim($this->login)); return true; } else { return false; } }
/** * Confirms user's account. If confirmation was successful logs the user and shows success message. Otherwise * shows error message. * * @param int $id * @param string $code * * @return string * @throws \yii\web\HttpException */ public function actionConfirm($id, $code) { $user = $this->finder->findUserById($id); if ($user === null || $this->module->enableConfirmation == false) { throw new NotFoundHttpException(); } $event = $this->getUserEvent($user); $this->trigger(self::EVENT_BEFORE_CONFIRM, $event); $user->attemptConfirmation($code); $this->trigger(self::EVENT_AFTER_CONFIRM, $event); return $this->render('/message', ['title' => Yii::t('user', 'Account confirmation'), 'module' => $this->module]); }
/** * Attempts changing user's email address. * * @param int $id * @param string $code * * @return string * @throws \yii\web\HttpException */ public function actionConfirm($id, $code) { $user = $this->finder->findUserById($id); if ($user === null || $this->module->emailChangeStrategy == Module::STRATEGY_INSECURE) { throw new NotFoundHttpException(); } $event = $this->getUserEvent($user); $this->trigger(self::EVENT_BEFORE_CONFIRM, $event); $user->attemptEmailChange($code); $this->trigger(self::EVENT_AFTER_CONFIRM, $event); return $this->redirect(['account']); }
/** @inheritdoc */ public function bootstrap($app) { /** @var Module $module */ /** @var \yii\db\ActiveRecord $modelName */ if ($app->hasModule('user') && ($module = $app->getModule('user')) instanceof UserControl) { $this->_modelMap = array_merge($this->_modelMap, $module->modelMap); foreach ($this->_modelMap as $name => $definition) { $class = "lnch\\users\\models\\" . $name; Yii::$container->set($class, $definition); $modelName = is_array($definition) ? $definition['class'] : $definition; $module->modelMap[$name] = $modelName; if (in_array($name, ['User', 'Profile', 'Token', 'UserType'])) { Yii::$container->set($name . 'Query', function () use($modelName) { return $modelName::find(); }); } } // Creates a 'model' that is an ActiveQuery of it's base class. Clever! Yii::$container->setSingleton(Finder::className(), ['userQuery' => Yii::$container->get('UserQuery'), 'profileQuery' => Yii::$container->get('ProfileQuery'), 'tokenQuery' => Yii::$container->get('TokenQuery'), 'userTypeQuery' => Yii::$container->get('UserTypeQuery')]); if ($app instanceof ConsoleApplication) { $module->controllerNamespace = 'lnch\\users\\commands'; } else { // Set the Yii user component Yii::$container->set('yii\\web\\User', ['enableAutoLogin' => true, 'loginUrl' => ['/user/security/login'], 'class' => $module->modelMap['BaseUser'], 'identityClass' => $module->modelMap['User']]); // Create the array for the Group URL rules $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 (!isset($app->get('i18n')->translations['user*'])) { $app->get('i18n')->translations['user*'] = ['class' => PhpMessageSource::className(), 'basePath' => __DIR__ . '/messages', 'sourceLanguage' => 'en-GB']; } // Load assets } }
/** * Finds the User model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * * @param int $id * * @return User the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { $user = $this->finder->findUserById($id); if ($user === null) { throw new NotFoundHttpException('The requested page does not exist'); } return $user; }
/** * @return Finder * @throws \yii\base\InvalidConfigException */ protected function getFinder() { return Yii::$container->get(Finder::className()); }