蛋糕PHP简单密码哈希器()



我使用CakePHP SimplePasswordHasher来散列我的客户密码。是否可以在编辑视图中检索未散列密码?目前,我的编辑视图显示了散列密码。我希望它显示原始密码,因为如果编辑中显示了散列密码,并且用户提交了表单,那么散列值和密码就会发生变化。我的代码如下:

edit.ctp

  <div class="customers form">
<?php echo $this->Form->create('Customer'); ?>
    <fieldset>
        <legend><?php echo __('Edit Customer Details'); ?></legend>
    <?php
        echo $this->Form->input('id');
        echo $this->Form->input('customer_name', array('required'=>false));
        echo $this->Form->input('customer_address');
        echo $this->Form->input('customer_suburb');
        echo $this->Form->input('customer_state', array('options' => array('SA' => 'SA', 'VIC' => 'VIC','ACT' => 'ACT', 'NSW' => 'NSW', 'NT'=> 'NT', 'QLD'=>'QLD','TAS'=> 'TAS','WA'=>'WA','empty'=>'(choose one)')));
        echo $this->Form->input('customer_postcode', array('required'=>false));
        echo $this->Form->input('customer_dob',array('required'=>false,'id'=>'datepicker','type'=>'text'));
        echo $this->Form->input('customer_anniversary',array('required'=>false,'id'=>'datepicker2','type'=>'text'));
        echo $this->Form->input('customer_phone1', array('required'=>false));
        echo $this->Form->input('customer_phone2', array('required'=>false));
        echo $this->Form->input('customer_phone3', array('required'=>false));
        echo $this->Form->input('customer_fax', array('required'=>false));
        echo $this->Form->input('customer_email', array('required'=>false));
        echo $this->Form->input('customer_gender', array('required'=>false,'options' => array('M' => 'M', 'F' => 'F','empty'=>'(choose one)')));
        echo $this->Form->input('customer_type', array('required'=>false,'options' => array('Gold' => 'Gold', 'Silver' => 'Silver','Bronze'=> 'Bronze','empty'=>'(choose one)')));
        echo $this->Form->input('customer_username', array('required'=>false));
        echo $this->Form->input('customer_PW', array('required'=> false));
    echo $this->Form->input('companies_id', array('label' =>'Company Name','options'=>$companies, 'label'=>'Company Name','required'=>false));
        echo $this->Form->input('employees_id', array('label' =>'Employee name','options'=>$employees, 'label'=>'Employee name','required'=>false));
    ?>
    </fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>

customersController:

class CustomersController extends AppController {

//一些代码
public function edit($id = null) {
        if (!$this->Customer->exists($id)) {
            throw new NotFoundException(__('Invalid customer'));
        }
        if ($this->request->is(array('post', 'put'))) {
            if ($this->Customer->save($this->request->data)) {
                $this->Session->setFlash(__('The customer has been saved.'));
                return $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The customer could not be saved. Please, try again.'));
            }
        } else {
            $options = array('conditions' => array('Customer.' . $this->Customer->primaryKey => $id));
            $this->request->data = $this->Customer->find('first', $options);
        }
        //$companies = $this->Customer->Companies->find('list');
        $companies= $this->Customer->Companies->find('list',array('order'=>'company_name ASC','fields'=>array('id','company_name')));       
        $employees= $this->Customer->Employees->find('list',array('order'=>'employee_name ASC','fields'=>array('id','employee_name')));         
        $this->set(compact('companies'));
        $this->set(compact('employees'));
    }

//一些代码}

有人能帮帮忙吗?

SimplePassword哈希器使用md5加密。

md5应该是单向加密。使用它的原因是,只有用户知道他们的密码,但您仍然可以验证密码。验证它的方法是为用户提供的密码创建md5哈希,并将其与数据库中密码的md5哈希进行比较。

单向加密背后的整个思想是生成一个哈希值,该值不能被解密以显示原始字符串。

这就是管理员在处理丢失的密码时通常将其重置为新值的原因。

我认为你只需要在编辑时清空密码字段。ctp像

echo $this->Form->input('customer_PW', array('value'=> ''));

您真的希望在表单上显示密码供用户编辑吗?

您可能只想清除密码字段,以便您的用户可以编辑和保存配置文件的其余部分,而不必担心密码。如果他们发布了表单,并且已经填写了密码字段,您就知道他们已经输入了一个新密码,该密码应该被散列并保存。如果密码字段为空,那么在从控制器中保存模型之前,请确保从数组中删除密码字段。

如果使用jquery,确保密码字段为空是很简单的。

$(document).ready(function() {
    $('#CustomerPW').val('');
});

最新更新