在我的项目中,我需要实现以下功能:
- 当用户决定删除其帐户时,在删除之前,应向该用户发送一封带有“$deletionUrl”的电子邮件,以便通过电子邮件确认该决定。
我正在使用 Yiimailer 扩展程序,它工作正常。但是,我不确定应该在何处以及如何放置有关删除用户的这些条件。这是我的操作删除:

public function actionDelete($id)
{
    $this->loadModel($id)->delete();
    if (!isset($_GET['ajax'])) {
        $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
    }
}

我在网上查了一下,发现CActiveRecord有一个保护方法beforeDelete()
protected function beforeDelete()
{
    if($this->hasEventHandler('onBeforeDelete'))
    {
        $event=new CModelEvent($this);
        $this->onBeforeDelete($event);
        return $event->isValid;
    }
    else
        return true;
}

http://www.yiiframework.com/doc/api/1.1/CActiveRecord#beforeDelete-detail

但不确定如何适应我的情况。还有其他方法可以做到这一点吗?

最佳答案

我设法通过以下方式解决了这个问题。我在 UserController 中的 actionDelete 是:

    public function actionDelete($id) {
    $model = $this->loadModel($id);
    $deletionUrl= Yii::app()->createAbsoluteUrl('user/confirm',array('aHash'=>$model->aHash));


       $message = new YiiMailer();
       $message->setView('contact');
       $message->setBody($deletionUrl);
       $message->setData(array('message' => '

        You have received this email because you requested a deletion of your account.
        If you did not make this request, please disregard this
        email. You do not need to unsubscribe or take any further action.
        </br>
        <hr>

        We require that you confirm  your request to ensure that
        the request made  was correct. This protects against
        unwanted spam and malicious abuse.

        To confirm deletion of your account, simply click on the following link:
        '.$deletionUrl.' <br> <br>
        (Some email client users may need to copy and paste the link into your web
        browser).','name' => 'yourname@123.com', 'description' => 'Please   click on the link below in order to confirm your request:'));
       $message->setLayout('mail');
       $message->IsSMTP();
       $message->setSubject ('Request for account deletion');
       $message->Host = 'smtp.123.com';
       $message->SMTPAuth = true;
       $message->Username = 'yourname@123.com';
       $message->Password = 'yourpassword';
       $message->setFrom('yourname@123.com', 'yourname');
       $message->setTo($model->aEmail);
       if (  $message->send())
     {
        $this->render ('removeuser');
     }
}

我在 UserController 中的 actionConfirm():
    public function actionConfirm ()
{
   $model = User::model()->findByAttributes(array('aHash' => $_GET['aHash']));
    if ($model === null)
        throw new CHttpException(404, 'Not found');
    else
        {
        $this->loadModel($model->aUserID)->delete();
        $model->save();
        $this->render('afterdelete');
        }
}

关于yii - 在 Yii 中删除用户时如何实现电子邮件确认,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23218356/

10-14 14:42
查看更多