public static function sendInviteEmail($user, $accountName, $link, $subject, $template = 'invitation') { $validation = new Validation(); $validation->userId = $user->_id; $validation->code = StringUtil::uuid(); $validation->expire = new \MongoDate(strtotime('+7 day')); if ($validation->save()) { $mail = Yii::$app->mail; $vars = ['name' => $accountName, 'email' => $user->email, 'host' => Yii::$app->request->hostInfo, 'link' => str_replace('code', $validation->code, $link)]; $mail->setView('//mail/' . $template, $vars, '//layouts/email'); $mail->sendMail($user->email, $subject); return true; } return false; }
/** * Activate * * <b>Request Type </b>: GET<br/> * <b>Request Endpoint </b>: http://{server-domain}/api/old-site/activate?code=abcd1234abcd1234<br/> * **/ public function actionActivate() { $code = $this->getQuery('code'); if (empty($code)) { $this->_activateFail(0); //此链接无效,请联系管理员 } $validation = Validation::findOne(['code' => $code]); if (empty($validation)) { $this->_activateFail(0); //此链接无效,请联系管理员 } if (empty($validation->expire) || MongodbUtil::isExpired($validation->expire)) { $this->_activateFail(1); //'此链接已过期,请联系管理员' } $userId = $validation->userId; if (User::updateAll(['isActivated' => User::ACTIVATED], ['_id' => $userId])) { $validation->delete(); $this->redirect('/old/activate?type=0&link=' . urlencode('/site/login')); Yii::$app->end(); } $this->_activateFail(1); //'此链接已过期,请联系管理员' }
/** * Reset password */ public function actionResetPassword() { $code = $this->getParams('code'); $newPassword = $this->getParams('password'); $result = Validation::validateCode($code); if ($result == Validation::LINK_INVALID) { throw new BadRequestHttpException(Yii::t('common', 'link_invalid')); } else { if ($result == Validation::LINK_EXPIRED) { throw new BadRequestHttpException(Yii::t('common', 'link_expired')); } } $userId = $result; $user = HelpDesk::findByPk($userId); if (empty($user)) { throw new BadRequestHttpException(Yii::t('commmon', 'incorrect_userid')); } // update the user password $user->password = HelpDesk::encryptPassword($newPassword, $user->salt); if (!$user->save()) { throw new ServerErrorHttpException("Save user failed!"); } return ['status' => 'ok']; }
/** * Validate code when activate user * @param $code, String. * @return String, error code or userId * * @author Sara Zhang */ public static function validateCode($code, $isDeleted = true) { if (empty($code)) { return self::LINK_INVALID; } $validation = Validation::findOne(['code' => $code]); if (empty($validation)) { return self::LINK_INVALID; } if (empty($validation->expire) || MongodbUtil::isExpired($validation->expire)) { return self::LINK_EXPIRED; } $userId = $validation->userId; if ($validation->toValidateAccount) { $user = User::findOne(['_id' => $userId]); $attributes = ['status' => Account::STATUS_ACTIVATED, 'trialStartAt' => new \MongoDate(), 'trialEndAt' => new \MongoDate(strtotime("+30 day"))]; Account::updateAll($attributes, ['_id' => $user->accountId]); } if ($isDeleted) { $validation->delete(); } return $userId; }