2016-05-03 17 views
0

cakephp 2.6を使用しています。アクティベーションリンクをユーザーがクリックすると、その後、私のデータベースのステータスフィールドは、私の電子メールが正しくなるだろうが、私は方法を知っているドン1.登録後のメールのアカウントアクティベーションリンクcakephp

で更新する場合、私は、登録が成功し、 後にメールでアカウントのアクティベーションリンクを送信する必要がありますlastinsertidを電子メールの一意の識別子として取得し、クリック後にアカウントを更新することにより、電子メール内のリンクを送信する。

私はこれを手伝ってください。以下

function add() {  
     $this->layout = "layout_login";  
      $this->pageTitle = "Add Admin"; 
      $this->set("pageTitle", "Add Admin");  
     if (!empty($this->request->data)) {   
      $this->User->set($this->data['User']); 
      $isValidated = $this->User->validates(); 
      if ($isValidated) { 
       // Generating UUID 
       $this->data['User']['activation_key'] = $activation_key = String::uuid(); 
       $this->User->save($this->data, array('validate' => false)); 
       $this->Session->setFlash(__('Please check your email for account verification.')); 
        $subject = "Account Activation link By Kaya Dispatch";     

        $to = trim($this->request->data['User']['email']); 
        $this->Email->sendAs = 'html'; 
        $this->Email->from = '[email protected]'; 
        $this->Email->to = $to; 
        $this->Email->subject = $subject; 
        $activationUrl = Router::url(['controller' => 'users', 'action' => 'activate/' . $activation_key ]); 
        $message = "Dear <span style='color:#666666'>" . $name . "</span>,<br/><br/>"; 
        $message .= "Your account has been created successfully by Administrator.<br/>"; 
        $message .= "Please find the below details of your account: <br/><br/>"; 
        $message .= "<b>Activate your account by clicking on the below url:</b> <br/>"; 
        $message .= "<a href='$activationUrl'>$activationUrl</a><br/><br/>"; 
        $message .= "<br/>Thanks, <br/>Support Team"; 
        $this->Email->send($message); 

        }    
       } 
      } 

アカウントのアクティベーションコードは、まあ、これは論理的なものである

function activate($activation_key) { 
     $userData = $this->User->find('first', array(
     'conditions' => array(
       'User.activation_key' => $activation_key, 
       'User.status' => 0 
     ) 
    )); 
    if(!empty($userData)){ 
      if ($userData['User']['status'] == 0) 
      { 
       $activeStatus = 1; 
      } 
    $status = $this->User->updateAll(array('User.status' => $activeStatus), array('User.id' => $id)); 
    if ($status) 
      { $this->Session->setFlash(__('Status updated successfully')); 

      } else 
      { $this->Session->setFlash(__('Something went wrong, please try again.')); 

      } 
      $this->redirect(array('controller' => 'Users', 'action' => 'index')); 

    } 
} 

答えて

1

ですが、私はこれは完全にあなたがそれを行うたいどのように依存であることを意味します。私は自分の方法を書いています:)

usersテーブルに新しい列を作成して、activation_keyに設定してからテーブルに挿入してください。このように:

// Finding user data from users table on behalf of activation key 
// and Status should be 0 (deactivate). So that, a URL can be use only ONCE. 
$userData = $this->User->find('first', array(
     'conditions' => array(
       'User.activation_key' => $activation_key, 
       'User.status' => 0 
     ) 
    )); 

if(!empty($userData)){ 
    // $userData as you have User's data update the status as 1 and set activation_key as empty ""; 
}else{ 
    // So, you don't find any user, it is an invalid request. 
} 

いつか、CakePHPはあなたがすることはできませんということが起こる:あなたのfunction activate($activation_key='')

if ($isValidated) { 

    $password = $this->data['User']['password']; 
    $this->data['User']['password'] = md5($this->data['User']['password']); 

    // Generating UUID 
    $this->data['User']['activation_key'] = $activation_key = String::uuid(); 


    $this->User->save($this->data, array('validate' => false));    
    $this->Session->setFlash("<div class='success-message flash notice'>Admin has been created successfully.</div>"); 
    $subject = "Account Activation link send on your email"; 
    $name = $this->request->data['User']['fname'] . " " . $this->request->data['User']['lname']; 
    $to = trim($this->request->data['User']['email']); 
    $this->Email->sendAs = 'html'; 
    $this->Email->from = '[email protected]'; 
    $this->Email->to = $to; 
    $this->Email->subject = $subject; 

    $activationUrl = Router::url(['controller' => 'users', 'action' => 'activate/' . $activation_key ]); 

    // Always try to write clean code, so that you can read it :) : 

    $message = "Dear <span style='color:#666666'>" . $name . "</span>,<br/><br/>"; 
    $message .= "Your account has been created successfully by Administrator.<br/>"; 
    $message .= "Please find the below details of your account: <br/><br/>"; 
    $message .= "<b>First Name:</b> " . $this->data['User']['fname'] . "<br/>"; 
    $message .= "<b>Last Name:</b> " . $this->data['User']['lname'] . ((!empty($this->data['User']['phone'])) ? "<br/>"; 
    $message .= "<b>Phone:</b> " . $this->data['User']['phone'] : "") . "<br/>"; 
    $message .= "<b>Address:</b> " . $this->data['User']['address1'] . " " . $this->data['User']['address2'] . "<br/>"; 
    $message .= "<b>Email:</b> " . $this->data['User']['email'] . "<br/>"; 
    $message .= "<b>Username:</b> " . $this->data['User']['username'] . "<br/>"; 
    $message .= "<b>Password:</b> " . $password . "<br/>"; 

    $message .= "<b>Activate your account by clicking on the below url:</b> <br/>"; 
    $message .= "<a href='$activationUrl'>$activationUrl</a><br/><br/>"; 
    $message .= "<br/>Thanks, <br/>Support Team"; 
    $this->Email->send($message); 

} 

そして、起動パラメータとしてキー、およびこのようなユーザーから見つけるのことを受け取ります$this->dataこの例では$this->data['User']['password']$this->data['User']['activation_key']の値を変更しようとしていますので、単に$postData = $this->dataを保存してから、$postDataを挿入してさらに操作することができます。

更新:String::uuid()があなたのために正常に動作している場合、それは仕事をdosn't場合は、この目的のために、以下の行を使用しよう/デバッグをご確認ください。:

// Generating activation_key 
$this->data['User']['activation_key'] = $activation_key = time(); 

ありがとうございました!

+0

テーブルに列activation_keyを追加しましたが、テーブル内のactivation_key列にデータが挿入されていませんでした。このリンクをクリックすると、ステータス列はテーブル –

+0

で更新されません。 。私の質問で.. –

+0

私はあなたのために私の答えを更新しました! '' User.status '=>を使って既にユーザデータを見つけているので、 '' if($ userData [' User '' ['status'] == 0) 'をアクティブにチェックインする必要はありません。 0 '状態になる。 – hmd

関連する問題