密码散列了两次


Password hashed twice?

我尝试了CakePHP网站上的教程Simple Acl控制的应用程序,但我在哈希密码方面遇到了问题。

我的桌子是:

CREATE TABLE users (
id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(255) NOT NULL UNIQUE,
pwd CHAR(40) NOT NULL,
group_id INT(11) NOT NULL

);

CREATE TABLE groups (
id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL

);

在User.php模型中,我有:

public function beforeSave($options = array()) {
  $this->data['User']['pwd'] = AuthComponent::password($this->data['User']['pwd']);
  return true;
}

如果我点击网站上的编辑用户,然后提交,pwd会再次被散列。我该怎么解决?

我尝试从这个论坛:

public function beforeSave($options = array()) {
  if(!empty($this->data['User']['pwd'])) {
    $this->data['User']['pwd'] = AuthComponent::password($this->data['User']['pwd']);
  } else {
      unset($this->data['User']['pwd']);
    }
  return true;

}

但它不起作用。

我的编辑.ctp:

<div class="users form">
<?php echo $this->Form->create('User'); ?>
<fieldset>
    <legend><?php echo __('Edit User'); ?></legend>
<?php
    echo $this->Form->input('id');
    echo $this->Form->input('username');
    echo $this->Form->input('pwd');
    echo $this->Form->input('group_id');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
<div class="actions">
<h3><?php echo __('Actions'); ?></h3>
<ul>
    <li><?php echo $this->Form->postLink(__('Delete'), array('action' => 'delete', $this->Form->value('User.id')), null, __('Are you sure you want to delete # %s?', $this->Form->value('User.id'))); ?></li>
    <li><?php echo $this->Html->link(__('List Users'), array('action' => 'index')); ?></li>
    <li><?php echo $this->Html->link(__('List Groups'), array('controller' => 'groups', 'action' => 'index')); ?> </li>
    <li><?php echo $this->Html->link(__('New Group'), array('controller' => 'groups', 'action' => 'add')); ?> </li>
</ul>

并添加.ctp:

<div class="users form">
<?php echo $this->Form->create('User'); ?>
<fieldset>
    <legend><?php echo __('Add User'); ?></legend>
<?php
    echo $this->Form->input('username');
    echo $this->Form->input('pwd');
    echo $this->Form->input('group_id');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
<div class="actions">
<h3><?php echo __('Actions'); ?></h3>
<ul>
    <li><?php echo $this->Html->link(__('List Users'), array('action' => 'index')); ?></li>
    <li><?php echo $this->Html->link(__('List Groups'), array('controller' => 'groups', 'action' => 'index')); ?> </li>
    <li><?php echo $this->Html->link(__('New Group'), array('controller' => 'groups', 'action' => 'add')); ?> </li>
</ul>

保存之前从中删除AuthComponent密码。您可以在保存用户时使用它。

如果你把它放在beforeSave方法中,它会在你每次添加、编辑、更新和删除时触发。

感谢您的帮助。最终通过这个解决:

https://groups.google.com/forum/#!消息/蛋糕php/sJLi4lCqgog/2X9VpzqWs-0J

比较散列:

您可以将保存在数据库中的哈希与密码字段中的文本进行比较。如果您没有触摸密码(更改了密码字段),散列应该匹配。假设您保存的哈希是xyz,而加载在密码哈希中的哈希仍然未受影响,即xyz;在这种情况下,您不必重新散列任何内容,散列应该保持不变。

在另一种情况下,假设您保存的哈希是xyz,但您将密码html字段编辑为abc;在这种情况下,您将不得不重新刷新新密码(abc),然后替换数据库记录中的旧密码。

所有这些都可以翻译成如下代码:

public function beforeSave($options = array()) {
    if (isset($this->data[$this->alias]['password'])) {
        if(isset($this->data[$this->alias]['id'])) {
            $id = $this->data[$this->alias]['id'];
            $user = $this->findById($id);
        } else {
            $id = false;
        }
        if(!$id || $this->data[$this->alias]['password'] != $user['User']['password']) {
            $passwordHasher = new SimplePasswordHasher();
            $this->data[$this->alias]['password'] = $passwordHasher->hash(
                $this->data[$this->alias]['password']
            );
        }
    }
    return true;
}

谨致问候,M