/** * Signs user up. * * @return User|null the saved model or null if saving fails */ public function signup() { if (!$this->validate()) { return false; } $transaction = Yii::$app->db->beginTransaction(); try { $key = User::generateDynamicKey(); $user = new User(); $user->username = $this->username; $user->email = $this->email; $user->setPassword($this->password); $user->generateAuthKey(); $user->setDynamicKey($key, User::DYNAMIC_KEY_ACTIVATE_LIFE); $user->save(); $title = 'Activate Account on find.forfreedomandlove.com'; $content = "<br><br>" . 'Click the following link to activate the account you have registered on find.forfreedomandlove.com'; $content .= "<br><a href=\"https://find.forfreedomandlove.com/user/index/activate/{$user->id}/{$key}\" target=\"_blank\">"; $content .= "https://find.forfreedomandlove.com/user/index/activate/{$user->id}/{$key}</a>"; $content .= "<br>" . 'You can also copy the link and open it in the Address Field' . "<br><br><br>"; $mail = Yii::$app->mailer->compose()->setTo($this->email)->setSubject($title)->setHtmlBody($content)->send(); if ($mail) { $transaction->commit(); return true; } } catch (Exception $e) { $transaction->rollBack(); $this->addError('user', 'fail to create account'); } return false; }
/** * Logs in a user using the provided username and password. * * @return boolean whether the user is logged in successfully */ public function sendKey() { if (!$this->validate()) { return false; } $user = $this->getUser(); $transaction = Yii::$app->db->beginTransaction(); try { if ($user->dynamic_key && $user->validateDynamicKey($user->dynamic_key)) { throw new HurryDynamicKeyException('Current Dynamic key has not expired,please check you email'); } $key = User::generateDynamicKey(); $user->setDynamicKey($key, User::DYNAMIC_KEY_LOGIN_LIFE); $user->save(); if ($this->password) { $title = 'Dynamic Key to Login find.forfreedomandlove.com'; $content = "<br><br>Dynamic Key: " . $key; $content .= "<br><br>" . 'the dynamic key will expired after ' . User::DYNAMIC_KEY_LOGIN_LIFE . ' minutes'; $content .= "<br>" . 'If this dynamic key is not sent by you ,your password has disclosed,and you should change your password as soon as possible ' . "<br><br><br><br><br>"; } else { $title = 'Dynamic Key to Reset Password for the account on find.forfreedomandlove.com'; $content = "<br><br>" . $key; $content .= "<br>" . 'the dynamic key will expired after ' . User::DYNAMIC_KEY_LOGIN_LIFE . ' minutes'; $content .= "<br><br><br><br><br><br>"; } $mail = Yii::$app->mailer->compose()->setTo($user->email)->setSubject($title)->setHtmlBody($content)->send(); if ($mail) { $transaction->commit(); return true; } } catch (\yii\base\Exception $e) { $transaction->rollBack(); if ($e instanceof \yii\db\Exception) { $this->addError('dynamic_key', 'fail to save dynamic key to database'); } else { $this->addError('dynamic_key', $e->getMessage()); } } return false; }
/** **显示修改邮箱的表单(get);修改用户的认证邮箱(post),若修改成功,将账号变为未激活状态,并自动下线,用户需要到新的认证邮箱中点击激活链接对账号进行激活. * @return string whether or not the email has been changed successfully */ public function actionChangeEmail() { if (Yii::$app->request->isGet) { $csrf = Yii::$app->request->csrfToken; return $this->render('change-email', ['csrf' => $csrf]); } $new_email = Yii::$app->request->post('new_email'); $key = Yii::$app->request->post('dynamic_key'); if (!$new_email || !$key) { AjaxResponse::fail(null, 'Both new email and dynamic key are required'); } if (User::findOne(['email' => $new_email])) { AjaxResponse::fail(null, 'the email you provided has been used'); } $user = User::getCurrent(); if (!$user->validateDynamicKey($key)) { AjaxResponse::fail(null, 'Incorrect Dynamic Key is provided'); } $user->email = $new_email; $user->is_activated = false; $activate_key = User::generateDynamicKey(); $user->setDynamicKey($activate_key, User::DYNAMIC_KEY_ACTIVATE_LIFE); $user->save(); $title = 'Activate Account on find.forfreedomandlove.com'; $content = "<br><br>" . 'Click the Activate Link to activate the account you have registered on find.forfreedomandlove.com'; $content .= "<br><br>Activate Link: <a href=\"https://find.forfreedomandlove.com/user/index/activate/{$user->id}/{$activate_key}\" target=\"_blank\">"; $content .= "https://find.forfreedomandlove.com/user/index/activate/{$user->id}/{$activate_key}</a>"; $content .= "<br><br>" . 'You can also copy the link and open it in the Address Field' . "<br><br><br>"; Yii::$app->mailer->compose()->setTo($new_email)->setSubject($title)->setHtmlBody($content)->send(); Yii::$app->user->logout(); AjaxResponse::success(); }