示例#1
0
文件: User.php 项目: rzzdy/yii2-user
 /**
  * This method attempts changing user email. If user's "unconfirmed_email" field is empty is returns false, else if
  * somebody already has email that equals user's "unconfirmed_email" it returns false, otherwise returns true and
  * updates user's password.
  *
  * @param string $code
  *
  * @return bool
  * @throws \Exception
  */
 public function attemptEmailChange($code)
 {
     // TODO refactor method
     /** @var Token $token */
     $token = $this->finder->findToken(['user_id' => $this->id, 'code' => $code])->andWhere(['in', 'type', [Token::TYPE_CONFIRM_NEW_EMAIL, Token::TYPE_CONFIRM_OLD_EMAIL]])->one();
     if (empty($this->unconfirmed_email) || $token === null || $token->isExpired) {
         Yii::$app->session->setFlash('danger', Yii::t('user', 'Your confirmation token is invalid or expired'));
     } else {
         $token->delete();
         if (empty($this->unconfirmed_email)) {
             Yii::$app->session->setFlash('danger', Yii::t('user', 'An error occurred processing your request'));
         } elseif ($this->finder->findUser(['email' => $this->unconfirmed_email])->exists() == false) {
             if ($this->module->emailChangeStrategy == Module::STRATEGY_SECURE) {
                 switch ($token->type) {
                     case Token::TYPE_CONFIRM_NEW_EMAIL:
                         $this->flags |= self::NEW_EMAIL_CONFIRMED;
                         Yii::$app->session->setFlash('success', Yii::t('user', 'Awesome, almost there. Now you need to click the confirmation link sent to your old email address'));
                         break;
                     case Token::TYPE_CONFIRM_OLD_EMAIL:
                         $this->flags |= self::OLD_EMAIL_CONFIRMED;
                         Yii::$app->session->setFlash('success', Yii::t('user', 'Awesome, almost there. Now you need to click the confirmation link sent to your new email address'));
                         break;
                 }
             }
             if ($this->module->emailChangeStrategy == Module::STRATEGY_DEFAULT || $this->flags & self::NEW_EMAIL_CONFIRMED && $this->flags & self::OLD_EMAIL_CONFIRMED) {
                 $this->email = $this->unconfirmed_email;
                 $this->unconfirmed_email = null;
                 Yii::$app->session->setFlash('success', Yii::t('user', 'Your email address has been changed'));
             }
             $this->save(false);
         }
     }
 }
示例#2
0
 /** @inheritdoc */
 public function beforeValidate()
 {
     if (parent::beforeValidate()) {
         if (!empty($this->Login)) {
             $this->user = $this->finder->findUser(['Login' => $this->Login])->one();
             /**
              * Generate password
              */
             $hash = Yii::$app->security->generatePasswordHash($this->Password);
             ////$this->Password = $this->Password . ':' . $hash;
             ////list($password, $hash) = explode(':', $this->Password);
             //                if ($this->user !== null && Yii::$app->getSecurity()->validatePassword($this->Password, $hash) ) {
             //                    $this->user->updateAttributes(['Password' => $hash]);
             //                    echo $this->Password . ':' . $hash. ' OK  ';
             //                }
             //                exit;
         }
         if ($this->user === null) {
             if (CardRecord::check($this->Login)) {
                 $card = CardRecord::findCard($this->Login);
                 if ($card !== null && $card->person) {
                     //                    $this->user = $card->person->ServiceCard ? $card->person : null;
                     $this->user = $card->person;
                     return true;
                 }
             }
             $this->addError('Login', \Yii::t('user', 'Invalid login or password'));
             return false;
         } else {
             return true;
         }
     } else {
         return false;
     }
 }