(前回の続き)
メールアドレス入力 → パスワードリセットのリンクをメール送信
まずビューを実装。
1 2 3 4 5 6 7 8 |
<?php /*app/View/Users/recover.ctp*/ ?> <h2>Recover Password</h2> <?php echo $this->Form->create('User', array('action' => 'recover')); echo $this->Form->input('email'); echo $this->Form->end('Recover'); ?> |
そしてアクションを実装。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
// app/Controller/UsersController.php ... class UsersController extends AppController { ... // Allows the user to email themselves a password redemption token. public function recover() { if ($this->Auth->user()) { $this->redirect(array('action' => 'password')); } if (!empty($this->data['User']['email'])) { $user = $this->User->find('first', array( 'recursive' => -1, 'conditions' => array('User.email' => $this->data['User']['email']), )); if ($user === false || empty($user)) { $this->Session->setFlash('No matching user found.'); return false; } $Token = ClassRegistry::init('Token'); $token = $Token->generate(array('User' => $user['User'])); $this->Session->setFlash('An email has been sent to your account, please follow the instruction in this email.'); $email = new CakeEmail(); $email->template('recover', 'default'); $email->viewVars(array('user' => $user, 'token' => $token)); $email->from(array('sender@domain.com' => 'Sender')); $email->to($this->data['User']['email']); $email->subject('Password recovery'); $email->send(); } } .... } |
メールのテンプレートはこんな感じ。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php /* app/View/Emails/text/recover.ctp*/ ?> Someone is attempting to reset your password. Your username for this account is: <?php echo $user['User']['username']; ?> If you wish to continue, you may reset your password by following this link: <?php echo Router::url(array('controller' => 'users', 'action' => 'verify', $token), true); ?> If you did not initiate this action, please contact support. You can log in to change your password at this address: <?php echo Router::url(array('controller' => 'users', 'action' => 'login'), true); ?> Thanks, Support |
(次回へ続く)