(前回の続き)
パスワードリセットのリンクにアクセス → パスワードリセットしてメール通知
まずビューを実装。
1 2 3 4 5 6 7 8 9 10 11 |
<?php /*app/View/Users/verify.ctp*/ ?> <h2>Recover Password</h2> <?php if (isset($success)): ?> <div class="message">Access verified. Your new password has been emailed to you.</div> <p>A new password has been generated for your account and mailed to you. After you've logged in, you should change your password to something memorable via the account information page.</p> <?php else: ?> <div class="warning">Invalid token. This page has expired, or the link was not copied from your email client correctly.</div> <p>Make sure you have copied the entire link correctly, pasting it together if the link was split over two lines. If you're copying the link correctly and still can't get access, please contact us.</p> <?php endif; ?> |
そしてアクションを実装。
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 |
// app/Controller/UsersController.php ... class UsersController extends AppController { .... // Accepts a valid token and resets the users password. public function verify($token_str = null) { if ($this->Auth->user()) { $this->redirect(array('controller' => 'users', 'action' => 'password')); } $Token = ClassRegistry::init('Token'); $res = $Token->get($token_str); if ($res) { // Update the users password. $password = $this->User->generatePassword(); $this->User->id = $res['User']['id']; //$this->User->saveField('password', $this->Auth->password($password)); $this->User->saveField('password', $password); $this->set('success', true); $email = new CakeEmail(); $email->template('verify', 'default'); $email->viewVars(array('user' => $res, 'password' => $password)); $email->from(array('sender@domain.com' => 'Sender')); $email->to($res['User']['email']); $email->subject('Password changed'); $email->send(); } } .... } |
メールのテンプレートはこんな感じ。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php /* app/View/Emails/text/verify.ctp*/ ?> Your password has been reset, please use the following details to log into our site. Username: <?php echo $user['User']['username']; ?> Password: <?php echo $password; ?> Please change your password to something more memorable. You can log in to change your password at this address: <?php echo Router::url(array('controller' => 'users', 'action' => 'login'), true); ?> Thanks, Support |
これにて完成。