douri4459 2013-11-11 21:12
浏览 51
已采纳

上传记录md5 cakephp

I have a sit developed in cakephp, and I have a page to edit user.
My user table has many field and one of this is password in md5.

The user can modify all its fields and password but if he leave blank this field I have t take from the database the old password and save it.

But return me error on save on the password field.

This is my action into the controller:

if ($this->request->is ('post')){
    $this->User->id = $this->request->data['User']['id'];

    if($this->request->data['User']['password'] == ''){
        $user = $this->User->find('first',array('conditions'=>array('User.id' => $this->request->data['User']['id'])));
        $this->request->data['User']['password'] = md5($user['User']['password']);
        $this->request->data['User']['password_confirm'] = md5($user['User']['password']);
    }

    if ($this->User->save($this->request->data)) {
        $this->redirect (array ('action'=>'index'));
    }
    else{
        debug($this->User->validationErrors);
        $this->Session->write('flash_element','error');
        $this->Session->setFlash ('Errore di salvataggio dello user.');
    }
}

And this is the method beforeSave into the UserModel:

public function beforeSave(){
    if (isset($this->data['User']['password'])){
        $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
    }
}

The problem is when I try to save return me error on the field password lie is inappropriate type. If I print the field password before save I see something like: ***** but if I print the variable md5($user['User']['password']) return me the right value of password crypted.

Thanks

  • 写回答

2条回答 默认 最新

  • dreamice2013 2013-11-11 22:24
    关注

    IMO, don't have the "password" field where the user edits his profile information.

    You can have 2 forms on the page, where the second one is a change password form. This way, if the user changes their "first name" (which is in the first form) for example, your code does not have to check or do anything with their password.

    After seeing many different frameworks, and creating systems myself, I can't say I recall anything where I have seen in the "wild" something handled like your doing. You are doing an extra step by getting their old password and "putting it back" just so you don't lose their password in the database when they want to change their profile details.

    If its for security, you can make them "confirm" their password so it must match before changing the profile details.

    Having the "password" box on the "edit profile" form is just bad code logic.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据