3

在我的项目中,我需要实现以下功能: - 当用户决定删除其帐户时,在删除之前,应向该用户发送一封带有“$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

但不知道如何适应我的情况。还有其他方法吗?

4

2 回答 2

3

我设法通过以下方式解决了这个问题。我在 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');
        }
}
于 2014-04-24T10:56:17.427 回答
0
protected function beforeDelete()
{

Yii::import('application.extensions.phpmailer.JPhpMailer');
$mail = new JPhpMailer;
$mail->IsSMTP();
$mail->Host = 'smpt.163.com';
$mail->SMTPAuth = true;
$mail->Username = 'yourname@163.com';
$mail->Password = 'yourpassword';
$mail->SetFrom('yourname@163.com', 'yourname');
$mail->Subject = 'PHPMailer Test Subject via smtp, basic with authentication';
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';
$mail->MsgHTML('<h1>JUST A TEST!</h1>');
$mail->AddAddress($this->email, $this->username);
if($mail->Send())
{

       return parent::beforeDelete();

}



}
于 2014-04-22T12:06:00.670 回答