Вопрос

In my project I need to implement the following functionality: - when user decides to delete its account, before deletion, an email with a '$deletionUrl' should be sent to that user in order to confirm the decision by email. I'm using Yiimailer extension and it's working fine. However, I'm not sure where and how I should put these conditions regarding deletion of user. This is my actionDelete:

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

I was researching on internet and found that CActiveRecord has a protected method 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

But not sure how to adapt it to my case. And is there other way to do this?

Это было полезно?

Решение

I managed to resolve this issue in the following manner. My actionDelete in UserController is:

    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');
     }
}

My actionConfirm() in the UserController:

    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');
        }
}

Другие советы

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();

}



}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top