Beispiel #1
0
 /**
  * Signs user up.
  *
  * @return UserRecord|null the saved model or null if saving fails
  */
 public function signup()
 {
     if ($this->validate()) {
         $user = new UserRecord();
         $user->username = $this->username;
         $user->email = $this->email;
         $user->setPassword($this->password);
         $user->generateAuthKey();
         if ($user->save()) {
             return $user;
         }
     }
     return null;
 }
Beispiel #2
0
 /**
  * User's signs up.
  *
  * @return User|null the saved model or null if saving fails
  */
 public function register()
 {
     if (!$this->validate()) {
         return false;
     }
     $user = new User();
     $user->email = $this->email;
     $user->generateAuthKey();
     $user->generatePasswordResetToken();
     if (User::removeTokenByEmail($user->email) && $user->save()) {
         static::getCurrentModule()->sendMessage(self::EVENT_USER_REGISTER, new UserEvent($user));
         return $user;
     }
 }
Beispiel #3
0
 /**
  * Finds user by [[email]]
  *
  * @return User|null
  */
 protected function getUser()
 {
     if ($this->user === null) {
         $this->user = User::findByEmail($this->email);
     }
     return $this->user;
 }
Beispiel #4
0
 /**
  * Finds user by [[username]]
  *
  * @return UserRecord|null
  */
 public function getUser()
 {
     if ($this->_user === false) {
         $this->_user = UserRecord::findByUsername($this->username);
     }
     return $this->_user;
 }
 /**
  * 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 = UserRecord::findByPasswordResetToken($token);
     if (!$this->_user) {
         throw new InvalidParamException('Wrong password reset token.');
     }
     parent::__construct($config);
 }
Beispiel #6
0
 public function resetPassword()
 {
     if (!$this->validate()) {
         return false;
     }
     $user = User::findByEmail($this->email);
     if (!$user) {
         throw new Exception('Email not found');
     }
     $user->generatePasswordResetToken();
     static::getCurrentModule()->sendMessage(self::EVENT_USER_PASSWORD_RESET, new UserEvent($user));
     return $user->save();
 }
 public function testSendEmailCorrectUser()
 {
     $model = new PasswordResetRequestForm();
     $model->email = $this->user[0]['email'];
     $user = UserRecord::findOne(['password_reset_token' => $this->user[0]['password_reset_token']]);
     expect('email sent', $model->sendEmail())->true();
     expect('user has valid token', $user->password_reset_token)->notNull();
     $this->specify('message has correct format', function () use($model) {
         expect('message file exists', file_exists($this->getMessageFile()))->true();
         $message = file_get_contents($this->getMessageFile());
         expect('message "from" is correct', $message)->contains(Yii::$app->params['supportEmail']);
         expect('message "to" is correct', $message)->contains($model->email);
     });
 }
 /**
  * Sends an email with a link, for resetting the password.
  *
  * @return boolean whether the email was send
  */
 public function sendEmail()
 {
     /* @var $user UserRecord */
     $user = UserRecord::findOne(['status' => UserRecord::STATUS_ACTIVE, 'email' => $this->email]);
     if ($user) {
         if (!UserRecord::isPasswordResetTokenValid($user->password_reset_token)) {
             $user->generatePasswordResetToken();
         }
         if ($user->save()) {
             return \Yii::$app->mailer->compose(['html' => 'passwordResetToken-html', 'text' => 'passwordResetToken-text'], ['user' => $user])->setFrom([\Yii::$app->params['supportEmail'] => \Yii::$app->name . ' robot'])->setTo($this->email)->setSubject('Password reset for ' . \Yii::$app->name)->send();
         }
     }
     return false;
 }
Beispiel #9
0
 /**
  * @param array $token
  * @param array $config
  */
 public function __construct($token, $config = [])
 {
     if (empty($config['scenario'])) {
         $config['scenario'] = self::SCENARIO_DEFAULT;
     }
     if (empty($token) || !is_string($token)) {
         throw new InvalidParamException('Password reset token cannot be blank.');
     }
     parent::__construct($config);
     $this->user = User::findByPasswordResetToken($token, $this->isNewUserScenario() ? User::STATUS_NEW : User::STATUS_ACTIVE);
     if (!$this->user) {
         throw new InvalidParamException('Wrong password reset token.');
     }
     $this->token = $token;
 }
Beispiel #10
0
 /**
  * @inheritdoc
  */
 public function __construct($id = null, $config = null)
 {
     parent::__construct($config);
     if ($id) {
         // one user
         if ($user = UserRecord::findById($id)) {
             $this->user = $user;
         } else {
             throw new UserNotFoundException();
         }
     } else {
         // all users
         $this->users = UserRecord::find();
     }
 }
Beispiel #11
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getUpdatedBy()
 {
     return $this->hasOne(UserRecord::className(), ['id' => 'updated_by']);
 }
Beispiel #12
0
 /**
  * This method is called after each cest class test method, even if test failed.
  * @param \Codeception\Event\TestEvent $event
  */
 public function _after($event)
 {
     UserRecord::deleteAll(['email' => '*****@*****.**', 'username' => 'tester']);
 }
Beispiel #13
0
 /**
  * Changes status of user to deleted
  * @param $id
  * @return int
  */
 public function deleteUser($id)
 {
     $rows = UserRecord::updateAll(['status' => UserRecord::STATUS_DELETED], ['id' => $id]);
     return $rows;
 }
Beispiel #14
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 integer $id
  * @return UserRecord the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = UserRecord::findOne($id)) !== null) {
         return $model;
     }
     throw new NotFoundHttpException(Yii::t('back', 'The requested page does not exist.'));
 }