Beispiel #1
0
 /**
  * @return User
  */
 public function getUser()
 {
     if ($this->_user === null) {
         $this->_user = $this->finder->findUserByEmail($this->email);
     }
     return $this->_user;
 }
Beispiel #2
0
 /** @inheritdoc */
 public function rules()
 {
     return ['emailTrim' => ['email', 'filter', 'filter' => 'trim'], 'emailRequired' => ['email', 'required'], 'emailPattern' => ['email', 'email'], 'emailExist' => ['email', 'exist', 'targetClass' => User::className(), '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->isBlocked) {
             $this->addError($attribute, Yii::t('user', 'Your account has been blocked'));
         }
     }], 'passwordRequired' => ['password', 'required'], 'passwordLength' => ['password', 'string', 'min' => 6]];
 }
 /**
  * Tries to authenticate user via social network. If user has already used
  * this network's account, he will be logged in. Otherwise, it will try
  * to create new user account.
  *
  * @param ClientInterface $client
  */
 public function authenticate(ClientInterface $client)
 {
     $account = $this->finder->findAccount()->byClient($client)->one();
     if (!$this->module->enableRegistration && ($account === null || $account->user === null)) {
         Yii::$app->session->setFlash('danger', Yii::t('user', 'Registration on this website is disabled'));
         $this->action->successUrl = Url::to(['/user/security/login']);
         return;
     }
     if ($account === null) {
         /** @var Account $account */
         $accountObj = Yii::createObject(Account::className());
         $account = $accountObj::create($client);
     }
     $event = $this->getAuthEvent($account, $client);
     $this->trigger(self::EVENT_BEFORE_AUTHENTICATE, $event);
     if ($account->user instanceof User) {
         if ($account->user->isBlocked) {
             Yii::$app->session->setFlash('danger', Yii::t('user', 'Your account has been blocked.'));
             $this->action->successUrl = Url::to(['/user/security/login']);
         } else {
             Yii::$app->user->login($account->user, $this->module->rememberFor);
             $this->action->successUrl = Yii::$app->getUser()->getReturnUrl();
         }
     } else {
         $this->action->successUrl = $account->getConnectUrl();
     }
     $this->trigger(self::EVENT_AFTER_AUTHENTICATE, $event);
 }
 /**
  * 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]);
 }
Beispiel #5
0
 /** @inheritdoc */
 public function beforeValidate()
 {
     if (parent::beforeValidate()) {
         $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]);
 }
 /**
  * Disconnects a network account from user.
  *
  * @param int $id
  *
  * @return \yii\web\Response
  * @throws \yii\web\NotFoundHttpException
  * @throws \yii\web\ForbiddenHttpException
  */
 public function actionDisconnect($id)
 {
     $account = $this->finder->findAccount()->byId($id)->one();
     if ($account === null) {
         throw new NotFoundHttpException();
     }
     if ($account->user_id != Yii::$app->user->id) {
         throw new ForbiddenHttpException();
     }
     $event = $this->getConnectEvent($account, $account->user);
     $this->trigger(self::EVENT_BEFORE_DISCONNECT, $event);
     $account->delete();
     $this->trigger(self::EVENT_AFTER_DISCONNECT, $event);
     return $this->redirect(['networks']);
 }
Beispiel #8
0
 /** @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);
     }
 }
Beispiel #9
0
 /**
  * @return UserFinder
  * @throws \yii\base\InvalidConfigException
  */
 protected function getUserFinder()
 {
     return Yii::$container->get(UserFinder::className());
 }
Beispiel #10
0
 /**
  * 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;
 }
Beispiel #11
0
 /**
  * @return UserFinder
  */
 protected static function getUserFinder()
 {
     if (static::$finder === null) {
         static::$finder = \Yii::$container->get(UserFinder::className());
     }
     return static::$finder;
 }