2014-04-22 92 views
3

在我的項目中,我需要實現以下功能: - 當用戶決定刪除其帳戶時,在刪除之前,應該發送一封包含'$ deletionUrl'的電子郵件給該用戶爲了通過電子郵件確認決定。 我使用Yiimailer擴展,它工作正常。但是,我不確定我應該在哪些方面以及如何將這些條件刪除用戶。這是我的actionDelete:如何在Yii中刪除用戶時執行電子郵件確認

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

但不知道如何使它適應我的情況。還有其他方法可以做到這一點嗎?

+1

onbeforeDelete用於從模型中刪除記錄一些額外的紀錄。你爲什麼在這裏使用。你可以修改actionDelete。使用一些垃圾密鑰刪除用戶。檢查您之前發送的返回網址是否有密鑰。 – Babu

+0

如果用戶從數據庫中刪除。確認郵件觸發給用戶。這是你的目標我是否正確? – Babu

+0

用戶正在請求刪除,但在從數據庫刪除之前確認郵件被觸發給用戶,以防止誤用。 – BeeCoding

回答

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' => '[email protected]', '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 = '[email protected]';        
     $message->Password = 'yourpassword'; 
     $message->setFrom('[email protected]', 'yourname'); 
     $message->setTo($model->aEmail); 
     if ( $message->send()) 
    { 
     $this->render ('removeuser'); 
    } 
} 

我actionConfirm()在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'); 
     } 
} 
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 = '[email protected]'; 
$mail->Password = 'yourpassword'; 
$mail->SetFrom('[email protected]', '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(); 

} 



} 
+0

好的,讓我試試看。非常感謝你! – BeeCoding

+0

通過這種情況下的一個更多的信息不要努力刪除嘗試一些softdelete。你發送一些確認。有些用戶報告沒有被原始用戶調用。您無法檢索您刪除的記錄。最佳做法是軟刪除。將列狀態添加到用戶表。您只需更改標誌狀態。這對你未來的方面很有用 – Babu

+0

我會記住這一點。謝謝! – BeeCoding