0

ログインしたユーザーがアカウント設定(メールアドレスとパスワード)を変更するためのフォームを作成しています。これを行うには、現在のパスワードを確認できる必要があります。これは私が私のフォームを構築する方法ですsettings.ctp

<div id="content-complex-image">
    <div id="sidebar">
        <ul>
            <li><a href="/account/images">Your images</a></li>
            <li><a href="/account/settings">Account settings</a></li>
        </ul>
    </div>
    <div id="content-inner">
        <p>Modify your account settings</p>
        <?php echo $this->Session->flash(); ?>
        <?php
            echo $this->Form->create('User');
            echo $this->Form->input('currentPassword', array('type' => 'password'));
            echo $this->Form->input('username', array('disabled' => 'disabled', 'value' => $username));
            echo $this->Form->input('email');
            echo $this->Form->input('password', array('type' => 'password'));
            echo $this->Form->end('Update');
        ?>
    </div>
    <div class="clear"></div>
</div>

これが私のコントローラーのアクションです:

public function settings() {
    $this->set('title_for_layout', 'Account Settings');

    $this->User->id = $this->Auth->user('id');
    if ($this->request->is('post')) {
        if($this->Auth->user('password') == $this->request->data['User']['currentPassword']) {
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash('Your details have been saved');
                $this->redirect(array('action' => 'settings'));
            } else {
                $this->Session->setFlash('Your details could not be updated. Try again.');
            }
        } else {
             $this->Session->setFlash('Invalid password. Try again.');
        }            
    }
}

ただし、if()for a password checkは常にfalseと評価され、常に「invalidpassword」メッセージが表示されます。パスワードを正しくチェックしていないと思いますが、正しい方法がわかりません。

また、ユーザーがユーザー名を変更できないようにしたいと思います。フォームフィールドを無効に設定したことは知っていますが、ユーザーが設定アクションに投稿リクエストを送信すると、ユーザー名が変更される可能性があります。ユーザー名が更新されないようにするにはどうすればよい$this->User->save()ですか?

編集:2つの問題のように見えます。1つは比較前にパスワードをハッシュしていなかった、2つ$this->Auth->user('password')は実際にはNULLです。これは別の問題を提起します、それを比較するためにデータベースからユーザーにハッシュされたパスワードを取得するにはどうすればよいですか?

4

2 に答える 2

0

パスワードがプレーンテキストとしてデータベースに保存されていない場合、フォームから渡されたデータをエンコード/暗号化していないため、パスワードチェックは失敗します。これを確認する簡単な方法(テストマシンでのみお願いします)は、次のように置き換えることです$this->Session->setFlash('Invalid password. Try again.');

$this->Session->setFlash('The password should be ' . $this->Auth->user('password') . 'but it is actually ' . $this->request->data['User']['currentPassword']);

または同様のもの。

それが役に立てば幸い

編集:

パスワードがsha1を使用して保存されている場合は、次のように確認できます

if($this->Auth->user('password') == sha1($this->request->data['User']['currentPassword']))
{
     //code for successful password here
}
于 2012-07-25T22:30:36.457 に答える
0

Authコンポーネントがデータにパスワードを保持していないことについては正しいです。このコードを試してみてください:

public function settings() {
    $this->set('title_for_layout', 'Account Settings');

    if ($this->request->is('post')) {
        $user = $this->User->findById($this->Auth->user('id'));
        if($user['User']['password'] == AuthComponent::password($this->request->data['User']['currentPassword'])) {
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash('Your details have been saved');
                $this->redirect(array('action' => 'settings'));
            } else {
                $this->Session->setFlash('Your details could not be updated. Try again.');
            }
        } else {
             $this->Session->setFlash('Invalid password. Try again.');
        }            
    }
}

そして、ユーザー名の変更を阻止するための私の提案は、その入力を完全に破棄することですか?入力ボックスを無効にするのではなく、単にユーザー名をエコーすることができます。

于 2012-07-25T22:59:17.590 に答える