public function validateActivation() { if (!$this->hasErrors()) { $activation = Activation::findOne(['email' => $this->email]); if ($activation) { $this->addError('email', '邮箱未激活'); } } }
/** * 激活 * @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'); }
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()); }