パスワード変更周りの改修
現在、パスワードを変えると一旦ログアウトした方が良いので、パスワードのみ変えるアクションを別途実装。
確認用パスワードのバリデーションとセットはユーザ追加と全く同様で簡単でしたが、
パスワード変更後、ログアウト→再ログイン周りでかなりてこずりました・・・。
- パスワード変更画面→ログアウトしてログイン画面 から再ログインすると、パスワード変更画面が表示される
ログイン前画面へ戻る機能が働いているようなのですが、どの要素がいけないのか不明。
少なくとも、セッションのAuth.redirectには入っていません。
ログアウトアクションを実行した場合は、再ログインで前の画面に戻ったりしないのに何故・・・
「パスワードを変更しました。新しいパスワードでログインしてください」的なメッセージを出す誘導画面を間に置いて解決しましたが、理屈がわからないのが悔しい・・・。
そして、誘導画面でも1点トラブル。
- htmlヘルパーでrefreshメタタグを吐けない
refreshタグの書き方を書いたページがありましたが、これだと別のページに遷移できない。
CakePHP: Meta Refresh
htmlヘルパー本体も見ましたが、
content="10;URL=http://example.com/" のように、contentの中で秒数の設定と遷移先を両方指定するのがなかなかうまく行きません。。
細かいところで手間取りすぎても何なので、layout.ctpにタグを直書きしましたが、リダイレクト周りとあわせて、かなり悔しい (>_<)
システム判ってきたら、書き直したいです・・・・
Index: controllers/app_controller.php =================================================================== --- controllers/app_controller.php (revision 149) +++ controllers/app_controller.php (working copy) @@ -104,7 +104,17 @@ $this->redirect($pc_url); } + + // 基本のrefresh設定(refreshなし) + $this->set('refresh', array('time' => 0, 'url' => '')); } } Index: controllers/users_controller.php =================================================================== --- controllers/users_controller.php (revision 149) +++ controllers/users_controller.php (working copy) @@ -12,19 +12,27 @@ $this->MemberAuth->allow('admin_login'); $this->MemberAuth->allow('admin_add'); $this->MemberAuth->allow('admin_logout'); + $this->MemberAuth->allow('admin_to_login'); // ログイン後遷移先 $this->MemberAuth->loginRedirect = '/admin/users/index'; } - function admin_login() {} + function admin_login() { + } function admin_logout() { $this->Session->setFlash(__('Logout.', true)); $this->redirect($this->MemberAuth->logout()); } + function admin_to_login($refresh = 5) + { + $this->set('refresh', array('time' => $refresh, 'url' => Router::url('index', false))); + + } + function admin_index() { $this->User->recursive = 0; $this->set('users', $this->paginate()); @@ -52,8 +60,7 @@ } } - function admin_edit($id = null) { - $this->set('idLength', Configure::read('User.UserId.Length')); + function admin_change_password($id = null) { $this->set('passwordLength', Configure::read('User.Password.Length')); if (!$id && empty($this->data)) { @@ -61,11 +68,25 @@ $this->redirect(array('action'=>'index')); } if (!empty($this->data)) { - if ($this->User->save($this->data)) { - $this->Session->setFlash(__('The User has been saved', true)); - $this->redirect(array('action'=>'index')); - } else { - $this->Session->setFlash(__('The User could not be saved. Please, try again.', true)); + // バリデーション + $this->User->set($this->data); + if ($this->User->validates()) { + // passwordセット + $this->data[$this->MemberAuth->userModel]['password'] = $this->MemberAuth->password($this->data[$this->MemberAuth->userModel]['password1']); + + if ($this->User->save($this->data)) { + $this->Session->setFlash(__('The password has been changed. Please login at new password.', true)); + + if ($data[$this->MemberAuth->userModel]['id'] == $AuthUser[$this->MemberAuth->userModel]['id']) { + $this->MemberAuth->logout(); + $this->redirect('/admin/users/to_login'); + } else { + $this->redirect('/admin/users/index'); + } + + } else { + $this->Session->setFlash(__('The User could not be saved. Please, try again.', true)); + } } } if (empty($this->data)) { Index: views/layouts/default.ctp =================================================================== --- views/layouts/default.ctp (revision 149) +++ views/layouts/default.ctp (working copy) @@ -21,6 +21,11 @@ echo $html->css('cake.generic'); echo $scripts_for_layout; ?> + +<?php if ($refresh['time'] > 0): ?> +<meta http-equiv="Refresh" content="<?php echo $refresh['time']; ?>;url=<?php echo $refresh['url']; ?>"/> +<?php endif; ?> + </head> <body> <div id="container"> Index: views/users/admin_change_password.ctp =================================================================== --- views/users/admin_change_password.ctp (revision 0) +++ views/users/admin_change_password.ctp (revision 0) @@ -0,0 +1,29 @@ +<div class="users form"> +<?php echo $form->create('User', array('action'=>'change_password/'));?> + <fieldset> + <legend><?php __('Change Password');?></legend> + <?php + echo $form->input('id'); + echo $form->input('password1', array( + 'label' => __('New Password', true), + 'maxlength' => $passwordLength['max'], + 'after' => sprintf(__('Between %d to %d characters', true), $passwordLength['min'], $passwordLength['max']), + 'type' => 'password', + ) + ); + echo $form->input('password2', array( + 'label' => __('New Password [confirm]', true), + 'maxlength' => $passwordLength['max'], + 'type' => 'password', + ) + ); + ?> + </fieldset> +<?php echo $form->end('Submit');?> +</div> +<div class="actions"> + <ul> + <li><?php echo $html->link(__('Delete', true), array('action' => 'delete', $form->value('User.id')), null, sprintf(__('Are you sure you want to delete # %s?', true), $form->value('User.id'))); ?></li> + <li><?php echo $html->link(__('List Users', true), array('action' => 'index'));?></li> + </ul> +</div> Index: views/users/admin_to_login.ctp =================================================================== --- views/users/admin_to_login.ctp (revision 0) +++ views/users/admin_to_login.ctp (revision 0) @@ -0,0 +1,8 @@ +<h4><?php echo sprintf(__('If you can not automatically move in %d seconds, please click.', true), $refresh['time']);?></h4> + +<div class="actions"> + <ul> + <li><?php echo $html->link(__('Login', true), array('action' => 'login')); ?></li> + </ul> +</div>