/** * @return User */ public function getUser() { if ($this->_user === null) { $this->_user = $this->finder->findUserByEmail($this->email); } return $this->_user; }
/** * Shows user's profile. * @param integer $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]); }
/** @inheritdoc */ public function rules() { return [['email', 'filter', 'filter' => 'trim'], ['email', 'required'], ['email', 'email'], ['email', 'exist', 'targetClass' => $this->module->modelMap['User'], 'message' => \Yii::t('user', 'There is no user with this email address')], ['email', function ($attribute) { $this->user = $this->finder->findUserByEmail($this->email); if ($this->user !== null && $this->module->enableConfirmation && !$this->user->getIsConfirmed()) { $this->addError($attribute, \Yii::t('user', 'You need to confirm your email address')); } }], ['password', 'required'], ['password', 'string', 'min' => 6]]; }
/** * @param $params * @return ActiveDataProvider */ public function search($params) { $userQuery = $this->finder->getUserQuery(); $modelClass = $userQuery->modelClass; $query = $modelClass::find()->joinWith(['profile']); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { return $dataProvider; } $query->andFilterWhere(['created_at' => $this->created_at])->orFilterWhere(['like', 'username', $this->username])->orFilterWhere(['like', 'profile.name', isset($params['UserSearch']) ? $params['UserSearch']['profile.name'] : ''])->orFilterWhere(['like', 'email', $this->email])->orFilterWhere(['registration_ip' => $this->registration_ip]); return $dataProvider; }
/** * Updates user's password to given. * * @param string $search Email or username * @param string $password New password */ public function actionIndex($search, $password) { $user = $this->finder->findUserByUsernameOrEmail($search); if ($user === null) { $this->stdout(\Yii::t('user', 'User is not found') . "\n", Console::FG_RED); } else { if ($user->resetPassword($password)) { $this->stdout(\Yii::t('user', 'Password has been changed') . "\n", Console::FG_GREEN); } else { $this->stdout(\Yii::t('user', 'Error occurred while changing password') . "\n", Console::FG_RED); } } }
/** @inheritdoc */ public function init() { $this->finder = \Yii::$container->get(Finder::className()); $this->mailer = \Yii::$container->get(Mailer::className()); $this->module = \Yii::$app->getModule('user'); parent::init(); }
/** * Logs the user in if this social account has been already used. Otherwise shows registration form. * @param ClientInterface $client * @return \yii\web\Response */ public function authenticate(ClientInterface $client) { $attributes = $client->getUserAttributes(); $provider = $client->getId(); $clientId = $attributes['id']; $account = $this->finder->findAccountByProviderAndClientId($provider, $clientId); if ($account === null) { $account = \Yii::createObject(['class' => Account::className(), 'provider' => $provider, 'client_id' => $clientId, 'data' => json_encode($attributes)]); $account->save(false); } if (null === ($user = $account->user)) { $this->action->successUrl = Url::to(['/user/registration/connect', 'account_id' => $account->id]); } else { \Yii::$app->user->login($user, $this->module->rememberFor); } }
/** * Finds the User model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $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; }
/** @inheritdoc */ public function beforeValidate() { if (parent::beforeValidate()) { $this->user = $this->finder->findUserByUsernameOrEmail($this->login); return true; } else { return false; } }
/** * Displays page where user can reset password. * @param integer $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(); if ($token === null || $token->isExpired || $token->user === null) { \Yii::$app->session->setFlash('danger', \Yii::t('user', 'Recovery link is invalid or expired. Please try requesting a new one.')); return $this->redirect(['request']); } $model = \Yii::createObject(['class' => RecoveryForm::className(), 'scenario' => 'reset']); $this->performAjaxValidation($model); if ($model->load(\Yii::$app->getRequest()->post()) && $model->resetPassword($token)) { return $this->redirect(['security/login']); } return $this->render('reset', ['model' => $model]); }
/** * Connects social account to user. * @param ClientInterface $client * @return \yii\web\Response */ public function connect(ClientInterface $client) { $attributes = $client->getUserAttributes(); $provider = $client->getId(); $clientId = $attributes['id']; $account = $this->finder->findAccountByProviderAndClientId($provider, $clientId); if ($account === null) { $account = \Yii::createObject(['class' => Account::className(), 'provider' => $provider, 'client_id' => $clientId, 'data' => json_encode($attributes), 'user_id' => \Yii::$app->user->id]); $account->save(false); \Yii::$app->session->setFlash('success', \Yii::t('user', 'Your account has been connected')); } else { \Yii::$app->session->setFlash('error', \Yii::t('user', 'This account has already been connected to another user')); } $this->action->successUrl = Url::to(['/user/settings/networks']); }
/** @inheritdoc */ public function bootstrap($app) { /** @var $module Module */ if ($app->hasModule('user') && ($module = $app->getModule('user')) instanceof Module) { $this->_modelMap = array_merge($this->_modelMap, $module->modelMap); foreach ($this->_modelMap as $name => $definition) { $class = "matacms\\user\\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', 'Account'])) { \Yii::$container->set($name . 'Query', function () use($modelName) { return $modelName::find(); }); } } \Yii::$container->setSingleton(Finder::className(), ['userQuery' => \Yii::$container->get('UserQuery'), 'profileQuery' => \Yii::$container->get('ProfileQuery'), 'tokenQuery' => \Yii::$container->get('TokenQuery'), 'accountQuery' => \Yii::$container->get('AccountQuery')]); if ($app instanceof ConsoleApplication) { $module->controllerNamespace = 'matacms\\user\\commands'; } else { \Yii::$container->set('yii\\web\\User', ['enableAutoLogin' => true, 'loginUrl' => ['/user/security/login'], 'identityClass' => $module->modelMap['User']]); $configUrlRule = ['prefix' => $module->urlPrefix, 'rules' => $module->urlRules]; if ($module->urlPrefix != 'user') { $configUrlRule['routePrefix'] = 'user'; } $app->get('urlManager')->rules[] = new GroupUrlRule($configUrlRule); if (!$app->has('authClientCollection')) { $app->set('authClientCollection', ['class' => Collection::className()]); } } $app->get('i18n')->translations['user*'] = ['class' => PhpMessageSource::className(), 'basePath' => __DIR__ . '/messages']; $defaults = ['welcomeSubject' => \Yii::t('user', 'Welcome to {0}', \Yii::$app->name), 'confirmationSubject' => \Yii::t('user', 'Confirm account on {0}', \Yii::$app->name), 'reconfirmationSubject' => \Yii::t('user', 'Confirm email change on {0}', \Yii::$app->name), 'recoverySubject' => \Yii::t('user', 'Complete password reset on {0}', \Yii::$app->name), 'passwordChangedSubject' => \Yii::t('user', 'Your password has been changed on {0}', \Yii::$app->name)]; \Yii::$container->set('matacms\\user\\Mailer', array_merge($defaults, $module->mailer)); } Event::on(ActiveField::className(), ActiveField::EVENT_INIT_DONE, function (MessageEvent $event) { $event->getMessage()->attachBehavior('moduleAccessibility', new ModuleAccessibilityActiveFormBehavior()); }); Event::on(AdminController::class, Controller::EVENT_MODEL_UPDATED, function (\matacms\base\MessageEvent $event) { $this->processSave($event->getMessage()); }); Event::on(AdminController::class, Controller::EVENT_MODEL_CREATED, function (\matacms\base\MessageEvent $event) { $this->processSave($event->getMessage()); }); }