コード例 #1
0
ファイル: SignInForm.php プロジェクト: NikDevPHP/yii2-blog
 public function validateActivation()
 {
     if (!$this->hasErrors()) {
         $activation = Activation::findOne(['email' => $this->email]);
         if ($activation) {
             $this->addError('email', '邮箱未激活');
         }
     }
 }
コード例 #2
0
ファイル: SignUpForm.php プロジェクト: NikDevPHP/yii2-blog
 public function register()
 {
     if ($this->validate()) {
         $user = new User();
         $user->email = $this->email;
         $user->salt = Str::random(10);
         $user->password = $user->generatePassword($this->password);
         if ($user->save(false)) {
             // 添加成功
             // 生成激活码
             $activation = new Activation();
             $activation->email = $user->email;
             $activation->token = Str::random(40);
             $activation->created_at = new Carbon();
             $activation->save(false);
             Yii::$app->mail->compose('activation', ['activationCode' => $activation->token])->setTo($user->email)->setSubject(Yii::$app->id . ' 账号激活邮件')->send();
             return true;
         }
     }
     $this->addError('email', '注册失败');
     return false;
 }
コード例 #3
0
 /**
  * 激活
  * @param $activationCode
  * @return string
  * @throws \yii\web\HttpException
  */
 public function actionActivate($activationCode)
 {
     // 数据库验证令牌
     $activation = Activation::findOne(['token' => $activationCode]);
     if (is_null($activation)) {
         throw new NotFoundHttpException('请求页面不存在');
     }
     // 激活对应用户
     $user = User::findByEmail($activation->email, true);
     $user->activated_at = new Carbon();
     $user->save();
     $activation->delete();
     // 删除令牌
     return $this->render('activation-success');
 }
コード例 #4
0
 public function testActivation()
 {
     $activationCode = $this->activation['one']['token'];
     // 数据库验证令牌
     $activation = Activation::findOne(['token' => $activationCode]);
     $this->assertNotEmpty($activation);
     // 激活对应用户
     $sql = "UPDATE {{%user}} SET activated_at=:activated_at WHERE email=:email";
     $command = Yii::$app->db->createCommand($sql);
     $command->bindValue(':activated_at', new Carbon());
     $command->bindValue(':email', $activation->email);
     $ret = $command->execute();
     $this->assertNotEquals(0, $ret);
     $this->assertNotEquals(0, $activation->delete());
 }