/** * @param bool|int $id * @return bool */ public function saveUser($id = false) { if ($this->validate()) { if ($id) { if (!($user = UserRecord::findOne($id))) { return false; } } else { $user = new UserRecord(); } $user->username = $this->username; $user->email = $this->email; if ($this->password) { $user->setPassword($this->password); $user->generateAuthKey(); } $user->status = $this->status; $user->save(false); // save role $auth = \Yii::$app->authManager; if ($id) { $roles = $auth->getRolesByUser($id); if (count($roles)) { $role = array_keys($roles)[0]; $auth->revoke($roles[$role], $id); } } $role = $auth->getRole($this->role); $auth->assign($role, $user->getId()); return true; } return false; }
/** * 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; }
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); }); }
/** * 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.')); }