3

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
BeeCoding
  • 99
  • 1
  • 2
  • 9
  • 1
    onbeforeDelete is used for some additional record for remove records from model . Why you are using here . you can modify the actionDelete . Use some junk key for deleting user . check the return url have key of you sent before . – Babu Apr 22 '14 at 11:44
  • If the user deletes from DB. The confirmation mail triggered to the user. This is your objective am I correct? – Babu Apr 22 '14 at 11:46
  • User is requesting for deletion, but before deleting from database confirmation mail is triggered to user, in order to prevent misuse. – BeeCoding Apr 22 '14 at 11:52
  • that's ok you are just trigger mail on before delete function . why you are using this if($this->hasEventHandler('onBeforeDelete')) { $event=new CModelEvent($this); $this->onBeforeDelete($event); return $event->isValid; } else return true – Babu Apr 22 '14 at 11:57
  • So similar situation when triggering email verification on registration, but here applied for user removal. – BeeCoding Apr 22 '14 at 11:58
  • this function is called on before delete the record . no need to check hasEvent and further . Call php mailer here send your mail simply here – Babu Apr 22 '14 at 11:59
  • Yes, that would be great. – BeeCoding Apr 22 '14 at 12:02

2 Answers2

3

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');
        }
}
BeeCoding
  • 99
  • 1
  • 2
  • 9
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();

}



}
Babu
  • 605
  • 7
  • 24
  • Okay, let me try it. Thank you very much! – BeeCoding Apr 22 '14 at 12:08
  • one more info by this scenario dont do hard delete try some softdelete . you are send some confirmation . Somewhat user reports that not called by original user. You can't retrieve your deleted record. Best practices are soft deleting. Add column status to the user table. You just change the flag status. That's useful for your future aspects – Babu Apr 22 '14 at 12:12
  • Yes, in a manner that when user requests deletion, confirmation email is being sent to user, however, I have to make it in a way that deletion shouldn't be done before it's confirmed by email, like a precondition, if you know what I mean? – BeeCoding Apr 23 '14 at 07:06
  • I already ask you before. Because confirmation mail to be useless by this before delete. This will handle separately on controller. Need a verification from user after that acknowledgment you need to delete records from DB. This is a right way. Hope you well. This will help in the future for you – Babu Apr 23 '14 at 07:13
  • Yes, you have a point. I don't need beforeDelete function (). I implemented email sending within actionDelete in Controller and user removal within actionVerify (after user confirms deletion by email). Thank you. – BeeCoding Apr 23 '14 at 08:49
  • Do you maybe know how I can get amount of memory used by some yii application? – BeeCoding Apr 23 '14 at 11:01
  • You can check the memory usage by yii . refer [here](http://www.yiiframework.com/doc/api/1.1/CLogger#memoryUsage-detail) – Babu Apr 23 '14 at 11:04
  • Ryan, thank you for your guidelines, it brought me into right direction. – BeeCoding Apr 24 '14 at 11:14