/**
  * @param string $attribute
  * @param array $params
  */
 public function validatePassword($attribute, $params)
 {
     if (!$this->hasErrors()) {
         if (!$this->_user->validatePassword($this->{$attribute})) {
             $this->addError($attribute, Yii::t('user', 'ERROR_WRONG_CURRENT_PASSWORD'));
         }
     }
 }
Example #2
0
 /**
  * Finds user by [[username]]
  *
  * @return User|null
  */
 public function getUser()
 {
     if ($this->_user === false) {
         $this->_user = User::findByUsername($this->username);
     }
     return $this->_user;
 }
Example #3
0
 public function beforeSave($insert)
 {
     if (parent::beforeSave($insert)) {
         if (!empty($this->password)) {
             $this->setPassword($this->password);
         }
         return true;
     }
     return false;
 }
Example #4
0
 /**
  * Creates a form model given a token.
  *
  * @param  string $token
  * @param  array $config
  * @throws \yii\base\InvalidParamException if token is empty or not valid
  */
 public function __construct($token, $config = [])
 {
     if (empty($token) || !is_string($token)) {
         throw new InvalidParamException('Email confirm token cannot be blank.');
     }
     $this->_user = User::findByEmailConfirmToken($token);
     if (!$this->_user) {
         throw new InvalidParamException('Wrong Email confirm token.');
     }
     parent::__construct($config);
 }
 /**
  * Creates a form model given a token.
  *
  * @param  string                          $token
  * @param  array                           $config name-value pairs that will be used to initialize the object properties
  * @throws \yii\base\InvalidParamException if token is empty or not valid
  */
 public function __construct($token, $config = [])
 {
     if (empty($token) || !is_string($token)) {
         throw new InvalidParamException('Password reset token cannot be blank.');
     }
     $this->_user = User::findByPasswordResetToken($token);
     if (!$this->_user) {
         throw new InvalidParamException('Wrong password reset token.');
     }
     parent::__construct($config);
 }
 /**
  * Sends an email with a link, for resetting the password.
  *
  * @return boolean whether the email was send
  */
 public function sendEmail()
 {
     /* @var $user User */
     $user = User::findOne(['email' => $this->email]);
     if ($user && $user->status == User::STATUS_BLOCKED) {
         Yii::$app->session->setFlash('error', Yii::t('user', 'ERROR_PROFILE_BLOCKED'));
     } elseif ($user) {
         if (!User::isPasswordResetTokenValid($user->password_reset_token)) {
             $user->generatePasswordResetToken();
         }
         if ($user->save()) {
             return \Yii::$app->mailer->compose('@worstinme/user/mail/passwordResetToken', ['user' => $user])->setFrom([Yii::$app->params['adminEmail'] => 'robot'])->setTo($this->email)->setSubject(Yii::t('user', 'EMAIL_TITLE_PASSWORD_RESET', ['sitename' => \Yii::$app->name]))->send();
         }
     }
     return false;
 }
 public function onAuthSuccess($client)
 {
     $attributes = $client->getUserAttributes();
     $service = $client->getId();
     if (empty($attributes['email']) && $service == 'vkontakte') {
         $attributes['email'] = $attributes['id'] . '@vk.com';
     } elseif (empty($attributes['email']) && $service == 'twitter') {
         $attributes['email'] = $attributes['id'] . '@twitter.com';
     } elseif (empty($attributes['email'])) {
         $attributes['email'] = $attributes['id'] . "@{$service}.com";
     }
     /* @var $auth Auth */
     $auth = UserService::find()->where(['source' => $client->getId(), 'source_id' => $attributes['id']])->one();
     if (Yii::$app->user->isGuest) {
         if ($auth) {
             // login
             $user = $auth->user;
             Yii::$app->user->login($user);
         } else {
             // signup
             if (User::find()->where(['email' => $attributes['email']])->exists()) {
                 Yii::$app->session->setFlash('error', Yii::t('user', "SERVICE_USER_EMAIL_EXISTS"));
             } else {
                 $user = new User(['username' => $attributes['email'], 'email' => $attributes['email'], 'status' => User::STATUS_SOCIAL]);
                 $user->setPassword(Yii::$app->security->generateRandomString(6));
                 $user->generateAuthKey();
                 $user->generatePasswordResetToken();
                 $transaction = $user->getDb()->beginTransaction();
                 if ($user->save()) {
                     $auth = new UserService(['user_id' => $user->id, 'source' => $client->getId(), 'source_id' => (string) $attributes['id']]);
                     if ($auth->save()) {
                         $transaction->commit();
                         Yii::$app->user->login($user);
                     } else {
                         Yii::$app->session->setFlash('error', Yii::t('user', "SERVICE_REG_FAIL") . ' ' . \yii\helpers\Json::encode($auth->getErrors()));
                     }
                 } else {
                     Yii::$app->session->setFlash('error', Yii::t('user', "SERVICE_REG_FAIL") . ' ' . \yii\helpers\Json::encode($user->getErrors()));
                 }
             }
         }
     } else {
         // user already logged in
         if (!$auth) {
             // add auth provider
             $auth = new UserService(['user_id' => Yii::$app->user->identity->id, 'source' => $client->getId(), 'source_id' => $attributes['id']]);
             $auth->save();
         }
     }
     if (!Yii::$app->user->isGuest) {
         $this->action->successUrl = \yii\helpers\Url::previous();
     }
 }
Example #8
0
 public function getUser()
 {
     return $this->hasOne(User::className(), ['id' => 'user_id']);
 }
Example #9
0
 public function login()
 {
     if (($user = User::findByUsername($this->username)) !== null) {
         return Yii::$app->user->login($user, 0);
     }
 }
Example #10
0
 private function findUser($id)
 {
     $user = User::findIdentity($id);
     if (empty($user)) {
         throw new NotFoundHttpException(Yii::t('user', 'Пользователь не найден'));
     } else {
         return $user;
     }
 }